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