Eclipse 3.x compatible;
[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.io.File;
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.HashMap;
18 import java.util.HashSet;
19 import java.util.Iterator;
20 import java.util.List;
21 import java.util.Set;
22
23 import net.sourceforge.phpdt.externaltools.internal.model.ColorManager;
24 import net.sourceforge.phpdt.externaltools.internal.model.ExternalToolsPlugin;
25 import net.sourceforge.phpdt.externaltools.internal.model.VariableContextManager;
26 import net.sourceforge.phpdt.internal.ui.preferences.MembersOrderPreferenceCache;
27 import net.sourceforge.phpdt.internal.ui.preferences.TemplatePreferencePage;
28 import net.sourceforge.phpdt.internal.ui.text.java.hover.JavaEditorTextHoverDescriptor;
29 import net.sourceforge.phpdt.internal.ui.viewsupport.ImageDescriptorRegistry;
30 import net.sourceforge.phpdt.internal.ui.viewsupport.ProblemMarkerManager;
31 import net.sourceforge.phpdt.ui.IContextMenuConstants;
32 import net.sourceforge.phpdt.ui.IWorkingCopyManager;
33 import net.sourceforge.phpdt.ui.PreferenceConstants;
34 import net.sourceforge.phpdt.ui.text.JavaTextTools;
35 import net.sourceforge.phpeclipse.builder.ExternalEditorInput;
36 import net.sourceforge.phpeclipse.builder.ExternalStorageDocumentProvider;
37 import net.sourceforge.phpeclipse.builder.FileStorage;
38 import net.sourceforge.phpeclipse.builder.IdentifierIndexManager;
39 import net.sourceforge.phpeclipse.phpeditor.PHPDocumentProvider;
40 import net.sourceforge.phpeclipse.phpeditor.PHPSyntaxRdr;
41 import net.sourceforge.phpeclipse.phpeditor.WorkingCopyManager;
42 import net.sourceforge.phpeclipse.phpeditor.util.PHPColorProvider;
43 import net.sourceforge.phpeclipse.resourcesview.PHPElement;
44 import net.sourceforge.phpeclipse.resourcesview.PHPElementAdapterFactory;
45 import net.sourceforge.phpeclipse.resourcesview.ResourceAdapterFactory;
46
47 import org.eclipse.core.boot.BootLoader;
48 import org.eclipse.core.resources.IFile;
49 import org.eclipse.core.resources.IProject;
50 import org.eclipse.core.resources.IResource;
51 import org.eclipse.core.resources.IWorkspace;
52 import org.eclipse.core.resources.ResourcesPlugin;
53 import org.eclipse.core.runtime.CoreException;
54 import org.eclipse.core.runtime.IAdapterManager;
55 import org.eclipse.core.runtime.IPath;
56 import org.eclipse.core.runtime.IPluginDescriptor;
57 import org.eclipse.core.runtime.IStatus;
58 import org.eclipse.core.runtime.Path;
59 import org.eclipse.core.runtime.Platform;
60 import org.eclipse.core.runtime.Status;
61 import org.eclipse.jface.action.GroupMarker;
62 import org.eclipse.jface.action.IMenuManager;
63 import org.eclipse.jface.action.Separator;
64 import org.eclipse.jface.preference.IPreferenceStore;
65 import org.eclipse.jface.preference.PreferenceConverter;
66 import org.eclipse.jface.text.BadLocationException;
67 import org.eclipse.jface.text.IDocument;
68 import org.eclipse.swt.widgets.Display;
69 import org.eclipse.swt.widgets.Shell;
70 import org.eclipse.ui.IEditorDescriptor;
71 import org.eclipse.ui.IEditorInput;
72 import org.eclipse.ui.IEditorPart;
73 import org.eclipse.ui.IEditorRegistry;
74 import org.eclipse.ui.IWorkbench;
75 import org.eclipse.ui.IWorkbenchPage;
76 import org.eclipse.ui.IWorkbenchWindow;
77 import org.eclipse.ui.PlatformUI;
78 import org.eclipse.ui.ide.IDE;
79 import org.eclipse.ui.plugin.AbstractUIPlugin;
80 import org.eclipse.ui.texteditor.IDocumentProvider;
81 import org.eclipse.ui.texteditor.ITextEditor;
82
83 /**
84  * The main plugin class to be used in the desktop.
85  */
86 public class PHPeclipsePlugin extends AbstractUIPlugin implements IPreferenceConstants {
87   /**
88    * The id of the PHP plugin (value <code>"net.sourceforge.phpeclipse"</code>).
89    */
90   public static final String PLUGIN_ID = "net.sourceforge.phpeclipse"; //$NON-NLS-1$
91
92   /** 
93    * id of builder - matches plugin.xml (concatenate pluginid.builderid) 
94    */
95   public static final String BUILDER_PARSER_ID = PLUGIN_ID + ".parserbuilder";
96   //public static final String BUILDER_INDEX_ID = PLUGIN_ID + ".indexbuilder";
97
98   /** General debug flag*/
99   public static final boolean DEBUG = false;
100
101   /**
102    * The maximum number of allowed proposals by category
103    */
104   public final static int MAX_PROPOSALS = 200;
105
106   private static ExternalToolsPlugin externalTools;
107
108   /**
109   * The Java virtual machine that we are running on.
110   */
111   private static int jvm;
112
113   /** MRJ 2.0 */
114   private static final int MRJ_2_0 = 0;
115
116   /** MRJ 2.1 or later */
117   private static final int MRJ_2_1 = 1;
118
119   /** Java on Mac OS X 10.0 (MRJ 3.0) */
120   private static final int MRJ_3_0 = 3;
121
122   /** MRJ 3.1 */
123   private static final int MRJ_3_1 = 4;
124
125   /** JVM constant for any other platform */
126   private static final int OTHER = -1;
127
128   // public static final String PHP_RESOURCES_VIEW_ID = PLUGIN_ID + ".resourcesview.ViewPHPResources"; //$NON-NLS-1$
129   public static final String PHP_CODING_ACTION_SET_ID = PLUGIN_ID + ".ui.CodingActionSet"; //$NON-NLS-1$
130   public final static String PHP_NATURE_ID = PLUGIN_ID + ".phpnature";
131
132   public static final String PHPPARSER_ORIGINAL = "net.sourceforge.phpdt.internal.compiler.parser.Parser";
133
134   public static final String PHPPARSER_NEW = "test.PHPParser";
135
136   /** Change this if you want to switch PHP Parser. */
137   public static final String PHPPARSER = PHPPARSER_ORIGINAL;
138
139   //The shared instance.
140   private static PHPeclipsePlugin plugin;
141
142   /** Windows 9x  */
143   private static final int WINDOWS_9x = 6;
144
145   /** Windows NT  */
146   private static final int WINDOWS_NT = 5;
147
148   private ImageDescriptorRegistry fImageDescriptorRegistry;
149   private HashMap fIndexManagerMap = new HashMap();
150
151   private IWorkingCopyManager fWorkingCopyManager;
152   private PHPDocumentProvider fCompilationUnitDocumentProvider;
153   private JavaTextTools fJavaTextTools;
154   private ProblemMarkerManager fProblemMarkerManager;
155         private MembersOrderPreferenceCache fMembersOrderPreferenceCache;
156
157   private IFile fLastEditorFile = null;
158
159         private JavaEditorTextHoverDescriptor[] fJavaEditorTextHoverDescriptors;
160         
161   /**
162    * The constructor.
163    */
164   public PHPeclipsePlugin(IPluginDescriptor descriptor) {
165     super(descriptor);
166     plugin = this;
167     setJVM();
168     externalTools = new ExternalToolsPlugin();
169     //    try {
170     //      resourceBundle = ResourceBundle.getBundle("net.sourceforge.PHPeclipsePluginResources");
171     //    } catch (MissingResourceException x) {
172     //      resourceBundle = null;
173     //    }
174   }
175         /**
176          * Returns all Java editor text hovers contributed to the workbench.
177          * 
178          * @return an array of JavaEditorTextHoverDescriptor
179          * @since 2.1
180          */
181         public JavaEditorTextHoverDescriptor[] getJavaEditorTextHoverDescriptors() {
182                 if (fJavaEditorTextHoverDescriptors == null)
183                         fJavaEditorTextHoverDescriptors= JavaEditorTextHoverDescriptor.getContributedHovers();
184                 return fJavaEditorTextHoverDescriptors;
185         } 
186
187         /**
188          * Resets the Java editor text hovers contributed to the workbench.
189          * <p>
190          * This will force a rebuild of the descriptors the next time
191          * a client asks for them.
192          * </p>
193          * 
194          * @return an array of JavaEditorTextHoverDescriptor
195          * @since 2.1
196          */
197         public void resetJavaEditorTextHoverDescriptors() {
198                 fJavaEditorTextHoverDescriptors= null;
199         }
200         /**
201          * Creates the PHP plugin standard groups in a context menu.
202          */
203         public static void createStandardGroups(IMenuManager menu) {
204                 if (!menu.isEmpty())
205                         return;
206                         
207                 menu.add(new Separator(IContextMenuConstants.GROUP_NEW));
208                 menu.add(new GroupMarker(IContextMenuConstants.GROUP_GOTO));
209                 menu.add(new Separator(IContextMenuConstants.GROUP_OPEN));
210                 menu.add(new GroupMarker(IContextMenuConstants.GROUP_SHOW));
211                 menu.add(new Separator(IContextMenuConstants.GROUP_REORGANIZE));
212                 menu.add(new Separator(IContextMenuConstants.GROUP_GENERATE));
213                 menu.add(new Separator(IContextMenuConstants.GROUP_SEARCH));
214                 menu.add(new Separator(IContextMenuConstants.GROUP_BUILD));
215                 menu.add(new Separator(IContextMenuConstants.GROUP_ADDITIONS));
216                 menu.add(new Separator(IContextMenuConstants.GROUP_VIEWER_SETUP));
217                 menu.add(new Separator(IContextMenuConstants.GROUP_PROPERTIES));
218         }
219   public static IWorkbenchPage getActivePage() {
220     return getDefault().internalGetActivePage();
221   }
222
223   public static Shell getActiveWorkbenchShell() {
224     return getActiveWorkbenchWindow().getShell();
225   }
226
227   /**
228          * Returns an array of all editors that have an unsaved content. If the identical content is 
229          * presented in more than one editor, only one of those editor parts is part of the result.
230          * 
231          * @return an array of all dirty editor parts.
232          */
233   public static IEditorPart[] getDirtyEditors() {
234     Set inputs = new HashSet();
235     List result = new ArrayList(0);
236     IWorkbench workbench = getDefault().getWorkbench();
237     IWorkbenchWindow[] windows = workbench.getWorkbenchWindows();
238     for (int i = 0; i < windows.length; i++) {
239       IWorkbenchPage[] pages = windows[i].getPages();
240       for (int x = 0; x < pages.length; x++) {
241         IEditorPart[] editors = pages[x].getDirtyEditors();
242         for (int z = 0; z < editors.length; z++) {
243           IEditorPart ep = editors[z];
244           IEditorInput input = ep.getEditorInput();
245           if (!inputs.contains(input)) {
246             inputs.add(input);
247             result.add(ep);
248           }
249         }
250       }
251     }
252     return (IEditorPart[]) result.toArray(new IEditorPart[result.size()]);
253   }
254
255   public static IWorkbenchWindow getActiveWorkbenchWindow() {
256     return getDefault().getWorkbench().getActiveWorkbenchWindow();
257   }
258   /**
259    * Returns the shared instance.
260    */
261   public static PHPeclipsePlugin getDefault() {
262     return plugin;
263   }
264
265   public static ImageDescriptorRegistry getImageDescriptorRegistry() {
266     return getDefault().internalGetImageDescriptorRegistry();
267   }
268
269   static IPath getInstallLocation() {
270     return new Path(getDefault().getDescriptor().getInstallURL().getFile());
271   }
272   public static int getJVM() {
273     return jvm;
274   }
275
276   public static String getPluginId() {
277     return getDefault().getDescriptor().getUniqueIdentifier();
278   }
279
280   /**
281    * Returns the standard display to be used. The method first checks, if
282    * the thread calling this method has an associated display. If so, this
283    * display is returned. Otherwise the method returns the default display.
284    */
285   public static Display getStandardDisplay() {
286     Display display = Display.getCurrent();
287     if (display == null) {
288       display = Display.getDefault();
289     }
290     return display;
291   }
292
293   //  public static ExternalToolsPlugin getExternalTools() {
294   //    return externalTools;
295   //  }
296   /**
297    * Returns the workspace instance.
298    */
299   public static IWorkspace getWorkspace() {
300     return ResourcesPlugin.getWorkspace();
301   }
302
303   public static boolean isDebug() {
304     return getDefault().isDebugging();
305   }
306
307   //  public static void logErrorMessage(String message) {
308   //    log(new Status(IStatus.ERROR, getPluginId(), JavaStatusConstants.INTERNAL_ERROR, message, null));
309   //  }
310   //
311   //  public static void logErrorStatus(String message, IStatus status) {
312   //    if (status == null) {
313   //      logErrorMessage(message);
314   //      return;
315   //    }
316   //    MultiStatus multi= new MultiStatus(getPluginId(), JavaStatusConstants.INTERNAL_ERROR, message, null);
317   //    multi.add(status);
318   //    log(multi);
319   //  }
320   //
321   //  public static void log(Throwable e) {
322   //    log(new Status(IStatus.ERROR, getPluginId(), JavaStatusConstants.INTERNAL_ERROR, JavaUIMessages.getString("JavaPlugin.internal_error"), e)); //$NON-NLS-1$
323   //  }
324
325   public static void log(int severity, String message) {
326     Status status = new Status(severity, PLUGIN_ID, IStatus.OK, message, null);
327     log(status);
328   }
329
330   public static void log(IStatus status) {
331     getDefault().getLog().log(status);
332   }
333   public static void log(Throwable e) {
334     log(new Status(IStatus.ERROR, PLUGIN_ID, IStatus.ERROR, "PHPeclipsePlugin.internalErrorOccurred", e)); //$NON-NLS-1$
335   }
336
337   private static void setJVM() {
338     String osName = System.getProperty("os.name");
339
340     if (osName.startsWith("Mac OS")) {
341       String mrjVersion = System.getProperty("mrj.version");
342       String majorMRJVersion = mrjVersion.substring(0, 3);
343       jvm = OTHER;
344       try {
345
346         double version = Double.valueOf(majorMRJVersion).doubleValue();
347
348         if (version == 2) {
349           jvm = MRJ_2_0;
350         } else if (version >= 2.1 && version < 3) {
351           jvm = MRJ_2_1;
352         } else if (version == 3.0) {
353           jvm = MRJ_3_0;
354         } else if (version >= 3.1) {
355           jvm = MRJ_3_1;
356         }
357
358       } catch (NumberFormatException nfe) {
359
360       }
361
362     } else if (osName.startsWith("Windows")) {
363       if (osName.indexOf("9") != -1) {
364         jvm = WINDOWS_9x;
365       } else {
366         jvm = WINDOWS_NT;
367       }
368     }
369   }
370
371   // TODO: refactor this into a better method name !
372   public synchronized PHPDocumentProvider getCompilationUnitDocumentProvider() {
373     if (fCompilationUnitDocumentProvider == null)
374       fCompilationUnitDocumentProvider = new PHPDocumentProvider();
375     return fCompilationUnitDocumentProvider;
376   }
377
378   /**
379    * Get the identifier index manager for the given project
380    * 
381    * @param iProject the current project
382    * @return
383    */
384   public IdentifierIndexManager getIndexManager(IProject iProject) {
385     String indexFilename = iProject.getLocation() + File.separator + "project.index";
386     IdentifierIndexManager indexManager = (IdentifierIndexManager) fIndexManagerMap.get(indexFilename);
387     if (indexManager == null) {
388       indexManager = new IdentifierIndexManager(indexFilename);
389       fIndexManagerMap.put(indexFilename, indexManager);
390     }
391     return indexManager;
392   }
393
394   public synchronized IWorkingCopyManager getWorkingCopyManager() {
395     if (fWorkingCopyManager == null) {
396       PHPDocumentProvider provider = getCompilationUnitDocumentProvider();
397       fWorkingCopyManager = new WorkingCopyManager(provider);
398     }
399     return fWorkingCopyManager;
400   }
401   
402         public synchronized MembersOrderPreferenceCache getMemberOrderPreferenceCache() {
403                 if (fMembersOrderPreferenceCache == null)
404                         fMembersOrderPreferenceCache= new MembersOrderPreferenceCache();
405                 return fMembersOrderPreferenceCache;
406         }       
407         
408   public synchronized ProblemMarkerManager getProblemMarkerManager() {
409     if (fProblemMarkerManager == null)
410       fProblemMarkerManager = new ProblemMarkerManager();
411     return fProblemMarkerManager;
412   }
413
414   public synchronized JavaTextTools getJavaTextTools() {
415     if (fJavaTextTools == null)
416       fJavaTextTools = new JavaTextTools(getPreferenceStore());
417     return fJavaTextTools;
418   }
419
420   public IFile getLastEditorFile() {
421     return fLastEditorFile;
422   }
423
424   /**
425    * Returns the string from the plugin's resource bundle,
426    * or 'key' if not found.
427    */
428   //  public static String getResourceString(String key) {
429   //    ResourceBundle bundle = PHPeclipsePlugin.getDefault().getResourceBundle();
430   //    try {
431   //      return bundle.getString(key);
432   //    } catch (MissingResourceException e) {
433   //      return key;
434   //    }
435   //  }
436
437   /**
438    * Returns the plugin's resource bundle,
439    */
440   //  public ResourceBundle getResourceBundle() {
441   //    return resourceBundle;
442   //  }
443
444   protected void initializeDefaultPreferences(IPreferenceStore store) {
445     // windows preferences:
446     store.setDefault(LOCALHOST_PREF, "http://localhost");
447
448     store.setDefault(SHOW_EXTERNAL_PREVIEW_PREF, "true");
449     store.setDefault(USE_EXTERNAL_BROWSER_PREF, "false");
450     store.setDefault(SHOW_OUTPUT_IN_CONSOLE, "true");
451
452     String windowsSystem = BootLoader.getWS();
453
454     if (jvm == WINDOWS_9x) {
455       store.setDefault(EXTERNAL_BROWSER_PREF, "command.com /c start iexplore {0}");
456     } else if (windowsSystem.equals(BootLoader.WS_WIN32)) {
457       store.setDefault(EXTERNAL_BROWSER_PREF, "rundll32 url.dll,FileProtocolHandler {0}");
458     } else if (windowsSystem.equals(BootLoader.WS_CARBON)) {
459       // TODO How do we start Safari on Mac OS X ?
460       store.setDefault(EXTERNAL_BROWSER_PREF, "netscape {0}");
461     } else {
462       store.setDefault(EXTERNAL_BROWSER_PREF, "netscape {0}");
463     }
464     store.setDefault(DOCUMENTROOT_PREF, getWorkspace().getRoot().getLocation().toString());
465
466     //  if ((jvm == WINDOWS_9x) || (jvm == WINDOWS_NT)) {
467     //  
468     if (windowsSystem.equals(BootLoader.WS_WIN32)) {
469       store.setDefault(PHP_RUN_PREF, "c:\\apache\\php\\php.exe");
470       store.setDefault(EXTERNAL_PARSER_PREF, "c:\\apache\\php\\php -l -f {0}");
471       store.setDefault(MYSQL_RUN_PREF, "c:\\apache\\mysql\\bin\\mysqld-nt.exe");
472       store.setDefault(APACHE_RUN_PREF, "c:\\apache\\apache.exe");
473     } else {
474
475       store.setDefault(PHP_RUN_PREF, "/apache/php/php");
476       store.setDefault(EXTERNAL_PARSER_PREF, "/apache/php/php -l -f {0}");
477       store.setDefault(MYSQL_RUN_PREF, "/apache/mysql/bin/mysqld");
478       store.setDefault(APACHE_RUN_PREF, "/apache/apache");
479
480     }
481     store.setDefault(MYSQL_PREF, "--standalone");
482     store.setDefault(APACHE_START_PREF, "-c \"DocumentRoot \"{0}\"\"");
483     store.setDefault(APACHE_STOP_PREF, "-k shutdown");
484     store.setDefault(APACHE_RESTART_PREF, "-k restart");
485
486     store.setDefault(MYSQL_START_BACKGROUND, "true");
487     store.setDefault(APACHE_START_BACKGROUND, "true");
488     store.setDefault(APACHE_STOP_BACKGROUND, "true");
489     store.setDefault(APACHE_RESTART_BACKGROUND, "true");
490
491     store.setDefault(PHP_PARSER_DEFAULT, PHP_EXTERNAL_PARSER);
492     store.setDefault(PHP_INTERNAL_PARSER, "false");
493     store.setDefault(PHP_EXTERNAL_PARSER, "true");
494
495     //    store.setDefault(PHP_PARSE_ON_SAVE, "true");
496
497     // show line numbers:
498     //   store.setDefault(LINE_NUMBER_RULER, "false");
499     //    store.setDefault(FORMATTER_TAB_SIZE, "4");
500
501     // php syntax highlighting
502     store.setDefault(PHP_USERDEF_XMLFILE, "");
503     //assume there is none  chooA
504
505     PreferenceConverter.setDefault(store, PHP_MULTILINE_COMMENT, PHPColorProvider.MULTI_LINE_COMMENT);
506     PreferenceConverter.setDefault(store, PHP_SINGLELINE_COMMENT, PHPColorProvider.SINGLE_LINE_COMMENT);
507     PreferenceConverter.setDefault(store, PHP_TAG, PHPColorProvider.TAG);
508     PreferenceConverter.setDefault(store, PHP_KEYWORD, PHPColorProvider.KEYWORD);
509     PreferenceConverter.setDefault(store, PHP_VARIABLE, PHPColorProvider.VARIABLE);
510     PreferenceConverter.setDefault(store, PHP_FUNCTIONNAME, PHPColorProvider.FUNCTION_NAME);
511     PreferenceConverter.setDefault(store, PHP_CONSTANT, PHPColorProvider.CONSTANT);
512     PreferenceConverter.setDefault(store, PHP_TYPE, PHPColorProvider.TYPE);
513     PreferenceConverter.setDefault(store, PHP_STRING, PHPColorProvider.STRING);
514     PreferenceConverter.setDefault(store, PHP_DEFAULT, PHPColorProvider.DEFAULT);
515
516     PreferenceConverter.setDefault(store, PHPDOC_KEYWORD, PHPColorProvider.PHPDOC_KEYWORD);
517     PreferenceConverter.setDefault(store, PHPDOC_TAG, PHPColorProvider.PHPDOC_TAG);
518     PreferenceConverter.setDefault(store, PHPDOC_LINK, PHPColorProvider.PHPDOC_LINK);
519     PreferenceConverter.setDefault(store, PHPDOC_DEFAULT, PHPColorProvider.PHPDOC_DEFAULT);
520
521     //    PreferenceConverter.setDefault(
522     //      store,
523     //      PHP_EDITOR_BACKGROUND,
524     //      PHPColorProvider.BACKGROUND);
525     //    PreferenceConverter.setDefault(
526     //      store,
527     //      LINKED_POSITION_COLOR,
528     //      PHPColorProvider.LINKED_POSITION_COLOR);
529     //    PreferenceConverter.setDefault(
530     //      store,
531     //      LINE_NUMBER_COLOR,
532     //      PHPColorProvider.LINE_NUMBER_COLOR);
533
534     //    // set default PHPDoc colors:
535     //    PreferenceConverter.setDefault(
536     //      store,
537     //      PHPDOC_KEYWORD, 
538     //      PHPColorProvider.PHPDOC_KEYWORD);
539     //    PreferenceConverter.setDefault(
540     //      store,
541     //      PHPDOC_LINK, 
542     //      PHPColorProvider.PHPDOC_LINK);      
543     //    PreferenceConverter.setDefault(
544     //      store,
545     //      PHPDOC_DEFAULT, 
546     //      PHPColorProvider.PHPDOC_DEFAULT);
547     //    PreferenceConverter.setDefault(
548     //      store,
549     //      PHPDOC_TAG, 
550     //      PHPColorProvider.PHPDOC_TAG);
551
552     //    store.setDefault(PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT, "true");
553     //    PreferenceConverter.setDefault(
554     //      store,
555     //      PREFERENCE_COLOR_BACKGROUND,
556     //      PHPColorProvider.BACKGROUND_COLOR);
557
558     //language stuff
559     //    store.setDefault(RESOURCE_BUNDLE, LANGUAGE_DEFAULT);
560     //    store.setDefault(RESOURCE_BUNDLE_EN_GB, "true");
561     //    store.setDefault(RESOURCE_BUNDLE_DE, "false");
562     //    store.setDefault(RESOURCE_BUNDLE_FR, "false");
563     //    store.setDefault(RESOURCE_BUNDLE_ES, "false");
564
565     store.setDefault(PHP_OUTLINE_CLASS, "true"); //$NON-NLS-1$
566     store.setDefault(PHP_OUTLINE_FUNC, "true"); //$NON-NLS-1$
567     store.setDefault(PHP_OUTLINE_VAR, "true"); //$NON-NLS-1$
568
569     TemplatePreferencePage.initDefaults(store);
570     //this will initialize the static fields in the syntaxrdr class
571     new PHPSyntaxRdr();
572
573     PHPCore.initializeDefaultPluginPreferences();
574     PreferenceConstants.initializeDefaultValues(store);
575
576     externalTools.initializeDefaultPreferences(store);
577   }
578
579   private IWorkbenchPage internalGetActivePage() {
580     IWorkbenchWindow window = getWorkbench().getActiveWorkbenchWindow();
581     if (window != null)
582       return window.getActivePage();
583     return null;
584   }
585
586   private ImageDescriptorRegistry internalGetImageDescriptorRegistry() {
587     if (fImageDescriptorRegistry == null)
588       fImageDescriptorRegistry = new ImageDescriptorRegistry();
589     return fImageDescriptorRegistry;
590   }
591   /**
592         * Open a file in the Workbench that may or may not exist in the workspace.
593         * Must be run on the UI thread.
594         * @param filename
595         * @throws CoreException
596         */
597   public ITextEditor openFileInTextEditor(String filename) throws CoreException {
598
599     //  reject directories
600     if (new File(filename).isDirectory())
601       return null;
602
603     IWorkbench workbench = PlatformUI.getWorkbench();
604     IWorkbenchWindow window = workbench.getWorkbenchWindows()[0];
605     IWorkbenchPage page = window.getActivePage();
606     IPath path = new Path(filename);
607
608     // If the file exists in the workspace, open it
609     IFile file = getWorkspace().getRoot().getFileForLocation(path);
610     IEditorPart editor;
611     ITextEditor textEditor;
612     if (file != null && file.exists()) {
613       editor = IDE.openEditor(page, file, true);
614       textEditor = (ITextEditor) editor.getAdapter(ITextEditor.class);
615     } else {
616       // Otherwise open the stream directly
617       if (page == null)
618         return null;
619       FileStorage storage = new FileStorage(path);
620       IEditorRegistry registry = getWorkbench().getEditorRegistry();
621       IEditorDescriptor desc = registry.getDefaultEditor(filename);
622       if (desc == null) {
623         desc = registry.getDefaultEditor();
624       }
625       IEditorInput input = new ExternalEditorInput(storage);
626       editor = page.openEditor(input, desc.getId());
627       textEditor = (ITextEditor) editor.getAdapter(ITextEditor.class);
628
629       // If the storage provider is not ours, we can't guarantee read/write.
630       if (textEditor != null) {
631         IDocumentProvider documentProvider = textEditor.getDocumentProvider();
632         if (!(documentProvider instanceof ExternalStorageDocumentProvider)) {
633           storage.setReadOnly();
634         }
635       }
636     }
637     return textEditor;
638   }
639   /**
640         * Open a file in the Workbench that may or may not exist in the workspace.
641         * Must be run on the UI thread.
642         * @param filename
643         * @param line
644         * @throws CoreException
645         */
646   public void openFileAndGotoLine(String filename, int line) throws CoreException {
647
648     ITextEditor textEditor = openFileInTextEditor(filename);
649     if (textEditor != null) {
650       // If a line number was given, go to it
651       if (line > 0) {
652         try {
653           line--; // document is 0 based
654           IDocument document = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());
655           textEditor.selectAndReveal(document.getLineOffset(line), document.getLineLength(line));
656
657         } catch (BadLocationException e) {
658           // invalid text position -> do nothing
659         }
660       }
661     }
662   }
663
664   /**
665         * Open a file in the Workbench that may or may not exist in the workspace.
666         * Must be run on the UI thread.
667         * @param filename
668         * @param offset
669         * @throws CoreException
670         */
671   public void openFileAndGotoOffset(String filename, int offset, int length) throws CoreException {
672
673     ITextEditor textEditor = openFileInTextEditor(filename);
674     if (textEditor != null) {
675       // If a line number was given, go to it
676       if (offset >= 0) {
677         IDocument document = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());
678         textEditor.selectAndReveal(offset, length);
679       }
680     }
681   }
682
683   public void openFileAndFindString(String filename, String findString) throws CoreException {
684
685     ITextEditor textEditor = openFileInTextEditor(filename);
686     if (textEditor != null) {
687       //                If a string was given, go to it
688       if (findString != null) {
689         try {
690           IDocument document = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());
691           int offset = document.search(0, findString, true, false, true);
692           textEditor.selectAndReveal(offset, findString.length());
693
694         } catch (BadLocationException e) {
695           // invalid text position -> do nothing
696         }
697
698       }
699     }
700   }
701   public void setLastEditorFile(IFile textEditor) {
702     this.fLastEditorFile = textEditor;
703   }
704
705   /**
706    * @see org.eclipse.ui.plugin.AbstractUIPlugin#shutdown()
707    */
708   public void shutdown() throws CoreException {
709         // moved down:
710     //    super.shutdown();
711
712     //  externalTools.shutDown();
713     ColorManager.getDefault().dispose();
714
715     // save the information from the php index files if necessary
716     Collection collection = fIndexManagerMap.values();
717     Iterator iterator = collection.iterator();
718     IdentifierIndexManager indexManager = null;
719     while (iterator.hasNext()) {
720       indexManager = (IdentifierIndexManager) iterator.next();
721       indexManager.writeFile();
722     }
723
724     if (fImageDescriptorRegistry != null)
725       fImageDescriptorRegistry.dispose();
726
727     //                          unregisterAdapters();
728
729     super.shutdown();
730
731     if (fWorkingCopyManager != null) {
732       fWorkingCopyManager.shutdown();
733       fWorkingCopyManager = null;
734     }
735
736     if (fCompilationUnitDocumentProvider != null) {
737       fCompilationUnitDocumentProvider.shutdown();
738       fCompilationUnitDocumentProvider = null;
739     }
740
741     if (fJavaTextTools != null) {
742       fJavaTextTools.dispose();
743       fJavaTextTools = null;
744     }
745
746     //                          JavaDocLocations.shutdownJavadocLocations();
747     //          
748     //                          JFaceResources.getFontRegistry().removeListener(fFontPropertyChangeListener);
749
750   }
751
752   public void startup() throws CoreException {
753     super.startup();
754     IAdapterManager manager = Platform.getAdapterManager();
755     manager.registerAdapters(new PHPElementAdapterFactory(), PHPElement.class);
756     manager.registerAdapters(new ResourceAdapterFactory(), IResource.class);
757     //  externalTools.startUp(); 
758     getStandardDisplay().asyncExec(new Runnable() {
759       public void run() {
760         //initialize the variable context manager
761         VariableContextManager.getDefault();
762       }
763     });
764   }
765
766 }