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