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