Replace deprecated org.eclipse.core.runtime.Platform.Run with org.eclipse.core.runtim...
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpeclipse / ui / WebUI.java
1 /*
2  * Copyright (c) 2004 Christopher Lenz 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  *     Christopher Lenz - initial implementation
10  *
11  * $Id: WebUI.java,v 1.11 2007-11-08 01:37:06 scorphus Exp $
12  */
13
14 package net.sourceforge.phpeclipse.ui;
15
16 import org.eclipse.jface.resource.JFaceResources;
17 import java.io.File;
18 import java.io.IOException;
19 //import java.io.InputStream;
20 import java.net.URL;
21 //import java.util.Enumeration;
22 import java.util.HashMap;
23 //import java.util.PropertyResourceBundle;
24
25 //import net.sourceforge.phpdt.core.JavaCore;
26 import net.sourceforge.phpdt.core.IBuffer;
27 import net.sourceforge.phpdt.core.ICompilationUnit;
28 import net.sourceforge.phpdt.core.IJavaElement;
29 import net.sourceforge.phpdt.core.WorkingCopyOwner;
30 import net.sourceforge.phpdt.internal.core.JavaModelManager;
31 import net.sourceforge.phpdt.internal.core.util.Util;
32 import net.sourceforge.phpdt.internal.corext.template.php.CodeTemplateContextType;
33 import net.sourceforge.phpdt.internal.corext.template.php.HTMLContextType;
34 import net.sourceforge.phpdt.internal.corext.template.php.JavaContextType;
35 import net.sourceforge.phpdt.internal.corext.template.php.JavaDocContextType;
36 import net.sourceforge.phpdt.internal.ui.IJavaStatusConstants;
37 import net.sourceforge.phpdt.internal.ui.JavaElementAdapterFactory;
38 import net.sourceforge.phpdt.internal.ui.ResourceAdapterFactory;
39 import net.sourceforge.phpdt.internal.ui.preferences.MembersOrderPreferenceCache;
40 import net.sourceforge.phpdt.internal.ui.preferences.MockupPreferenceStore;
41 import net.sourceforge.phpdt.internal.ui.text.PreferencesAdapter;
42 import net.sourceforge.phpdt.internal.ui.text.folding.JavaFoldingStructureProviderRegistry;
43 import net.sourceforge.phpdt.internal.ui.text.java.hover.JavaEditorTextHoverDescriptor;
44 import net.sourceforge.phpdt.internal.ui.viewsupport.ImageDescriptorRegistry;
45 import net.sourceforge.phpdt.internal.ui.viewsupport.ProblemMarkerManager;
46 import net.sourceforge.phpdt.ui.IContextMenuConstants;
47 import net.sourceforge.phpdt.ui.IWorkingCopyManager;
48 import net.sourceforge.phpdt.ui.text.JavaTextTools;
49 import net.sourceforge.phpdt.ui.PreferenceConstants;
50 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
51 import net.sourceforge.phpeclipse.builder.ExternalEditorInput;
52 import net.sourceforge.phpeclipse.builder.ExternalStorageDocumentProvider;
53 import net.sourceforge.phpeclipse.builder.FileStorage;
54 import net.sourceforge.phpeclipse.builder.IdentifierIndexManager;
55 import net.sourceforge.phpeclipse.phpeditor.DocumentAdapter;
56 import net.sourceforge.phpeclipse.phpeditor.ICompilationUnitDocumentProvider;
57 import net.sourceforge.phpeclipse.phpeditor.PHPDocumentProvider;
58 //import net.sourceforge.phpeclipse.phpeditor.PHPSyntaxRdr;
59 import net.sourceforge.phpeclipse.phpeditor.WorkingCopyManager;
60 //import net.sourceforge.phpeclipse.phpeditor.util.PHPColorProvider;
61
62
63 import net.sourceforge.phpeclipse.ui.templates.template.HTMLTemplateContextType;
64 //import net.sourceforge.phpdt.internal.corext.template.php.HTMLContextType;
65
66 import net.sourceforge.phpeclipse.ui.templates.template.JSTemplateContextType;
67 import net.sourceforge.phpeclipse.ui.templates.template.SmartyTemplateContextType;
68 import net.sourceforge.phpeclipse.ui.templates.template.XMLTemplateContextType;
69
70 import org.eclipse.core.resources.IFile;
71 import org.eclipse.core.resources.IProject;
72 import org.eclipse.core.resources.IResource;
73 import org.eclipse.core.resources.IResourceChangeEvent;
74 import org.eclipse.core.resources.ISavedState;
75 import org.eclipse.core.resources.IWorkspace;
76 import org.eclipse.core.resources.ResourcesPlugin;
77 import org.eclipse.core.resources.WorkspaceJob;
78 import org.eclipse.core.runtime.CoreException;
79 import org.eclipse.core.runtime.IAdapterManager;
80 import org.eclipse.core.runtime.IConfigurationElement;
81 import org.eclipse.core.runtime.IPath;
82 import org.eclipse.core.runtime.IProgressMonitor;
83 import org.eclipse.core.runtime.IStatus;
84 import org.eclipse.core.runtime.Path;
85 import org.eclipse.core.runtime.Platform;
86 //import org.eclipse.core.runtime.Platform;
87 import org.eclipse.core.runtime.Status;
88 import org.eclipse.core.runtime.Preferences.IPropertyChangeListener;
89 import org.eclipse.core.runtime.Preferences.PropertyChangeEvent;
90 import org.eclipse.core.runtime.jobs.Job;
91 import org.eclipse.jface.action.GroupMarker;
92 import org.eclipse.jface.action.IMenuManager;
93 import org.eclipse.jface.action.Separator;
94 import org.eclipse.jface.preference.IPreferenceStore;
95 import org.eclipse.jface.preference.PreferenceConverter;
96 //import org.eclipse.jface.preference.PreferenceConverter;
97 import org.eclipse.jface.resource.ImageDescriptor;
98 import org.eclipse.jface.resource.ImageRegistry;
99 //import org.eclipse.jface.resource.JFaceResources;
100 import org.eclipse.jface.text.BadLocationException;
101 import org.eclipse.jface.text.IDocument;
102 import org.eclipse.jface.text.templates.ContextTypeRegistry;
103 import org.eclipse.jface.text.templates.persistence.TemplateStore;
104 //import org.eclipse.swt.graphics.RGB;
105 import org.eclipse.swt.widgets.Shell;
106 import org.eclipse.ui.IEditorDescriptor;
107 import org.eclipse.ui.IEditorInput;
108 import org.eclipse.ui.IEditorPart;
109 import org.eclipse.ui.IEditorRegistry;
110 import org.eclipse.ui.IWorkbench;
111 import org.eclipse.ui.IWorkbenchPage;
112 import org.eclipse.ui.IWorkbenchWindow;
113 import org.eclipse.ui.PlatformUI;
114 import org.eclipse.ui.editors.text.EditorsUI;
115 import org.eclipse.ui.editors.text.templates.ContributionContextTypeRegistry;
116 import org.eclipse.ui.editors.text.templates.ContributionTemplateStore;
117 import org.eclipse.ui.ide.IDE;
118 import org.eclipse.ui.plugin.AbstractUIPlugin;
119 import org.eclipse.ui.texteditor.ChainedPreferenceStore;
120 import org.eclipse.ui.texteditor.ConfigurationElementSorter;
121 import org.eclipse.ui.texteditor.IDocumentProvider;
122 import org.eclipse.ui.texteditor.ITextEditor;
123 import org.osgi.framework.BundleContext;
124
125 /**
126  * The web development tools UI plugin.
127  */
128 public class WebUI extends AbstractUIPlugin implements IPreferenceConstants {
129
130         public static final String PLUGIN_ID = "net.sourceforge.phpeclipse.ui";
131
132         public static final String EDITOR_ID = WebUI.PLUGIN_ID + ".PHPUnitEditor";
133
134         private static final String CUSTOM_TEMPLATES_KEY = "net.sourceforge.phpeclipse.ui.templates"; //$NON-NLS-1$
135
136         /**
137          * The key to store customized code templates.
138          * 
139          * @since 3.0
140          */
141         private static final String CODE_TEMPLATES_KEY = "net.sourceforge.phpdt.ui.text.custom_code_templates"; //$NON-NLS-1$
142         /**
143          * The maximum number of allowed proposals by category
144          */
145         public final static int MAX_PROPOSALS = 200;
146
147         public final static String PHP_NATURE_ID = PLUGIN_ID + ".phpnature";
148
149         // Constants ---------------------------------------------------------------
150
151         public static final String ICON_OVERLAY_ERROR = "full/ovr16/error_co.gif"; //$NON-NLS-1$
152
153         public static final String ICON_OVERLAY_WARNING = "full/ovr16/warning_co.gif"; //$NON-NLS-1$
154
155         // Instance Variables ------------------------------------------------------
156
157         /** The shared instance. */
158         private static WebUI plugin;
159         
160         /**
161          * The combined preference store.
162          * 
163          * @since 3.0
164          */
165         private IPreferenceStore fCombinedPreferenceStore;
166
167         /**
168          * The coded template store for the java editor.
169          * 
170          * @since 3.0
171          */
172         private TemplateStore fCodeTemplateStore;
173
174         /**
175          * The code template context type registry for the java editor.
176          * 
177          * @since 3.0
178          */
179         private ContextTypeRegistry fCodeTemplateContextTypeRegistry;
180
181         /**
182          * The template context type registry for the java editor.
183          * 
184          * @since 3.0
185          */
186         private ContextTypeRegistry fContextTypeRegistry;
187
188         private ResourceAdapterFactory fResourceAdapterFactory;
189
190         private JavaElementAdapterFactory fJavaElementAdapterFactory;
191         
192         private ICompilationUnitDocumentProvider fCompilationUnitDocumentProvider;
193         
194         private JavaTextTools fJavaTextTools;
195
196         private IWorkingCopyManager fWorkingCopyManager;
197         
198         private IPropertyChangeListener fFontPropertyChangeListener;
199         
200         private ProblemMarkerManager fProblemMarkerManager;
201
202         private MockupPreferenceStore fMockupPreferenceStore;
203
204         private ImageDescriptorRegistry fImageDescriptorRegistry;
205         
206         private JavaEditorTextHoverDescriptor[] fJavaEditorTextHoverDescriptors;
207
208         private MembersOrderPreferenceCache fMembersOrderPreferenceCache;
209
210         private HashMap fIndexManagerMap = new HashMap();
211
212         /**
213          * The extension point registry for the
214          * <code>net.sourceforge.phpdt.ui.javaFoldingStructureProvider</code>
215          * extension point.
216          * 
217          * @since 3.0
218          */
219         private JavaFoldingStructureProviderRegistry fFoldingStructureProviderRegistry;
220
221         public static IWorkbenchPage getActivePage() {
222                 return getDefault().internalGetActivePage();
223         }
224
225         private IWorkbenchPage internalGetActivePage() {
226                 return getWorkbench().getActiveWorkbenchWindow().getActivePage();
227         }
228
229         public static Shell getActiveWorkbenchShell() {
230                 return getActiveWorkbenchWindow().getShell();
231         }
232
233         public static IWorkbenchWindow getActiveWorkbenchWindow() {
234                 return getDefault().getWorkbench().getActiveWorkbenchWindow();
235         }
236
237         // Public Methods ----------------------------------------------------------
238
239         /**
240          * Returns the shared instance.
241          */
242         public static WebUI getDefault() {
243                 return plugin;
244         }
245
246         /**
247          * Returns the workspace instance.
248          */
249         public static IWorkspace getWorkspace() {
250                 return ResourcesPlugin.getWorkspace();
251         }
252
253         /** The context type registry. */
254         private ContributionContextTypeRegistry fRegistry;
255
256         /** The template store. */
257         private TemplateStore fStore;
258
259         // Constructors ------------------------------------------------------------
260
261         /**
262          * The constructor.
263          */
264         public WebUI() {
265                 plugin = this;
266         }
267
268         /**
269          * Returns this plug-in's context type registry.
270          * 
271          * @return the context type registry for this plug-in instance
272          */
273         public ContextTypeRegistry getContextTypeRegistry() {
274                 if (fRegistry == null) {
275                         // create an configure the contexts available in the editor
276                         fRegistry = new ContributionContextTypeRegistry();
277                         fRegistry.addContextType(XMLTemplateContextType.XML_CONTEXT_TYPE);
278                         fRegistry.addContextType(HTMLTemplateContextType.HTML_CONTEXT_TYPE);
279                         fRegistry.addContextType(SmartyTemplateContextType.SMARTY_CONTEXT_TYPE);
280                         fRegistry.addContextType(JSTemplateContextType.JS_CONTEXT_TYPE);
281                 }
282                 return fRegistry;
283         }
284
285         // Private Methods ---------------------------------------------------------
286
287         /**
288          * Returns an image descriptor for the image corresponding to the specified
289          * key (which is the name of the image file).
290          * 
291          * @param key
292          *            The key of the image
293          * @return The descriptor for the requested image, or <code>null</code> if
294          *         the image could not be found
295          */
296         private ImageDescriptor getImageDescriptor(String key) {
297                 try {
298                         URL url = getBundle().getEntry("/icons/" + key); //$NON-NLS-1$
299                         return ImageDescriptor.createFromURL(url);
300                 } catch (IllegalStateException e) {
301                         return null;
302                 }
303         }
304
305         /**
306          * Returns this plug-in's template store.
307          * 
308          * @return the template store of this plug-in instance
309          */
310         public TemplateStore getTemplateStore() {
311                 if (fStore == null) {
312                         fStore = new ContributionTemplateStore(getContextTypeRegistry(),
313                                         getDefault().getPreferenceStore(), CUSTOM_TEMPLATES_KEY);
314                         try {
315                                 fStore.load();
316                         } catch (IOException e) {
317                                 WebUI
318                                                 .getDefault()
319                                                 .getLog()
320                                                 .log(
321                                                                 new Status(
322                                                                                 IStatus.ERROR,
323                                                                                 "net.sourceforge.phpeclipse.ui", IStatus.OK, "", e)); //$NON-NLS-1$ //$NON-NLS-2$
324                         }
325                 }
326                 return fStore;
327         }
328
329         /*
330          * @see AbstractUIPlugin#initializeImageRegistry(ImageRegistry)
331          */
332         protected void initializeImageRegistry(ImageRegistry reg) {
333                 reg.put(ICON_OVERLAY_ERROR, getImageDescriptor(ICON_OVERLAY_ERROR));
334                 reg.put(ICON_OVERLAY_WARNING, getImageDescriptor(ICON_OVERLAY_WARNING));
335         }
336
337         // private IWorkbenchPage internalGetActivePage() {
338         // IWorkbenchWindow window = getWorkbench().getActiveWorkbenchWindow();
339         // if (window != null)
340         // return window.getActivePage();
341         // return null;
342         // }
343
344         public static void log(IStatus status) {
345                 getDefault().getLog().log(status);
346         }
347
348         public static void log(Throwable e) {
349                 log(new Status(IStatus.ERROR, PLUGIN_ID, IStatus.ERROR, ""
350                                 + e.getLocalizedMessage(), e));
351         }
352         
353         public static String getPluginId() {
354                 return getDefault().getBundle().getSymbolicName();
355         }
356         
357         /**
358          * Returns a combined preference store, this store is read-only.
359          * 
360          * @return the combined preference store
361          * 
362          * @since 3.0
363          */
364         public IPreferenceStore getCombinedPreferenceStore() {
365                 if (fCombinedPreferenceStore == null) {
366                         IPreferenceStore generalTextStore = EditorsUI.getPreferenceStore();
367                         fCombinedPreferenceStore = new ChainedPreferenceStore(
368                                         new IPreferenceStore[] {
369                                                         getPreferenceStore(),
370                                                         new PreferencesAdapter(WebUI
371                                                                         .getDefault().getPluginPreferences()),
372                                                         generalTextStore });
373                 }
374                 return fCombinedPreferenceStore;
375         }
376         
377         // TODO: refactor this into a better method name !
378         public synchronized ICompilationUnitDocumentProvider getCompilationUnitDocumentProvider() {
379                 if (fCompilationUnitDocumentProvider == null)
380                         fCompilationUnitDocumentProvider = new PHPDocumentProvider();
381                 return fCompilationUnitDocumentProvider;
382         }
383         
384         public synchronized JavaTextTools getJavaTextTools() {
385                 if (fJavaTextTools == null)
386                         fJavaTextTools = new JavaTextTools(getPreferenceStore(), /*JavaCore
387                                         .getPlugin().*/getPluginPreferences());
388                 return fJavaTextTools;
389         }
390         
391         public synchronized IWorkingCopyManager getWorkingCopyManager() {
392                 if (fWorkingCopyManager == null) {
393                         ICompilationUnitDocumentProvider provider = getCompilationUnitDocumentProvider();
394                         fWorkingCopyManager = new WorkingCopyManager(provider);
395                 }
396                 return fWorkingCopyManager;
397         }
398
399         public synchronized ProblemMarkerManager getProblemMarkerManager() {
400                 if (fProblemMarkerManager == null)
401                         fProblemMarkerManager = new ProblemMarkerManager();
402                 return fProblemMarkerManager;
403         }       
404
405         /**
406          * Returns the mockup preference store for firing events and registering
407          * listeners on project setting changes. Temporary solution.
408          */
409         public MockupPreferenceStore getMockupPreferenceStore() {
410                 if (fMockupPreferenceStore == null)
411                         fMockupPreferenceStore = new MockupPreferenceStore();
412
413                 return fMockupPreferenceStore;
414         }
415
416         public static ImageDescriptorRegistry getImageDescriptorRegistry() {
417                 return getDefault().internalGetImageDescriptorRegistry();
418         }
419         
420         private ImageDescriptorRegistry internalGetImageDescriptorRegistry() {
421                 if (fImageDescriptorRegistry == null)
422                         fImageDescriptorRegistry = new ImageDescriptorRegistry();
423                 return fImageDescriptorRegistry;
424         }
425         
426         /**
427          * Returns the registry of the extensions to the
428          * <code>net.sourceforge.phpdt.ui.javaFoldingStructureProvider</code>
429          * extension point.
430          * 
431          * @return the registry of contributed
432          *         <code>IJavaFoldingStructureProvider</code>
433          * @since 3.0
434          */
435         public synchronized JavaFoldingStructureProviderRegistry getFoldingStructureProviderRegistry() {
436                 if (fFoldingStructureProviderRegistry == null)
437                         fFoldingStructureProviderRegistry = new JavaFoldingStructureProviderRegistry();
438                 return fFoldingStructureProviderRegistry;
439         }       
440         /**
441          * Returns all Java editor text hovers contributed to the workbench.
442          * 
443          * @return an array of JavaEditorTextHoverDescriptor
444          * @since 2.1
445          */
446         public JavaEditorTextHoverDescriptor[] getJavaEditorTextHoverDescriptors() {
447                 if (fJavaEditorTextHoverDescriptors == null) {
448                         fJavaEditorTextHoverDescriptors = JavaEditorTextHoverDescriptor
449                                         .getContributedHovers();
450                         ConfigurationElementSorter sorter = new ConfigurationElementSorter() {
451                                 /*
452                                  * @see org.eclipse.ui.texteditor.ConfigurationElementSorter#getConfigurationElement(java.lang.Object)
453                                  */
454                                 public IConfigurationElement getConfigurationElement(
455                                                 Object object) {
456                                         return ((JavaEditorTextHoverDescriptor) object)
457                                                         .getConfigurationElement();
458                                 }
459                         };
460                         sorter.sort(fJavaEditorTextHoverDescriptors);
461
462                         // The Problem hover has to be the first and the Annotation hover
463                         // has to
464                         // be the last one in the JDT UI's hover list
465                         int length = fJavaEditorTextHoverDescriptors.length;
466                         int first = -1;
467                         int last = length - 1;
468                         int problemHoverIndex = -1;
469                         int annotationHoverIndex = -1;
470                         for (int i = 0; i < length; i++) {
471                                 if (!fJavaEditorTextHoverDescriptors[i].getId().startsWith(
472                                                 PLUGIN_ID)) {
473                                         if (problemHoverIndex == -1 || annotationHoverIndex == -1)
474                                                 continue;
475                                         else {
476                                                 last = i - 1;
477                                                 break;
478                                         }
479                                 }
480                                 if (first == -1)
481                                         first = i;
482
483                                 if (fJavaEditorTextHoverDescriptors[i].getId().equals(
484                                                 "net.sourceforge.phpdt.ui.AnnotationHover")) { //$NON-NLS-1$
485                                         annotationHoverIndex = i;
486                                         continue;
487                                 }
488                                 if (fJavaEditorTextHoverDescriptors[i].getId().equals(
489                                                 "net.sourceforge.phpdt.ui.ProblemHover")) { //$NON-NLS-1$
490                                         problemHoverIndex = i;
491                                         continue;
492                                 }
493                         }
494
495                         JavaEditorTextHoverDescriptor hoverDescriptor = null;
496
497                         if (first > -1 && problemHoverIndex > -1
498                                         && problemHoverIndex != first) {
499                                 // move problem hover to beginning
500                                 hoverDescriptor = fJavaEditorTextHoverDescriptors[first];
501                                 fJavaEditorTextHoverDescriptors[first] = fJavaEditorTextHoverDescriptors[problemHoverIndex];
502                                 fJavaEditorTextHoverDescriptors[problemHoverIndex] = hoverDescriptor;
503
504                                 // update annotation hover index if needed
505                                 if (annotationHoverIndex == first)
506                                         annotationHoverIndex = problemHoverIndex;
507                         }
508
509                         if (annotationHoverIndex > -1 && annotationHoverIndex != last) {
510                                 // move annotation hover to end
511                                 hoverDescriptor = fJavaEditorTextHoverDescriptors[last];
512                                 fJavaEditorTextHoverDescriptors[last] = fJavaEditorTextHoverDescriptors[annotationHoverIndex];
513                                 fJavaEditorTextHoverDescriptors[annotationHoverIndex] = hoverDescriptor;
514                         }
515
516                         // Move Best Match hover to front
517                         for (int i = 0; i < fJavaEditorTextHoverDescriptors.length - 1; i++) {
518                                 if (PreferenceConstants.ID_BESTMATCH_HOVER
519                                                 .equals(fJavaEditorTextHoverDescriptors[i].getId())) {
520                                         hoverDescriptor = fJavaEditorTextHoverDescriptors[i];
521                                         for (int j = i; j > 0; j--)
522                                                 fJavaEditorTextHoverDescriptors[j] = fJavaEditorTextHoverDescriptors[j - 1];
523                                         fJavaEditorTextHoverDescriptors[0] = hoverDescriptor;
524                                         break;
525                                 }
526
527                         }
528                 }
529
530                 return fJavaEditorTextHoverDescriptors;
531         }
532
533         public synchronized MembersOrderPreferenceCache getMemberOrderPreferenceCache() {
534                 if (fMembersOrderPreferenceCache == null)
535                         fMembersOrderPreferenceCache = new MembersOrderPreferenceCache();
536                 return fMembersOrderPreferenceCache;
537         }
538         /**
539          * Open a file in the Workbench that may or may not exist in the workspace.
540          * Must be run on the UI thread.
541          * 
542          * @param filename
543          * @throws CoreException
544          */
545         public ITextEditor openFileInTextEditor(String filename)
546                         throws CoreException {
547                 // reject directories
548                 if (new File(filename).isDirectory())
549                         return null;
550                 IWorkbench workbench = PlatformUI.getWorkbench();
551                 IWorkbenchWindow window = workbench.getWorkbenchWindows()[0];
552                 IWorkbenchPage page = window.getActivePage();
553                 IPath path = new Path(filename);
554                 // If the file exists in the workspace, open it
555                 IFile file = getWorkspace().getRoot().getFile(path);
556                 IEditorPart editor;
557                 ITextEditor textEditor;
558                 if (file != null && file.exists()) {
559                         editor = IDE.openEditor(page, file, true);
560                         textEditor = (ITextEditor) editor.getAdapter(ITextEditor.class);
561                 } else {
562                         // Otherwise open the stream directly
563                         if (page == null)
564                                 return null;
565                         FileStorage storage = new FileStorage(path);
566                         IEditorRegistry registry = getWorkbench().getEditorRegistry();
567                         IEditorDescriptor desc = registry.getDefaultEditor(filename);
568                         if (desc == null) {
569                                 desc = registry
570                                                 .findEditor(IEditorRegistry.SYSTEM_EXTERNAL_EDITOR_ID);
571                                 // desc = registry.getDefaultEditor();
572                         }
573                         IEditorInput input = new ExternalEditorInput(storage);
574                         editor = page.openEditor(input, desc.getId());
575                         textEditor = (ITextEditor) editor.getAdapter(ITextEditor.class);
576                         // If the storage provider is not ours, we can't guarantee
577                         // read/write.
578                         if (textEditor != null) {
579                                 IDocumentProvider documentProvider = textEditor
580                                                 .getDocumentProvider();
581                                 if (!(documentProvider instanceof ExternalStorageDocumentProvider)) {
582                                         storage.setReadOnly();
583                                 }
584                         }
585                 }
586                 return textEditor;
587         }
588         /**
589          * Creates the PHP plugin standard groups in a context menu.
590          */
591         public static void createStandardGroups(IMenuManager menu) {
592                 if (!menu.isEmpty())
593                         return;
594                 menu.add(new Separator(IContextMenuConstants.GROUP_NEW));
595                 menu.add(new GroupMarker(IContextMenuConstants.GROUP_GOTO));
596                 menu.add(new Separator(IContextMenuConstants.GROUP_OPEN));
597                 menu.add(new GroupMarker(IContextMenuConstants.GROUP_SHOW));
598                 menu.add(new Separator(IContextMenuConstants.GROUP_REORGANIZE));
599                 menu.add(new Separator(IContextMenuConstants.GROUP_GENERATE));
600                 menu.add(new Separator(IContextMenuConstants.GROUP_SEARCH));
601                 menu.add(new Separator(IContextMenuConstants.GROUP_BUILD));
602                 menu.add(new Separator(IContextMenuConstants.GROUP_ADDITIONS));
603                 menu.add(new Separator(IContextMenuConstants.GROUP_VIEWER_SETUP));
604                 menu.add(new Separator(IContextMenuConstants.GROUP_PROPERTIES));
605         }
606
607         public IdentifierIndexManager getIndexManager(IProject iProject) {
608                 IPath path = iProject.getWorkingLocation(/*WebUI*/PHPeclipsePlugin.PLUGIN_ID);
609                 path = path.append("project.index");
610                 String indexFilename = path.toString();
611                 // try {
612                 // IdentDB db = IdentDB.getInstance();
613                 // } catch (ClassNotFoundException e) {
614                 // e.printStackTrace();
615                 // } catch (SQLException e) {
616                 // e.printStackTrace();
617                 // }
618                 IdentifierIndexManager indexManager = (IdentifierIndexManager) fIndexManagerMap
619                                 .get(indexFilename);
620                 if (indexManager == null) {
621                         indexManager = new IdentifierIndexManager(indexFilename);
622                         fIndexManagerMap.put(indexFilename, indexManager);
623                 }
624                 return indexManager;
625         }
626
627         /**
628          * Returns the template store for the code generation templates.
629          * 
630          * @return the template store for the code generation templates
631          * @since 3.0
632          */
633         public TemplateStore getCodeTemplateStore() {
634                 if (fCodeTemplateStore == null) {
635                         fCodeTemplateStore = new ContributionTemplateStore(
636                                         getCodeTemplateContextRegistry(), getPreferenceStore(),
637                                         CODE_TEMPLATES_KEY);
638                         try {
639                                 fCodeTemplateStore.load();
640                         } catch (IOException e) {
641                                 log(e);
642                         }
643                 }
644
645                 return fCodeTemplateStore;
646         }
647         /**
648          * Returns the template context type registry for the code generation
649          * templates.
650          * 
651          * @return the template context type registry for the code generation
652          *         templates
653          * @since 3.0
654          */
655         public ContextTypeRegistry getCodeTemplateContextRegistry() {
656                 if (fCodeTemplateContextTypeRegistry == null) {
657                         fCodeTemplateContextTypeRegistry = new ContributionContextTypeRegistry();
658
659                         CodeTemplateContextType
660                                         .registerContextTypes(fCodeTemplateContextTypeRegistry);
661                 }
662
663                 return fCodeTemplateContextTypeRegistry;
664         }
665
666         public void openFileAndGotoOffset(String filename, int offset, int length)
667         throws CoreException {
668                 ITextEditor textEditor = openFileInTextEditor(filename);
669                 if (textEditor != null) {
670                         // If a line number was given, go to it
671                         if (offset >= 0) {
672                                 IDocument document = textEditor.getDocumentProvider()
673                                                 .getDocument(textEditor.getEditorInput());
674                                 textEditor.selectAndReveal(offset, length);
675                         }
676                 }
677         }
678
679         public void openFileAndFindString(String filename, String findString)
680         throws CoreException {
681                 ITextEditor textEditor = openFileInTextEditor(filename);
682                 if (textEditor != null) {
683                         // If a string was given, go to it
684                         if (findString != null) {
685                                 try {
686                                         IDocument document = textEditor.getDocumentProvider()
687                                                         .getDocument(textEditor.getEditorInput());
688                                         int offset = document.search(0, findString, true, false,
689                                                         true);
690                                         textEditor.selectAndReveal(offset, findString.length());
691                                 } catch (BadLocationException e) {
692                                         // invalid text position -> do nothing
693                                 }
694                         }
695                 }
696         }
697
698          public static void logErrorMessage(String message) {
699                  log(new Status(IStatus.ERROR, getPluginId(),IJavaStatusConstants.INTERNAL_ERROR, message, null));
700          }
701                 public static void log(String message, Throwable t) {
702                         log(error(message, t));
703                 }
704                 public static IStatus error(Throwable t) {
705                         return error("PHPeclipsePlugin.internalErrorOccurred", t); //$NON-NLS-1$
706                 }
707
708                 public static IStatus error(String message, Throwable t) {
709                         return new Status(IStatus.ERROR, PLUGIN_ID, IStatus.ERROR, message, t);
710                 }
711
712                 /**
713                  * Resets the Java editor text hovers contributed to the workbench.
714                  * <p>
715                  * This will force a rebuild of the descriptors the next time a client asks
716                  * for them.
717                  * </p>
718                  * 
719                  * @return an array of JavaEditorTextHoverDescriptor
720                  * @since 2.1
721                  */
722                 public void resetJavaEditorTextHoverDescriptors() {
723                         fJavaEditorTextHoverDescriptors = null;
724                 }
725
726                 /**
727                  * Returns the template context type registry for the java plugin.
728                  * 
729                  * @return the template context type registry for the java plugin
730                  * @since 3.0
731                  */
732                 public ContextTypeRegistry getTemplateContextRegistry() {
733                         if (fContextTypeRegistry == null) {
734                                 fContextTypeRegistry = new ContributionContextTypeRegistry();
735         
736                                 fContextTypeRegistry.addContextType(new JavaContextType());
737                                 fContextTypeRegistry.addContextType(new JavaDocContextType());
738                                 fContextTypeRegistry.addContextType(new HTMLContextType());
739                         }
740         
741                         return fContextTypeRegistry;
742                 }
743                 // public static void logErrorMessage(String message) {
744                 // log(new Status(IStatus.ERROR, getPluginId(),
745                 // JavaStatusConstants.INTERNAL_ERROR, message, null));
746                 // }
747
748
749
750                 /*
751                  * (non - Javadoc) Method declared in Plugin
752                  */
753                 public void start(BundleContext context) throws Exception {
754                         super.start(context);
755         
756                         // JavaCore.start(this, context);
757                         final JavaModelManager modelManager = JavaModelManager
758                                         .getJavaModelManager();
759                         try {
760                                 modelManager.configurePluginDebugOptions();
761         
762                                 // request state folder creation (workaround 19885)
763                                 getStateLocation();
764                                 // retrieve variable values
765                                 PHPeclipsePlugin.getDefault().getPluginPreferences()
766                                                 .addPropertyChangeListener(
767                                                                 new JavaModelManager.PluginPreferencesListener());
768                                 // manager.loadVariablesAndContainers();
769         
770                                 final IWorkspace workspace = ResourcesPlugin.getWorkspace();
771                                 workspace.addResourceChangeListener(modelManager.deltaState,
772                                                 IResourceChangeEvent.PRE_BUILD
773                                                                 | IResourceChangeEvent.POST_BUILD
774                                                                 | IResourceChangeEvent.POST_CHANGE
775                                                                 | IResourceChangeEvent.PRE_DELETE
776                                                                 | IResourceChangeEvent.PRE_CLOSE);
777         
778                                 ISavedState savedState = workspace.addSaveParticipant(
779                                                 /*PHPeclipsePlugin*/WebUI.this, modelManager);
780         
781                                 WorkspaceJob processSavedState = new WorkspaceJob(
782                                                 Util.bind("savedState.jobName")) { //$NON-NLS-1$
783                                         public IStatus runInWorkspace(IProgressMonitor monitor)
784                                                         throws CoreException {
785                                                 ISavedState savedState = workspace.addSaveParticipant(
786                                                                 /*PHPeclipsePlugin*/WebUI.this, modelManager);
787                                                 if (savedState != null) {
788                                                         modelManager.deltaState.getDeltaProcessor().overridenEventType
789                                                                         = IResourceChangeEvent.POST_CHANGE;
790                                                         savedState.processResourceChangeEvents(modelManager.deltaState);
791                                                 }
792                                                 return Status.OK_STATUS;
793                                         }
794                                 };
795                                 processSavedState.setSystem(true);
796                                 processSavedState.setPriority(Job.SHORT); // process asap
797                                 processSavedState.schedule();
798                         } catch (RuntimeException e) {
799                                 modelManager.shutdown();
800                                 throw e;
801                         }
802         
803                         registerAdapters();
804         
805                         // if (USE_WORKING_COPY_OWNERS) {
806                         WorkingCopyOwner.setPrimaryBufferProvider(new WorkingCopyOwner() {
807                                 public IBuffer createBuffer(ICompilationUnit workingCopy) {
808                                         ICompilationUnit original = workingCopy.getPrimary();
809                                         IResource resource = original.getResource();
810                                         if (resource instanceof IFile)
811                                                 return new DocumentAdapter(workingCopy, (IFile) resource);
812                                         return DocumentAdapter.NULL;
813                                 }
814                         });
815                         // }
816         
817                         installPreferenceStoreBackwardsCompatibility();
818         
819                 }
820         
821
822                 private void registerAdapters() {
823                 fJavaElementAdapterFactory = new JavaElementAdapterFactory();
824                 fResourceAdapterFactory = new ResourceAdapterFactory();
825
826                 IAdapterManager manager = Platform.getAdapterManager();
827                 manager
828                                 .registerAdapters(fJavaElementAdapterFactory,
829                                                 IJavaElement.class);
830                 manager.registerAdapters(fResourceAdapterFactory, IResource.class);
831         }
832
833
834
835                 /**
836                  * Installs backwards compatibility for the preference store.
837                  */
838                 private void installPreferenceStoreBackwardsCompatibility() {
839         
840                         /*
841                          * Installs backwards compatibility: propagate the Java editor font from
842                          * a pre-2.1 plug-in to the Platform UI's preference store to preserve
843                          * the Java editor font from a pre-2.1 workspace. This is done only
844                          * once.
845                          */
846                         String fontPropagatedKey = "fontPropagated"; //$NON-NLS-1$
847                         if (getPreferenceStore().contains(JFaceResources.TEXT_FONT)
848                                         && !getPreferenceStore().isDefault(JFaceResources.TEXT_FONT)) {
849                                 if (!getPreferenceStore().getBoolean(fontPropagatedKey))
850                                         PreferenceConverter
851                                                         .setValue(PlatformUI.getWorkbench()
852                                                                         .getPreferenceStore(),
853                                                                         PreferenceConstants.EDITOR_TEXT_FONT,
854                                                                         PreferenceConverter.getFontDataArray(
855                                                                                         getPreferenceStore(),
856                                                                                         JFaceResources.TEXT_FONT));
857                         }
858                         getPreferenceStore().setValue(fontPropagatedKey, true);
859         
860                         /*
861                          * Backwards compatibility: set the Java editor font in this plug-in's
862                          * preference store to let older versions access it. Since 2.1 the Java
863                          * editor font is managed by the workbench font preference page.
864                          */
865                         PreferenceConverter.putValue(getPreferenceStore(),
866                                         JFaceResources.TEXT_FONT, JFaceResources.getFontRegistry()
867                                                         .getFontData(PreferenceConstants.EDITOR_TEXT_FONT));
868         
869                         fFontPropertyChangeListener = new IPropertyChangeListener() {
870                                 public void propertyChange(PropertyChangeEvent event) {
871                                         if (PreferenceConstants.EDITOR_TEXT_FONT.equals(event
872                                                         .getProperty()))
873                                                 PreferenceConverter.putValue(getPreferenceStore(),
874                                                                 JFaceResources.TEXT_FONT,
875                                                                 JFaceResources.getFontRegistry().getFontData(
876                                                                                 PreferenceConstants.EDITOR_TEXT_FONT));
877                                 }
878                         };
879                         // incastrix
880                         /*JFaceResources.getFontRegistry().addListener(
881                                         fFontPropertyChangeListener);*/
882                 }
883                 
884
885 }