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
9 IBM Corporation - Initial implementation
10 Klaus Hartlage - www.eclipseproject.de
11 **********************************************************************/
12 package net.sourceforge.phpeclipse;
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;
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;
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;
74 * The main plugin class to be used in the desktop.
76 public class PHPeclipsePlugin extends AbstractUIPlugin implements IPreferenceConstants {
78 * The id of the PHP plugin (value <code>"net.sourceforge.phpeclipse"</code>).
80 public static final String PLUGIN_ID = "net.sourceforge.phpeclipse"; //$NON-NLS-1$
83 * id of builder - matches plugin.xml (concatenate pluginid.builderid)
85 public static final String BUILDER_PARSER_ID = PLUGIN_ID + ".parserbuilder";
86 //public static final String BUILDER_INDEX_ID = PLUGIN_ID + ".indexbuilder";
88 /** General debug flag*/
89 public static final boolean DEBUG = false;
92 * The maximum number of allowed proposals by category
94 public final static int MAX_PROPOSALS = 200;
96 private static ExternalToolsPlugin externalTools;
99 * The Java virtual machine that we are running on.
101 private static int jvm;
104 private static final int MRJ_2_0 = 0;
106 /** MRJ 2.1 or later */
107 private static final int MRJ_2_1 = 1;
109 /** Java on Mac OS X 10.0 (MRJ 3.0) */
110 private static final int MRJ_3_0 = 3;
113 private static final int MRJ_3_1 = 4;
115 /** JVM constant for any other platform */
116 private static final int OTHER = -1;
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";
122 public static final String PHPPARSER_ORIGINAL = "net.sourceforge.phpdt.internal.compiler.parser.Parser";
124 public static final String PHPPARSER_NEW = "test.PHPParser";
126 /** Change this if you want to switch PHP Parser. */
127 public static final String PHPPARSER = PHPPARSER_ORIGINAL;
129 //The shared instance.
130 private static PHPeclipsePlugin plugin;
133 private static final int WINDOWS_9x = 6;
136 private static final int WINDOWS_NT = 5;
137 private PHPDocumentProvider fCompilationUnitDocumentProvider;
139 private ImageDescriptorRegistry fImageDescriptorRegistry;
140 private HashMap fIndexManagerMap = new HashMap();
142 private JavaTextTools fJavaTextTools;
143 private IFile fLastEditorFile = null;
148 public PHPeclipsePlugin(IPluginDescriptor descriptor) {
152 externalTools = new ExternalToolsPlugin();
154 // resourceBundle = ResourceBundle.getBundle("net.sourceforge.PHPeclipsePluginResources");
155 // } catch (MissingResourceException x) {
156 // resourceBundle = null;
160 public static IWorkbenchPage getActivePage() {
161 return getDefault().internalGetActivePage();
164 public static Shell getActiveWorkbenchShell() {
165 return getActiveWorkbenchWindow().getShell();
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.
172 * @return an array of all dirty editor parts.
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)) {
193 return (IEditorPart[]) result.toArray(new IEditorPart[result.size()]);
196 public static IWorkbenchWindow getActiveWorkbenchWindow() {
197 return getDefault().getWorkbench().getActiveWorkbenchWindow();
200 * Returns the shared instance.
202 public static PHPeclipsePlugin getDefault() {
206 public static ImageDescriptorRegistry getImageDescriptorRegistry() {
207 return getDefault().internalGetImageDescriptorRegistry();
210 static IPath getInstallLocation() {
211 return new Path(getDefault().getDescriptor().getInstallURL().getFile());
213 public static int getJVM() {
217 public static String getPluginId() {
218 return getDefault().getDescriptor().getUniqueIdentifier();
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.
226 public static Display getStandardDisplay() {
227 Display display = Display.getCurrent();
228 if (display == null) {
229 display = Display.getDefault();
234 // public static ExternalToolsPlugin getExternalTools() {
235 // return externalTools;
238 * Returns the workspace instance.
240 public static IWorkspace getWorkspace() {
241 return ResourcesPlugin.getWorkspace();
244 public static boolean isDebug() {
245 return getDefault().isDebugging();
248 // public static void logErrorMessage(String message) {
249 // log(new Status(IStatus.ERROR, getPluginId(), JavaStatusConstants.INTERNAL_ERROR, message, null));
252 // public static void logErrorStatus(String message, IStatus status) {
253 // if (status == null) {
254 // logErrorMessage(message);
257 // MultiStatus multi= new MultiStatus(getPluginId(), JavaStatusConstants.INTERNAL_ERROR, message, null);
258 // multi.add(status);
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$
266 public static void log(int severity, String message) {
267 Status status = new Status(severity, PLUGIN_ID, IStatus.OK, message, null);
271 public static void log(IStatus status) {
272 getDefault().getLog().log(status);
274 public static void log(Throwable e) {
275 log(new Status(IStatus.ERROR, PLUGIN_ID, IStatus.ERROR, "PHPeclipsePlugin.internalErrorOccurred", e)); //$NON-NLS-1$
278 private static void setJVM() {
279 String osName = System.getProperty("os.name");
281 if (osName.startsWith("Mac OS")) {
282 String mrjVersion = System.getProperty("mrj.version");
283 String majorMRJVersion = mrjVersion.substring(0, 3);
287 double version = Double.valueOf(majorMRJVersion).doubleValue();
291 } else if (version >= 2.1 && version < 3) {
293 } else if (version == 3.0) {
295 } else if (version >= 3.1) {
299 } catch (NumberFormatException nfe) {
303 } else if (osName.startsWith("Windows")) {
304 if (osName.indexOf("9") != -1) {
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;
320 * Get the identifier index manager for the given project
322 * @param iProject the current project
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);
335 public synchronized JavaTextTools getJavaTextTools() {
336 if (fJavaTextTools == null)
337 fJavaTextTools = new JavaTextTools(getPreferenceStore());
338 return fJavaTextTools;
341 public IFile getLastEditorFile() {
342 return fLastEditorFile;
346 * Returns the string from the plugin's resource bundle,
347 * or 'key' if not found.
349 // public static String getResourceString(String key) {
350 // ResourceBundle bundle = PHPeclipsePlugin.getDefault().getResourceBundle();
352 // return bundle.getString(key);
353 // } catch (MissingResourceException e) {
359 * Returns the plugin's resource bundle,
361 // public ResourceBundle getResourceBundle() {
362 // return resourceBundle;
365 protected void initializeDefaultPreferences(IPreferenceStore store) {
366 // windows preferences:
367 store.setDefault(LOCALHOST_PREF, "http://localhost");
369 store.setDefault(SHOW_EXTERNAL_PREVIEW_PREF, "true");
370 store.setDefault(USE_EXTERNAL_BROWSER_PREF, "false");
371 store.setDefault(SHOW_OUTPUT_IN_CONSOLE, "true");
373 String windowsSystem = BootLoader.getWS();
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}");
383 store.setDefault(EXTERNAL_BROWSER_PREF, "netscape {0}");
385 store.setDefault(DOCUMENTROOT_PREF, getWorkspace().getRoot().getLocation().toString());
387 // if ((jvm == WINDOWS_9x) || (jvm == WINDOWS_NT)) {
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");
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");
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");
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");
412 store.setDefault(PHP_PARSER_DEFAULT, PHP_EXTERNAL_PARSER);
413 store.setDefault(PHP_INTERNAL_PARSER, "false");
414 store.setDefault(PHP_EXTERNAL_PARSER, "true");
416 // store.setDefault(PHP_PARSE_ON_SAVE, "true");
418 // show line numbers:
419 // store.setDefault(LINE_NUMBER_RULER, "false");
420 // store.setDefault(FORMATTER_TAB_SIZE, "4");
422 // php syntax highlighting
423 store.setDefault(PHP_USERDEF_XMLFILE, "");
424 //assume there is none chooA
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);
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);
442 // PreferenceConverter.setDefault(
444 // PHP_EDITOR_BACKGROUND,
445 // PHPColorProvider.BACKGROUND);
446 // PreferenceConverter.setDefault(
448 // LINKED_POSITION_COLOR,
449 // PHPColorProvider.LINKED_POSITION_COLOR);
450 // PreferenceConverter.setDefault(
452 // LINE_NUMBER_COLOR,
453 // PHPColorProvider.LINE_NUMBER_COLOR);
455 // // set default PHPDoc colors:
456 // PreferenceConverter.setDefault(
459 // PHPColorProvider.PHPDOC_KEYWORD);
460 // PreferenceConverter.setDefault(
463 // PHPColorProvider.PHPDOC_LINK);
464 // PreferenceConverter.setDefault(
467 // PHPColorProvider.PHPDOC_DEFAULT);
468 // PreferenceConverter.setDefault(
471 // PHPColorProvider.PHPDOC_TAG);
473 // store.setDefault(PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT, "true");
474 // PreferenceConverter.setDefault(
476 // PREFERENCE_COLOR_BACKGROUND,
477 // PHPColorProvider.BACKGROUND_COLOR);
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");
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$
490 TemplatePreferencePage.initDefaults(store);
491 //this will initialize the static fields in the syntaxrdr class
494 PHPCore.initializeDefaultPluginPreferences();
495 PreferenceConstants.initializeDefaultValues(store);
497 externalTools.initializeDefaultPreferences(store);
500 private IWorkbenchPage internalGetActivePage() {
501 IWorkbenchWindow window = getWorkbench().getActiveWorkbenchWindow();
503 return window.getActivePage();
507 private ImageDescriptorRegistry internalGetImageDescriptorRegistry() {
508 if (fImageDescriptorRegistry == null)
509 fImageDescriptorRegistry = new ImageDescriptorRegistry();
510 return fImageDescriptorRegistry;
513 * Open a file in the Workbench that may or may not exist in the workspace.
514 * Must be run on the UI thread.
516 * @throws CoreException
518 public ITextEditor openFileInTextEditor(String filename) throws CoreException {
520 // reject directories
521 if (new File(filename).isDirectory())
524 IWorkbench workbench = PlatformUI.getWorkbench();
525 IWorkbenchWindow window = workbench.getWorkbenchWindows()[0];
526 IWorkbenchPage page = window.getActivePage();
527 IPath path = new Path(filename);
529 // If the file exists in the workspace, open it
530 IFile file = getWorkspace().getRoot().getFileForLocation(path);
532 ITextEditor textEditor;
533 if (file != null && file.exists()) {
534 editor = page.openEditor(file);
535 textEditor = (ITextEditor) editor.getAdapter(ITextEditor.class);
537 // Otherwise open the stream directly
540 FileStorage storage = new FileStorage(path);
541 IEditorRegistry registry = getWorkbench().getEditorRegistry();
542 IEditorDescriptor desc = registry.getDefaultEditor(filename);
544 desc = registry.getDefaultEditor();
546 IEditorInput input = new ExternalEditorInput(storage);
547 editor = page.openEditor(input, desc.getId());
548 textEditor = (ITextEditor) editor.getAdapter(ITextEditor.class);
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();
561 * Open a file in the Workbench that may or may not exist in the workspace.
562 * Must be run on the UI thread.
565 * @throws CoreException
567 public void openFileAndGotoLine(String filename, int line) throws CoreException {
569 ITextEditor textEditor = openFileInTextEditor(filename);
570 if (textEditor != null) {
571 // If a line number was given, go to it
574 line--; // document is 0 based
575 IDocument document = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());
576 textEditor.selectAndReveal(document.getLineOffset(line), document.getLineLength(line));
578 } catch (BadLocationException e) {
579 // invalid text position -> do nothing
586 * Open a file in the Workbench that may or may not exist in the workspace.
587 * Must be run on the UI thread.
590 * @throws CoreException
592 public void openFileAndGotoOffset(String filename, int offset, int length) throws CoreException {
594 ITextEditor textEditor = openFileInTextEditor(filename);
595 if (textEditor != null) {
596 // If a line number was given, go to it
598 IDocument document = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());
599 textEditor.selectAndReveal(offset, length);
604 public void openFileAndFindString(String filename, String findString) throws CoreException {
606 ITextEditor textEditor = openFileInTextEditor(filename);
607 if (textEditor != null) {
608 // If a string was given, go to it
609 if (findString != null) {
611 IDocument document = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());
612 int offset = document.search(0, findString, true, false, true);
613 textEditor.selectAndReveal(offset, findString.length());
615 } catch (BadLocationException e) {
616 // invalid text position -> do nothing
622 public void setLastEditorFile(IFile textEditor) {
623 this.fLastEditorFile = textEditor;
627 * @see org.eclipse.ui.plugin.AbstractUIPlugin#shutdown()
629 public void shutdown() throws CoreException {
632 // externalTools.shutDown();
633 ColorManager.getDefault().dispose();
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();
646 public void startup() throws CoreException {
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() {
654 //initialize the variable context manager
655 VariableContextManager.getDefault();