improved php parser for keywords do-while, null, false, true...
[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 java.util.MissingResourceException;
15 import java.util.ResourceBundle;
16
17 import net.sourceforge.phpeclipse.phpeditor.PHPDocumentProvider;
18 import org.eclipse.core.resources.IWorkspace;
19 import org.eclipse.core.resources.ResourcesPlugin;
20 import org.eclipse.core.runtime.IPath;
21 import org.eclipse.core.runtime.IPluginDescriptor;
22 import org.eclipse.core.runtime.IStatus;
23 import org.eclipse.core.runtime.MultiStatus;
24 import org.eclipse.core.runtime.Path;
25 import org.eclipse.core.runtime.Status;
26 //import org.eclipse.jdt.internal.ui.JavaStatusConstants;
27 //import org.eclipse.jdt.internal.ui.JavaUIMessages;
28 import org.eclipse.jface.preference.IPreferenceStore;
29 import org.eclipse.swt.widgets.Shell;
30 import org.eclipse.ui.IWorkbenchPage;
31 import org.eclipse.ui.IWorkbenchWindow;
32 import org.eclipse.ui.plugin.AbstractUIPlugin;
33
34 /**
35  * The main plugin class to be used in the desktop.
36  */
37 public class PHPeclipsePlugin extends AbstractUIPlugin {
38         public static final String LOCALHOST_PREF = "_localhost";
39         public static final String DOCUMENTROOT_PREF = "_documentroot";
40         public static final String USE_EXTERNAL_BROWSER_PREF = "_use_external_browser";
41         public static final String EXTERNAL_BROWSER_PREF = "_external_browser";
42         public static final String MYSQL_PREF = "_my_sql";
43         public static final String APACHE_START_PREF = "_apache_start";
44         public static final String APACHE_STOP_PREF = "_apache_stop";
45         public static final String APACHE_RESTART_PREF = "_apache_restart";
46   public static final String SHOW_OUTPUT_IN_CONSOLE = "_sho_output_in_console";
47   public static final String EXTERNAL_PARSER_PREF = "_external_parser";
48   
49   /**
50    * The id of the PHP plugin (value <code>"net.sourceforge.phpeclipse"</code>).
51    */ 
52   public static final String ID_PLUGIN= "net.sourceforge.phpeclipse"; //$NON-NLS-1$
53   
54         //The shared instance.
55         private static PHPeclipsePlugin plugin;
56         //Resource bundle.
57         private ResourceBundle resourceBundle;
58   
59   private PHPDocumentProvider fCompilationUnitDocumentProvider;
60         /**
61         * The Java virtual machine that we are running on.  
62         */
63         private static int jvm;
64
65         /** MRJ 2.0 */
66         private static final int MRJ_2_0 = 0;
67
68         /** MRJ 2.1 or later */
69         private static final int MRJ_2_1 = 1;
70
71         /** Java on Mac OS X 10.0 (MRJ 3.0) */
72         private static final int MRJ_3_0 = 3;
73
74         /** MRJ 3.1 */
75         private static final int MRJ_3_1 = 4;
76
77         /** Windows NT  */
78         private static final int WINDOWS_NT = 5;
79
80         /** Windows 9x  */
81         private static final int WINDOWS_9x = 6;
82
83         /** JVM constant for any other platform */
84         private static final int OTHER = -1;
85         /**
86          * The constructor.
87          */
88         public PHPeclipsePlugin(IPluginDescriptor descriptor) {
89                 super(descriptor);
90                 plugin = this;
91                 setJVM();
92                 try {
93                         resourceBundle = ResourceBundle.getBundle("net.sourceforge.PHPeclipsePluginResources");
94                 } catch (MissingResourceException x) {
95                         resourceBundle = null;
96                 }
97         }
98
99   // @TODO: refactor this into a better method name !
100   public PHPDocumentProvider getCompilationUnitDocumentProvider() {
101     if (fCompilationUnitDocumentProvider == null)
102       fCompilationUnitDocumentProvider= new PHPDocumentProvider();
103     return fCompilationUnitDocumentProvider;
104   }
105   
106         public static void setJVM() {
107                 String osName = System.getProperty("os.name");
108
109                 if (osName.startsWith("Mac OS")) {
110                         String mrjVersion = System.getProperty("mrj.version");
111                         String majorMRJVersion = mrjVersion.substring(0, 3);
112                         jvm = OTHER;
113                         try {
114
115                                 double version = Double.valueOf(majorMRJVersion).doubleValue();
116
117                                 if (version == 2) {
118                                         jvm = MRJ_2_0;
119                                 } else if (version >= 2.1 && version < 3) {
120                                         jvm = MRJ_2_1;
121                                 } else if (version == 3.0) {
122                                         jvm = MRJ_3_0;
123                                 } else if (version >= 3.1) {
124                                         jvm = MRJ_3_1;
125                                 }
126
127                         } catch (NumberFormatException nfe) {
128
129                         }
130
131                 } else if (osName.startsWith("Windows")) {
132                         if (osName.indexOf("9") != -1) {
133                                 jvm = WINDOWS_9x;
134                         } else {
135                                 jvm = WINDOWS_NT;
136                         }
137                 }
138         }
139         public static int getJVM() {
140                 return jvm;
141         }
142         /**
143          * Returns the shared instance.
144          */
145         public static PHPeclipsePlugin getDefault() {
146                 return plugin;
147         }
148
149         /**
150          * Returns the workspace instance.
151          */
152         public static IWorkspace getWorkspace() {
153                 return ResourcesPlugin.getWorkspace();
154         }
155  
156   public static IWorkbenchPage getActivePage() {
157     return getDefault().getActivePage();
158   }
159   
160   public static IWorkbenchWindow getActiveWorkbenchWindow() {
161     return getDefault().getWorkbench().getActiveWorkbenchWindow();
162   }
163   
164   public static Shell getActiveWorkbenchShell() {
165     return getActiveWorkbenchWindow().getShell();
166   }
167   
168   public static String getPluginId() {
169     return getDefault().getDescriptor().getUniqueIdentifier();
170   }
171
172   public static void log(IStatus status) {
173     getDefault().getLog().log(status);
174   }
175   
176 //  public static void logErrorMessage(String message) {
177 //    log(new Status(IStatus.ERROR, getPluginId(), JavaStatusConstants.INTERNAL_ERROR, message, null));
178 //  }
179 //
180 //  public static void logErrorStatus(String message, IStatus status) {
181 //    if (status == null) {
182 //      logErrorMessage(message);
183 //      return; 
184 //    }
185 //    MultiStatus multi= new MultiStatus(getPluginId(), JavaStatusConstants.INTERNAL_ERROR, message, null);
186 //    multi.add(status);
187 //    log(multi);
188 //  }
189 //  
190 //  public static void log(Throwable e) {
191 //    log(new Status(IStatus.ERROR, getPluginId(), JavaStatusConstants.INTERNAL_ERROR, JavaUIMessages.getString("JavaPlugin.internal_error"), e)); //$NON-NLS-1$
192 //  }
193   
194   public static boolean isDebug() {
195     return getDefault().isDebugging();
196   }
197   
198   static IPath getInstallLocation() {
199     return new Path(getDefault().getDescriptor().getInstallURL().getFile());
200   }
201
202         /**
203          * Returns the string from the plugin's resource bundle,
204          * or 'key' if not found.
205          */
206         public static String getResourceString(String key) {
207                 ResourceBundle bundle = PHPeclipsePlugin.getDefault().getResourceBundle();
208                 try {
209                         return bundle.getString(key);
210                 } catch (MissingResourceException e) {
211                         return key;
212                 }
213         }
214
215         /**
216          * Returns the plugin's resource bundle,
217          */
218         public ResourceBundle getResourceBundle() {
219                 return resourceBundle;
220         }
221
222         protected void initializeDefaultPreferences(IPreferenceStore store) {
223                 // windows preferences:
224                 store.setDefault(LOCALHOST_PREF, "http://localhost");
225                 
226                 store.setDefault(USE_EXTERNAL_BROWSER_PREF, "false");
227     store.setDefault(SHOW_OUTPUT_IN_CONSOLE, "true");
228                 if (jvm == WINDOWS_9x) {
229                         store.setDefault(EXTERNAL_BROWSER_PREF, "command.com /c start iexplore {0}");
230                 } else if (jvm == WINDOWS_NT) {
231                         store.setDefault(EXTERNAL_BROWSER_PREF, "rundll32 url.dll,FileProtocolHandler {0}");
232                 } else {
233                         store.setDefault(EXTERNAL_BROWSER_PREF, "netscape {0}");
234                 }
235                 if ((jvm == WINDOWS_9x) || (jvm == WINDOWS_NT)) {
236       store.setDefault(EXTERNAL_PARSER_PREF, "c:\\apache\\php\\php -l -f {0}");
237       store.setDefault(DOCUMENTROOT_PREF, "c:\\eclipse\\workspace");
238                         store.setDefault(MYSQL_PREF, "c:\\apache\\mysql\\bin\\mysqld.exe --standalone");
239                         store.setDefault(APACHE_START_PREF, "c:\\apache\\apache.exe -c \"DocumentRoot \"{0}\"\"");
240                         store.setDefault(APACHE_STOP_PREF, "c:\\apache\\apache.exe -k shutdown");
241                         store.setDefault(APACHE_RESTART_PREF, "c:\\apache\\apache.exe -k restart");
242                 } else {
243       store.setDefault(EXTERNAL_PARSER_PREF, "/apache/php/php -l -f {0}");
244       store.setDefault(DOCUMENTROOT_PREF, "/eclipse/workspace");
245                         store.setDefault(MYSQL_PREF, "/apache/mysql/bin/mysqld --standalone");
246                         store.setDefault(APACHE_START_PREF, "/apache/apache -c \"DocumentRoot \"{0}\"\"");
247                         store.setDefault(APACHE_STOP_PREF, "/apache/apache.exe -k shutdown");
248                         store.setDefault(APACHE_RESTART_PREF, "/apache/apache -k restart");
249       
250                 }
251         }
252 }