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.internal.ui.preferences.TemplatePreferencePage;
 
  15 import net.sourceforge.phpdt.internal.ui.viewsupport.ImageDescriptorRegistry;
 
  16 import net.sourceforge.phpdt.ui.PreferenceConstants;
 
  17 import net.sourceforge.phpdt.ui.text.JavaTextTools;
 
  18 import net.sourceforge.phpeclipse.phpeditor.PHPDocumentProvider;
 
  19 import net.sourceforge.phpeclipse.phpeditor.PHPSyntaxRdr;
 
  20 import net.sourceforge.phpeclipse.phpeditor.util.PHPColorProvider;
 
  21 import net.sourceforge.phpeclipse.resourcesview.PHPElement;
 
  22 import net.sourceforge.phpeclipse.resourcesview.PHPElementAdapterFactory;
 
  23 import net.sourceforge.phpeclipse.resourcesview.ResourceAdapterFactory;
 
  25 import org.eclipse.core.boot.BootLoader;
 
  26 import org.eclipse.core.resources.IResource;
 
  27 import org.eclipse.core.resources.IWorkspace;
 
  28 import org.eclipse.core.resources.ResourcesPlugin;
 
  29 import org.eclipse.core.runtime.CoreException;
 
  30 import org.eclipse.core.runtime.IAdapterManager;
 
  31 import org.eclipse.core.runtime.IPath;
 
  32 import org.eclipse.core.runtime.IPluginDescriptor;
 
  33 import org.eclipse.core.runtime.IStatus;
 
  34 import org.eclipse.core.runtime.Path;
 
  35 import org.eclipse.core.runtime.Platform;
 
  36 import org.eclipse.core.runtime.Status;
 
  37 import org.eclipse.jface.preference.IPreferenceStore;
 
  38 import org.eclipse.jface.preference.PreferenceConverter;
 
  39 import org.eclipse.swt.widgets.Shell;
 
  40 import org.eclipse.ui.IWorkbenchPage;
 
  41 import org.eclipse.ui.IWorkbenchWindow;
 
  42 import org.eclipse.ui.plugin.AbstractUIPlugin;
 
  45  * The main plugin class to be used in the desktop.
 
  47 public class PHPeclipsePlugin
 
  48   extends AbstractUIPlugin
 
  49   implements IPreferenceConstants {
 
  50   //    public static final String LOCALHOST_PREF = "_localhost";
 
  51   //    public static final String DOCUMENTROOT_PREF = "_documentroot";
 
  52   //    public static final String USE_EXTERNAL_BROWSER_PREF = "_use_external_browser";
 
  53   //    public static final String EXTERNAL_BROWSER_PREF = "_external_browser";
 
  54   //    public static final String MYSQL_PREF = "_my_sql";
 
  55   //    public static final String APACHE_START_PREF = "_apache_start";
 
  56   //    public static final String APACHE_STOP_PREF = "_apache_stop";
 
  57   //    public static final String APACHE_RESTART_PREF = "_apache_restart";
 
  58   //  public static final String SHOW_OUTPUT_IN_CONSOLE = "_sho_output_in_console";
 
  59   //  public static final String EXTERNAL_PARSER_PREF = "_external_parser";
 
  62    * The id of the PHP plugin (value <code>"net.sourceforge.phpeclipse"</code>).
 
  64   public static final String PLUGIN_ID = "net.sourceforge.phpeclipse"; //$NON-NLS-1$
 
  65   public final static String PHP_NATURE_ID = PLUGIN_ID + ".phpnature";
 
  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 = 
 
  71     "net.sourceforge.phpdt.internal.compiler.parser.Parser";
 
  73   /** Change this if you want to switch PHP Parser. */
 
  74   public static final String PHPPARSER = PHPPARSER_ORIGINAL;
 
  76   //The shared instance.
 
  77   private static PHPeclipsePlugin plugin;
 
  79   //private ResourceBundle resourceBundle;
 
  81   private ImageDescriptorRegistry fImageDescriptorRegistry;
 
  82   private PHPDocumentProvider fCompilationUnitDocumentProvider;
 
  84   private JavaTextTools fJavaTextTools;
 
  87   * The Java virtual machine that we are running on.
 
  89   private static int jvm;
 
  92   private static final int MRJ_2_0 = 0;
 
  94   /** MRJ 2.1 or later */
 
  95   private static final int MRJ_2_1 = 1;
 
  97   /** Java on Mac OS X 10.0 (MRJ 3.0) */
 
  98   private static final int MRJ_3_0 = 3;
 
 101   private static final int MRJ_3_1 = 4;
 
 104   private static final int WINDOWS_NT = 5;
 
 107   private static final int WINDOWS_9x = 6;
 
 109   /** JVM constant for any other platform */
 
 110   private static final int OTHER = -1;
 
 112   /** General debug flag*/
 
 113   public static final boolean DEBUG = false;
 
 117   public PHPeclipsePlugin(IPluginDescriptor descriptor) {
 
 122     //      resourceBundle = ResourceBundle.getBundle("net.sourceforge.PHPeclipsePluginResources");
 
 123     //    } catch (MissingResourceException x) {
 
 124     //      resourceBundle = null;
 
 128   public static ImageDescriptorRegistry getImageDescriptorRegistry() {
 
 129     return getDefault().internalGetImageDescriptorRegistry();
 
 132   private ImageDescriptorRegistry internalGetImageDescriptorRegistry() {
 
 133     if (fImageDescriptorRegistry == null)
 
 134       fImageDescriptorRegistry = new ImageDescriptorRegistry();
 
 135     return fImageDescriptorRegistry;
 
 137   // @TODO: refactor this into a better method name !
 
 138   public synchronized PHPDocumentProvider getCompilationUnitDocumentProvider() {
 
 139     if (fCompilationUnitDocumentProvider == null)
 
 140       fCompilationUnitDocumentProvider= new PHPDocumentProvider();
 
 141     return fCompilationUnitDocumentProvider;
 
 144   private static void setJVM() {
 
 145     String osName = System.getProperty("os.name");
 
 147     if (osName.startsWith("Mac OS")) {
 
 148       String mrjVersion = System.getProperty("mrj.version");
 
 149       String majorMRJVersion = mrjVersion.substring(0, 3);
 
 153         double version = Double.valueOf(majorMRJVersion).doubleValue();
 
 157         } else if (version >= 2.1 && version < 3) {
 
 159         } else if (version == 3.0) {
 
 161         } else if (version >= 3.1) {
 
 165       } catch (NumberFormatException nfe) {
 
 169     } else if (osName.startsWith("Windows")) {
 
 170       if (osName.indexOf("9") != -1) {
 
 177   public static int getJVM() {
 
 181    * Returns the shared instance.
 
 183   public static PHPeclipsePlugin getDefault() {
 
 188    * Returns the workspace instance.
 
 190   public static IWorkspace getWorkspace() {
 
 191     return ResourcesPlugin.getWorkspace();
 
 194   public static IWorkbenchPage getActivePage() {
 
 195     return PHPeclipsePlugin.getActivePage();
 
 198   public static IWorkbenchWindow getActiveWorkbenchWindow() {
 
 199     return getDefault().getWorkbench().getActiveWorkbenchWindow();
 
 202   public static Shell getActiveWorkbenchShell() {
 
 203     return getActiveWorkbenchWindow().getShell();
 
 206   public static String getPluginId() {
 
 207     return getDefault().getDescriptor().getUniqueIdentifier();
 
 210   public static void log(IStatus status) {
 
 211     getDefault().getLog().log(status);
 
 214   //  public static void logErrorMessage(String message) {
 
 215   //    log(new Status(IStatus.ERROR, getPluginId(), JavaStatusConstants.INTERNAL_ERROR, message, null));
 
 218   //  public static void logErrorStatus(String message, IStatus status) {
 
 219   //    if (status == null) {
 
 220   //      logErrorMessage(message);
 
 223   //    MultiStatus multi= new MultiStatus(getPluginId(), JavaStatusConstants.INTERNAL_ERROR, message, null);
 
 224   //    multi.add(status);
 
 228   //  public static void log(Throwable e) {
 
 229   //    log(new Status(IStatus.ERROR, getPluginId(), JavaStatusConstants.INTERNAL_ERROR, JavaUIMessages.getString("JavaPlugin.internal_error"), e)); //$NON-NLS-1$
 
 232   public static void log(int severity, String message) {
 
 233     Status status = new Status(severity, PLUGIN_ID, IStatus.OK, message, null);
 
 236   public static void log(Throwable e) {
 
 237     log(new Status(IStatus.ERROR, PLUGIN_ID, IStatus.ERROR, "PHPeclipsePlugin.internalErrorOccurred", e)); //$NON-NLS-1$
 
 240   public static boolean isDebug() {
 
 241     return getDefault().isDebugging();
 
 244   static IPath getInstallLocation() {
 
 245     return new Path(getDefault().getDescriptor().getInstallURL().getFile());
 
 248   public synchronized JavaTextTools getJavaTextTools() {
 
 249     if (fJavaTextTools == null)
 
 250       fJavaTextTools= new JavaTextTools(getPreferenceStore());
 
 251     return fJavaTextTools;
 
 255    * Returns the string from the plugin's resource bundle,
 
 256    * or 'key' if not found.
 
 258   //  public static String getResourceString(String key) {
 
 259   //    ResourceBundle bundle = PHPeclipsePlugin.getDefault().getResourceBundle();
 
 261   //      return bundle.getString(key);
 
 262   //    } catch (MissingResourceException e) {
 
 268    * Returns the plugin's resource bundle,
 
 270   //  public ResourceBundle getResourceBundle() {
 
 271   //    return resourceBundle;
 
 274   protected void initializeDefaultPreferences(IPreferenceStore store) {
 
 275     // windows preferences:
 
 276     store.setDefault(LOCALHOST_PREF, "http://localhost");
 
 278     store.setDefault(USE_EXTERNAL_BROWSER_PREF, "false");
 
 279     store.setDefault(SHOW_OUTPUT_IN_CONSOLE, "true");
 
 281     String windowsSystem = BootLoader.getWS();
 
 283     if (jvm == WINDOWS_9x) {
 
 285         EXTERNAL_BROWSER_PREF,
 
 286         "command.com /c start iexplore {0}");
 
 287     } else if (windowsSystem.equals(BootLoader.WS_WIN32)) {
 
 289         EXTERNAL_BROWSER_PREF,
 
 290         "rundll32 url.dll,FileProtocolHandler {0}");
 
 292       store.setDefault(EXTERNAL_BROWSER_PREF, "netscape {0}");
 
 296       getWorkspace().getRoot().getLocation().toString());
 
 297     //  if ((jvm == WINDOWS_9x) || (jvm == WINDOWS_NT)) {
 
 298     if (windowsSystem.equals(BootLoader.WS_WIN32)) {
 
 299       store.setDefault(EXTERNAL_PARSER_PREF, "c:\\apache\\php\\php -l -f {0}");
 
 302         "c:\\apache\\mysql\\bin\\mysqld.exe --standalone");
 
 305         "c:\\apache\\apache.exe -c \"DocumentRoot \"{0}\"\"");
 
 306       store.setDefault(APACHE_STOP_PREF, "c:\\apache\\apache.exe -k shutdown");
 
 309         "c:\\apache\\apache.exe -k restart");
 
 311       store.setDefault(EXTERNAL_PARSER_PREF, "/apache/php/php -l -f {0}");
 
 312       store.setDefault(MYSQL_PREF, "/apache/mysql/bin/mysqld --standalone");
 
 315         "/apache/apache -c \"DocumentRoot \"{0}\"\"");
 
 316       store.setDefault(APACHE_STOP_PREF, "/apache/apache.exe -k shutdown");
 
 317       store.setDefault(APACHE_RESTART_PREF, "/apache/apache -k restart");
 
 321     store.setDefault(PHP_PARSER_DEFAULT, PHP_EXTERNAL_PARSER);
 
 322     store.setDefault(PHP_INTERNAL_PARSER, "false");
 
 323     store.setDefault(PHP_EXTERNAL_PARSER, "true");
 
 325     store.setDefault(PHP_PARSE_ON_SAVE, "true");
 
 327     // show line numbers:
 
 328  //   store.setDefault(LINE_NUMBER_RULER, "false");
 
 329 //    store.setDefault(FORMATTER_TAB_SIZE, "4");
 
 331     // php syntax highlighting
 
 332     store.setDefault(PHP_USERDEF_XMLFILE, ""); //assume there is none  chooA
 
 334     PreferenceConverter.setDefault(
 
 336       PHP_MULTILINE_COMMENT,
 
 337       PHPColorProvider.MULTI_LINE_COMMENT);
 
 338     PreferenceConverter.setDefault(
 
 340       PHP_SINGLELINE_COMMENT,
 
 341       PHPColorProvider.SINGLE_LINE_COMMENT);
 
 342     PreferenceConverter.setDefault(
 
 345       PHPColorProvider.KEYWORD);
 
 346     PreferenceConverter.setDefault(
 
 349       PHPColorProvider.VARIABLE);
 
 350     PreferenceConverter.setDefault(
 
 353       PHPColorProvider.FUNCTION_NAME); 
 
 354     PreferenceConverter.setDefault(
 
 357       PHPColorProvider.CONSTANT);
 
 358     PreferenceConverter.setDefault(store, PHP_TYPE, PHPColorProvider.TYPE);
 
 359     PreferenceConverter.setDefault(store, PHP_STRING, PHPColorProvider.STRING);
 
 360     PreferenceConverter.setDefault(
 
 363       PHPColorProvider.DEFAULT);
 
 364 //    PreferenceConverter.setDefault(
 
 366 //      PHP_EDITOR_BACKGROUND,
 
 367 //      PHPColorProvider.BACKGROUND);
 
 368 //    PreferenceConverter.setDefault(
 
 370 //      LINKED_POSITION_COLOR,
 
 371 //      PHPColorProvider.LINKED_POSITION_COLOR);
 
 372 //    PreferenceConverter.setDefault(
 
 374 //      LINE_NUMBER_COLOR,
 
 375 //      PHPColorProvider.LINE_NUMBER_COLOR);
 
 377 //    // set default PHPDoc colors:
 
 378 //    PreferenceConverter.setDefault(
 
 381 //      PHPColorProvider.PHPDOC_KEYWORD);
 
 382 //    PreferenceConverter.setDefault(
 
 385 //      PHPColorProvider.PHPDOC_LINK);      
 
 386 //    PreferenceConverter.setDefault(
 
 389 //      PHPColorProvider.PHPDOC_DEFAULT);
 
 390 //    PreferenceConverter.setDefault(
 
 393 //      PHPColorProvider.PHPDOC_TAG);
 
 395 //    store.setDefault(PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT, "true");
 
 396 //    PreferenceConverter.setDefault(
 
 398 //      PREFERENCE_COLOR_BACKGROUND,
 
 399 //      PHPColorProvider.BACKGROUND_COLOR);
 
 402     store.setDefault(RESOURCE_BUNDLE, LANGUAGE_DEFAULT);
 
 403     store.setDefault(RESOURCE_BUNDLE_EN_GB, "true");
 
 404     store.setDefault(RESOURCE_BUNDLE_DE, "false");
 
 405     store.setDefault(RESOURCE_BUNDLE_FR, "false");
 
 406     store.setDefault(RESOURCE_BUNDLE_ES, "false");
 
 408     store.setDefault(PHP_OUTLINE_CLASS, "true"); //$NON-NLS-1$
 
 409     store.setDefault(PHP_OUTLINE_FUNC, "true"); //$NON-NLS-1$
 
 410     store.setDefault(PHP_OUTLINE_VAR, "true"); //$NON-NLS-1$
 
 412     TemplatePreferencePage.initDefaults(store);
 
 413     //this will initialize the static fields in the syntaxrdr class
 
 416     PHPCore.initializeDefaultPluginPreferences();
 
 417     PreferenceConstants.initializeDefaultValues(store);
 
 420   public void startup() throws CoreException {
 
 422     IAdapterManager manager = Platform.getAdapterManager();
 
 423     manager.registerAdapters(new PHPElementAdapterFactory(), PHPElement.class);
 
 424     manager.registerAdapters(new ResourceAdapterFactory(), IResource.class);