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