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";
 
  59   // public static final String PHP_RESOURCES_VIEW_ID = PLUGIN_ID + ".resourcesview.ViewPHPResources"; //$NON-NLS-1$
 
  60   public static final String PHP_CODING_ACTION_SET_ID = PLUGIN_ID + ".ui.CodingActionSet"; //$NON-NLS-1$
 
  62   public static final String PHPPARSER_NEW = "test.PHPParser";
 
  63   public static final String PHPPARSER_ORIGINAL = "net.sourceforge.phpdt.internal.compiler.parser.Parser";
 
  65   /** Change this if you want to switch PHP Parser. */
 
  66   public static final String PHPPARSER = PHPPARSER_ORIGINAL;
 
  68   //The shared instance.
 
  69   private static PHPeclipsePlugin plugin;
 
  71   private static ExternalToolsPlugin externalTools;
 
  73   //private ResourceBundle resourceBundle;
 
  75   private ImageDescriptorRegistry fImageDescriptorRegistry;
 
  76   private PHPDocumentProvider fCompilationUnitDocumentProvider;
 
  77   private IFile fLastEditorFile = null;
 
  79   private JavaTextTools fJavaTextTools;
 
  82   * The Java virtual machine that we are running on.
 
  84   private static int jvm;
 
  87   private static final int MRJ_2_0 = 0;
 
  89   /** MRJ 2.1 or later */
 
  90   private static final int MRJ_2_1 = 1;
 
  92   /** Java on Mac OS X 10.0 (MRJ 3.0) */
 
  93   private static final int MRJ_3_0 = 3;
 
  96   private static final int MRJ_3_1 = 4;
 
  99   private static final int WINDOWS_NT = 5;
 
 102   private static final int WINDOWS_9x = 6;
 
 104   /** JVM constant for any other platform */
 
 105   private static final int OTHER = -1;
 
 107   /** General debug flag*/
 
 108   public static final boolean DEBUG = false;
 
 112   public PHPeclipsePlugin(IPluginDescriptor descriptor) {
 
 116     externalTools = new ExternalToolsPlugin();
 
 118     //      resourceBundle = ResourceBundle.getBundle("net.sourceforge.PHPeclipsePluginResources");
 
 119     //    } catch (MissingResourceException x) {
 
 120     //      resourceBundle = null;
 
 124   public static ImageDescriptorRegistry getImageDescriptorRegistry() {
 
 125     return getDefault().internalGetImageDescriptorRegistry();
 
 128   private ImageDescriptorRegistry internalGetImageDescriptorRegistry() {
 
 129     if (fImageDescriptorRegistry == null)
 
 130       fImageDescriptorRegistry = new ImageDescriptorRegistry();
 
 131     return fImageDescriptorRegistry;
 
 133   // @TODO: refactor this into a better method name !
 
 134   public synchronized PHPDocumentProvider getCompilationUnitDocumentProvider() {
 
 135     if (fCompilationUnitDocumentProvider == null)
 
 136       fCompilationUnitDocumentProvider = new PHPDocumentProvider();
 
 137     return fCompilationUnitDocumentProvider;
 
 140   private static void setJVM() {
 
 141     String osName = System.getProperty("os.name");
 
 143     if (osName.startsWith("Mac OS")) {
 
 144       String mrjVersion = System.getProperty("mrj.version");
 
 145       String majorMRJVersion = mrjVersion.substring(0, 3);
 
 149         double version = Double.valueOf(majorMRJVersion).doubleValue();
 
 153         } else if (version >= 2.1 && version < 3) {
 
 155         } else if (version == 3.0) {
 
 157         } else if (version >= 3.1) {
 
 161       } catch (NumberFormatException nfe) {
 
 165     } else if (osName.startsWith("Windows")) {
 
 166       if (osName.indexOf("9") != -1) {
 
 173   public static int getJVM() {
 
 177    * Returns the shared instance.
 
 179   public static PHPeclipsePlugin getDefault() {
 
 183   //  public static ExternalToolsPlugin getExternalTools() {
 
 184   //    return externalTools;
 
 187    * Returns the workspace instance.
 
 189   public static IWorkspace getWorkspace() {
 
 190     return ResourcesPlugin.getWorkspace();
 
 193   public static IWorkbenchPage getActivePage() {
 
 194     return getDefault().internalGetActivePage();
 
 197   private IWorkbenchPage internalGetActivePage() {
 
 198     IWorkbenchWindow window = getWorkbench().getActiveWorkbenchWindow();
 
 200       return window.getActivePage();
 
 204   public static IWorkbenchWindow getActiveWorkbenchWindow() {
 
 205     return getDefault().getWorkbench().getActiveWorkbenchWindow();
 
 208   public static Shell getActiveWorkbenchShell() {
 
 209     return getActiveWorkbenchWindow().getShell();
 
 212   public static String getPluginId() {
 
 213     return getDefault().getDescriptor().getUniqueIdentifier();
 
 216   public static void log(IStatus status) {
 
 217     getDefault().getLog().log(status);
 
 220   //  public static void logErrorMessage(String message) {
 
 221   //    log(new Status(IStatus.ERROR, getPluginId(), JavaStatusConstants.INTERNAL_ERROR, message, null));
 
 224   //  public static void logErrorStatus(String message, IStatus status) {
 
 225   //    if (status == null) {
 
 226   //      logErrorMessage(message);
 
 229   //    MultiStatus multi= new MultiStatus(getPluginId(), JavaStatusConstants.INTERNAL_ERROR, message, null);
 
 230   //    multi.add(status);
 
 234   //  public static void log(Throwable e) {
 
 235   //    log(new Status(IStatus.ERROR, getPluginId(), JavaStatusConstants.INTERNAL_ERROR, JavaUIMessages.getString("JavaPlugin.internal_error"), e)); //$NON-NLS-1$
 
 238   public static void log(int severity, String message) {
 
 239     Status status = new Status(severity, PLUGIN_ID, IStatus.OK, message, null);
 
 242   public static void log(Throwable e) {
 
 243     log(new Status(IStatus.ERROR, PLUGIN_ID, IStatus.ERROR, "PHPeclipsePlugin.internalErrorOccurred", e)); //$NON-NLS-1$
 
 246   public static boolean isDebug() {
 
 247     return getDefault().isDebugging();
 
 250   static IPath getInstallLocation() {
 
 251     return new Path(getDefault().getDescriptor().getInstallURL().getFile());
 
 254   public synchronized JavaTextTools getJavaTextTools() {
 
 255     if (fJavaTextTools == null)
 
 256       fJavaTextTools = new JavaTextTools(getPreferenceStore());
 
 257     return fJavaTextTools;
 
 261    * Returns the string from the plugin's resource bundle,
 
 262    * or 'key' if not found.
 
 264   //  public static String getResourceString(String key) {
 
 265   //    ResourceBundle bundle = PHPeclipsePlugin.getDefault().getResourceBundle();
 
 267   //      return bundle.getString(key);
 
 268   //    } catch (MissingResourceException e) {
 
 274    * Returns the plugin's resource bundle,
 
 276   //  public ResourceBundle getResourceBundle() {
 
 277   //    return resourceBundle;
 
 280   protected void initializeDefaultPreferences(IPreferenceStore store) {
 
 281     // windows preferences:
 
 282     store.setDefault(LOCALHOST_PREF, "http://localhost");
 
 284     store.setDefault(SHOW_EXTERNAL_PREVIEW_PREF, "true");
 
 285     store.setDefault(USE_EXTERNAL_BROWSER_PREF, "false");
 
 286     store.setDefault(SHOW_OUTPUT_IN_CONSOLE, "true");
 
 288     String windowsSystem = BootLoader.getWS();
 
 290     if (jvm == WINDOWS_9x) {
 
 291       store.setDefault(EXTERNAL_BROWSER_PREF, "command.com /c start iexplore {0}");
 
 292     } else if (windowsSystem.equals(BootLoader.WS_WIN32)) {
 
 293       store.setDefault(EXTERNAL_BROWSER_PREF, "rundll32 url.dll,FileProtocolHandler {0}");
 
 295       store.setDefault(EXTERNAL_BROWSER_PREF, "netscape {0}");
 
 297     store.setDefault(DOCUMENTROOT_PREF, getWorkspace().getRoot().getLocation().toString());
 
 299     //  if ((jvm == WINDOWS_9x) || (jvm == WINDOWS_NT)) {
 
 301     if (windowsSystem.equals(BootLoader.WS_WIN32)) {
 
 302       store.setDefault(PHP_RUN_PREF, "c:\\apache\\php\\php.exe");
 
 303       store.setDefault(EXTERNAL_PARSER_PREF, "c:\\apache\\php\\php -l -f {0}");
 
 304       store.setDefault(MYSQL_RUN_PREF, "c:\\apache\\mysql\\bin\\mysqld-nt.exe");
 
 305       store.setDefault(APACHE_RUN_PREF, "c:\\apache\\apache.exe");
 
 307       store.setDefault(PHP_RUN_PREF, "/apache/php/php");
 
 308       store.setDefault(EXTERNAL_PARSER_PREF, "/apache/php/php -l -f {0}");
 
 309       store.setDefault(MYSQL_RUN_PREF, "/apache/mysql/bin/mysqld");
 
 310       store.setDefault(APACHE_RUN_PREF, "/apache/apache");
 
 312     store.setDefault(MYSQL_PREF, "--standalone");
 
 313     store.setDefault(APACHE_START_PREF, "-c \"DocumentRoot \"{0}\"\"");
 
 314     store.setDefault(APACHE_STOP_PREF, "-k shutdown");
 
 315     store.setDefault(APACHE_RESTART_PREF, "-k restart");
 
 317     store.setDefault(MYSQL_START_BACKGROUND, "true");
 
 318     store.setDefault(APACHE_START_BACKGROUND, "true");
 
 319     store.setDefault(APACHE_STOP_BACKGROUND, "true");
 
 320     store.setDefault(APACHE_RESTART_BACKGROUND, "true");
 
 322     store.setDefault(PHP_PARSER_DEFAULT, PHP_EXTERNAL_PARSER);
 
 323     store.setDefault(PHP_INTERNAL_PARSER, "false");
 
 324     store.setDefault(PHP_EXTERNAL_PARSER, "true");
 
 326     store.setDefault(PHP_PARSE_ON_SAVE, "true");
 
 328     // show line numbers:
 
 329     //   store.setDefault(LINE_NUMBER_RULER, "false");
 
 330     //    store.setDefault(FORMATTER_TAB_SIZE, "4");
 
 332     // php syntax highlighting
 
 333     store.setDefault(PHP_USERDEF_XMLFILE, ""); //assume there is none  chooA
 
 335     PreferenceConverter.setDefault(store, PHP_MULTILINE_COMMENT, PHPColorProvider.MULTI_LINE_COMMENT);
 
 336     PreferenceConverter.setDefault(store, PHP_SINGLELINE_COMMENT, PHPColorProvider.SINGLE_LINE_COMMENT);
 
 337     PreferenceConverter.setDefault(store, PHP_KEYWORD, PHPColorProvider.KEYWORD);
 
 338     PreferenceConverter.setDefault(store, PHP_VARIABLE, PHPColorProvider.VARIABLE);
 
 339     PreferenceConverter.setDefault(store, PHP_FUNCTIONNAME, PHPColorProvider.FUNCTION_NAME);
 
 340     PreferenceConverter.setDefault(store, PHP_CONSTANT, PHPColorProvider.CONSTANT);
 
 341     PreferenceConverter.setDefault(store, PHP_TYPE, PHPColorProvider.TYPE);
 
 342     PreferenceConverter.setDefault(store, PHP_STRING, PHPColorProvider.STRING);
 
 343     PreferenceConverter.setDefault(store, PHP_DEFAULT, PHPColorProvider.DEFAULT);
 
 344     //    PreferenceConverter.setDefault(
 
 346     //      PHP_EDITOR_BACKGROUND,
 
 347     //      PHPColorProvider.BACKGROUND);
 
 348     //    PreferenceConverter.setDefault(
 
 350     //      LINKED_POSITION_COLOR,
 
 351     //      PHPColorProvider.LINKED_POSITION_COLOR);
 
 352     //    PreferenceConverter.setDefault(
 
 354     //      LINE_NUMBER_COLOR,
 
 355     //      PHPColorProvider.LINE_NUMBER_COLOR);
 
 357     //    // set default PHPDoc colors:
 
 358     //    PreferenceConverter.setDefault(
 
 361     //      PHPColorProvider.PHPDOC_KEYWORD);
 
 362     //    PreferenceConverter.setDefault(
 
 365     //      PHPColorProvider.PHPDOC_LINK);      
 
 366     //    PreferenceConverter.setDefault(
 
 369     //      PHPColorProvider.PHPDOC_DEFAULT);
 
 370     //    PreferenceConverter.setDefault(
 
 373     //      PHPColorProvider.PHPDOC_TAG);
 
 375     //    store.setDefault(PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT, "true");
 
 376     //    PreferenceConverter.setDefault(
 
 378     //      PREFERENCE_COLOR_BACKGROUND,
 
 379     //      PHPColorProvider.BACKGROUND_COLOR);
 
 382     store.setDefault(RESOURCE_BUNDLE, LANGUAGE_DEFAULT);
 
 383     store.setDefault(RESOURCE_BUNDLE_EN_GB, "true");
 
 384     store.setDefault(RESOURCE_BUNDLE_DE, "false");
 
 385     store.setDefault(RESOURCE_BUNDLE_FR, "false");
 
 386     store.setDefault(RESOURCE_BUNDLE_ES, "false");
 
 388     store.setDefault(PHP_OUTLINE_CLASS, "true"); //$NON-NLS-1$
 
 389     store.setDefault(PHP_OUTLINE_FUNC, "true"); //$NON-NLS-1$
 
 390     store.setDefault(PHP_OUTLINE_VAR, "true"); //$NON-NLS-1$
 
 392     TemplatePreferencePage.initDefaults(store);
 
 393     //this will initialize the static fields in the syntaxrdr class
 
 396     PHPCore.initializeDefaultPluginPreferences();
 
 397     PreferenceConstants.initializeDefaultValues(store);
 
 399     externalTools.initializeDefaultPreferences(store);
 
 403    * Returns the standard display to be used. The method first checks, if
 
 404    * the thread calling this method has an associated display. If so, this
 
 405    * display is returned. Otherwise the method returns the default display.
 
 407   public static Display getStandardDisplay() {
 
 408     Display display = Display.getCurrent();
 
 409     if (display == null) {
 
 410       display = Display.getDefault();
 
 415   public void startup() throws CoreException {
 
 417     IAdapterManager manager = Platform.getAdapterManager();
 
 418     manager.registerAdapters(new PHPElementAdapterFactory(), PHPElement.class);
 
 419     manager.registerAdapters(new ResourceAdapterFactory(), IResource.class);
 
 420     //  externalTools.startUp(); 
 
 421     getStandardDisplay().asyncExec(new Runnable() {
 
 423         //initialize the variable context manager
 
 424         VariableContextManager.getDefault();
 
 430    * @see org.eclipse.core.runtime.Plugin#shutdown()
 
 432   public void shutdown() throws CoreException {
 
 433     //  externalTools.shutDown();
 
 434     ColorManager.getDefault().dispose();
 
 437   public void setLastEditorFile(IFile textEditor) {
 
 438     this.fLastEditorFile = textEditor;
 
 441   public IFile getLastEditorFile() {
 
 442     return fLastEditorFile;