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