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