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