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