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
 
   9     IBM Corporation - Initial implementation
 
  10     Klaus Hartlage - www.eclipseproject.de
 
  11 **********************************************************************/
 
  12 package net.sourceforge.phpeclipse;
 
  14 import net.sourceforge.phpdt.externaltools.internal.model.ColorManager;
 
  15 import net.sourceforge.phpdt.externaltools.internal.model.ExternalToolsPlugin;
 
  16 import net.sourceforge.phpdt.externaltools.internal.model.VariableContextManager;
 
  17 import net.sourceforge.phpdt.internal.ui.preferences.TemplatePreferencePage;
 
  18 import net.sourceforge.phpdt.internal.ui.viewsupport.ImageDescriptorRegistry;
 
  19 import net.sourceforge.phpdt.ui.PreferenceConstants;
 
  20 import net.sourceforge.phpdt.ui.text.JavaTextTools;
 
  21 import net.sourceforge.phpeclipse.phpeditor.PHPDocumentProvider;
 
  22 import net.sourceforge.phpeclipse.phpeditor.PHPSyntaxRdr;
 
  23 import net.sourceforge.phpeclipse.phpeditor.util.PHPColorProvider;
 
  24 import net.sourceforge.phpeclipse.resourcesview.PHPElement;
 
  25 import net.sourceforge.phpeclipse.resourcesview.PHPElementAdapterFactory;
 
  26 import net.sourceforge.phpeclipse.resourcesview.ResourceAdapterFactory;
 
  28 import org.eclipse.core.boot.BootLoader;
 
  29 import org.eclipse.core.resources.IFile;
 
  30 import org.eclipse.core.resources.IResource;
 
  31 import org.eclipse.core.resources.IWorkspace;
 
  32 import org.eclipse.core.resources.ResourcesPlugin;
 
  33 import org.eclipse.core.runtime.CoreException;
 
  34 import org.eclipse.core.runtime.IAdapterManager;
 
  35 import org.eclipse.core.runtime.IPath;
 
  36 import org.eclipse.core.runtime.IPluginDescriptor;
 
  37 import org.eclipse.core.runtime.IStatus;
 
  38 import org.eclipse.core.runtime.Path;
 
  39 import org.eclipse.core.runtime.Platform;
 
  40 import org.eclipse.core.runtime.Status;
 
  41 import org.eclipse.jface.preference.IPreferenceStore;
 
  42 import org.eclipse.jface.preference.PreferenceConverter;
 
  43 import org.eclipse.swt.widgets.Display;
 
  44 import org.eclipse.swt.widgets.Shell;
 
  45 import org.eclipse.ui.IWorkbenchPage;
 
  46 import org.eclipse.ui.IWorkbenchWindow;
 
  47 import org.eclipse.ui.plugin.AbstractUIPlugin;
 
  50  * The main plugin class to be used in the desktop.
 
  52 public class PHPeclipsePlugin extends AbstractUIPlugin implements IPreferenceConstants {
 
  55    * The id of the PHP plugin (value <code>"net.sourceforge.phpeclipse"</code>).
 
  57   public static final String PLUGIN_ID = "net.sourceforge.phpeclipse"; //$NON-NLS-1$
 
  58   public final static String PHP_NATURE_ID = PLUGIN_ID + ".phpnature";
 
  61          * id of builder - matches plugin.xml (concatenate pluginid.builderid) 
 
  63         public static final String BUILDER_INDEX_ID =   PLUGIN_ID + ".indexbuilder";
 
  64         public static final String BUILDER_PARSER_ID =  PLUGIN_ID + ".parserbuilder";
 
  66   // public static final String PHP_RESOURCES_VIEW_ID = PLUGIN_ID + ".resourcesview.ViewPHPResources"; //$NON-NLS-1$
 
  67   public static final String PHP_CODING_ACTION_SET_ID = PLUGIN_ID + ".ui.CodingActionSet"; //$NON-NLS-1$
 
  69   public static final String PHPPARSER_NEW = "test.PHPParser";
 
  70   public static final String PHPPARSER_ORIGINAL = "net.sourceforge.phpdt.internal.compiler.parser.Parser";
 
  72   /** Change this if you want to switch PHP Parser. */
 
  73   public static final String PHPPARSER = PHPPARSER_ORIGINAL;
 
  75   //The shared instance.
 
  76   private static PHPeclipsePlugin plugin;
 
  78   private static ExternalToolsPlugin externalTools;
 
  80   //private ResourceBundle resourceBundle;
 
  82   private ImageDescriptorRegistry fImageDescriptorRegistry;
 
  83   private PHPDocumentProvider fCompilationUnitDocumentProvider;
 
  84   private IFile fLastEditorFile = null;
 
  86   private JavaTextTools fJavaTextTools;
 
  89   * The Java virtual machine that we are running on.
 
  91   private static int jvm;
 
  94   private static final int MRJ_2_0 = 0;
 
  96   /** MRJ 2.1 or later */
 
  97   private static final int MRJ_2_1 = 1;
 
  99   /** Java on Mac OS X 10.0 (MRJ 3.0) */
 
 100   private static final int MRJ_3_0 = 3;
 
 103   private static final int MRJ_3_1 = 4;
 
 106   private static final int WINDOWS_NT = 5;
 
 109   private static final int WINDOWS_9x = 6;
 
 111   /** JVM constant for any other platform */
 
 112   private static final int OTHER = -1;
 
 114   /** General debug flag*/
 
 115   public static final boolean DEBUG = false;
 
 119   public PHPeclipsePlugin(IPluginDescriptor descriptor) {
 
 123     externalTools = new ExternalToolsPlugin();
 
 125     //      resourceBundle = ResourceBundle.getBundle("net.sourceforge.PHPeclipsePluginResources");
 
 126     //    } catch (MissingResourceException x) {
 
 127     //      resourceBundle = null;
 
 131   public static ImageDescriptorRegistry getImageDescriptorRegistry() {
 
 132     return getDefault().internalGetImageDescriptorRegistry();
 
 135   private ImageDescriptorRegistry internalGetImageDescriptorRegistry() {
 
 136     if (fImageDescriptorRegistry == null)
 
 137       fImageDescriptorRegistry = new ImageDescriptorRegistry();
 
 138     return fImageDescriptorRegistry;
 
 140   // @TODO: refactor this into a better method name !
 
 141   public synchronized PHPDocumentProvider getCompilationUnitDocumentProvider() {
 
 142     if (fCompilationUnitDocumentProvider == null)
 
 143       fCompilationUnitDocumentProvider = new PHPDocumentProvider();
 
 144     return fCompilationUnitDocumentProvider;
 
 147   private static void setJVM() {
 
 148     String osName = System.getProperty("os.name");
 
 150     if (osName.startsWith("Mac OS")) {
 
 151       String mrjVersion = System.getProperty("mrj.version");
 
 152       String majorMRJVersion = mrjVersion.substring(0, 3);
 
 156         double version = Double.valueOf(majorMRJVersion).doubleValue();
 
 160         } else if (version >= 2.1 && version < 3) {
 
 162         } else if (version == 3.0) {
 
 164         } else if (version >= 3.1) {
 
 168       } catch (NumberFormatException nfe) {
 
 172     } else if (osName.startsWith("Windows")) {
 
 173       if (osName.indexOf("9") != -1) {
 
 180   public static int getJVM() {
 
 184    * Returns the shared instance.
 
 186   public static PHPeclipsePlugin getDefault() {
 
 190   //  public static ExternalToolsPlugin getExternalTools() {
 
 191   //    return externalTools;
 
 194    * Returns the workspace instance.
 
 196   public static IWorkspace getWorkspace() {
 
 197     return ResourcesPlugin.getWorkspace();
 
 200   public static IWorkbenchPage getActivePage() {
 
 201     return getDefault().internalGetActivePage();
 
 204   private IWorkbenchPage internalGetActivePage() {
 
 205     IWorkbenchWindow window = getWorkbench().getActiveWorkbenchWindow();
 
 207       return window.getActivePage();
 
 211   public static IWorkbenchWindow getActiveWorkbenchWindow() {
 
 212     return getDefault().getWorkbench().getActiveWorkbenchWindow();
 
 215   public static Shell getActiveWorkbenchShell() {
 
 216     return getActiveWorkbenchWindow().getShell();
 
 219   public static String getPluginId() {
 
 220     return getDefault().getDescriptor().getUniqueIdentifier();
 
 223   public static void log(IStatus status) {
 
 224     getDefault().getLog().log(status);
 
 227   //  public static void logErrorMessage(String message) {
 
 228   //    log(new Status(IStatus.ERROR, getPluginId(), JavaStatusConstants.INTERNAL_ERROR, message, null));
 
 231   //  public static void logErrorStatus(String message, IStatus status) {
 
 232   //    if (status == null) {
 
 233   //      logErrorMessage(message);
 
 236   //    MultiStatus multi= new MultiStatus(getPluginId(), JavaStatusConstants.INTERNAL_ERROR, message, null);
 
 237   //    multi.add(status);
 
 241   //  public static void log(Throwable e) {
 
 242   //    log(new Status(IStatus.ERROR, getPluginId(), JavaStatusConstants.INTERNAL_ERROR, JavaUIMessages.getString("JavaPlugin.internal_error"), e)); //$NON-NLS-1$
 
 245   public static void log(int severity, String message) {
 
 246     Status status = new Status(severity, PLUGIN_ID, IStatus.OK, message, null);
 
 249   public static void log(Throwable e) {
 
 250     log(new Status(IStatus.ERROR, PLUGIN_ID, IStatus.ERROR, "PHPeclipsePlugin.internalErrorOccurred", e)); //$NON-NLS-1$
 
 253   public static boolean isDebug() {
 
 254     return getDefault().isDebugging();
 
 257   static IPath getInstallLocation() {
 
 258     return new Path(getDefault().getDescriptor().getInstallURL().getFile());
 
 261   public synchronized JavaTextTools getJavaTextTools() {
 
 262     if (fJavaTextTools == null)
 
 263       fJavaTextTools = new JavaTextTools(getPreferenceStore());
 
 264     return fJavaTextTools;
 
 268    * Returns the string from the plugin's resource bundle,
 
 269    * or 'key' if not found.
 
 271   //  public static String getResourceString(String key) {
 
 272   //    ResourceBundle bundle = PHPeclipsePlugin.getDefault().getResourceBundle();
 
 274   //      return bundle.getString(key);
 
 275   //    } catch (MissingResourceException e) {
 
 281    * Returns the plugin's resource bundle,
 
 283   //  public ResourceBundle getResourceBundle() {
 
 284   //    return resourceBundle;
 
 287   protected void initializeDefaultPreferences(IPreferenceStore store) {
 
 288     // windows preferences:
 
 289     store.setDefault(LOCALHOST_PREF, "http://localhost");
 
 291     store.setDefault(SHOW_EXTERNAL_PREVIEW_PREF, "true");
 
 292     store.setDefault(USE_EXTERNAL_BROWSER_PREF, "false");
 
 293     store.setDefault(SHOW_OUTPUT_IN_CONSOLE, "true");
 
 295     String windowsSystem = BootLoader.getWS();
 
 297     if (jvm == WINDOWS_9x) {
 
 298       store.setDefault(EXTERNAL_BROWSER_PREF, "command.com /c start iexplore {0}");
 
 299     } else if (windowsSystem.equals(BootLoader.WS_WIN32)) {
 
 300       store.setDefault(EXTERNAL_BROWSER_PREF, "rundll32 url.dll,FileProtocolHandler {0}");
 
 302       store.setDefault(EXTERNAL_BROWSER_PREF, "netscape {0}");
 
 304     store.setDefault(DOCUMENTROOT_PREF, getWorkspace().getRoot().getLocation().toString());
 
 306     //  if ((jvm == WINDOWS_9x) || (jvm == WINDOWS_NT)) {
 
 308     if (windowsSystem.equals(BootLoader.WS_WIN32)) {
 
 309       store.setDefault(PHP_RUN_PREF, "c:\\apache\\php\\php.exe");
 
 310       store.setDefault(EXTERNAL_PARSER_PREF, "c:\\apache\\php\\php -l -f {0}");
 
 311       store.setDefault(MYSQL_RUN_PREF, "c:\\apache\\mysql\\bin\\mysqld-nt.exe");
 
 312       store.setDefault(APACHE_RUN_PREF, "c:\\apache\\apache.exe");
 
 314       store.setDefault(PHP_RUN_PREF, "/apache/php/php");
 
 315       store.setDefault(EXTERNAL_PARSER_PREF, "/apache/php/php -l -f {0}");
 
 316       store.setDefault(MYSQL_RUN_PREF, "/apache/mysql/bin/mysqld");
 
 317       store.setDefault(APACHE_RUN_PREF, "/apache/apache");
 
 319     store.setDefault(MYSQL_PREF, "--standalone");
 
 320     store.setDefault(APACHE_START_PREF, "-c \"DocumentRoot \"{0}\"\"");
 
 321     store.setDefault(APACHE_STOP_PREF, "-k shutdown");
 
 322     store.setDefault(APACHE_RESTART_PREF, "-k restart");
 
 324     store.setDefault(MYSQL_START_BACKGROUND, "true");
 
 325     store.setDefault(APACHE_START_BACKGROUND, "true");
 
 326     store.setDefault(APACHE_STOP_BACKGROUND, "true");
 
 327     store.setDefault(APACHE_RESTART_BACKGROUND, "true");
 
 329     store.setDefault(PHP_PARSER_DEFAULT, PHP_EXTERNAL_PARSER);
 
 330     store.setDefault(PHP_INTERNAL_PARSER, "false");
 
 331     store.setDefault(PHP_EXTERNAL_PARSER, "true");
 
 333 //    store.setDefault(PHP_PARSE_ON_SAVE, "true");
 
 335     // show line numbers:
 
 336     //   store.setDefault(LINE_NUMBER_RULER, "false");
 
 337     //    store.setDefault(FORMATTER_TAB_SIZE, "4");
 
 339     // php syntax highlighting
 
 340     store.setDefault(PHP_USERDEF_XMLFILE, ""); //assume there is none  chooA
 
 342     PreferenceConverter.setDefault(store, PHP_MULTILINE_COMMENT, PHPColorProvider.MULTI_LINE_COMMENT);
 
 343     PreferenceConverter.setDefault(store, PHP_SINGLELINE_COMMENT, PHPColorProvider.SINGLE_LINE_COMMENT);
 
 344     PreferenceConverter.setDefault(store, PHP_KEYWORD, PHPColorProvider.KEYWORD);
 
 345     PreferenceConverter.setDefault(store, PHP_VARIABLE, PHPColorProvider.VARIABLE);
 
 346     PreferenceConverter.setDefault(store, PHP_FUNCTIONNAME, PHPColorProvider.FUNCTION_NAME);
 
 347     PreferenceConverter.setDefault(store, PHP_CONSTANT, PHPColorProvider.CONSTANT);
 
 348     PreferenceConverter.setDefault(store, PHP_TYPE, PHPColorProvider.TYPE);
 
 349     PreferenceConverter.setDefault(store, PHP_STRING, PHPColorProvider.STRING);
 
 350     PreferenceConverter.setDefault(store, PHP_DEFAULT, PHPColorProvider.DEFAULT);
 
 351     //    PreferenceConverter.setDefault(
 
 353     //      PHP_EDITOR_BACKGROUND,
 
 354     //      PHPColorProvider.BACKGROUND);
 
 355     //    PreferenceConverter.setDefault(
 
 357     //      LINKED_POSITION_COLOR,
 
 358     //      PHPColorProvider.LINKED_POSITION_COLOR);
 
 359     //    PreferenceConverter.setDefault(
 
 361     //      LINE_NUMBER_COLOR,
 
 362     //      PHPColorProvider.LINE_NUMBER_COLOR);
 
 364     //    // set default PHPDoc colors:
 
 365     //    PreferenceConverter.setDefault(
 
 368     //      PHPColorProvider.PHPDOC_KEYWORD);
 
 369     //    PreferenceConverter.setDefault(
 
 372     //      PHPColorProvider.PHPDOC_LINK);      
 
 373     //    PreferenceConverter.setDefault(
 
 376     //      PHPColorProvider.PHPDOC_DEFAULT);
 
 377     //    PreferenceConverter.setDefault(
 
 380     //      PHPColorProvider.PHPDOC_TAG);
 
 382     //    store.setDefault(PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT, "true");
 
 383     //    PreferenceConverter.setDefault(
 
 385     //      PREFERENCE_COLOR_BACKGROUND,
 
 386     //      PHPColorProvider.BACKGROUND_COLOR);
 
 389     store.setDefault(RESOURCE_BUNDLE, LANGUAGE_DEFAULT);
 
 390     store.setDefault(RESOURCE_BUNDLE_EN_GB, "true");
 
 391     store.setDefault(RESOURCE_BUNDLE_DE, "false");
 
 392     store.setDefault(RESOURCE_BUNDLE_FR, "false");
 
 393     store.setDefault(RESOURCE_BUNDLE_ES, "false");
 
 395     store.setDefault(PHP_OUTLINE_CLASS, "true"); //$NON-NLS-1$
 
 396     store.setDefault(PHP_OUTLINE_FUNC, "true"); //$NON-NLS-1$
 
 397     store.setDefault(PHP_OUTLINE_VAR, "true"); //$NON-NLS-1$
 
 399     TemplatePreferencePage.initDefaults(store);
 
 400     //this will initialize the static fields in the syntaxrdr class
 
 403     PHPCore.initializeDefaultPluginPreferences();
 
 404     PreferenceConstants.initializeDefaultValues(store);
 
 406     externalTools.initializeDefaultPreferences(store);
 
 410    * Returns the standard display to be used. The method first checks, if
 
 411    * the thread calling this method has an associated display. If so, this
 
 412    * display is returned. Otherwise the method returns the default display.
 
 414   public static Display getStandardDisplay() {
 
 415     Display display = Display.getCurrent();
 
 416     if (display == null) {
 
 417       display = Display.getDefault();
 
 422   public void startup() throws CoreException {
 
 424     IAdapterManager manager = Platform.getAdapterManager();
 
 425     manager.registerAdapters(new PHPElementAdapterFactory(), PHPElement.class);
 
 426     manager.registerAdapters(new ResourceAdapterFactory(), IResource.class);
 
 427     //  externalTools.startUp(); 
 
 428     getStandardDisplay().asyncExec(new Runnable() {
 
 430         //initialize the variable context manager
 
 431         VariableContextManager.getDefault();
 
 437    * @see org.eclipse.core.runtime.Plugin#shutdown()
 
 439   public void shutdown() throws CoreException {
 
 440     //  externalTools.shutDown();
 
 441     ColorManager.getDefault().dispose();
 
 444   public void setLastEditorFile(IFile textEditor) {
 
 445     this.fLastEditorFile = textEditor;
 
 448   public IFile getLastEditorFile() {
 
 449     return fLastEditorFile;