deleted refs to org.eclipse.update.internal.ui.UpdateUIPlugin
[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
44 /**
45  * The main plugin class to be used in the desktop.
46  */
47 public class PHPeclipsePlugin
48   extends AbstractUIPlugin
49   implements IPreferenceConstants {
50   //    public static final String LOCALHOST_PREF = "_localhost";
51   //    public static final String DOCUMENTROOT_PREF = "_documentroot";
52   //    public static final String USE_EXTERNAL_BROWSER_PREF = "_use_external_browser";
53   //    public static final String EXTERNAL_BROWSER_PREF = "_external_browser";
54   //    public static final String MYSQL_PREF = "_my_sql";
55   //    public static final String APACHE_START_PREF = "_apache_start";
56   //    public static final String APACHE_STOP_PREF = "_apache_stop";
57   //    public static final String APACHE_RESTART_PREF = "_apache_restart";
58   //  public static final String SHOW_OUTPUT_IN_CONSOLE = "_sho_output_in_console";
59   //  public static final String EXTERNAL_PARSER_PREF = "_external_parser";
60
61   /**
62    * The id of the PHP plugin (value <code>"net.sourceforge.phpeclipse"</code>).
63    */
64   public static final String PLUGIN_ID = "net.sourceforge.phpeclipse"; //$NON-NLS-1$
65   public final static String PHP_NATURE_ID = PLUGIN_ID + ".phpnature";
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 = 
71     "net.sourceforge.phpdt.internal.compiler.parser.Parser";
72
73   /** Change this if you want to switch PHP Parser. */
74   public static final String PHPPARSER = PHPPARSER_ORIGINAL;
75
76   //The shared instance.
77   private static PHPeclipsePlugin plugin;
78   //Resource bundle.
79   //private ResourceBundle resourceBundle;
80
81   private ImageDescriptorRegistry fImageDescriptorRegistry;
82   private PHPDocumentProvider fCompilationUnitDocumentProvider;
83   
84   private JavaTextTools fJavaTextTools;
85   
86   /**
87   * The Java virtual machine that we are running on.
88   */
89   private static int jvm;
90
91   /** MRJ 2.0 */
92   private static final int MRJ_2_0 = 0;
93
94   /** MRJ 2.1 or later */
95   private static final int MRJ_2_1 = 1;
96
97   /** Java on Mac OS X 10.0 (MRJ 3.0) */
98   private static final int MRJ_3_0 = 3;
99
100   /** MRJ 3.1 */
101   private static final int MRJ_3_1 = 4;
102
103   /** Windows NT  */
104   private static final int WINDOWS_NT = 5;
105
106   /** Windows 9x  */
107   private static final int WINDOWS_9x = 6;
108
109   /** JVM constant for any other platform */
110   private static final int OTHER = -1;
111
112   /** General debug flag*/
113   public static final boolean DEBUG = false;
114   /**
115    * The constructor.
116    */
117   public PHPeclipsePlugin(IPluginDescriptor descriptor) {
118     super(descriptor);
119     plugin = this;
120     setJVM();
121     //    try {
122     //      resourceBundle = ResourceBundle.getBundle("net.sourceforge.PHPeclipsePluginResources");
123     //    } catch (MissingResourceException x) {
124     //      resourceBundle = null;
125     //    }
126   }
127
128   public static ImageDescriptorRegistry getImageDescriptorRegistry() {
129     return getDefault().internalGetImageDescriptorRegistry();
130   }
131
132   private ImageDescriptorRegistry internalGetImageDescriptorRegistry() {
133     if (fImageDescriptorRegistry == null)
134       fImageDescriptorRegistry = new ImageDescriptorRegistry();
135     return fImageDescriptorRegistry;
136   }
137   // @TODO: refactor this into a better method name !
138   public synchronized PHPDocumentProvider getCompilationUnitDocumentProvider() {
139     if (fCompilationUnitDocumentProvider == null)
140       fCompilationUnitDocumentProvider= new PHPDocumentProvider();
141     return fCompilationUnitDocumentProvider;
142   }
143   
144   private static void setJVM() {
145     String osName = System.getProperty("os.name");
146
147     if (osName.startsWith("Mac OS")) {
148       String mrjVersion = System.getProperty("mrj.version");
149       String majorMRJVersion = mrjVersion.substring(0, 3);
150       jvm = OTHER;
151       try {
152
153         double version = Double.valueOf(majorMRJVersion).doubleValue();
154
155         if (version == 2) {
156           jvm = MRJ_2_0;
157         } else if (version >= 2.1 && version < 3) {
158           jvm = MRJ_2_1;
159         } else if (version == 3.0) {
160           jvm = MRJ_3_0;
161         } else if (version >= 3.1) {
162           jvm = MRJ_3_1;
163         }
164
165       } catch (NumberFormatException nfe) {
166
167       }
168
169     } else if (osName.startsWith("Windows")) {
170       if (osName.indexOf("9") != -1) {
171         jvm = WINDOWS_9x;
172       } else {
173         jvm = WINDOWS_NT;
174       }
175     }
176   }
177   public static int getJVM() {
178     return jvm;
179   }
180   /**
181    * Returns the shared instance.
182    */
183   public static PHPeclipsePlugin getDefault() {
184     return plugin;
185   }
186
187   /**
188    * Returns the workspace instance.
189    */
190   public static IWorkspace getWorkspace() {
191     return ResourcesPlugin.getWorkspace();
192   }
193
194   public static IWorkbenchPage getActivePage() {
195         return getDefault().internalGetActivePage();
196   }
197
198   private IWorkbenchPage internalGetActivePage() {
199           IWorkbenchWindow window = getWorkbench().getActiveWorkbenchWindow();
200           if (window!=null) 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(
292         EXTERNAL_BROWSER_PREF,
293         "command.com /c start iexplore {0}");
294     } else if (windowsSystem.equals(BootLoader.WS_WIN32)) {
295       store.setDefault(
296         EXTERNAL_BROWSER_PREF,
297         "rundll32 url.dll,FileProtocolHandler {0}");
298     } else {
299       store.setDefault(EXTERNAL_BROWSER_PREF, "netscape {0}");
300     }
301     store.setDefault(
302       DOCUMENTROOT_PREF,
303       getWorkspace().getRoot().getLocation().toString());
304     //  if ((jvm == WINDOWS_9x) || (jvm == WINDOWS_NT)) {
305     if (windowsSystem.equals(BootLoader.WS_WIN32)) {
306       store.setDefault(EXTERNAL_PARSER_PREF, "c:\\apache\\php\\php -l -f {0}");
307       store.setDefault(
308         MYSQL_PREF,
309         "c:\\apache\\mysql\\bin\\mysqld-nt.exe --standalone");
310       store.setDefault(
311         APACHE_START_PREF,
312         "c:\\apache\\apache.exe -c \"DocumentRoot \"{0}\"\"");
313       store.setDefault(APACHE_STOP_PREF, "c:\\apache\\apache.exe -k shutdown");
314       store.setDefault(
315         APACHE_RESTART_PREF,
316         "c:\\apache\\apache.exe -k restart");
317     } else {
318       store.setDefault(EXTERNAL_PARSER_PREF, "/apache/php/php -l -f {0}");
319       store.setDefault(MYSQL_PREF, "/apache/mysql/bin/mysqld --standalone");
320       store.setDefault(
321         APACHE_START_PREF,
322         "/apache/apache -c \"DocumentRoot \"{0}\"\"");
323       store.setDefault(APACHE_STOP_PREF, "/apache/apache.exe -k shutdown");
324       store.setDefault(APACHE_RESTART_PREF, "/apache/apache -k restart");
325
326     }
327
328     store.setDefault(PHP_PARSER_DEFAULT, PHP_EXTERNAL_PARSER);
329     store.setDefault(PHP_INTERNAL_PARSER, "false");
330     store.setDefault(PHP_EXTERNAL_PARSER, "true");
331
332     store.setDefault(PHP_PARSE_ON_SAVE, "true");
333
334     // show line numbers:
335  //   store.setDefault(LINE_NUMBER_RULER, "false");
336 //    store.setDefault(FORMATTER_TAB_SIZE, "4");
337
338     // php syntax highlighting
339     store.setDefault(PHP_USERDEF_XMLFILE, ""); //assume there is none  chooA
340
341     PreferenceConverter.setDefault(
342       store,
343       PHP_MULTILINE_COMMENT,
344       PHPColorProvider.MULTI_LINE_COMMENT);
345     PreferenceConverter.setDefault(
346       store,
347       PHP_SINGLELINE_COMMENT,
348       PHPColorProvider.SINGLE_LINE_COMMENT);
349     PreferenceConverter.setDefault(
350       store,
351       PHP_KEYWORD,
352       PHPColorProvider.KEYWORD);
353     PreferenceConverter.setDefault(
354       store,
355       PHP_VARIABLE,
356       PHPColorProvider.VARIABLE);
357     PreferenceConverter.setDefault(
358       store,
359       PHP_FUNCTIONNAME,
360       PHPColorProvider.FUNCTION_NAME); 
361     PreferenceConverter.setDefault(
362       store,
363       PHP_CONSTANT,
364       PHPColorProvider.CONSTANT);
365     PreferenceConverter.setDefault(store, PHP_TYPE, PHPColorProvider.TYPE);
366     PreferenceConverter.setDefault(store, PHP_STRING, PHPColorProvider.STRING);
367     PreferenceConverter.setDefault(
368       store,
369       PHP_DEFAULT,
370       PHPColorProvider.DEFAULT);
371 //    PreferenceConverter.setDefault(
372 //      store,
373 //      PHP_EDITOR_BACKGROUND,
374 //      PHPColorProvider.BACKGROUND);
375 //    PreferenceConverter.setDefault(
376 //      store,
377 //      LINKED_POSITION_COLOR,
378 //      PHPColorProvider.LINKED_POSITION_COLOR);
379 //    PreferenceConverter.setDefault(
380 //      store,
381 //      LINE_NUMBER_COLOR,
382 //      PHPColorProvider.LINE_NUMBER_COLOR);
383
384 //    // set default PHPDoc colors:
385 //    PreferenceConverter.setDefault(
386 //      store,
387 //      PHPDOC_KEYWORD, 
388 //      PHPColorProvider.PHPDOC_KEYWORD);
389 //    PreferenceConverter.setDefault(
390 //      store,
391 //      PHPDOC_LINK, 
392 //      PHPColorProvider.PHPDOC_LINK);      
393 //    PreferenceConverter.setDefault(
394 //      store,
395 //      PHPDOC_DEFAULT, 
396 //      PHPColorProvider.PHPDOC_DEFAULT);
397 //    PreferenceConverter.setDefault(
398 //      store,
399 //      PHPDOC_TAG, 
400 //      PHPColorProvider.PHPDOC_TAG);
401       
402 //    store.setDefault(PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT, "true");
403 //    PreferenceConverter.setDefault(
404 //      store,
405 //      PREFERENCE_COLOR_BACKGROUND,
406 //      PHPColorProvider.BACKGROUND_COLOR);
407
408     //language stuff
409     store.setDefault(RESOURCE_BUNDLE, LANGUAGE_DEFAULT);
410     store.setDefault(RESOURCE_BUNDLE_EN_GB, "true");
411     store.setDefault(RESOURCE_BUNDLE_DE, "false");
412     store.setDefault(RESOURCE_BUNDLE_FR, "false");
413     store.setDefault(RESOURCE_BUNDLE_ES, "false");
414
415     store.setDefault(PHP_OUTLINE_CLASS, "true"); //$NON-NLS-1$
416     store.setDefault(PHP_OUTLINE_FUNC, "true"); //$NON-NLS-1$
417     store.setDefault(PHP_OUTLINE_VAR, "true"); //$NON-NLS-1$
418
419     TemplatePreferencePage.initDefaults(store);
420     //this will initialize the static fields in the syntaxrdr class
421     new PHPSyntaxRdr();
422
423     PHPCore.initializeDefaultPluginPreferences();
424     PreferenceConstants.initializeDefaultValues(store);
425   }
426
427   public void startup() throws CoreException {
428     super.startup();
429     IAdapterManager manager = Platform.getAdapterManager();
430     manager.registerAdapters(new PHPElementAdapterFactory(), PHPElement.class);
431     manager.registerAdapters(new ResourceAdapterFactory(), IResource.class);
432   }
433 }