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