1 package net.sourceforge.phpeclipse.externaltools;
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
10 **********************************************************************/
12 import java.io.InputStream;
13 import java.net.MalformedURLException;
15 import java.util.Enumeration;
16 import java.util.PropertyResourceBundle;
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;
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;
45 * External tools plug-in class
47 public final class ExternalToolsPlugin extends AbstractUIPlugin {
48 public static final String XAMPP_START_PREF = "_xampp_start_pref";
50 public static final String XAMPP_STOP_PREF = "_xampp_stop_pref";
52 public static final String MYSQL_RUN_PREF = "_mysql_run_pref";
54 public static final String MYSQL_START_BACKGROUND = "_mysql_start_background";
56 public static final String MYSQL_PREF = "__mysql_start";
58 public static final String APACHE_RUN_PREF = "_apache_run_pref";
60 public static final String APACHE_START_BACKGROUND = "_apache_start_background";
62 public static final String APACHE_START_PREF = "__apache_start";
64 public static final String APACHE_STOP_BACKGROUND = "_apache_stop_background";
66 public static final String APACHE_STOP_PREF = "__apache_stop";
68 public static final String APACHE_RESTART_BACKGROUND = "_apache_restart_background";
70 public static final String APACHE_RESTART_PREF = "__apache_restart";
72 public static final String HTTPD_CONF_PATH_PREF = "__httpd_conf_path";
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";
77 public static final String PHP_RUN_PREF = "_php_run_pref";
79 public static final String EXTERNAL_PARSER_PREF = "_external_parser";
82 * Status representing no problems encountered during operation.
84 public static final IStatus OK_STATUS = new Status(IStatus.OK, IExternalToolConstants.PLUGIN_ID, 0, "", null); //$NON-NLS-1$
86 private static ExternalToolsPlugin plugin;
88 private RefreshScopeVariableRegistry refreshVarRegistry;
90 private PathLocationVariableRegistry fileLocVarRegistry;
92 private PathLocationVariableRegistry dirLocVarRegistry;
94 private ArgumentVariableRegistry argumentVarRegistry;
98 * This version is recommended for eclipse3.0 and above
100 public ExternalToolsPlugin() {
106 * Returns the default instance of the receiver. This represents the runtime plugin.
108 public static ExternalToolsPlugin getDefault() {
113 * Returns a new <code>IStatus</code> for this plug-in
115 public static IStatus newErrorStatus(String message, Throwable exception) {
116 return new Status(Status.ERROR, IExternalToolConstants.PLUGIN_ID, 0, message, exception);
120 * Returns a new <code>CoreException</code> for this plug-in
122 public static CoreException newError(String message, Throwable exception) {
123 return new CoreException(new Status(Status.ERROR, IExternalToolConstants.PLUGIN_ID, 0, message, exception));
127 * Returns the registry of refresh scope variables.
129 public ArgumentVariableRegistry getArgumentVariableRegistry() {
130 if (argumentVarRegistry == null)
131 argumentVarRegistry = new ArgumentVariableRegistry();
132 return argumentVarRegistry;
136 * Returns the registry of directory location variables.
138 public PathLocationVariableRegistry getDirectoryLocationVariableRegistry() {
139 if (dirLocVarRegistry == null)
140 dirLocVarRegistry = new PathLocationVariableRegistry(IExternalToolConstants.EXTENSION_POINT_DIRECTORY_VARIABLES);
141 return dirLocVarRegistry;
145 * Returns the registry of file location variables.
147 public PathLocationVariableRegistry getFileLocationVariableRegistry() {
148 if (fileLocVarRegistry == null)
149 fileLocVarRegistry = new PathLocationVariableRegistry(IExternalToolConstants.EXTENSION_POINT_FILE_VARIABLES);
150 return fileLocVarRegistry;
154 * Returns the registry of refresh scope variables.
156 public RefreshScopeVariableRegistry getRefreshVariableRegistry() {
157 if (refreshVarRegistry == null)
158 refreshVarRegistry = new RefreshScopeVariableRegistry();
159 return refreshVarRegistry;
163 * Writes the message to the plug-in's log
166 * the text to write to the log
168 public void log(String message, Throwable exception) {
169 IStatus status = newErrorStatus(message, exception);
170 // getLog().log(status);
171 ExternalToolsPlugin.log(status);
174 public static void log(IStatus status) {
175 getDefault().getLog().log(status);
179 * Returns the ImageDescriptor for the icon with the given path
181 * @return the ImageDescriptor object
183 public ImageDescriptor getImageDescriptor(String path) {
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) {
195 * (non-Javadoc) Method declared in AbstractUIPlugin.
198 protected void initializeDefaultPreferences(IPreferenceStore store) {
199 String operatingSystem = Platform.getOS();
200 // maxosx, linux, solaris, win32,...
202 InputStream is = getDefault().openStream(new Path("prefs/default_" + operatingSystem + ".properties"));
203 PropertyResourceBundle resourceBundle = new PropertyResourceBundle(is);
204 Enumeration e = resourceBundle.getKeys();
206 while (e.hasMoreElements()) {
207 key = (String) e.nextElement();
208 store.setDefault(key, resourceBundle.getString(key));
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");
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");
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");
238 // store.setDefault(SHOW_OUTPUT_IN_CONSOLE, "true");
240 store.setDefault(IPreferenceConstants.PROMPT_FOR_MIGRATION, true);
242 PreferenceConverter.setDefault(store, IPreferenceConstants.CONSOLE_ERROR_RGB, new RGB(255, 0, 0)); // red - exactly the same as
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
250 public static IWorkbenchWindow getActiveWorkbenchWindow() {
251 return ExternalToolsPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow();
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.
258 public static Display getStandardDisplay() {
259 Display display = Display.getCurrent();
260 if (display == null) {
261 display = Display.getDefault();
267 * @see org.eclipse.ui.plugin.AbstractUIPlugin#createImageRegistry()
269 protected ImageRegistry createImageRegistry() {
270 return ExternalToolsImages.initializeImageRegistry();
275 * @see org.eclipse.core.runtime.Plugin#start(BundleContext context)
277 public void start(BundleContext context) throws Exception {
278 super.start(context);
279 getStandardDisplay().asyncExec(new Runnable() {
281 //initialize the variable context manager
282 VariableContextManager.getDefault();
289 * @see org.eclipse.core.runtime.Plugin#stop(BundleContext context)
291 public void stop(BundleContext context) throws Exception {
292 ColorManager.getDefault().dispose();
297 * Returns the preference color, identified by the given preference.
299 public static Color getPreferenceColor(String pref) {
300 return ColorManager.getDefault().getColor(
301 PreferenceConverter.getColor(ExternalToolsPlugin.getDefault().getPreferenceStore(), pref));