added a first php $f command to the PHP console
[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.ui.texteditor.ITextEditor;
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   private ITextEditor fTextEditor = 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     //    try {
124     //      resourceBundle = ResourceBundle.getBundle("net.sourceforge.PHPeclipsePluginResources");
125     //    } catch (MissingResourceException x) {
126     //      resourceBundle = null;
127     //    }
128   }
129
130   public static ImageDescriptorRegistry getImageDescriptorRegistry() {
131     return getDefault().internalGetImageDescriptorRegistry();
132   }
133
134   private ImageDescriptorRegistry internalGetImageDescriptorRegistry() {
135     if (fImageDescriptorRegistry == null)
136       fImageDescriptorRegistry = new ImageDescriptorRegistry();
137     return fImageDescriptorRegistry;
138   }
139   // @TODO: refactor this into a better method name !
140   public synchronized PHPDocumentProvider getCompilationUnitDocumentProvider() {
141     if (fCompilationUnitDocumentProvider == null)
142       fCompilationUnitDocumentProvider= new PHPDocumentProvider();
143     return fCompilationUnitDocumentProvider;
144   }
145   
146   private static void setJVM() {
147     String osName = System.getProperty("os.name");
148
149     if (osName.startsWith("Mac OS")) {
150       String mrjVersion = System.getProperty("mrj.version");
151       String majorMRJVersion = mrjVersion.substring(0, 3);
152       jvm = OTHER;
153       try {
154
155         double version = Double.valueOf(majorMRJVersion).doubleValue();
156
157         if (version == 2) {
158           jvm = MRJ_2_0;
159         } else if (version >= 2.1 && version < 3) {
160           jvm = MRJ_2_1;
161         } else if (version == 3.0) {
162           jvm = MRJ_3_0;
163         } else if (version >= 3.1) {
164           jvm = MRJ_3_1;
165         }
166
167       } catch (NumberFormatException nfe) {
168
169       }
170
171     } else if (osName.startsWith("Windows")) {
172       if (osName.indexOf("9") != -1) {
173         jvm = WINDOWS_9x;
174       } else {
175         jvm = WINDOWS_NT;
176       }
177     }
178   }
179   public static int getJVM() {
180     return jvm;
181   }
182   /**
183    * Returns the shared instance.
184    */
185   public static PHPeclipsePlugin getDefault() {
186     return plugin;
187   }
188
189   /**
190    * Returns the workspace instance.
191    */
192   public static IWorkspace getWorkspace() {
193     return ResourcesPlugin.getWorkspace();
194   }
195
196   public static IWorkbenchPage getActivePage() {
197         return getDefault().internalGetActivePage();
198   }
199
200   private IWorkbenchPage internalGetActivePage() {
201           IWorkbenchWindow window = getWorkbench().getActiveWorkbenchWindow();
202           if (window!=null) return window.getActivePage();
203           return null;
204   }
205   
206   public static IWorkbenchWindow getActiveWorkbenchWindow() {
207     return getDefault().getWorkbench().getActiveWorkbenchWindow();
208   }
209
210   public static Shell getActiveWorkbenchShell() {
211     return getActiveWorkbenchWindow().getShell();
212   }
213
214   public static String getPluginId() {
215     return getDefault().getDescriptor().getUniqueIdentifier();
216   }
217
218   public static void log(IStatus status) {
219     getDefault().getLog().log(status);
220   }
221
222   //  public static void logErrorMessage(String message) {
223   //    log(new Status(IStatus.ERROR, getPluginId(), JavaStatusConstants.INTERNAL_ERROR, message, null));
224   //  }
225   //
226   //  public static void logErrorStatus(String message, IStatus status) {
227   //    if (status == null) {
228   //      logErrorMessage(message);
229   //      return;
230   //    }
231   //    MultiStatus multi= new MultiStatus(getPluginId(), JavaStatusConstants.INTERNAL_ERROR, message, null);
232   //    multi.add(status);
233   //    log(multi);
234   //  }
235   //
236   //  public static void log(Throwable e) {
237   //    log(new Status(IStatus.ERROR, getPluginId(), JavaStatusConstants.INTERNAL_ERROR, JavaUIMessages.getString("JavaPlugin.internal_error"), e)); //$NON-NLS-1$
238   //  }
239
240   public static void log(int severity, String message) {
241     Status status = new Status(severity, PLUGIN_ID, IStatus.OK, message, null);
242     log(status);
243   }
244   public static void log(Throwable e) {
245     log(new Status(IStatus.ERROR, PLUGIN_ID, IStatus.ERROR, "PHPeclipsePlugin.internalErrorOccurred", e)); //$NON-NLS-1$
246   }
247
248   public static boolean isDebug() {
249     return getDefault().isDebugging();
250   }
251
252   static IPath getInstallLocation() {
253     return new Path(getDefault().getDescriptor().getInstallURL().getFile());
254   }
255   
256   public synchronized JavaTextTools getJavaTextTools() {
257     if (fJavaTextTools == null)
258       fJavaTextTools= new JavaTextTools(getPreferenceStore());
259     return fJavaTextTools;
260   }
261   
262   /**
263    * Returns the string from the plugin's resource bundle,
264    * or 'key' if not found.
265    */
266   //  public static String getResourceString(String key) {
267   //    ResourceBundle bundle = PHPeclipsePlugin.getDefault().getResourceBundle();
268   //    try {
269   //      return bundle.getString(key);
270   //    } catch (MissingResourceException e) {
271   //      return key;
272   //    }
273   //  }
274
275   /**
276    * Returns the plugin's resource bundle,
277    */
278   //  public ResourceBundle getResourceBundle() {
279   //    return resourceBundle;
280   //  }
281
282   protected void initializeDefaultPreferences(IPreferenceStore store) {
283     // windows preferences:
284     store.setDefault(LOCALHOST_PREF, "http://localhost");
285
286     store.setDefault(SHOW_EXTERNAL_PREVIEW_PREF, "true");
287     store.setDefault(USE_EXTERNAL_BROWSER_PREF, "false");
288     store.setDefault(SHOW_OUTPUT_IN_CONSOLE, "true");
289
290     String windowsSystem = BootLoader.getWS();
291
292     if (jvm == WINDOWS_9x) {
293       store.setDefault(
294         EXTERNAL_BROWSER_PREF,
295         "command.com /c start iexplore {0}");
296     } else if (windowsSystem.equals(BootLoader.WS_WIN32)) {
297       store.setDefault(
298         EXTERNAL_BROWSER_PREF,
299         "rundll32 url.dll,FileProtocolHandler {0}");
300     } else {
301       store.setDefault(EXTERNAL_BROWSER_PREF, "netscape {0}");
302     }
303     store.setDefault(
304       DOCUMENTROOT_PREF,
305       getWorkspace().getRoot().getLocation().toString());
306       
307     //  if ((jvm == WINDOWS_9x) || (jvm == WINDOWS_NT)) {
308     if (windowsSystem.equals(BootLoader.WS_WIN32)) {
309       store.setDefault(PHP_RUN_PREF, "c:\\apache\\php\\php.exe");
310       store.setDefault(EXTERNAL_PARSER_PREF, "c:\\apache\\php\\php -l -f {0}");
311       store.setDefault(
312         MYSQL_PREF,
313         "c:\\apache\\mysql\\bin\\mysqld-nt.exe --standalone");
314       store.setDefault(
315         APACHE_START_PREF,
316         "c:\\apache\\apache.exe -c \"DocumentRoot \"{0}\"\"");
317       store.setDefault(APACHE_STOP_PREF, "c:\\apache\\apache.exe -k shutdown");
318       store.setDefault(
319         APACHE_RESTART_PREF,
320         "c:\\apache\\apache.exe -k restart");
321     } else {
322       store.setDefault(PHP_RUN_PREF, "/apache/php/php");
323       store.setDefault(EXTERNAL_PARSER_PREF, "/apache/php/php -l -f {0}");
324       store.setDefault(MYSQL_PREF, "/apache/mysql/bin/mysqld --standalone");
325       store.setDefault(
326         APACHE_START_PREF,
327         "/apache/apache -c \"DocumentRoot \"{0}\"\"");
328       store.setDefault(APACHE_STOP_PREF, "/apache/apache.exe -k shutdown");
329       store.setDefault(APACHE_RESTART_PREF, "/apache/apache -k restart");
330
331     }
332
333     store.setDefault(PHP_PARSER_DEFAULT, PHP_EXTERNAL_PARSER);
334     store.setDefault(PHP_INTERNAL_PARSER, "false");
335     store.setDefault(PHP_EXTERNAL_PARSER, "true");
336
337     store.setDefault(PHP_PARSE_ON_SAVE, "true");
338
339     // show line numbers:
340  //   store.setDefault(LINE_NUMBER_RULER, "false");
341 //    store.setDefault(FORMATTER_TAB_SIZE, "4");
342
343     // php syntax highlighting
344     store.setDefault(PHP_USERDEF_XMLFILE, ""); //assume there is none  chooA
345
346     PreferenceConverter.setDefault(
347       store,
348       PHP_MULTILINE_COMMENT,
349       PHPColorProvider.MULTI_LINE_COMMENT);
350     PreferenceConverter.setDefault(
351       store,
352       PHP_SINGLELINE_COMMENT,
353       PHPColorProvider.SINGLE_LINE_COMMENT);
354     PreferenceConverter.setDefault(
355       store,
356       PHP_KEYWORD,
357       PHPColorProvider.KEYWORD);
358     PreferenceConverter.setDefault(
359       store,
360       PHP_VARIABLE,
361       PHPColorProvider.VARIABLE);
362     PreferenceConverter.setDefault(
363       store,
364       PHP_FUNCTIONNAME,
365       PHPColorProvider.FUNCTION_NAME); 
366     PreferenceConverter.setDefault(
367       store,
368       PHP_CONSTANT,
369       PHPColorProvider.CONSTANT);
370     PreferenceConverter.setDefault(store, PHP_TYPE, PHPColorProvider.TYPE);
371     PreferenceConverter.setDefault(store, PHP_STRING, PHPColorProvider.STRING);
372     PreferenceConverter.setDefault(
373       store,
374       PHP_DEFAULT,
375       PHPColorProvider.DEFAULT);
376 //    PreferenceConverter.setDefault(
377 //      store,
378 //      PHP_EDITOR_BACKGROUND,
379 //      PHPColorProvider.BACKGROUND);
380 //    PreferenceConverter.setDefault(
381 //      store,
382 //      LINKED_POSITION_COLOR,
383 //      PHPColorProvider.LINKED_POSITION_COLOR);
384 //    PreferenceConverter.setDefault(
385 //      store,
386 //      LINE_NUMBER_COLOR,
387 //      PHPColorProvider.LINE_NUMBER_COLOR);
388
389 //    // set default PHPDoc colors:
390 //    PreferenceConverter.setDefault(
391 //      store,
392 //      PHPDOC_KEYWORD, 
393 //      PHPColorProvider.PHPDOC_KEYWORD);
394 //    PreferenceConverter.setDefault(
395 //      store,
396 //      PHPDOC_LINK, 
397 //      PHPColorProvider.PHPDOC_LINK);      
398 //    PreferenceConverter.setDefault(
399 //      store,
400 //      PHPDOC_DEFAULT, 
401 //      PHPColorProvider.PHPDOC_DEFAULT);
402 //    PreferenceConverter.setDefault(
403 //      store,
404 //      PHPDOC_TAG, 
405 //      PHPColorProvider.PHPDOC_TAG);
406       
407 //    store.setDefault(PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT, "true");
408 //    PreferenceConverter.setDefault(
409 //      store,
410 //      PREFERENCE_COLOR_BACKGROUND,
411 //      PHPColorProvider.BACKGROUND_COLOR);
412
413     //language stuff
414     store.setDefault(RESOURCE_BUNDLE, LANGUAGE_DEFAULT);
415     store.setDefault(RESOURCE_BUNDLE_EN_GB, "true");
416     store.setDefault(RESOURCE_BUNDLE_DE, "false");
417     store.setDefault(RESOURCE_BUNDLE_FR, "false");
418     store.setDefault(RESOURCE_BUNDLE_ES, "false");
419
420     store.setDefault(PHP_OUTLINE_CLASS, "true"); //$NON-NLS-1$
421     store.setDefault(PHP_OUTLINE_FUNC, "true"); //$NON-NLS-1$
422     store.setDefault(PHP_OUTLINE_VAR, "true"); //$NON-NLS-1$
423
424     TemplatePreferencePage.initDefaults(store);
425     //this will initialize the static fields in the syntaxrdr class
426     new PHPSyntaxRdr();
427
428     PHPCore.initializeDefaultPluginPreferences();
429     PreferenceConstants.initializeDefaultValues(store);
430   }
431
432   public void startup() throws CoreException {
433     super.startup();
434     IAdapterManager manager = Platform.getAdapterManager();
435     manager.registerAdapters(new PHPElementAdapterFactory(), PHPElement.class);
436     manager.registerAdapters(new ResourceAdapterFactory(), IResource.class);
437   }
438
439   public void setTextEditor(ITextEditor textEditor) {
440     this.fTextEditor = textEditor;
441   }
442
443   public ITextEditor getTextEditor() {
444     return fTextEditor;
445   }
446 }