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