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.Collection;
16 import java.util.HashMap;
17 import java.util.Iterator;
19 import net.sourceforge.phpdt.externaltools.internal.model.ColorManager;
20 import net.sourceforge.phpdt.externaltools.internal.model.ExternalToolsPlugin;
21 import net.sourceforge.phpdt.externaltools.internal.model.VariableContextManager;
22 import net.sourceforge.phpdt.internal.ui.preferences.TemplatePreferencePage;
23 import net.sourceforge.phpdt.internal.ui.viewsupport.ImageDescriptorRegistry;
24 import net.sourceforge.phpdt.ui.PreferenceConstants;
25 import net.sourceforge.phpdt.ui.text.JavaTextTools;
26 import net.sourceforge.phpeclipse.builder.ExternalEditorInput;
27 import net.sourceforge.phpeclipse.builder.ExternalStorageDocumentProvider;
28 import net.sourceforge.phpeclipse.builder.FileStorage;
29 import net.sourceforge.phpeclipse.builder.IdentifierIndexManager;
30 import net.sourceforge.phpeclipse.phpeditor.PHPDocumentProvider;
31 import net.sourceforge.phpeclipse.phpeditor.PHPSyntaxRdr;
32 import net.sourceforge.phpeclipse.phpeditor.util.PHPColorProvider;
33 import net.sourceforge.phpeclipse.resourcesview.PHPElement;
34 import net.sourceforge.phpeclipse.resourcesview.PHPElementAdapterFactory;
35 import net.sourceforge.phpeclipse.resourcesview.ResourceAdapterFactory;
37 import org.eclipse.core.boot.BootLoader;
38 import org.eclipse.core.resources.IFile;
39 import org.eclipse.core.resources.IProject;
40 import org.eclipse.core.resources.IResource;
41 import org.eclipse.core.resources.IWorkspace;
42 import org.eclipse.core.resources.ResourcesPlugin;
43 import org.eclipse.core.runtime.CoreException;
44 import org.eclipse.core.runtime.IAdapterManager;
45 import org.eclipse.core.runtime.IPath;
46 import org.eclipse.core.runtime.IPluginDescriptor;
47 import org.eclipse.core.runtime.IStatus;
48 import org.eclipse.core.runtime.Path;
49 import org.eclipse.core.runtime.Platform;
50 import org.eclipse.core.runtime.Status;
51 import org.eclipse.jface.preference.IPreferenceStore;
52 import org.eclipse.jface.preference.PreferenceConverter;
53 import org.eclipse.jface.text.BadLocationException;
54 import org.eclipse.jface.text.IDocument;
55 import org.eclipse.swt.widgets.Display;
56 import org.eclipse.swt.widgets.Shell;
57 import org.eclipse.ui.IEditorDescriptor;
58 import org.eclipse.ui.IEditorInput;
59 import org.eclipse.ui.IEditorPart;
60 import org.eclipse.ui.IEditorRegistry;
61 import org.eclipse.ui.IWorkbench;
62 import org.eclipse.ui.IWorkbenchPage;
63 import org.eclipse.ui.IWorkbenchWindow;
64 import org.eclipse.ui.PlatformUI;
65 import org.eclipse.ui.plugin.AbstractUIPlugin;
66 import org.eclipse.ui.texteditor.IDocumentProvider;
67 import org.eclipse.ui.texteditor.ITextEditor;
70 * The main plugin class to be used in the desktop.
72 public class PHPeclipsePlugin extends AbstractUIPlugin implements IPreferenceConstants {
74 * The id of the PHP plugin (value <code>"net.sourceforge.phpeclipse"</code>).
76 public static final String PLUGIN_ID = "net.sourceforge.phpeclipse"; //$NON-NLS-1$
79 * id of builder - matches plugin.xml (concatenate pluginid.builderid)
81 public static final String BUILDER_INDEX_ID = PLUGIN_ID + ".indexbuilder";
82 public static final String BUILDER_PARSER_ID = PLUGIN_ID + ".parserbuilder";
84 /** General debug flag*/
85 public static final boolean DEBUG = false;
87 private static ExternalToolsPlugin externalTools;
90 * The Java virtual machine that we are running on.
92 private static int jvm;
95 private static final int MRJ_2_0 = 0;
97 /** MRJ 2.1 or later */
98 private static final int MRJ_2_1 = 1;
100 /** Java on Mac OS X 10.0 (MRJ 3.0) */
101 private static final int MRJ_3_0 = 3;
104 private static final int MRJ_3_1 = 4;
106 /** JVM constant for any other platform */
107 private static final int OTHER = -1;
109 // public static final String PHP_RESOURCES_VIEW_ID = PLUGIN_ID + ".resourcesview.ViewPHPResources"; //$NON-NLS-1$
110 public static final String PHP_CODING_ACTION_SET_ID = PLUGIN_ID + ".ui.CodingActionSet"; //$NON-NLS-1$
111 public final static String PHP_NATURE_ID = PLUGIN_ID + ".phpnature";
113 public static final String PHPPARSER_ORIGINAL = "net.sourceforge.phpdt.internal.compiler.parser.Parser";
115 public static final String PHPPARSER_NEW = "test.PHPParser";
117 /** Change this if you want to switch PHP Parser. */
118 public static final String PHPPARSER = PHPPARSER_ORIGINAL;
120 //The shared instance.
121 private static PHPeclipsePlugin plugin;
124 private static final int WINDOWS_9x = 6;
127 private static final int WINDOWS_NT = 5;
128 private PHPDocumentProvider fCompilationUnitDocumentProvider;
130 //private ResourceBundle resourceBundle;
132 private ImageDescriptorRegistry fImageDescriptorRegistry;
133 private HashMap fIndexManagerMap = new HashMap();
135 private JavaTextTools fJavaTextTools;
136 private IFile fLastEditorFile = null;
140 public PHPeclipsePlugin(IPluginDescriptor descriptor) {
144 externalTools = new ExternalToolsPlugin();
146 // resourceBundle = ResourceBundle.getBundle("net.sourceforge.PHPeclipsePluginResources");
147 // } catch (MissingResourceException x) {
148 // resourceBundle = null;
152 public static IWorkbenchPage getActivePage() {
153 return getDefault().internalGetActivePage();
156 public static Shell getActiveWorkbenchShell() {
157 return getActiveWorkbenchWindow().getShell();
160 public static IWorkbenchWindow getActiveWorkbenchWindow() {
161 return getDefault().getWorkbench().getActiveWorkbenchWindow();
164 * Returns the shared instance.
166 public static PHPeclipsePlugin getDefault() {
170 public static ImageDescriptorRegistry getImageDescriptorRegistry() {
171 return getDefault().internalGetImageDescriptorRegistry();
174 static IPath getInstallLocation() {
175 return new Path(getDefault().getDescriptor().getInstallURL().getFile());
177 public static int getJVM() {
181 public static String getPluginId() {
182 return getDefault().getDescriptor().getUniqueIdentifier();
186 * Returns the standard display to be used. The method first checks, if
187 * the thread calling this method has an associated display. If so, this
188 * display is returned. Otherwise the method returns the default display.
190 public static Display getStandardDisplay() {
191 Display display = Display.getCurrent();
192 if (display == null) {
193 display = Display.getDefault();
198 // public static ExternalToolsPlugin getExternalTools() {
199 // return externalTools;
202 * Returns the workspace instance.
204 public static IWorkspace getWorkspace() {
205 return ResourcesPlugin.getWorkspace();
208 public static boolean isDebug() {
209 return getDefault().isDebugging();
212 // public static void logErrorMessage(String message) {
213 // log(new Status(IStatus.ERROR, getPluginId(), JavaStatusConstants.INTERNAL_ERROR, message, null));
216 // public static void logErrorStatus(String message, IStatus status) {
217 // if (status == null) {
218 // logErrorMessage(message);
221 // MultiStatus multi= new MultiStatus(getPluginId(), JavaStatusConstants.INTERNAL_ERROR, message, null);
222 // multi.add(status);
226 // public static void log(Throwable e) {
227 // log(new Status(IStatus.ERROR, getPluginId(), JavaStatusConstants.INTERNAL_ERROR, JavaUIMessages.getString("JavaPlugin.internal_error"), e)); //$NON-NLS-1$
230 public static void log(int severity, String message) {
231 Status status = new Status(severity, PLUGIN_ID, IStatus.OK, message, null);
235 public static void log(IStatus status) {
236 getDefault().getLog().log(status);
238 public static void log(Throwable e) {
239 log(new Status(IStatus.ERROR, PLUGIN_ID, IStatus.ERROR, "PHPeclipsePlugin.internalErrorOccurred", e)); //$NON-NLS-1$
242 private static void setJVM() {
243 String osName = System.getProperty("os.name");
245 if (osName.startsWith("Mac OS")) {
246 String mrjVersion = System.getProperty("mrj.version");
247 String majorMRJVersion = mrjVersion.substring(0, 3);
251 double version = Double.valueOf(majorMRJVersion).doubleValue();
255 } else if (version >= 2.1 && version < 3) {
257 } else if (version == 3.0) {
259 } else if (version >= 3.1) {
263 } catch (NumberFormatException nfe) {
267 } else if (osName.startsWith("Windows")) {
268 if (osName.indexOf("9") != -1) {
276 // TODO: refactor this into a better method name !
277 public synchronized PHPDocumentProvider getCompilationUnitDocumentProvider() {
278 if (fCompilationUnitDocumentProvider == null)
279 fCompilationUnitDocumentProvider = new PHPDocumentProvider();
280 return fCompilationUnitDocumentProvider;
284 * Get the identifier index manager for the given project
286 * @param iProject the current project
289 public IdentifierIndexManager getIndexManager(IProject iProject) {
290 String indexFilename = iProject.getLocation() + File.separator + "project.index";
291 IdentifierIndexManager indexManager = (IdentifierIndexManager) fIndexManagerMap.get(indexFilename);
292 if (indexManager == null) {
293 indexManager = new IdentifierIndexManager(indexFilename);
294 fIndexManagerMap.put(indexFilename, indexManager);
299 public synchronized JavaTextTools getJavaTextTools() {
300 if (fJavaTextTools == null)
301 fJavaTextTools = new JavaTextTools(getPreferenceStore());
302 return fJavaTextTools;
305 public IFile getLastEditorFile() {
306 return fLastEditorFile;
310 * Returns the string from the plugin's resource bundle,
311 * or 'key' if not found.
313 // public static String getResourceString(String key) {
314 // ResourceBundle bundle = PHPeclipsePlugin.getDefault().getResourceBundle();
316 // return bundle.getString(key);
317 // } catch (MissingResourceException e) {
323 * Returns the plugin's resource bundle,
325 // public ResourceBundle getResourceBundle() {
326 // return resourceBundle;
329 protected void initializeDefaultPreferences(IPreferenceStore store) {
330 // windows preferences:
331 store.setDefault(LOCALHOST_PREF, "http://localhost");
333 store.setDefault(SHOW_EXTERNAL_PREVIEW_PREF, "true");
334 store.setDefault(USE_EXTERNAL_BROWSER_PREF, "false");
335 store.setDefault(SHOW_OUTPUT_IN_CONSOLE, "true");
337 String windowsSystem = BootLoader.getWS();
339 if (jvm == WINDOWS_9x) {
340 store.setDefault(EXTERNAL_BROWSER_PREF, "command.com /c start iexplore {0}");
341 } else if (windowsSystem.equals(BootLoader.WS_WIN32)) {
342 store.setDefault(EXTERNAL_BROWSER_PREF, "rundll32 url.dll,FileProtocolHandler {0}");
343 } else if (windowsSystem.equals(BootLoader.WS_CARBON)) {
344 // TODO How do we start Safari on Mac OS X ?
345 store.setDefault(EXTERNAL_BROWSER_PREF, "netscape {0}");
347 store.setDefault(EXTERNAL_BROWSER_PREF, "netscape {0}");
349 store.setDefault(DOCUMENTROOT_PREF, getWorkspace().getRoot().getLocation().toString());
351 // if ((jvm == WINDOWS_9x) || (jvm == WINDOWS_NT)) {
353 if (windowsSystem.equals(BootLoader.WS_WIN32)) {
354 store.setDefault(PHP_RUN_PREF, "c:\\apache\\php\\php.exe");
355 store.setDefault(EXTERNAL_PARSER_PREF, "c:\\apache\\php\\php -l -f {0}");
356 store.setDefault(MYSQL_RUN_PREF, "c:\\apache\\mysql\\bin\\mysqld-nt.exe");
357 store.setDefault(APACHE_RUN_PREF, "c:\\apache\\apache.exe");
360 store.setDefault(PHP_RUN_PREF, "/apache/php/php");
361 store.setDefault(EXTERNAL_PARSER_PREF, "/apache/php/php -l -f {0}");
362 store.setDefault(MYSQL_RUN_PREF, "/apache/mysql/bin/mysqld");
363 store.setDefault(APACHE_RUN_PREF, "/apache/apache");
366 store.setDefault(MYSQL_PREF, "--standalone");
367 store.setDefault(APACHE_START_PREF, "-c \"DocumentRoot \"{0}\"\"");
368 store.setDefault(APACHE_STOP_PREF, "-k shutdown");
369 store.setDefault(APACHE_RESTART_PREF, "-k restart");
371 store.setDefault(MYSQL_START_BACKGROUND, "true");
372 store.setDefault(APACHE_START_BACKGROUND, "true");
373 store.setDefault(APACHE_STOP_BACKGROUND, "true");
374 store.setDefault(APACHE_RESTART_BACKGROUND, "true");
376 store.setDefault(PHP_PARSER_DEFAULT, PHP_EXTERNAL_PARSER);
377 store.setDefault(PHP_INTERNAL_PARSER, "false");
378 store.setDefault(PHP_EXTERNAL_PARSER, "true");
380 // store.setDefault(PHP_PARSE_ON_SAVE, "true");
382 // show line numbers:
383 // store.setDefault(LINE_NUMBER_RULER, "false");
384 // store.setDefault(FORMATTER_TAB_SIZE, "4");
386 // php syntax highlighting
387 store.setDefault(PHP_USERDEF_XMLFILE, "");
388 //assume there is none chooA
390 PreferenceConverter.setDefault(store, PHP_MULTILINE_COMMENT, PHPColorProvider.MULTI_LINE_COMMENT);
391 PreferenceConverter.setDefault(store, PHP_SINGLELINE_COMMENT, PHPColorProvider.SINGLE_LINE_COMMENT);
392 PreferenceConverter.setDefault(store, PHP_KEYWORD, PHPColorProvider.KEYWORD);
393 PreferenceConverter.setDefault(store, PHP_VARIABLE, PHPColorProvider.VARIABLE);
394 PreferenceConverter.setDefault(store, PHP_FUNCTIONNAME, PHPColorProvider.FUNCTION_NAME);
395 PreferenceConverter.setDefault(store, PHP_CONSTANT, PHPColorProvider.CONSTANT);
396 PreferenceConverter.setDefault(store, PHP_TYPE, PHPColorProvider.TYPE);
397 PreferenceConverter.setDefault(store, PHP_STRING, PHPColorProvider.STRING);
398 PreferenceConverter.setDefault(store, PHP_DEFAULT, PHPColorProvider.DEFAULT);
400 PreferenceConverter.setDefault(store, PHPDOC_KEYWORD, PHPColorProvider.PHPDOC_KEYWORD);
401 PreferenceConverter.setDefault(store, PHPDOC_TAG, PHPColorProvider.PHPDOC_TAG);
402 PreferenceConverter.setDefault(store, PHPDOC_LINK, PHPColorProvider.PHPDOC_LINK);
403 PreferenceConverter.setDefault(store, PHPDOC_DEFAULT, PHPColorProvider.PHPDOC_DEFAULT);
405 // PreferenceConverter.setDefault(
407 // PHP_EDITOR_BACKGROUND,
408 // PHPColorProvider.BACKGROUND);
409 // PreferenceConverter.setDefault(
411 // LINKED_POSITION_COLOR,
412 // PHPColorProvider.LINKED_POSITION_COLOR);
413 // PreferenceConverter.setDefault(
415 // LINE_NUMBER_COLOR,
416 // PHPColorProvider.LINE_NUMBER_COLOR);
418 // // set default PHPDoc colors:
419 // PreferenceConverter.setDefault(
422 // PHPColorProvider.PHPDOC_KEYWORD);
423 // PreferenceConverter.setDefault(
426 // PHPColorProvider.PHPDOC_LINK);
427 // PreferenceConverter.setDefault(
430 // PHPColorProvider.PHPDOC_DEFAULT);
431 // PreferenceConverter.setDefault(
434 // PHPColorProvider.PHPDOC_TAG);
436 // store.setDefault(PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT, "true");
437 // PreferenceConverter.setDefault(
439 // PREFERENCE_COLOR_BACKGROUND,
440 // PHPColorProvider.BACKGROUND_COLOR);
443 store.setDefault(RESOURCE_BUNDLE, LANGUAGE_DEFAULT);
444 store.setDefault(RESOURCE_BUNDLE_EN_GB, "true");
445 store.setDefault(RESOURCE_BUNDLE_DE, "false");
446 store.setDefault(RESOURCE_BUNDLE_FR, "false");
447 store.setDefault(RESOURCE_BUNDLE_ES, "false");
449 store.setDefault(PHP_OUTLINE_CLASS, "true"); //$NON-NLS-1$
450 store.setDefault(PHP_OUTLINE_FUNC, "true"); //$NON-NLS-1$
451 store.setDefault(PHP_OUTLINE_VAR, "true"); //$NON-NLS-1$
453 TemplatePreferencePage.initDefaults(store);
454 //this will initialize the static fields in the syntaxrdr class
457 PHPCore.initializeDefaultPluginPreferences();
458 PreferenceConstants.initializeDefaultValues(store);
460 externalTools.initializeDefaultPreferences(store);
463 private IWorkbenchPage internalGetActivePage() {
464 IWorkbenchWindow window = getWorkbench().getActiveWorkbenchWindow();
466 return window.getActivePage();
470 private ImageDescriptorRegistry internalGetImageDescriptorRegistry() {
471 if (fImageDescriptorRegistry == null)
472 fImageDescriptorRegistry = new ImageDescriptorRegistry();
473 return fImageDescriptorRegistry;
476 * Open a file in the Workbench that may or may not exist in the workspace.
477 * Must be run on the UI thread.
479 * @throws CoreException
481 public ITextEditor openFileInTextEditor(String filename) throws CoreException {
483 // reject directories
484 if (new File(filename).isDirectory())
487 IWorkbench workbench = PlatformUI.getWorkbench();
488 IWorkbenchWindow window = workbench.getWorkbenchWindows()[0];
489 IWorkbenchPage page = window.getActivePage();
490 IPath path = new Path(filename);
492 // If the file exists in the workspace, open it
493 IFile file = getWorkspace().getRoot().getFileForLocation(path);
495 ITextEditor textEditor;
496 if (file != null && file.exists()) {
497 editor = page.openEditor(file);
498 textEditor = (ITextEditor) editor.getAdapter(ITextEditor.class);
500 // Otherwise open the stream directly
503 FileStorage storage = new FileStorage(path);
504 IEditorRegistry registry = getWorkbench().getEditorRegistry();
505 IEditorDescriptor desc = registry.getDefaultEditor(filename);
507 desc = registry.getDefaultEditor();
509 IEditorInput input = new ExternalEditorInput(storage);
510 editor = page.openEditor(input, desc.getId());
511 textEditor = (ITextEditor) editor.getAdapter(ITextEditor.class);
513 // If the storage provider is not ours, we can't guarantee read/write.
514 if (textEditor != null) {
515 IDocumentProvider documentProvider = textEditor.getDocumentProvider();
516 if (!(documentProvider instanceof ExternalStorageDocumentProvider)) {
517 storage.setReadOnly();
524 * Open a file in the Workbench that may or may not exist in the workspace.
525 * Must be run on the UI thread.
528 * @throws CoreException
530 public void openFileAndGotoLine(String filename, int line) throws CoreException {
532 ITextEditor textEditor = openFileInTextEditor(filename);
533 if (textEditor != null) {
534 // If a line number was given, go to it
537 line--; // document is 0 based
538 IDocument document = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());
539 textEditor.selectAndReveal(document.getLineOffset(line), document.getLineLength(line));
541 } catch (BadLocationException e) {
542 // invalid text position -> do nothing
549 * Open a file in the Workbench that may or may not exist in the workspace.
550 * Must be run on the UI thread.
553 * @throws CoreException
555 public void openFileAndGotoOffset(String filename, int offset, int length) throws CoreException {
557 ITextEditor textEditor = openFileInTextEditor(filename);
558 if (textEditor != null) {
559 // If a line number was given, go to it
561 IDocument document = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());
562 textEditor.selectAndReveal(offset, length);
567 public void openFileAndFindString(String filename, String findString) throws CoreException {
569 ITextEditor textEditor = openFileInTextEditor(filename);
570 if (textEditor != null) {
571 // If a string was given, go to it
572 if (findString != null) {
574 IDocument document = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());
575 int offset = document.search(0, findString, true, false, true);
576 textEditor.selectAndReveal(offset, findString.length());
578 } catch (BadLocationException e) {
579 // invalid text position -> do nothing
585 public void setLastEditorFile(IFile textEditor) {
586 this.fLastEditorFile = textEditor;
590 * @see org.eclipse.ui.plugin.AbstractUIPlugin#shutdown()
592 public void shutdown() throws CoreException {
595 // externalTools.shutDown();
596 ColorManager.getDefault().dispose();
598 // save the information from the php index files if necessary
599 Collection collection = fIndexManagerMap.values();
600 Iterator iterator = collection.iterator();
601 IdentifierIndexManager indexManager = null;
602 while (iterator.hasNext()) {
603 indexManager = (IdentifierIndexManager) iterator.next();
604 indexManager.writeFile();
609 public void startup() throws CoreException {
611 IAdapterManager manager = Platform.getAdapterManager();
612 manager.registerAdapters(new PHPElementAdapterFactory(), PHPElement.class);
613 manager.registerAdapters(new ResourceAdapterFactory(), IResource.class);
614 // externalTools.startUp();
615 getStandardDisplay().asyncExec(new Runnable() {
617 //initialize the variable context manager
618 VariableContextManager.getDefault();