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