Use external tools package to run commands for apache, mysql, php
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / PHPeclipsePlugin.java
1 /**********************************************************************
2 Copyright (c) 2000, 2002 IBM Corp. and others.
3 All rights reserved. This program and the accompanying materials
4 are made available under the terms of the Common Public License v1.0
5 which accompanies this distribution, and is available at
6 http://www.eclipse.org/legal/cpl-v10.html
7
8 Contributors:
9     IBM Corporation - Initial implementation
10     Klaus Hartlage - www.eclipseproject.de
11 **********************************************************************/
12 package net.sourceforge.phpeclipse;
13
14 import net.sourceforge.phpdt.externaltools.internal.model.ColorManager;
15 import net.sourceforge.phpdt.externaltools.internal.model.ExternalToolsPlugin;
16 import net.sourceforge.phpdt.externaltools.internal.model.VariableContextManager;
17 import net.sourceforge.phpdt.internal.ui.preferences.TemplatePreferencePage;
18 import net.sourceforge.phpdt.internal.ui.viewsupport.ImageDescriptorRegistry;
19 import net.sourceforge.phpdt.ui.PreferenceConstants;
20 import net.sourceforge.phpdt.ui.text.JavaTextTools;
21 import net.sourceforge.phpeclipse.phpeditor.PHPDocumentProvider;
22 import net.sourceforge.phpeclipse.phpeditor.PHPSyntaxRdr;
23 import net.sourceforge.phpeclipse.phpeditor.util.PHPColorProvider;
24 import net.sourceforge.phpeclipse.resourcesview.PHPElement;
25 import net.sourceforge.phpeclipse.resourcesview.PHPElementAdapterFactory;
26 import net.sourceforge.phpeclipse.resourcesview.ResourceAdapterFactory;
27
28 import org.eclipse.core.boot.BootLoader;
29 import org.eclipse.core.resources.IFile;
30 import org.eclipse.core.resources.IResource;
31 import org.eclipse.core.resources.IWorkspace;
32 import org.eclipse.core.resources.ResourcesPlugin;
33 import org.eclipse.core.runtime.CoreException;
34 import org.eclipse.core.runtime.IAdapterManager;
35 import org.eclipse.core.runtime.IPath;
36 import org.eclipse.core.runtime.IPluginDescriptor;
37 import org.eclipse.core.runtime.IStatus;
38 import org.eclipse.core.runtime.Path;
39 import org.eclipse.core.runtime.Platform;
40 import org.eclipse.core.runtime.Status;
41 import org.eclipse.jface.preference.IPreferenceStore;
42 import org.eclipse.jface.preference.PreferenceConverter;
43 import org.eclipse.swt.widgets.Display;
44 import org.eclipse.swt.widgets.Shell;
45 import org.eclipse.ui.IWorkbenchPage;
46 import org.eclipse.ui.IWorkbenchWindow;
47 import org.eclipse.ui.plugin.AbstractUIPlugin;
48
49 /**
50  * The main plugin class to be used in the desktop.
51  */
52 public class PHPeclipsePlugin extends AbstractUIPlugin implements IPreferenceConstants {
53
54   /**
55    * The id of the PHP plugin (value <code>"net.sourceforge.phpeclipse"</code>).
56    */
57   public static final String PLUGIN_ID = "net.sourceforge.phpeclipse"; //$NON-NLS-1$
58   public final static String PHP_NATURE_ID = PLUGIN_ID + ".phpnature";
59   // public static final String PHP_RESOURCES_VIEW_ID = PLUGIN_ID + ".resourcesview.ViewPHPResources"; //$NON-NLS-1$
60   public static final String PHP_CODING_ACTION_SET_ID = PLUGIN_ID + ".ui.CodingActionSet"; //$NON-NLS-1$
61
62   public static final String PHPPARSER_NEW = "test.PHPParser";
63   public static final String PHPPARSER_ORIGINAL = "net.sourceforge.phpdt.internal.compiler.parser.Parser";
64
65   /** Change this if you want to switch PHP Parser. */
66   public static final String PHPPARSER = PHPPARSER_ORIGINAL;
67
68   //The shared instance.
69   private static PHPeclipsePlugin plugin;
70
71   private static ExternalToolsPlugin externalTools;
72   //Resource bundle.
73   //private ResourceBundle resourceBundle;
74
75   private ImageDescriptorRegistry fImageDescriptorRegistry;
76   private PHPDocumentProvider fCompilationUnitDocumentProvider;
77   private IFile fLastEditorFile = null;
78
79   private JavaTextTools fJavaTextTools;
80
81   /**
82   * The Java virtual machine that we are running on.
83   */
84   private static int jvm;
85
86   /** MRJ 2.0 */
87   private static final int MRJ_2_0 = 0;
88
89   /** MRJ 2.1 or later */
90   private static final int MRJ_2_1 = 1;
91
92   /** Java on Mac OS X 10.0 (MRJ 3.0) */
93   private static final int MRJ_3_0 = 3;
94
95   /** MRJ 3.1 */
96   private static final int MRJ_3_1 = 4;
97
98   /** Windows NT  */
99   private static final int WINDOWS_NT = 5;
100
101   /** Windows 9x  */
102   private static final int WINDOWS_9x = 6;
103
104   /** JVM constant for any other platform */
105   private static final int OTHER = -1;
106
107   /** General debug flag*/
108   public static final boolean DEBUG = false;
109   /**
110    * The constructor.
111    */
112   public PHPeclipsePlugin(IPluginDescriptor descriptor) {
113     super(descriptor);
114     plugin = this;
115     setJVM();
116     externalTools = new ExternalToolsPlugin();
117     //    try {
118     //      resourceBundle = ResourceBundle.getBundle("net.sourceforge.PHPeclipsePluginResources");
119     //    } catch (MissingResourceException x) {
120     //      resourceBundle = null;
121     //    }
122   }
123
124   public static ImageDescriptorRegistry getImageDescriptorRegistry() {
125     return getDefault().internalGetImageDescriptorRegistry();
126   }
127
128   private ImageDescriptorRegistry internalGetImageDescriptorRegistry() {
129     if (fImageDescriptorRegistry == null)
130       fImageDescriptorRegistry = new ImageDescriptorRegistry();
131     return fImageDescriptorRegistry;
132   }
133   // @TODO: refactor this into a better method name !
134   public synchronized PHPDocumentProvider getCompilationUnitDocumentProvider() {
135     if (fCompilationUnitDocumentProvider == null)
136       fCompilationUnitDocumentProvider = new PHPDocumentProvider();
137     return fCompilationUnitDocumentProvider;
138   }
139
140   private static void setJVM() {
141     String osName = System.getProperty("os.name");
142
143     if (osName.startsWith("Mac OS")) {
144       String mrjVersion = System.getProperty("mrj.version");
145       String majorMRJVersion = mrjVersion.substring(0, 3);
146       jvm = OTHER;
147       try {
148
149         double version = Double.valueOf(majorMRJVersion).doubleValue();
150
151         if (version == 2) {
152           jvm = MRJ_2_0;
153         } else if (version >= 2.1 && version < 3) {
154           jvm = MRJ_2_1;
155         } else if (version == 3.0) {
156           jvm = MRJ_3_0;
157         } else if (version >= 3.1) {
158           jvm = MRJ_3_1;
159         }
160
161       } catch (NumberFormatException nfe) {
162
163       }
164
165     } else if (osName.startsWith("Windows")) {
166       if (osName.indexOf("9") != -1) {
167         jvm = WINDOWS_9x;
168       } else {
169         jvm = WINDOWS_NT;
170       }
171     }
172   }
173   public static int getJVM() {
174     return jvm;
175   }
176   /**
177    * Returns the shared instance.
178    */
179   public static PHPeclipsePlugin getDefault() {
180     return plugin;
181   }
182
183   //  public static ExternalToolsPlugin getExternalTools() {
184   //    return externalTools;
185   //  }
186   /**
187    * Returns the workspace instance.
188    */
189   public static IWorkspace getWorkspace() {
190     return ResourcesPlugin.getWorkspace();
191   }
192
193   public static IWorkbenchPage getActivePage() {
194     return getDefault().internalGetActivePage();
195   }
196
197   private IWorkbenchPage internalGetActivePage() {
198     IWorkbenchWindow window = getWorkbench().getActiveWorkbenchWindow();
199     if (window != null)
200       return window.getActivePage();
201     return null;
202   }
203
204   public static IWorkbenchWindow getActiveWorkbenchWindow() {
205     return getDefault().getWorkbench().getActiveWorkbenchWindow();
206   }
207
208   public static Shell getActiveWorkbenchShell() {
209     return getActiveWorkbenchWindow().getShell();
210   }
211
212   public static String getPluginId() {
213     return getDefault().getDescriptor().getUniqueIdentifier();
214   }
215
216   public static void log(IStatus status) {
217     getDefault().getLog().log(status);
218   }
219
220   //  public static void logErrorMessage(String message) {
221   //    log(new Status(IStatus.ERROR, getPluginId(), JavaStatusConstants.INTERNAL_ERROR, message, null));
222   //  }
223   //
224   //  public static void logErrorStatus(String message, IStatus status) {
225   //    if (status == null) {
226   //      logErrorMessage(message);
227   //      return;
228   //    }
229   //    MultiStatus multi= new MultiStatus(getPluginId(), JavaStatusConstants.INTERNAL_ERROR, message, null);
230   //    multi.add(status);
231   //    log(multi);
232   //  }
233   //
234   //  public static void log(Throwable e) {
235   //    log(new Status(IStatus.ERROR, getPluginId(), JavaStatusConstants.INTERNAL_ERROR, JavaUIMessages.getString("JavaPlugin.internal_error"), e)); //$NON-NLS-1$
236   //  }
237
238   public static void log(int severity, String message) {
239     Status status = new Status(severity, PLUGIN_ID, IStatus.OK, message, null);
240     log(status);
241   }
242   public static void log(Throwable e) {
243     log(new Status(IStatus.ERROR, PLUGIN_ID, IStatus.ERROR, "PHPeclipsePlugin.internalErrorOccurred", e)); //$NON-NLS-1$
244   }
245
246   public static boolean isDebug() {
247     return getDefault().isDebugging();
248   }
249
250   static IPath getInstallLocation() {
251     return new Path(getDefault().getDescriptor().getInstallURL().getFile());
252   }
253
254   public synchronized JavaTextTools getJavaTextTools() {
255     if (fJavaTextTools == null)
256       fJavaTextTools = new JavaTextTools(getPreferenceStore());
257     return fJavaTextTools;
258   }
259
260   /**
261    * Returns the string from the plugin's resource bundle,
262    * or 'key' if not found.
263    */
264   //  public static String getResourceString(String key) {
265   //    ResourceBundle bundle = PHPeclipsePlugin.getDefault().getResourceBundle();
266   //    try {
267   //      return bundle.getString(key);
268   //    } catch (MissingResourceException e) {
269   //      return key;
270   //    }
271   //  }
272
273   /**
274    * Returns the plugin's resource bundle,
275    */
276   //  public ResourceBundle getResourceBundle() {
277   //    return resourceBundle;
278   //  }
279
280   protected void initializeDefaultPreferences(IPreferenceStore store) {
281     // windows preferences:
282     store.setDefault(LOCALHOST_PREF, "http://localhost");
283
284     store.setDefault(SHOW_EXTERNAL_PREVIEW_PREF, "true");
285     store.setDefault(USE_EXTERNAL_BROWSER_PREF, "false");
286     store.setDefault(SHOW_OUTPUT_IN_CONSOLE, "true");
287
288     String windowsSystem = BootLoader.getWS();
289
290     if (jvm == WINDOWS_9x) {
291       store.setDefault(EXTERNAL_BROWSER_PREF, "command.com /c start iexplore {0}");
292     } else if (windowsSystem.equals(BootLoader.WS_WIN32)) {
293       store.setDefault(EXTERNAL_BROWSER_PREF, "rundll32 url.dll,FileProtocolHandler {0}");
294     } else {
295       store.setDefault(EXTERNAL_BROWSER_PREF, "netscape {0}");
296     }
297     store.setDefault(DOCUMENTROOT_PREF, getWorkspace().getRoot().getLocation().toString());
298
299     //  if ((jvm == WINDOWS_9x) || (jvm == WINDOWS_NT)) {
300     //  
301     if (windowsSystem.equals(BootLoader.WS_WIN32)) {
302       store.setDefault(PHP_RUN_PREF, "c:\\apache\\php\\php.exe");
303       store.setDefault(EXTERNAL_PARSER_PREF, "c:\\apache\\php\\php -l -f {0}");
304       store.setDefault(MYSQL_RUN_PREF, "c:\\apache\\mysql\\bin\\mysqld-nt.exe");
305       store.setDefault(APACHE_RUN_PREF, "c:\\apache\\apache.exe");
306     } else {
307       store.setDefault(PHP_RUN_PREF, "/apache/php/php");
308       store.setDefault(EXTERNAL_PARSER_PREF, "/apache/php/php -l -f {0}");
309       store.setDefault(MYSQL_RUN_PREF, "/apache/mysql/bin/mysqld");
310       store.setDefault(APACHE_RUN_PREF, "/apache/apache");
311     }
312     store.setDefault(MYSQL_PREF, "--standalone");
313     store.setDefault(APACHE_START_PREF, "-c \"DocumentRoot \"{0}\"\"");
314     store.setDefault(APACHE_STOP_PREF, "-k shutdown");
315     store.setDefault(APACHE_RESTART_PREF, "-k restart");
316
317     store.setDefault(MYSQL_START_BACKGROUND, "true");
318     store.setDefault(APACHE_START_BACKGROUND, "true");
319     store.setDefault(APACHE_STOP_BACKGROUND, "true");
320     store.setDefault(APACHE_RESTART_BACKGROUND, "true");
321
322     store.setDefault(PHP_PARSER_DEFAULT, PHP_EXTERNAL_PARSER);
323     store.setDefault(PHP_INTERNAL_PARSER, "false");
324     store.setDefault(PHP_EXTERNAL_PARSER, "true");
325
326     store.setDefault(PHP_PARSE_ON_SAVE, "true");
327
328     // show line numbers:
329     //   store.setDefault(LINE_NUMBER_RULER, "false");
330     //    store.setDefault(FORMATTER_TAB_SIZE, "4");
331
332     // php syntax highlighting
333     store.setDefault(PHP_USERDEF_XMLFILE, ""); //assume there is none  chooA
334
335     PreferenceConverter.setDefault(store, PHP_MULTILINE_COMMENT, PHPColorProvider.MULTI_LINE_COMMENT);
336     PreferenceConverter.setDefault(store, PHP_SINGLELINE_COMMENT, PHPColorProvider.SINGLE_LINE_COMMENT);
337     PreferenceConverter.setDefault(store, PHP_KEYWORD, PHPColorProvider.KEYWORD);
338     PreferenceConverter.setDefault(store, PHP_VARIABLE, PHPColorProvider.VARIABLE);
339     PreferenceConverter.setDefault(store, PHP_FUNCTIONNAME, PHPColorProvider.FUNCTION_NAME);
340     PreferenceConverter.setDefault(store, PHP_CONSTANT, PHPColorProvider.CONSTANT);
341     PreferenceConverter.setDefault(store, PHP_TYPE, PHPColorProvider.TYPE);
342     PreferenceConverter.setDefault(store, PHP_STRING, PHPColorProvider.STRING);
343     PreferenceConverter.setDefault(store, PHP_DEFAULT, PHPColorProvider.DEFAULT);
344     //    PreferenceConverter.setDefault(
345     //      store,
346     //      PHP_EDITOR_BACKGROUND,
347     //      PHPColorProvider.BACKGROUND);
348     //    PreferenceConverter.setDefault(
349     //      store,
350     //      LINKED_POSITION_COLOR,
351     //      PHPColorProvider.LINKED_POSITION_COLOR);
352     //    PreferenceConverter.setDefault(
353     //      store,
354     //      LINE_NUMBER_COLOR,
355     //      PHPColorProvider.LINE_NUMBER_COLOR);
356
357     //    // set default PHPDoc colors:
358     //    PreferenceConverter.setDefault(
359     //      store,
360     //      PHPDOC_KEYWORD, 
361     //      PHPColorProvider.PHPDOC_KEYWORD);
362     //    PreferenceConverter.setDefault(
363     //      store,
364     //      PHPDOC_LINK, 
365     //      PHPColorProvider.PHPDOC_LINK);      
366     //    PreferenceConverter.setDefault(
367     //      store,
368     //      PHPDOC_DEFAULT, 
369     //      PHPColorProvider.PHPDOC_DEFAULT);
370     //    PreferenceConverter.setDefault(
371     //      store,
372     //      PHPDOC_TAG, 
373     //      PHPColorProvider.PHPDOC_TAG);
374
375     //    store.setDefault(PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT, "true");
376     //    PreferenceConverter.setDefault(
377     //      store,
378     //      PREFERENCE_COLOR_BACKGROUND,
379     //      PHPColorProvider.BACKGROUND_COLOR);
380
381     //language stuff
382     store.setDefault(RESOURCE_BUNDLE, LANGUAGE_DEFAULT);
383     store.setDefault(RESOURCE_BUNDLE_EN_GB, "true");
384     store.setDefault(RESOURCE_BUNDLE_DE, "false");
385     store.setDefault(RESOURCE_BUNDLE_FR, "false");
386     store.setDefault(RESOURCE_BUNDLE_ES, "false");
387
388     store.setDefault(PHP_OUTLINE_CLASS, "true"); //$NON-NLS-1$
389     store.setDefault(PHP_OUTLINE_FUNC, "true"); //$NON-NLS-1$
390     store.setDefault(PHP_OUTLINE_VAR, "true"); //$NON-NLS-1$
391
392     TemplatePreferencePage.initDefaults(store);
393     //this will initialize the static fields in the syntaxrdr class
394     new PHPSyntaxRdr();
395
396     PHPCore.initializeDefaultPluginPreferences();
397     PreferenceConstants.initializeDefaultValues(store);
398
399     externalTools.initializeDefaultPreferences(store);
400   }
401
402   /**
403    * Returns the standard display to be used. The method first checks, if
404    * the thread calling this method has an associated display. If so, this
405    * display is returned. Otherwise the method returns the default display.
406    */
407   public static Display getStandardDisplay() {
408     Display display = Display.getCurrent();
409     if (display == null) {
410       display = Display.getDefault();
411     }
412     return display;
413   }
414
415   public void startup() throws CoreException {
416     super.startup();
417     IAdapterManager manager = Platform.getAdapterManager();
418     manager.registerAdapters(new PHPElementAdapterFactory(), PHPElement.class);
419     manager.registerAdapters(new ResourceAdapterFactory(), IResource.class);
420     //  externalTools.startUp(); 
421     getStandardDisplay().asyncExec(new Runnable() {
422       public void run() {
423         //initialize the variable context manager
424         VariableContextManager.getDefault();
425       }
426     });
427   }
428
429   /**
430    * @see org.eclipse.core.runtime.Plugin#shutdown()
431    */
432   public void shutdown() throws CoreException {
433     //  externalTools.shutDown();
434     ColorManager.getDefault().dispose();
435   }
436
437   public void setLastEditorFile(IFile textEditor) {
438     this.fLastEditorFile = textEditor;
439   }
440
441   public IFile getLastEditorFile() {
442     return fLastEditorFile;
443   }
444 }