314cd05ac78a29156a806fab7a1718e4523a623e
[phpeclipse.git] / net.sourceforge.phpeclipse.externaltools / src / net / sourceforge / phpeclipse / externaltools / ExternalToolsPlugin.java
1 package net.sourceforge.phpeclipse.externaltools;
2
3 /**********************************************************************
4  Copyright (c) 2002 IBM Corp. and others. All rights reserved.
5  This file is made available under the terms of the Common Public License v1.0
6  which accompanies this distribution, and is available at
7  http://www.eclipse.org/legal/cpl-v10.html
8  �
9  Contributors:
10  **********************************************************************/
11
12 import java.net.MalformedURLException;
13 import java.net.URL;
14
15 import net.sourceforge.phpdt.externaltools.internal.model.ColorManager;
16 import net.sourceforge.phpdt.externaltools.internal.model.ExternalToolsImages;
17 import net.sourceforge.phpdt.externaltools.internal.model.IPreferenceConstants;
18 import net.sourceforge.phpdt.externaltools.internal.model.VariableContextManager;
19 import net.sourceforge.phpdt.externaltools.internal.registry.ArgumentVariableRegistry;
20 import net.sourceforge.phpdt.externaltools.internal.registry.PathLocationVariableRegistry;
21 import net.sourceforge.phpdt.externaltools.internal.registry.RefreshScopeVariableRegistry;
22 import net.sourceforge.phpdt.externaltools.model.IExternalToolConstants;
23
24 import org.eclipse.core.runtime.CoreException;
25 import org.eclipse.core.runtime.IStatus;
26 import org.eclipse.core.runtime.Status;
27 import org.eclipse.jface.preference.IPreferenceStore;
28 import org.eclipse.jface.preference.PreferenceConverter;
29 import org.eclipse.jface.resource.ImageDescriptor;
30 import org.eclipse.jface.resource.ImageRegistry;
31 import org.eclipse.swt.graphics.Color;
32 import org.eclipse.swt.graphics.RGB;
33 import org.eclipse.swt.widgets.Display;
34 import org.eclipse.ui.IWorkbenchWindow;
35 import org.eclipse.ui.plugin.AbstractUIPlugin;
36 import org.osgi.framework.Bundle;
37
38 /**
39  * External tools plug-in class
40  */
41 public final class ExternalToolsPlugin extends AbstractUIPlugin {
42   public static final String XAMPP_START_PREF = "_xampp_start_pref";
43
44   public static final String XAMPP_STOP_PREF = "_xampp_stop_pref";
45
46   public static final String MYSQL_RUN_PREF = "_mysql_run_pref";
47
48   public static final String MYSQL_START_BACKGROUND = "_mysql_start_background";
49
50   public static final String MYSQL_PREF = "__mysql_start";
51
52   public static final String APACHE_RUN_PREF = "_apache_run_pref";
53
54   public static final String APACHE_START_BACKGROUND = "_apache_start_background";
55
56   public static final String APACHE_START_PREF = "__apache_start";
57
58   public static final String APACHE_STOP_BACKGROUND = "_apache_stop_background";
59
60   public static final String APACHE_STOP_PREF = "__apache_stop";
61
62   public static final String APACHE_RESTART_BACKGROUND = "_apache_restart_background";
63
64   public static final String APACHE_RESTART_PREF = "__apache_restart";
65
66   public static final String SHOW_OUTPUT_IN_CONSOLE = "_show_output_in_console";
67
68   public static final String PHP_RUN_PREF = "_php_run_pref";
69
70   /**
71    * Status representing no problems encountered during operation.
72    */
73   public static final IStatus OK_STATUS = new Status(IStatus.OK, IExternalToolConstants.PLUGIN_ID, 0, "", null); //$NON-NLS-1$
74
75   private static ExternalToolsPlugin plugin;
76
77   private RefreshScopeVariableRegistry refreshVarRegistry;
78
79   private PathLocationVariableRegistry fileLocVarRegistry;
80
81   private PathLocationVariableRegistry dirLocVarRegistry;
82
83   private ArgumentVariableRegistry argumentVarRegistry;
84
85   /**
86    * Create an instance of the External Tools plug-in.
87    */
88   public ExternalToolsPlugin() { // IPluginDescriptor descriptor) {
89     // super(descriptor);
90     plugin = this;
91   }
92
93   /**
94    * Returns the default instance of the receiver. This represents the runtime plugin.
95    */
96   public static ExternalToolsPlugin getDefault() {
97     return plugin;
98   }
99
100   /**
101    * Returns a new <code>IStatus</code> for this plug-in
102    */
103   public static IStatus newErrorStatus(String message, Throwable exception) {
104     return new Status(Status.ERROR, IExternalToolConstants.PLUGIN_ID, 0, message, exception);
105   }
106
107   /**
108    * Returns a new <code>CoreException</code> for this plug-in
109    */
110   public static CoreException newError(String message, Throwable exception) {
111     return new CoreException(new Status(Status.ERROR, IExternalToolConstants.PLUGIN_ID, 0, message, exception));
112   }
113
114   /**
115    * Returns the registry of refresh scope variables.
116    */
117   public ArgumentVariableRegistry getArgumentVariableRegistry() {
118     if (argumentVarRegistry == null)
119       argumentVarRegistry = new ArgumentVariableRegistry();
120     return argumentVarRegistry;
121   }
122
123   /**
124    * Returns the registry of directory location variables.
125    */
126   public PathLocationVariableRegistry getDirectoryLocationVariableRegistry() {
127     if (dirLocVarRegistry == null)
128       dirLocVarRegistry = new PathLocationVariableRegistry(IExternalToolConstants.EXTENSION_POINT_DIRECTORY_VARIABLES);
129     return dirLocVarRegistry;
130   }
131
132   /**
133    * Returns the registry of file location variables.
134    */
135   public PathLocationVariableRegistry getFileLocationVariableRegistry() {
136     if (fileLocVarRegistry == null)
137       fileLocVarRegistry = new PathLocationVariableRegistry(IExternalToolConstants.EXTENSION_POINT_FILE_VARIABLES);
138     return fileLocVarRegistry;
139   }
140
141   /**
142    * Returns the registry of refresh scope variables.
143    */
144   public RefreshScopeVariableRegistry getRefreshVariableRegistry() {
145     if (refreshVarRegistry == null)
146       refreshVarRegistry = new RefreshScopeVariableRegistry();
147     return refreshVarRegistry;
148   }
149
150   /**
151    * Writes the message to the plug-in's log
152    * 
153    * @param message
154    *          the text to write to the log
155    */
156   public void log(String message, Throwable exception) {
157     IStatus status = newErrorStatus(message, exception);
158     //  getLog().log(status);
159     ExternalToolsPlugin.log(status);
160   }
161
162   public static void log(IStatus status) {
163     getDefault().getLog().log(status);
164   }
165
166   /**
167    * Returns the ImageDescriptor for the icon with the given path
168    * 
169    * @return the ImageDescriptor object
170    */
171   public ImageDescriptor getImageDescriptor(String path) {
172     try {
173       Bundle bundle = ExternalToolsPlugin.getDefault().getBundle();
174       URL installURL = bundle.getEntry("/"); //$NON-NLS-1$
175       URL url = new URL(installURL, path);
176       return ImageDescriptor.createFromURL(url);
177     } catch (MalformedURLException e) {
178       return null;
179     }
180   }
181
182   /*
183    * (non-Javadoc) Method declared in AbstractUIPlugin.
184    */
185   public void initializeDefaultPreferences(IPreferenceStore prefs) {
186     prefs.setDefault(IPreferenceConstants.PROMPT_FOR_MIGRATION, true);
187
188     PreferenceConverter.setDefault(prefs, IPreferenceConstants.CONSOLE_ERROR_RGB, new RGB(255, 0, 0)); // red - exactly the same as
189     // debug Consol
190     PreferenceConverter.setDefault(prefs, IPreferenceConstants.CONSOLE_WARNING_RGB, new RGB(255, 100, 0)); // orange
191     PreferenceConverter.setDefault(prefs, IPreferenceConstants.CONSOLE_INFO_RGB, new RGB(0, 0, 255)); // blue
192     PreferenceConverter.setDefault(prefs, IPreferenceConstants.CONSOLE_VERBOSE_RGB, new RGB(0, 200, 125)); // green
193     PreferenceConverter.setDefault(prefs, IPreferenceConstants.CONSOLE_DEBUG_RGB, new RGB(0, 0, 0)); // black
194   }
195
196   public static IWorkbenchWindow getActiveWorkbenchWindow() {
197     return ExternalToolsPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow();
198   }
199
200   /**
201    * Returns the standard display to be used. The method first checks, if the thread calling this method has an associated display.
202    * If so, this display is returned. Otherwise the method returns the default display.
203    */
204   public static Display getStandardDisplay() {
205     Display display = Display.getCurrent();
206     if (display == null) {
207       display = Display.getDefault();
208     }
209     return display;
210   }
211
212   /**
213    * @see org.eclipse.ui.plugin.AbstractUIPlugin#createImageRegistry()
214    */
215   protected ImageRegistry createImageRegistry() {
216     return ExternalToolsImages.initializeImageRegistry();
217   }
218
219   /**
220    * @see org.eclipse.core.runtime.Plugin#startup()
221    */
222   public void startup() throws CoreException {
223     //    super.startup();
224     getStandardDisplay().asyncExec(new Runnable() {
225       public void run() {
226         //initialize the variable context manager
227         VariableContextManager.getDefault();
228       }
229     });
230   }
231
232   /**
233    * @see org.eclipse.core.runtime.Plugin#shutdown()
234    */
235   public void shutdown() throws CoreException {
236     //          super.shutdown();
237     ColorManager.getDefault().dispose();
238   }
239
240   /**
241    * Returns the preference color, identified by the given preference.
242    */
243   public static Color getPreferenceColor(String pref) {
244     return ColorManager.getDefault().getColor(
245         PreferenceConverter.getColor(ExternalToolsPlugin.getDefault().getPreferenceStore(), pref));
246   }
247 }