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