migrated plugin from 3.0 to 3.1
[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.io.InputStream;
13 import java.net.MalformedURLException;
14 import java.net.URL;
15 import java.util.Enumeration;
16 import java.util.PropertyResourceBundle;
17
18 import net.sourceforge.phpdt.externaltools.internal.model.ColorManager;
19 import net.sourceforge.phpdt.externaltools.internal.model.ExternalToolsImages;
20 import net.sourceforge.phpdt.externaltools.internal.model.IPreferenceConstants;
21 import net.sourceforge.phpdt.externaltools.internal.model.VariableContextManager;
22 import net.sourceforge.phpdt.externaltools.internal.registry.ArgumentVariableRegistry;
23 import net.sourceforge.phpdt.externaltools.internal.registry.PathLocationVariableRegistry;
24 import net.sourceforge.phpdt.externaltools.internal.registry.RefreshScopeVariableRegistry;
25 import net.sourceforge.phpdt.externaltools.model.IExternalToolConstants;
26
27 import org.eclipse.core.runtime.CoreException;
28 import org.eclipse.core.runtime.IStatus;
29 import org.eclipse.core.runtime.Path;
30 import org.eclipse.core.runtime.Platform;
31 import org.eclipse.core.runtime.Status;
32 import org.eclipse.jface.preference.IPreferenceStore;
33 import org.eclipse.jface.preference.PreferenceConverter;
34 import org.eclipse.jface.resource.ImageDescriptor;
35 import org.eclipse.jface.resource.ImageRegistry;
36 import org.eclipse.swt.graphics.Color;
37 import org.eclipse.swt.graphics.RGB;
38 import org.eclipse.swt.widgets.Display;
39 import org.eclipse.ui.IWorkbenchWindow;
40 import org.eclipse.ui.plugin.AbstractUIPlugin;
41 import org.osgi.framework.Bundle;
42 import org.osgi.framework.BundleContext;
43
44 /**
45  * External tools plug-in class
46  */
47 public final class ExternalToolsPlugin extends AbstractUIPlugin {
48   public static final String XAMPP_START_PREF = "_xampp_start_pref";
49
50   public static final String XAMPP_STOP_PREF = "_xampp_stop_pref";
51
52   public static final String MYSQL_RUN_PREF = "_mysql_run_pref";
53
54   public static final String MYSQL_START_BACKGROUND = "_mysql_start_background";
55
56   public static final String MYSQL_PREF = "__mysql_start";
57
58   public static final String APACHE_RUN_PREF = "_apache_run_pref";
59
60   public static final String APACHE_START_BACKGROUND = "_apache_start_background";
61
62   public static final String APACHE_START_PREF = "__apache_start";
63
64   public static final String APACHE_STOP_BACKGROUND = "_apache_stop_background";
65
66   public static final String APACHE_STOP_PREF = "__apache_stop";
67
68   public static final String APACHE_RESTART_BACKGROUND = "_apache_restart_background";
69
70   public static final String APACHE_RESTART_PREF = "__apache_restart";
71
72   public static final String HTTPD_CONF_PATH_PREF = "__httpd_conf_path";
73
74   public static final String ETC_HOSTS_PATH_PREF = "__etc_hosts_path";
75   //  public static final String SHOW_OUTPUT_IN_CONSOLE = "_show_output_in_console";
76
77   public static final String PHP_RUN_PREF = "_php_run_pref";
78
79   public static final String EXTERNAL_PARSER_PREF = "_external_parser";
80
81   /**
82    * Status representing no problems encountered during operation.
83    */
84   public static final IStatus OK_STATUS = new Status(IStatus.OK, IExternalToolConstants.PLUGIN_ID, 0, "", null); //$NON-NLS-1$
85
86   private static ExternalToolsPlugin plugin;
87
88   private RefreshScopeVariableRegistry refreshVarRegistry;
89
90   private PathLocationVariableRegistry fileLocVarRegistry;
91
92   private PathLocationVariableRegistry dirLocVarRegistry;
93
94   private ArgumentVariableRegistry argumentVarRegistry;
95
96
97   /**
98    * This version is recommended for eclipse3.0 and above
99    */
100   public ExternalToolsPlugin() {
101     super();
102     plugin = this;
103   }
104
105   /**
106    * Returns the default instance of the receiver. This represents the runtime plugin.
107    */
108   public static ExternalToolsPlugin getDefault() {
109     return plugin;
110   }
111
112   /**
113    * Returns a new <code>IStatus</code> for this plug-in
114    */
115   public static IStatus newErrorStatus(String message, Throwable exception) {
116     return new Status(Status.ERROR, IExternalToolConstants.PLUGIN_ID, 0, message, exception);
117   }
118
119   /**
120    * Returns a new <code>CoreException</code> for this plug-in
121    */
122   public static CoreException newError(String message, Throwable exception) {
123     return new CoreException(new Status(Status.ERROR, IExternalToolConstants.PLUGIN_ID, 0, message, exception));
124   }
125
126   /**
127    * Returns the registry of refresh scope variables.
128    */
129   public ArgumentVariableRegistry getArgumentVariableRegistry() {
130     if (argumentVarRegistry == null)
131       argumentVarRegistry = new ArgumentVariableRegistry();
132     return argumentVarRegistry;
133   }
134
135   /**
136    * Returns the registry of directory location variables.
137    */
138   public PathLocationVariableRegistry getDirectoryLocationVariableRegistry() {
139     if (dirLocVarRegistry == null)
140       dirLocVarRegistry = new PathLocationVariableRegistry(IExternalToolConstants.EXTENSION_POINT_DIRECTORY_VARIABLES);
141     return dirLocVarRegistry;
142   }
143
144   /**
145    * Returns the registry of file location variables.
146    */
147   public PathLocationVariableRegistry getFileLocationVariableRegistry() {
148     if (fileLocVarRegistry == null)
149       fileLocVarRegistry = new PathLocationVariableRegistry(IExternalToolConstants.EXTENSION_POINT_FILE_VARIABLES);
150     return fileLocVarRegistry;
151   }
152
153   /**
154    * Returns the registry of refresh scope variables.
155    */
156   public RefreshScopeVariableRegistry getRefreshVariableRegistry() {
157     if (refreshVarRegistry == null)
158       refreshVarRegistry = new RefreshScopeVariableRegistry();
159     return refreshVarRegistry;
160   }
161
162   /**
163    * Writes the message to the plug-in's log
164    *
165    * @param message
166    *          the text to write to the log
167    */
168   public void log(String message, Throwable exception) {
169     IStatus status = newErrorStatus(message, exception);
170     //  getLog().log(status);
171     ExternalToolsPlugin.log(status);
172   }
173
174   public static void log(IStatus status) {
175     getDefault().getLog().log(status);
176   }
177
178   /**
179    * Returns the ImageDescriptor for the icon with the given path
180    *
181    * @return the ImageDescriptor object
182    */
183   public ImageDescriptor getImageDescriptor(String path) {
184     try {
185       Bundle bundle = ExternalToolsPlugin.getDefault().getBundle();
186       URL installURL = bundle.getEntry("/"); //$NON-NLS-1$
187       URL url = new URL(installURL, path);
188       return ImageDescriptor.createFromURL(url);
189     } catch (MalformedURLException e) {
190       return null;
191     }
192   }
193
194   /*
195    * (non-Javadoc) Method declared in AbstractUIPlugin.
196    */
197
198   protected void initializeDefaultPreferences(IPreferenceStore store) {
199     String operatingSystem = Platform.getOS();
200     // maxosx, linux, solaris, win32,...
201     try {
202       InputStream is = getDefault().openStream(new Path("prefs/default_" + operatingSystem + ".properties"));
203       PropertyResourceBundle resourceBundle = new PropertyResourceBundle(is);
204       Enumeration e = resourceBundle.getKeys();
205       String key;
206       while (e.hasMoreElements()) {
207         key = (String) e.nextElement();
208         store.setDefault(key, resourceBundle.getString(key));
209       }
210     } catch (Exception e) {
211       // no default properties found
212       if (operatingSystem.equals(Platform.OS_WIN32)) {
213         store.setDefault(PHP_RUN_PREF, "c:\\apache\\php\\php.exe");
214         store.setDefault(EXTERNAL_PARSER_PREF, "c:\\apache\\php\\php -l -f {0}");
215         store.setDefault(MYSQL_RUN_PREF, "c:\\apache\\mysql\\bin\\mysqld-nt.exe");
216         store.setDefault(APACHE_RUN_PREF, "c:\\apache\\apache.exe");
217         store.setDefault(XAMPP_START_PREF, "c:\\xampp\\xampp_start.exe");
218         store.setDefault(XAMPP_STOP_PREF, "c:\\xampp\\xampp_stop.exe");
219         store.setDefault(ETC_HOSTS_PATH_PREF, "c:\\windows\\system32\\drivers\\etc\\hosts");
220       } else {
221         store.setDefault(PHP_RUN_PREF, "/apache/php/php");
222         store.setDefault(EXTERNAL_PARSER_PREF, "/apache/php/php -l -f {0}");
223         store.setDefault(MYSQL_RUN_PREF, "/apache/mysql/bin/mysqld");
224         store.setDefault(APACHE_RUN_PREF, "/apache/apache");
225         store.setDefault(XAMPP_START_PREF, "xamp/xampp_start");
226         store.setDefault(XAMPP_STOP_PREF, "xampp/xampp_stop");
227       }
228       store.setDefault(MYSQL_PREF, "--standalone");
229       store.setDefault(APACHE_START_PREF, "-c \"DocumentRoot \"{0}\"\"");
230       store.setDefault(APACHE_STOP_PREF, "-k shutdown");
231       store.setDefault(APACHE_RESTART_PREF, "-k restart");
232       store.setDefault(MYSQL_START_BACKGROUND, "true");
233       store.setDefault(APACHE_START_BACKGROUND, "true");
234       store.setDefault(APACHE_STOP_BACKGROUND, "true");
235       store.setDefault(APACHE_RESTART_BACKGROUND, "true");
236     }
237
238     //    store.setDefault(SHOW_OUTPUT_IN_CONSOLE, "true");
239
240     store.setDefault(IPreferenceConstants.PROMPT_FOR_MIGRATION, true);
241
242     PreferenceConverter.setDefault(store, IPreferenceConstants.CONSOLE_ERROR_RGB, new RGB(255, 0, 0)); // red - exactly the same as
243     // debug Console
244     PreferenceConverter.setDefault(store, IPreferenceConstants.CONSOLE_WARNING_RGB, new RGB(255, 100, 0)); // orange
245     PreferenceConverter.setDefault(store, IPreferenceConstants.CONSOLE_INFO_RGB, new RGB(0, 0, 255)); // blue
246     PreferenceConverter.setDefault(store, IPreferenceConstants.CONSOLE_VERBOSE_RGB, new RGB(0, 200, 125)); // green
247     PreferenceConverter.setDefault(store, IPreferenceConstants.CONSOLE_DEBUG_RGB, new RGB(0, 0, 0)); // black
248   }
249
250   public static IWorkbenchWindow getActiveWorkbenchWindow() {
251     return ExternalToolsPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow();
252   }
253
254   /**
255    * Returns the standard display to be used. The method first checks, if the thread calling this method has an associated display.
256    * If so, this display is returned. Otherwise the method returns the default display.
257    */
258   public static Display getStandardDisplay() {
259     Display display = Display.getCurrent();
260     if (display == null) {
261       display = Display.getDefault();
262     }
263     return display;
264   }
265
266   /**
267    * @see org.eclipse.ui.plugin.AbstractUIPlugin#createImageRegistry()
268    */
269   protected ImageRegistry createImageRegistry() {
270     return ExternalToolsImages.initializeImageRegistry();
271   }
272
273   /**
274    * @throws Exception
275  * @see org.eclipse.core.runtime.Plugin#start(BundleContext context)
276    */
277   public void start(BundleContext context) throws Exception {
278     super.start(context);
279     getStandardDisplay().asyncExec(new Runnable() {
280       public void run() {
281         //initialize the variable context manager
282         VariableContextManager.getDefault();
283       }
284     });
285   }
286
287   /**
288    * @throws Exception
289  * @see org.eclipse.core.runtime.Plugin#stop(BundleContext context)
290    */
291   public void stop(BundleContext context) throws Exception {
292     ColorManager.getDefault().dispose();
293     super.stop(context);
294   }
295
296   /**
297    * Returns the preference color, identified by the given preference.
298    */
299   public static Color getPreferenceColor(String pref) {
300     return ColorManager.getDefault().getColor(
301         PreferenceConverter.getColor(ExternalToolsPlugin.getDefault().getPreferenceStore(), pref));
302   }
303 }