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
8 **********************************************************************/
9 package net.sourceforge.phpeclipse;
12 import java.io.IOException;
13 import java.io.InputStream;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Enumeration;
17 import java.util.HashMap;
18 import java.util.HashSet;
19 import java.util.Iterator;
20 import java.util.List;
21 import java.util.PropertyResourceBundle;
24 import net.sourceforge.phpdt.core.IBuffer;
25 import net.sourceforge.phpdt.core.IBufferFactory;
26 import net.sourceforge.phpdt.core.ICompilationUnit;
27 import net.sourceforge.phpdt.core.IJavaElement;
28 import net.sourceforge.phpdt.core.JavaCore;
29 import net.sourceforge.phpdt.core.WorkingCopyOwner;
30 import net.sourceforge.phpdt.internal.core.BatchOperation;
31 import net.sourceforge.phpdt.internal.core.JavaModelManager;
32 import net.sourceforge.phpdt.internal.core.util.Util;
33 import net.sourceforge.phpdt.internal.corext.template.php.CodeTemplateContextType;
34 import net.sourceforge.phpdt.internal.corext.template.php.HTMLContextType;
35 import net.sourceforge.phpdt.internal.corext.template.php.JavaContextType;
36 import net.sourceforge.phpdt.internal.corext.template.php.JavaDocContextType;
37 import net.sourceforge.phpdt.internal.ui.IJavaStatusConstants;
38 import net.sourceforge.phpdt.internal.ui.JavaElementAdapterFactory;
39 import net.sourceforge.phpdt.internal.ui.ResourceAdapterFactory;
40 import net.sourceforge.phpdt.internal.ui.preferences.MembersOrderPreferenceCache;
41 import net.sourceforge.phpdt.internal.ui.preferences.MockupPreferenceStore;
42 import net.sourceforge.phpdt.internal.ui.text.PreferencesAdapter;
43 import net.sourceforge.phpdt.internal.ui.text.folding.JavaFoldingStructureProviderRegistry;
44 import net.sourceforge.phpdt.internal.ui.text.java.hover.JavaEditorTextHoverDescriptor;
45 import net.sourceforge.phpdt.internal.ui.viewsupport.ImageDescriptorRegistry;
46 import net.sourceforge.phpdt.internal.ui.viewsupport.ProblemMarkerManager;
47 import net.sourceforge.phpdt.ui.IContextMenuConstants;
48 import net.sourceforge.phpdt.ui.IWorkingCopyManager;
49 import net.sourceforge.phpdt.ui.PreferenceConstants;
50 import net.sourceforge.phpdt.ui.text.JavaTextTools;
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.CustomBufferFactory;
56 import net.sourceforge.phpeclipse.phpeditor.DocumentAdapter;
57 import net.sourceforge.phpeclipse.phpeditor.ICompilationUnitDocumentProvider;
58 import net.sourceforge.phpeclipse.phpeditor.PHPDocumentProvider;
59 import net.sourceforge.phpeclipse.phpeditor.PHPSyntaxRdr;
60 import net.sourceforge.phpeclipse.phpeditor.WorkingCopyManager;
61 import net.sourceforge.phpeclipse.phpeditor.util.PHPColorProvider;
63 import org.eclipse.core.resources.IFile;
64 import org.eclipse.core.resources.IProject;
65 import org.eclipse.core.resources.IResource;
66 import org.eclipse.core.resources.IResourceChangeEvent;
67 import org.eclipse.core.resources.ISavedState;
68 import org.eclipse.core.resources.IWorkspace;
69 import org.eclipse.core.resources.IWorkspaceRunnable;
70 import org.eclipse.core.resources.ResourcesPlugin;
71 import org.eclipse.core.resources.WorkspaceJob;
72 import org.eclipse.core.runtime.CoreException;
73 import org.eclipse.core.runtime.IAdapterManager;
74 import org.eclipse.core.runtime.IConfigurationElement;
75 import org.eclipse.core.runtime.IPath;
76 import org.eclipse.core.runtime.IProgressMonitor;
77 import org.eclipse.core.runtime.IStatus;
78 import org.eclipse.core.runtime.Path;
79 import org.eclipse.core.runtime.Platform;
80 import org.eclipse.core.runtime.Status;
81 import org.eclipse.core.runtime.jobs.ISchedulingRule;
82 import org.eclipse.core.runtime.jobs.Job;
83 import org.eclipse.jface.action.GroupMarker;
84 import org.eclipse.jface.action.IMenuManager;
85 import org.eclipse.jface.action.Separator;
86 import org.eclipse.jface.preference.IPreferenceStore;
87 import org.eclipse.jface.preference.PreferenceConverter;
88 import org.eclipse.jface.resource.JFaceResources;
89 import org.eclipse.jface.text.BadLocationException;
90 import org.eclipse.jface.text.IDocument;
91 import org.eclipse.jface.text.templates.ContextTypeRegistry;
92 import org.eclipse.jface.text.templates.persistence.TemplateStore;
93 import org.eclipse.jface.util.IPropertyChangeListener;
94 import org.eclipse.jface.util.PropertyChangeEvent;
95 import org.eclipse.swt.graphics.RGB;
96 import org.eclipse.swt.widgets.Display;
97 import org.eclipse.swt.widgets.Shell;
98 import org.eclipse.ui.IEditorDescriptor;
99 import org.eclipse.ui.IEditorInput;
100 import org.eclipse.ui.IEditorPart;
101 import org.eclipse.ui.IEditorRegistry;
102 import org.eclipse.ui.IWorkbench;
103 import org.eclipse.ui.IWorkbenchPage;
104 import org.eclipse.ui.IWorkbenchWindow;
105 import org.eclipse.ui.PlatformUI;
106 import org.eclipse.ui.editors.text.EditorsUI;
107 import org.eclipse.ui.editors.text.templates.ContributionContextTypeRegistry;
108 import org.eclipse.ui.editors.text.templates.ContributionTemplateStore;
109 import org.eclipse.ui.ide.IDE;
110 import org.eclipse.ui.plugin.AbstractUIPlugin;
111 import org.eclipse.ui.texteditor.ChainedPreferenceStore;
112 import org.eclipse.ui.texteditor.ConfigurationElementSorter;
113 import org.eclipse.ui.texteditor.IDocumentProvider;
114 import org.eclipse.ui.texteditor.ITextEditor;
115 import org.eclipse.ui.texteditor.MarkerAnnotationPreferences;
116 import org.osgi.framework.BundleContext;
119 * The main plugin class to be used in the desktop.
121 public class PHPeclipsePlugin extends AbstractUIPlugin implements
122 IPreferenceConstants {
124 * The id of the PHP plugin (value <code>"net.sourceforge.phpeclipse"</code>).
126 public static final String PLUGIN_ID = "net.sourceforge.phpeclipse"; //$NON-NLS-1$
128 public static final String EDITOR_ID = PHPeclipsePlugin.PLUGIN_ID
131 public static final String ID_PERSPECTIVE = "net.sourceforge.phpeclipse.PHPPerspective"; //$NON-NLS-1$
136 * id of builder - matches plugin.xml (concatenate pluginid.builderid)
138 public static final String BUILDER_PARSER_ID = PLUGIN_ID + ".parserbuilder";
140 // public static final String BUILDER_INDEX_ID = PLUGIN_ID +
142 /** General debug flag */
144 public static final boolean DEBUG = false;
147 * The maximum number of allowed proposals by category
149 public final static int MAX_PROPOSALS = 200;
152 * The key to store customized templates.
156 private static final String TEMPLATES_KEY = "net.sourceforge.phpdt.ui.text.custom_templates"; //$NON-NLS-1$
159 * The key to store customized code templates.
163 private static final String CODE_TEMPLATES_KEY = "net.sourceforge.phpdt.ui.text.custom_code_templates"; //$NON-NLS-1$
166 * The key to store whether the legacy templates have been migrated
170 // private static final String TEMPLATES_MIGRATION_KEY=
171 // "net.sourceforge.phpdt.ui.text.templates_migrated"; //$NON-NLS-1$
173 * The key to store whether the legacy code templates have been migrated
177 // private static final String CODE_TEMPLATES_MIGRATION_KEY=
178 // "net.sourceforge.phpdt.ui.text.code_templates_migrated";
180 // private static ExternalToolsPlugin externalTools;
182 * The Java virtual machine that we are running on.
184 // private static int jvm;
186 // private static final int MRJ_2_0 = 0;
188 // /** MRJ 2.1 or later */
189 // private static final int MRJ_2_1 = 1;
191 // /** Java on Mac OS X 10.0 (MRJ 3.0) */
192 // private static final int MRJ_3_0 = 3;
195 // private static final int MRJ_3_1 = 4;
197 // /** JVM constant for any other platform */
198 // private static final int OTHER = -1;
199 // public static final String PHP_RESOURCES_VIEW_ID = PLUGIN_ID +
200 // ".resourcesview.ViewPHPResources"; //$NON-NLS-1$
201 public static final String PHP_CODING_ACTION_SET_ID = PLUGIN_ID
202 + ".ui.CodingActionSet"; //$NON-NLS-1$
204 public final static String PHP_NATURE_ID = PLUGIN_ID + ".phpnature";
206 public static final String PHPPARSER_ORIGINAL = "net.sourceforge.phpdt.internal.compiler.parser.Parser";
208 public static final String PHPPARSER_NEW = "test.PHPParser";
210 /** Change this if you want to switch PHP Parser. */
211 public static final String PHPPARSER = PHPPARSER_ORIGINAL;
213 // The shared instance.
214 private static PHPeclipsePlugin plugin;
217 * The template context type registry for the java editor.
221 private ContextTypeRegistry fContextTypeRegistry;
224 * The code template context type registry for the java editor.
228 private ContextTypeRegistry fCodeTemplateContextTypeRegistry;
231 * The template store for the java editor.
235 private TemplateStore fTemplateStore;
238 * The coded template store for the java editor.
242 private TemplateStore fCodeTemplateStore;
245 private static final int WINDOWS_9x = 6;
248 private static final int WINDOWS_NT = 5;
250 private ImageDescriptorRegistry fImageDescriptorRegistry;
252 private HashMap fIndexManagerMap = new HashMap();
254 private IWorkingCopyManager fWorkingCopyManager;
256 private IBufferFactory fBufferFactory;
258 private ICompilationUnitDocumentProvider fCompilationUnitDocumentProvider;
260 private JavaTextTools fJavaTextTools;
262 private ProblemMarkerManager fProblemMarkerManager;
264 private MembersOrderPreferenceCache fMembersOrderPreferenceCache;
266 private IFile fLastEditorFile = null;
268 private JavaEditorTextHoverDescriptor[] fJavaEditorTextHoverDescriptors;
270 private JavaElementAdapterFactory fJavaElementAdapterFactory;
272 // private MarkerAdapterFactory fMarkerAdapterFactory;
273 // private EditorInputAdapterFactory fEditorInputAdapterFactory;
274 private ResourceAdapterFactory fResourceAdapterFactory;
276 // private LogicalPackageAdapterFactory fLogicalPackageAdapterFactory;
277 private IPropertyChangeListener fFontPropertyChangeListener;
280 * Property change listener on this plugin's preference store.
284 // private IPropertyChangeListener fPropertyChangeListener;
286 * The combined preference store.
290 private IPreferenceStore fCombinedPreferenceStore;
293 * The extension point registry for the
294 * <code>net.sourceforge.phpdt.ui.javaFoldingStructureProvider</code>
299 private JavaFoldingStructureProviderRegistry fFoldingStructureProviderRegistry;
302 * Mockup preference store for firing events and registering listeners on
303 * project setting changes. FIXME: Temporary solution.
307 private MockupPreferenceStore fMockupPreferenceStore;
312 public PHPeclipsePlugin() {
315 // externalTools = new ExternalToolsPlugin();
319 // ResourceBundle.getBundle("net.sourceforge.PHPeclipsePluginResources");
320 // } catch (MissingResourceException x) {
321 // resourceBundle = null;
326 // * Returns all Java editor text hovers contributed to the workbench.
328 // * @return an array of JavaEditorTextHoverDescriptor
331 // public JavaEditorTextHoverDescriptor[]
332 // getJavaEditorTextHoverDescriptors()
334 // if (fJavaEditorTextHoverDescriptors == null)
335 // fJavaEditorTextHoverDescriptors = JavaEditorTextHoverDescriptor
336 // .getContributedHovers();
337 // return fJavaEditorTextHoverDescriptors;
340 * Returns all Java editor text hovers contributed to the workbench.
342 * @return an array of JavaEditorTextHoverDescriptor
345 public JavaEditorTextHoverDescriptor[] getJavaEditorTextHoverDescriptors() {
346 if (fJavaEditorTextHoverDescriptors == null) {
347 fJavaEditorTextHoverDescriptors = JavaEditorTextHoverDescriptor
348 .getContributedHovers();
349 ConfigurationElementSorter sorter = new ConfigurationElementSorter() {
351 * @see org.eclipse.ui.texteditor.ConfigurationElementSorter#getConfigurationElement(java.lang.Object)
353 public IConfigurationElement getConfigurationElement(
355 return ((JavaEditorTextHoverDescriptor) object)
356 .getConfigurationElement();
359 sorter.sort(fJavaEditorTextHoverDescriptors);
361 // The Problem hover has to be the first and the Annotation hover
363 // be the last one in the JDT UI's hover list
364 int length = fJavaEditorTextHoverDescriptors.length;
366 int last = length - 1;
367 int problemHoverIndex = -1;
368 int annotationHoverIndex = -1;
369 for (int i = 0; i < length; i++) {
370 if (!fJavaEditorTextHoverDescriptors[i].getId().startsWith(
372 if (problemHoverIndex == -1 || annotationHoverIndex == -1)
382 if (fJavaEditorTextHoverDescriptors[i].getId().equals(
383 "net.sourceforge.phpdt.ui.AnnotationHover")) { //$NON-NLS-1$
384 annotationHoverIndex = i;
387 if (fJavaEditorTextHoverDescriptors[i].getId().equals(
388 "net.sourceforge.phpdt.ui.ProblemHover")) { //$NON-NLS-1$
389 problemHoverIndex = i;
394 JavaEditorTextHoverDescriptor hoverDescriptor = null;
396 if (first > -1 && problemHoverIndex > -1
397 && problemHoverIndex != first) {
398 // move problem hover to beginning
399 hoverDescriptor = fJavaEditorTextHoverDescriptors[first];
400 fJavaEditorTextHoverDescriptors[first] = fJavaEditorTextHoverDescriptors[problemHoverIndex];
401 fJavaEditorTextHoverDescriptors[problemHoverIndex] = hoverDescriptor;
403 // update annotation hover index if needed
404 if (annotationHoverIndex == first)
405 annotationHoverIndex = problemHoverIndex;
408 if (annotationHoverIndex > -1 && annotationHoverIndex != last) {
409 // move annotation hover to end
410 hoverDescriptor = fJavaEditorTextHoverDescriptors[last];
411 fJavaEditorTextHoverDescriptors[last] = fJavaEditorTextHoverDescriptors[annotationHoverIndex];
412 fJavaEditorTextHoverDescriptors[annotationHoverIndex] = hoverDescriptor;
415 // Move Best Match hover to front
416 for (int i = 0; i < fJavaEditorTextHoverDescriptors.length - 1; i++) {
417 if (PreferenceConstants.ID_BESTMATCH_HOVER
418 .equals(fJavaEditorTextHoverDescriptors[i].getId())) {
419 hoverDescriptor = fJavaEditorTextHoverDescriptors[i];
420 for (int j = i; j > 0; j--)
421 fJavaEditorTextHoverDescriptors[j] = fJavaEditorTextHoverDescriptors[j - 1];
422 fJavaEditorTextHoverDescriptors[0] = hoverDescriptor;
429 return fJavaEditorTextHoverDescriptors;
433 * Resets the Java editor text hovers contributed to the workbench.
435 * This will force a rebuild of the descriptors the next time a client asks
439 * @return an array of JavaEditorTextHoverDescriptor
442 public void resetJavaEditorTextHoverDescriptors() {
443 fJavaEditorTextHoverDescriptors = null;
447 * Creates the PHP plugin standard groups in a context menu.
449 public static void createStandardGroups(IMenuManager menu) {
452 menu.add(new Separator(IContextMenuConstants.GROUP_NEW));
453 menu.add(new GroupMarker(IContextMenuConstants.GROUP_GOTO));
454 menu.add(new Separator(IContextMenuConstants.GROUP_OPEN));
455 menu.add(new GroupMarker(IContextMenuConstants.GROUP_SHOW));
456 menu.add(new Separator(IContextMenuConstants.GROUP_REORGANIZE));
457 menu.add(new Separator(IContextMenuConstants.GROUP_GENERATE));
458 menu.add(new Separator(IContextMenuConstants.GROUP_SEARCH));
459 menu.add(new Separator(IContextMenuConstants.GROUP_BUILD));
460 menu.add(new Separator(IContextMenuConstants.GROUP_ADDITIONS));
461 menu.add(new Separator(IContextMenuConstants.GROUP_VIEWER_SETUP));
462 menu.add(new Separator(IContextMenuConstants.GROUP_PROPERTIES));
465 public static IWorkbenchPage getActivePage() {
466 return getDefault().internalGetActivePage();
469 public static Shell getActiveWorkbenchShell() {
470 return getActiveWorkbenchWindow().getShell();
474 * Returns an array of all editors that have an unsaved content. If the
475 * identical content is presented in more than one editor, only one of those
476 * editor parts is part of the result.
478 * @return an array of all dirty editor parts.
480 public static IEditorPart[] getDirtyEditors() {
481 Set inputs = new HashSet();
482 List result = new ArrayList(0);
483 IWorkbench workbench = getDefault().getWorkbench();
484 IWorkbenchWindow[] windows = workbench.getWorkbenchWindows();
485 for (int i = 0; i < windows.length; i++) {
486 IWorkbenchPage[] pages = windows[i].getPages();
487 for (int x = 0; x < pages.length; x++) {
488 IEditorPart[] editors = pages[x].getDirtyEditors();
489 for (int z = 0; z < editors.length; z++) {
490 IEditorPart ep = editors[z];
491 IEditorInput input = ep.getEditorInput();
492 if (!inputs.contains(input)) {
499 return (IEditorPart[]) result.toArray(new IEditorPart[result.size()]);
502 public static IWorkbenchWindow getActiveWorkbenchWindow() {
503 return getDefault().getWorkbench().getActiveWorkbenchWindow();
507 * Returns the shared instance.
509 public static PHPeclipsePlugin getDefault() {
513 public static ImageDescriptorRegistry getImageDescriptorRegistry() {
514 return getDefault().internalGetImageDescriptorRegistry();
517 static IPath getInstallLocation() {
518 return new Path(getDefault().getBundle().getEntry("/").getFile());
521 // public static int getJVM() {
525 public static String getPluginId() {
526 return getDefault().getBundle().getSymbolicName();
530 * Returns the standard display to be used. The method first checks, if the
531 * thread calling this method has an associated display. If so, this display
532 * is returned. Otherwise the method returns the default display.
534 public static Display getStandardDisplay() {
535 Display display = Display.getCurrent();
536 if (display == null) {
537 display = Display.getDefault();
542 // public static ExternalToolsPlugin getExternalTools() {
543 // return externalTools;
546 * Returns the workspace instance.
548 public static IWorkspace getWorkspace() {
549 return ResourcesPlugin.getWorkspace();
552 public static boolean isDebug() {
553 return getDefault().isDebugging();
556 // public static void logErrorMessage(String message) {
557 // log(new Status(IStatus.ERROR, getPluginId(),
558 // JavaStatusConstants.INTERNAL_ERROR, message, null));
561 // public static void logErrorStatus(String message, IStatus status) {
562 // if (status == null) {
563 // logErrorMessage(message);
566 // MultiStatus multi= new MultiStatus(getPluginId(),
567 // JavaStatusConstants.INTERNAL_ERROR, message, null);
568 // multi.add(status);
572 // public static void log(Throwable e) {
573 // log(new Status(IStatus.ERROR, getPluginId(),
574 // JavaStatusConstants.INTERNAL_ERROR,
575 // JavaUIMessages.getString("JavaPlugin.internal_error"), e)); //$NON-NLS-1$
577 public static void log(int severity, String message) {
578 Status status = new Status(severity, PLUGIN_ID, IStatus.OK, message,
583 public static void log(IStatus status) {
584 getDefault().getLog().log(status);
587 public static void log(Throwable t) {
591 public static void log(String message, Throwable t) {
592 log(error(message, t));
595 public static void logErrorMessage(String message) {
596 log(new Status(IStatus.ERROR, getPluginId(),
597 IJavaStatusConstants.INTERNAL_ERROR, message, null));
600 public static IStatus error(Throwable t) {
601 return error("PHPeclipsePlugin.internalErrorOccurred", t); //$NON-NLS-1$
604 public static IStatus error(String message, Throwable t) {
605 return new Status(IStatus.ERROR, PLUGIN_ID, IStatus.ERROR, message, t);
608 // private static void setJVM() {
609 // String osName = System.getProperty("os.name");
610 // if (osName.startsWith("Mac OS")) {
611 // String mrjVersion = System.getProperty("mrj.version");
612 // String majorMRJVersion = mrjVersion.substring(0, 3);
615 // double version = Double.valueOf(majorMRJVersion).doubleValue();
616 // if (version == 2) {
618 // } else if (version >= 2.1 && version < 3) {
620 // } else if (version == 3.0) {
622 // } else if (version >= 3.1) {
625 // } catch (NumberFormatException nfe) {
627 // } else if (osName.startsWith("Windows")) {
628 // if (osName.indexOf("9") != -1) {
636 // TODO: refactor this into a better method name !
637 public synchronized ICompilationUnitDocumentProvider getCompilationUnitDocumentProvider() {
638 if (fCompilationUnitDocumentProvider == null)
639 fCompilationUnitDocumentProvider = new PHPDocumentProvider();
640 return fCompilationUnitDocumentProvider;
644 * Get the identifier index manager for the given project
647 * the current project
650 public IdentifierIndexManager getIndexManager(IProject iProject) {
651 IPath path = iProject.getWorkingLocation(PHPeclipsePlugin.PLUGIN_ID);
652 path = path.append("project.index");
653 String indexFilename = path.toString();
655 // IdentDB db = IdentDB.getInstance();
656 // } catch (ClassNotFoundException e) {
657 // e.printStackTrace();
658 // } catch (SQLException e) {
659 // e.printStackTrace();
661 IdentifierIndexManager indexManager = (IdentifierIndexManager) fIndexManagerMap
663 if (indexManager == null) {
664 indexManager = new IdentifierIndexManager(indexFilename);
665 fIndexManagerMap.put(indexFilename, indexManager);
670 public synchronized IWorkingCopyManager getWorkingCopyManager() {
671 if (fWorkingCopyManager == null) {
672 ICompilationUnitDocumentProvider provider = getCompilationUnitDocumentProvider();
673 fWorkingCopyManager = new WorkingCopyManager(provider);
675 return fWorkingCopyManager;
678 public synchronized MembersOrderPreferenceCache getMemberOrderPreferenceCache() {
679 if (fMembersOrderPreferenceCache == null)
680 fMembersOrderPreferenceCache = new MembersOrderPreferenceCache();
681 return fMembersOrderPreferenceCache;
685 * Returns the mockup preference store for firing events and registering
686 * listeners on project setting changes. Temporary solution.
688 public MockupPreferenceStore getMockupPreferenceStore() {
689 if (fMockupPreferenceStore == null)
690 fMockupPreferenceStore = new MockupPreferenceStore();
692 return fMockupPreferenceStore;
695 public synchronized ProblemMarkerManager getProblemMarkerManager() {
696 if (fProblemMarkerManager == null)
697 fProblemMarkerManager = new ProblemMarkerManager();
698 return fProblemMarkerManager;
701 // public synchronized JavaTextTools getJavaTextTools() {
702 // if (fJavaTextTools == null)
703 // fJavaTextTools = new JavaTextTools(getPreferenceStore());
704 // return fJavaTextTools;
706 public synchronized JavaTextTools getJavaTextTools() {
707 if (fJavaTextTools == null)
708 fJavaTextTools = new JavaTextTools(getPreferenceStore(), JavaCore
709 .getPlugin().getPluginPreferences());
710 return fJavaTextTools;
713 public IFile getLastEditorFile() {
714 return fLastEditorFile;
718 * Returns the string from the plugin's resource bundle, or 'key' if not
721 // public static String getResourceString(String key) {
722 // ResourceBundle bundle =
723 // PHPeclipsePlugin.getDefault().getResourceBundle();
725 // return bundle.getString(key);
726 // } catch (MissingResourceException e) {
731 * Returns the plugin's resource bundle,
733 // public ResourceBundle getResourceBundle() {
734 // return resourceBundle;
736 protected void initializeDefaultPreferences(IPreferenceStore store) {
737 String operatingSystem = Platform.getOS();
738 // maxosx, linux, solaris, win32,...
740 InputStream is = getDefault()
742 new Path("prefs/default_" + operatingSystem
744 PropertyResourceBundle resourceBundle = new PropertyResourceBundle(
746 Enumeration e = resourceBundle.getKeys();
748 while (e.hasMoreElements()) {
749 key = (String) e.nextElement();
750 store.setDefault(key, resourceBundle.getString(key));
752 } catch (Exception e) {
753 // no default properties found
754 if (operatingSystem.equals(Platform.OS_WIN32)) {
755 // store.setDefault(PHP_RUN_PREF, "c:\\apache\\php\\php.exe");
756 // store.setDefault(EXTERNAL_PARSER_PREF, "c:\\apache\\php\\php
758 // store.setDefault(MYSQL_RUN_PREF,
759 // "c:\\apache\\mysql\\bin\\mysqld-nt.exe");
760 // store.setDefault(APACHE_RUN_PREF, "c:\\apache\\apache.exe");
761 // store.setDefault(XAMPP_START_PREF,
762 // "c:\\xampp\\xampp_start.exe");
763 // store.setDefault(XAMPP_STOP_PREF,
764 // "c:\\xampp\\xampp_stop.exe");
766 // ETC_HOSTS_PATH_PREF,
767 // "c:\\windows\\system32\\drivers\\etc\\hosts");
769 // store.setDefault(PHP_RUN_PREF, "/apache/php/php");
770 // store.setDefault(EXTERNAL_PARSER_PREF, "/apache/php/php -l -f
772 // store.setDefault(MYSQL_RUN_PREF, "/apache/mysql/bin/mysqld");
773 // store.setDefault(APACHE_RUN_PREF, "/apache/apache");
774 // store.setDefault(XAMPP_START_PREF, "xamp/xampp_start");
775 // store.setDefault(XAMPP_STOP_PREF, "xampp/xampp_stop");
777 // store.setDefault(MYSQL_PREF, "--standalone");
778 // store.setDefault(APACHE_START_PREF, "-c \"DocumentRoot
780 // store.setDefault(APACHE_STOP_PREF, "-k shutdown");
781 // store.setDefault(APACHE_RESTART_PREF, "-k restart");
782 // store.setDefault(MYSQL_START_BACKGROUND, "true");
783 // store.setDefault(APACHE_START_BACKGROUND, "true");
784 // store.setDefault(APACHE_STOP_BACKGROUND, "true");
785 // store.setDefault(APACHE_RESTART_BACKGROUND, "true");
788 // php syntax highlighting
789 store.setDefault(PHP_USERDEF_XMLFILE, "");
790 PreferenceConverter.setDefault(store, PHP_TAG, PHPColorProvider.TAG);
791 PreferenceConverter.setDefault(store, PHP_KEYWORD,
792 PHPColorProvider.KEYWORD);
793 PreferenceConverter.setDefault(store, PHP_VARIABLE,
794 PHPColorProvider.VARIABLE);
795 PreferenceConverter.setDefault(store, PHP_VARIABLE_DOLLAR,
796 PHPColorProvider.VARIABLE);
797 PreferenceConverter.setDefault(store, PHP_FUNCTIONNAME,
798 PHPColorProvider.FUNCTION_NAME);
799 PreferenceConverter.setDefault(store, PHP_CONSTANT,
800 PHPColorProvider.CONSTANT);
801 PreferenceConverter.setDefault(store, PHP_TYPE, PHPColorProvider.TYPE);
802 PreferenceConverter.setDefault(store, PHP_DEFAULT,
803 PHPColorProvider.DEFAULT);
804 PreferenceConverter.setDefault(store, PHPDOC_KEYWORD,
805 PHPColorProvider.PHPDOC_KEYWORD);
806 PreferenceConverter.setDefault(store, PHPDOC_TAG,
807 PHPColorProvider.PHPDOC_TAG);
808 PreferenceConverter.setDefault(store, PHPDOC_LINK,
809 PHPColorProvider.PHPDOC_LINK);
810 PreferenceConverter.setDefault(store, PHPDOC_DEFAULT,
811 PHPColorProvider.PHPDOC_DEFAULT);
813 PreferenceConverter.setDefault(store, EDITOR_PHP_KEYWORD_RETURN_COLOR,
814 new RGB(127, 0, 85));
815 store.setDefault(EDITOR_PHP_KEYWORD_RETURN_BOLD, true);
816 store.setDefault(EDITOR_PHP_KEYWORD_RETURN_ITALIC, false);
818 PreferenceConverter.setDefault(store, EDITOR_PHP_OPERATOR_COLOR,
820 store.setDefault(EDITOR_PHP_OPERATOR_BOLD, false);
821 store.setDefault(EDITOR_PHP_OPERATOR_ITALIC, false);
823 PreferenceConverter.setDefault(store, EDITOR_PHP_BRACE_OPERATOR_COLOR,
825 store.setDefault(EDITOR_PHP_BRACE_OPERATOR_BOLD, false);
826 store.setDefault(EDITOR_PHP_BRACE_OPERATOR_ITALIC, false);
828 // this will initialize the static fields in the syntaxrdr class
830 JavaCore.initializeDefaultPluginPreferences();
831 PreferenceConstants.initializeDefaultValues(store);
832 // externalTools.initializeDefaultPreferences(store);
833 // MarkerAnnotationPreferences.initializeDefaultValues(store);
836 private IWorkbenchPage internalGetActivePage() {
837 IWorkbenchWindow window = getWorkbench().getActiveWorkbenchWindow();
839 return window.getActivePage();
843 private ImageDescriptorRegistry internalGetImageDescriptorRegistry() {
844 if (fImageDescriptorRegistry == null)
845 fImageDescriptorRegistry = new ImageDescriptorRegistry();
846 return fImageDescriptorRegistry;
850 * Open a file in the Workbench that may or may not exist in the workspace.
851 * Must be run on the UI thread.
854 * @throws CoreException
856 public ITextEditor openFileInTextEditor(String filename)
857 throws CoreException {
858 // reject directories
859 if (new File(filename).isDirectory())
861 IWorkbench workbench = PlatformUI.getWorkbench();
862 IWorkbenchWindow window = workbench.getWorkbenchWindows()[0];
863 IWorkbenchPage page = window.getActivePage();
864 IPath path = new Path(filename);
865 // If the file exists in the workspace, open it
866 IFile file = getWorkspace().getRoot().getFileForLocation(path);
868 ITextEditor textEditor;
869 if (file != null && file.exists()) {
870 editor = IDE.openEditor(page, file, true);
871 textEditor = (ITextEditor) editor.getAdapter(ITextEditor.class);
873 // Otherwise open the stream directly
876 FileStorage storage = new FileStorage(path);
877 IEditorRegistry registry = getWorkbench().getEditorRegistry();
878 IEditorDescriptor desc = registry.getDefaultEditor(filename);
881 .findEditor(IEditorRegistry.SYSTEM_EXTERNAL_EDITOR_ID);
882 // desc = registry.getDefaultEditor();
884 IEditorInput input = new ExternalEditorInput(storage);
885 editor = page.openEditor(input, desc.getId());
886 textEditor = (ITextEditor) editor.getAdapter(ITextEditor.class);
887 // If the storage provider is not ours, we can't guarantee
889 if (textEditor != null) {
890 IDocumentProvider documentProvider = textEditor
891 .getDocumentProvider();
892 if (!(documentProvider instanceof ExternalStorageDocumentProvider)) {
893 storage.setReadOnly();
901 * Open a file in the Workbench that may or may not exist in the workspace.
902 * Must be run on the UI thread.
906 * @throws CoreException
908 public void openFileAndGotoLine(String filename, int line)
909 throws CoreException {
910 ITextEditor textEditor = openFileInTextEditor(filename);
911 if (textEditor != null) {
912 // If a line number was given, go to it
915 line--; // document is 0 based
916 IDocument document = textEditor.getDocumentProvider()
917 .getDocument(textEditor.getEditorInput());
918 textEditor.selectAndReveal(document.getLineOffset(line),
919 document.getLineLength(line));
920 } catch (BadLocationException e) {
921 // invalid text position -> do nothing
928 * Open a file in the Workbench that may or may not exist in the workspace.
929 * Must be run on the UI thread.
933 * @throws CoreException
935 public void openFileAndGotoOffset(String filename, int offset, int length)
936 throws CoreException {
937 ITextEditor textEditor = openFileInTextEditor(filename);
938 if (textEditor != null) {
939 // If a line number was given, go to it
941 IDocument document = textEditor.getDocumentProvider()
942 .getDocument(textEditor.getEditorInput());
943 textEditor.selectAndReveal(offset, length);
948 public void openFileAndFindString(String filename, String findString)
949 throws CoreException {
950 ITextEditor textEditor = openFileInTextEditor(filename);
951 if (textEditor != null) {
952 // If a string was given, go to it
953 if (findString != null) {
955 IDocument document = textEditor.getDocumentProvider()
956 .getDocument(textEditor.getEditorInput());
957 int offset = document.search(0, findString, true, false,
959 textEditor.selectAndReveal(offset, findString.length());
960 } catch (BadLocationException e) {
961 // invalid text position -> do nothing
967 public void setLastEditorFile(IFile textEditor) {
968 this.fLastEditorFile = textEditor;
972 * @see org.eclipse.core.runtime.Plugin#stop
974 public void stop(BundleContext context) throws Exception {
976 // JavaCore.stop(this, context);
977 plugin.savePluginPreferences();
978 IWorkspace workspace = ResourcesPlugin.getWorkspace();
979 workspace.removeResourceChangeListener(JavaModelManager
980 .getJavaModelManager().deltaState);
981 workspace.removeSaveParticipant(plugin);
983 JavaModelManager.getJavaModelManager().shutdown();
985 // save the information from the php index files if necessary
986 Collection collection = fIndexManagerMap.values();
987 Iterator iterator = collection.iterator();
988 IdentifierIndexManager indexManager = null;
989 while (iterator.hasNext()) {
990 indexManager = (IdentifierIndexManager) iterator.next();
991 indexManager.writeFile();
993 if (fImageDescriptorRegistry != null)
994 fImageDescriptorRegistry.dispose();
996 // AllTypesCache.terminate();
998 if (fImageDescriptorRegistry != null)
999 fImageDescriptorRegistry.dispose();
1001 unregisterAdapters();
1003 // if (fASTProvider != null) {
1004 // fASTProvider.dispose();
1005 // fASTProvider= null;
1008 if (fWorkingCopyManager != null) {
1009 fWorkingCopyManager.shutdown();
1010 fWorkingCopyManager = null;
1013 if (fCompilationUnitDocumentProvider != null) {
1014 fCompilationUnitDocumentProvider.shutdown();
1015 fCompilationUnitDocumentProvider = null;
1018 if (fJavaTextTools != null) {
1019 fJavaTextTools.dispose();
1020 fJavaTextTools = null;
1022 // JavaDocLocations.shutdownJavadocLocations();
1024 uninstallPreferenceStoreBackwardsCompatibility();
1026 // RefactoringCore.getUndoManager().shutdown();
1028 super.stop(context);
1033 * Installs backwards compatibility for the preference store.
1035 private void installPreferenceStoreBackwardsCompatibility() {
1038 * Installs backwards compatibility: propagate the Java editor font from
1039 * a pre-2.1 plug-in to the Platform UI's preference store to preserve
1040 * the Java editor font from a pre-2.1 workspace. This is done only
1043 String fontPropagatedKey = "fontPropagated"; //$NON-NLS-1$
1044 if (getPreferenceStore().contains(JFaceResources.TEXT_FONT)
1045 && !getPreferenceStore().isDefault(JFaceResources.TEXT_FONT)) {
1046 if (!getPreferenceStore().getBoolean(fontPropagatedKey))
1048 .setValue(PlatformUI.getWorkbench()
1049 .getPreferenceStore(),
1050 PreferenceConstants.EDITOR_TEXT_FONT,
1051 PreferenceConverter.getFontDataArray(
1052 getPreferenceStore(),
1053 JFaceResources.TEXT_FONT));
1055 getPreferenceStore().setValue(fontPropagatedKey, true);
1058 * Backwards compatibility: set the Java editor font in this plug-in's
1059 * preference store to let older versions access it. Since 2.1 the Java
1060 * editor font is managed by the workbench font preference page.
1062 PreferenceConverter.putValue(getPreferenceStore(),
1063 JFaceResources.TEXT_FONT, JFaceResources.getFontRegistry()
1064 .getFontData(PreferenceConstants.EDITOR_TEXT_FONT));
1066 fFontPropertyChangeListener = new IPropertyChangeListener() {
1067 public void propertyChange(PropertyChangeEvent event) {
1068 if (PreferenceConstants.EDITOR_TEXT_FONT.equals(event
1070 PreferenceConverter.putValue(getPreferenceStore(),
1071 JFaceResources.TEXT_FONT,
1072 JFaceResources.getFontRegistry().getFontData(
1073 PreferenceConstants.EDITOR_TEXT_FONT));
1076 JFaceResources.getFontRegistry().addListener(
1077 fFontPropertyChangeListener);
1081 * Uninstalls backwards compatibility for the preference store.
1083 private void uninstallPreferenceStoreBackwardsCompatibility() {
1084 JFaceResources.getFontRegistry().removeListener(
1085 fFontPropertyChangeListener);
1086 // getPreferenceStore().removePropertyChangeListener(fPropertyChangeListener);
1090 * (non - Javadoc) Method declared in Plugin
1092 public void start(BundleContext context) throws Exception {
1093 super.start(context);
1095 // JavaCore.start(this, context);
1096 final JavaModelManager modelManager = JavaModelManager
1097 .getJavaModelManager();
1099 modelManager.configurePluginDebugOptions();
1101 // request state folder creation (workaround 19885)
1103 // retrieve variable values
1104 PHPeclipsePlugin.getDefault().getPluginPreferences()
1105 .addPropertyChangeListener(
1106 new JavaModelManager.PluginPreferencesListener());
1107 // manager.loadVariablesAndContainers();
1109 final IWorkspace workspace = ResourcesPlugin.getWorkspace();
1110 workspace.addResourceChangeListener(modelManager.deltaState,
1111 IResourceChangeEvent.PRE_BUILD
1112 | IResourceChangeEvent.POST_BUILD
1113 | IResourceChangeEvent.POST_CHANGE
1114 | IResourceChangeEvent.PRE_DELETE
1115 | IResourceChangeEvent.PRE_CLOSE);
1117 ISavedState savedState = workspace.addSaveParticipant(
1118 PHPeclipsePlugin.this, modelManager);
1120 // process deltas since last activated in indexer thread so that
1121 // indexes are up-to-date.
1122 // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=38658
1123 // This causes timeout at EclipseLazyStarter
1124 // Job processSavedState = new Job(Util.bind("savedState.jobName")) { //$NON-NLS-1$
1125 // protected IStatus run(IProgressMonitor monitor) {
1127 // // add save participant and process delta atomically
1129 // // https://bugs.eclipse.org/bugs/show_bug.cgi?id=59937
1130 // workspace.run(new IWorkspaceRunnable() {
1131 // public void run(IProgressMonitor progress)
1132 // throws CoreException {
1133 // ISavedState savedState = workspace
1134 // .addSaveParticipant(
1135 // PHPeclipsePlugin.this,
1137 // if (savedState != null) {
1138 // // the event type coming from the saved
1139 // // state is always POST_AUTO_BUILD
1140 // // force it to be POST_CHANGE so that the
1141 // // delta processor can handle it
1142 // modelManager.deltaState.getDeltaProcessor().overridenEventType = IResourceChangeEvent.POST_CHANGE;
1144 // .processResourceChangeEvents(modelManager.deltaState);
1148 // } catch (CoreException e) {
1149 // return e.getStatus();
1151 // return Status.OK_STATUS;
1154 // Replace Job + IWorkspace.run() to WorkspaceJob
1155 WorkspaceJob processSavedState = new WorkspaceJob(
1156 Util.bind("savedState.jobName")) { //$NON-NLS-1$
1157 public IStatus runInWorkspace(IProgressMonitor monitor)
1158 throws CoreException {
1159 ISavedState savedState = workspace.addSaveParticipant(
1160 PHPeclipsePlugin.this, modelManager);
1161 if (savedState != null) {
1162 modelManager.deltaState.getDeltaProcessor().overridenEventType
1163 = IResourceChangeEvent.POST_CHANGE;
1164 savedState.processResourceChangeEvents(modelManager.deltaState);
1166 return Status.OK_STATUS;
1169 processSavedState.setSystem(true);
1170 processSavedState.setPriority(Job.SHORT); // process asap
1171 processSavedState.schedule();
1172 } catch (RuntimeException e) {
1173 modelManager.shutdown();
1179 // if (USE_WORKING_COPY_OWNERS) {
1180 WorkingCopyOwner.setPrimaryBufferProvider(new WorkingCopyOwner() {
1181 public IBuffer createBuffer(ICompilationUnit workingCopy) {
1182 ICompilationUnit original = workingCopy.getPrimary();
1183 IResource resource = original.getResource();
1184 if (resource instanceof IFile)
1185 return new DocumentAdapter(workingCopy, (IFile) resource);
1186 return DocumentAdapter.NULL;
1191 installPreferenceStoreBackwardsCompatibility();
1195 private void registerAdapters() {
1196 fJavaElementAdapterFactory = new JavaElementAdapterFactory();
1197 fResourceAdapterFactory = new ResourceAdapterFactory();
1199 IAdapterManager manager = Platform.getAdapterManager();
1201 .registerAdapters(fJavaElementAdapterFactory,
1202 IJavaElement.class);
1203 manager.registerAdapters(fResourceAdapterFactory, IResource.class);
1206 private void unregisterAdapters() {
1207 IAdapterManager manager = Platform.getAdapterManager();
1208 manager.unregisterAdapters(fJavaElementAdapterFactory);
1209 manager.unregisterAdapters(fResourceAdapterFactory);
1213 * Returns a combined preference store, this store is read-only.
1215 * @return the combined preference store
1219 public IPreferenceStore getCombinedPreferenceStore() {
1220 if (fCombinedPreferenceStore == null) {
1221 IPreferenceStore generalTextStore = EditorsUI.getPreferenceStore();
1222 fCombinedPreferenceStore = new ChainedPreferenceStore(
1223 new IPreferenceStore[] {
1224 getPreferenceStore(),
1225 new PreferencesAdapter(PHPeclipsePlugin
1226 .getDefault().getPluginPreferences()),
1227 generalTextStore });
1229 return fCombinedPreferenceStore;
1232 public synchronized IBufferFactory getBufferFactory() {
1233 if (fBufferFactory == null)
1234 fBufferFactory = new CustomBufferFactory();
1235 return fBufferFactory;
1239 * Returns the registry of the extensions to the
1240 * <code>net.sourceforge.phpdt.ui.javaFoldingStructureProvider</code>
1243 * @return the registry of contributed
1244 * <code>IJavaFoldingStructureProvider</code>
1247 public synchronized JavaFoldingStructureProviderRegistry getFoldingStructureProviderRegistry() {
1248 if (fFoldingStructureProviderRegistry == null)
1249 fFoldingStructureProviderRegistry = new JavaFoldingStructureProviderRegistry();
1250 return fFoldingStructureProviderRegistry;
1254 * Runs the given action as an atomic Java model operation.
1256 * After running a method that modifies java elements, registered listeners
1257 * receive after-the-fact notification of what just transpired, in the form
1258 * of a element changed event. This method allows clients to call a number
1259 * of methods that modify java elements and only have element changed event
1260 * notifications reported at the end of the entire batch.
1263 * If this method is called outside the dynamic scope of another such call,
1264 * this method runs the action and then reports a single element changed
1265 * event describing the net effect of all changes done to java elements by
1269 * If this method is called in the dynamic scope of another such call, this
1270 * method simply runs the action.
1274 * the action to perform
1276 * a progress monitor, or <code>null</code> if progress
1277 * reporting and cancellation are not desired
1278 * @exception CoreException
1279 * if the operation failed.
1282 public static void run(IWorkspaceRunnable action, IProgressMonitor monitor)
1283 throws CoreException {
1284 run(action, ResourcesPlugin.getWorkspace().getRoot(), monitor);
1288 * Runs the given action as an atomic Java model operation.
1290 * After running a method that modifies java elements, registered listeners
1291 * receive after-the-fact notification of what just transpired, in the form
1292 * of a element changed event. This method allows clients to call a number
1293 * of methods that modify java elements and only have element changed event
1294 * notifications reported at the end of the entire batch.
1297 * If this method is called outside the dynamic scope of another such call,
1298 * this method runs the action and then reports a single element changed
1299 * event describing the net effect of all changes done to java elements by
1303 * If this method is called in the dynamic scope of another such call, this
1304 * method simply runs the action.
1307 * The supplied scheduling rule is used to determine whether this operation
1308 * can be run simultaneously with workspace changes in other threads. See
1309 * <code>IWorkspace.run(...)</code> for more details.
1313 * the action to perform
1315 * the scheduling rule to use when running this operation, or
1316 * <code>null</code> if there are no scheduling restrictions
1317 * for this operation.
1319 * a progress monitor, or <code>null</code> if progress
1320 * reporting and cancellation are not desired
1321 * @exception CoreException
1322 * if the operation failed.
1325 public static void run(IWorkspaceRunnable action, ISchedulingRule rule,
1326 IProgressMonitor monitor) throws CoreException {
1327 IWorkspace workspace = ResourcesPlugin.getWorkspace();
1328 if (workspace.isTreeLocked()) {
1329 new BatchOperation(action).run(monitor);
1331 // use IWorkspace.run(...) to ensure that a build will be done in
1333 workspace.run(new BatchOperation(action), rule,
1334 IWorkspace.AVOID_UPDATE, monitor);
1339 * Returns the template context type registry for the java plugin.
1341 * @return the template context type registry for the java plugin
1344 public ContextTypeRegistry getTemplateContextRegistry() {
1345 if (fContextTypeRegistry == null) {
1346 fContextTypeRegistry = new ContributionContextTypeRegistry();
1348 fContextTypeRegistry.addContextType(new JavaContextType());
1349 fContextTypeRegistry.addContextType(new JavaDocContextType());
1350 fContextTypeRegistry.addContextType(new HTMLContextType());
1353 return fContextTypeRegistry;
1357 * Returns the template store for the java editor templates.
1359 * @return the template store for the java editor templates
1362 public TemplateStore getTemplateStore() {
1363 if (fTemplateStore == null) {
1364 fTemplateStore = new ContributionTemplateStore(
1365 getTemplateContextRegistry(), getPreferenceStore(),
1368 fTemplateStore.load();
1369 } catch (IOException e) {
1374 return fTemplateStore;
1378 * Returns the template context type registry for the code generation
1381 * @return the template context type registry for the code generation
1385 public ContextTypeRegistry getCodeTemplateContextRegistry() {
1386 if (fCodeTemplateContextTypeRegistry == null) {
1387 fCodeTemplateContextTypeRegistry = new ContributionContextTypeRegistry();
1389 CodeTemplateContextType
1390 .registerContextTypes(fCodeTemplateContextTypeRegistry);
1393 return fCodeTemplateContextTypeRegistry;
1397 * Returns the template store for the code generation templates.
1399 * @return the template store for the code generation templates
1402 public TemplateStore getCodeTemplateStore() {
1403 if (fCodeTemplateStore == null) {
1404 fCodeTemplateStore = new ContributionTemplateStore(
1405 getCodeTemplateContextRegistry(), getPreferenceStore(),
1406 CODE_TEMPLATES_KEY);
1408 fCodeTemplateStore.load();
1409 } catch (IOException e) {
1414 return fCodeTemplateStore;