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