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