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;
88 * The maximum number of allowed proposals by category
90 public final static int MAX_PROPOSALS = 200;
92 private static ExternalToolsPlugin externalTools;
95 * The Java virtual machine that we are running on.
97 private static int jvm;
100 private static final int MRJ_2_0 = 0;
102 /** MRJ 2.1 or later */
103 private static final int MRJ_2_1 = 1;
105 /** Java on Mac OS X 10.0 (MRJ 3.0) */
106 private static final int MRJ_3_0 = 3;
109 private static final int MRJ_3_1 = 4;
111 /** JVM constant for any other platform */
112 private static final int OTHER = -1;
114 // public static final String PHP_RESOURCES_VIEW_ID = PLUGIN_ID + ".resourcesview.ViewPHPResources"; //$NON-NLS-1$
115 public static final String PHP_CODING_ACTION_SET_ID = PLUGIN_ID + ".ui.CodingActionSet"; //$NON-NLS-1$
116 public final static String PHP_NATURE_ID = PLUGIN_ID + ".phpnature";
118 public static final String PHPPARSER_ORIGINAL = "net.sourceforge.phpdt.internal.compiler.parser.Parser";
120 public static final String PHPPARSER_NEW = "test.PHPParser";
122 /** Change this if you want to switch PHP Parser. */
123 public static final String PHPPARSER = PHPPARSER_ORIGINAL;
125 //The shared instance.
126 private static PHPeclipsePlugin plugin;
129 private static final int WINDOWS_9x = 6;
132 private static final int WINDOWS_NT = 5;
133 private PHPDocumentProvider fCompilationUnitDocumentProvider;
135 private ImageDescriptorRegistry fImageDescriptorRegistry;
136 private HashMap fIndexManagerMap = new HashMap();
138 private JavaTextTools fJavaTextTools;
139 private IFile fLastEditorFile = null;
144 public PHPeclipsePlugin(IPluginDescriptor descriptor) {
148 externalTools = new ExternalToolsPlugin();
150 // resourceBundle = ResourceBundle.getBundle("net.sourceforge.PHPeclipsePluginResources");
151 // } catch (MissingResourceException x) {
152 // resourceBundle = null;
156 public static IWorkbenchPage getActivePage() {
157 return getDefault().internalGetActivePage();
160 public static Shell getActiveWorkbenchShell() {
161 return getActiveWorkbenchWindow().getShell();
164 public static IWorkbenchWindow getActiveWorkbenchWindow() {
165 return getDefault().getWorkbench().getActiveWorkbenchWindow();
168 * Returns the shared instance.
170 public static PHPeclipsePlugin getDefault() {
174 public static ImageDescriptorRegistry getImageDescriptorRegistry() {
175 return getDefault().internalGetImageDescriptorRegistry();
178 static IPath getInstallLocation() {
179 return new Path(getDefault().getDescriptor().getInstallURL().getFile());
181 public static int getJVM() {
185 public static String getPluginId() {
186 return getDefault().getDescriptor().getUniqueIdentifier();
190 * Returns the standard display to be used. The method first checks, if
191 * the thread calling this method has an associated display. If so, this
192 * display is returned. Otherwise the method returns the default display.
194 public static Display getStandardDisplay() {
195 Display display = Display.getCurrent();
196 if (display == null) {
197 display = Display.getDefault();
202 // public static ExternalToolsPlugin getExternalTools() {
203 // return externalTools;
206 * Returns the workspace instance.
208 public static IWorkspace getWorkspace() {
209 return ResourcesPlugin.getWorkspace();
212 public static boolean isDebug() {
213 return getDefault().isDebugging();
216 // public static void logErrorMessage(String message) {
217 // log(new Status(IStatus.ERROR, getPluginId(), JavaStatusConstants.INTERNAL_ERROR, message, null));
220 // public static void logErrorStatus(String message, IStatus status) {
221 // if (status == null) {
222 // logErrorMessage(message);
225 // MultiStatus multi= new MultiStatus(getPluginId(), JavaStatusConstants.INTERNAL_ERROR, message, null);
226 // multi.add(status);
230 // public static void log(Throwable e) {
231 // log(new Status(IStatus.ERROR, getPluginId(), JavaStatusConstants.INTERNAL_ERROR, JavaUIMessages.getString("JavaPlugin.internal_error"), e)); //$NON-NLS-1$
234 public static void log(int severity, String message) {
235 Status status = new Status(severity, PLUGIN_ID, IStatus.OK, message, null);
239 public static void log(IStatus status) {
240 getDefault().getLog().log(status);
242 public static void log(Throwable e) {
243 log(new Status(IStatus.ERROR, PLUGIN_ID, IStatus.ERROR, "PHPeclipsePlugin.internalErrorOccurred", e)); //$NON-NLS-1$
246 private static void setJVM() {
247 String osName = System.getProperty("os.name");
249 if (osName.startsWith("Mac OS")) {
250 String mrjVersion = System.getProperty("mrj.version");
251 String majorMRJVersion = mrjVersion.substring(0, 3);
255 double version = Double.valueOf(majorMRJVersion).doubleValue();
259 } else if (version >= 2.1 && version < 3) {
261 } else if (version == 3.0) {
263 } else if (version >= 3.1) {
267 } catch (NumberFormatException nfe) {
271 } else if (osName.startsWith("Windows")) {
272 if (osName.indexOf("9") != -1) {
280 // TODO: refactor this into a better method name !
281 public synchronized PHPDocumentProvider getCompilationUnitDocumentProvider() {
282 if (fCompilationUnitDocumentProvider == null)
283 fCompilationUnitDocumentProvider = new PHPDocumentProvider();
284 return fCompilationUnitDocumentProvider;
288 * Get the identifier index manager for the given project
290 * @param iProject the current project
293 public IdentifierIndexManager getIndexManager(IProject iProject) {
294 String indexFilename = iProject.getLocation() + File.separator + "project.index";
295 IdentifierIndexManager indexManager = (IdentifierIndexManager) fIndexManagerMap.get(indexFilename);
296 if (indexManager == null) {
297 indexManager = new IdentifierIndexManager(indexFilename);
298 fIndexManagerMap.put(indexFilename, indexManager);
303 public synchronized JavaTextTools getJavaTextTools() {
304 if (fJavaTextTools == null)
305 fJavaTextTools = new JavaTextTools(getPreferenceStore());
306 return fJavaTextTools;
309 public IFile getLastEditorFile() {
310 return fLastEditorFile;
314 * Returns the string from the plugin's resource bundle,
315 * or 'key' if not found.
317 // public static String getResourceString(String key) {
318 // ResourceBundle bundle = PHPeclipsePlugin.getDefault().getResourceBundle();
320 // return bundle.getString(key);
321 // } catch (MissingResourceException e) {
327 * Returns the plugin's resource bundle,
329 // public ResourceBundle getResourceBundle() {
330 // return resourceBundle;
333 protected void initializeDefaultPreferences(IPreferenceStore store) {
334 // windows preferences:
335 store.setDefault(LOCALHOST_PREF, "http://localhost");
337 store.setDefault(SHOW_EXTERNAL_PREVIEW_PREF, "true");
338 store.setDefault(USE_EXTERNAL_BROWSER_PREF, "false");
339 store.setDefault(SHOW_OUTPUT_IN_CONSOLE, "true");
341 String windowsSystem = BootLoader.getWS();
343 if (jvm == WINDOWS_9x) {
344 store.setDefault(EXTERNAL_BROWSER_PREF, "command.com /c start iexplore {0}");
345 } else if (windowsSystem.equals(BootLoader.WS_WIN32)) {
346 store.setDefault(EXTERNAL_BROWSER_PREF, "rundll32 url.dll,FileProtocolHandler {0}");
347 } else if (windowsSystem.equals(BootLoader.WS_CARBON)) {
348 // TODO How do we start Safari on Mac OS X ?
349 store.setDefault(EXTERNAL_BROWSER_PREF, "netscape {0}");
351 store.setDefault(EXTERNAL_BROWSER_PREF, "netscape {0}");
353 store.setDefault(DOCUMENTROOT_PREF, getWorkspace().getRoot().getLocation().toString());
355 // if ((jvm == WINDOWS_9x) || (jvm == WINDOWS_NT)) {
357 if (windowsSystem.equals(BootLoader.WS_WIN32)) {
358 store.setDefault(PHP_RUN_PREF, "c:\\apache\\php\\php.exe");
359 store.setDefault(EXTERNAL_PARSER_PREF, "c:\\apache\\php\\php -l -f {0}");
360 store.setDefault(MYSQL_RUN_PREF, "c:\\apache\\mysql\\bin\\mysqld-nt.exe");
361 store.setDefault(APACHE_RUN_PREF, "c:\\apache\\apache.exe");
364 store.setDefault(PHP_RUN_PREF, "/apache/php/php");
365 store.setDefault(EXTERNAL_PARSER_PREF, "/apache/php/php -l -f {0}");
366 store.setDefault(MYSQL_RUN_PREF, "/apache/mysql/bin/mysqld");
367 store.setDefault(APACHE_RUN_PREF, "/apache/apache");
370 store.setDefault(MYSQL_PREF, "--standalone");
371 store.setDefault(APACHE_START_PREF, "-c \"DocumentRoot \"{0}\"\"");
372 store.setDefault(APACHE_STOP_PREF, "-k shutdown");
373 store.setDefault(APACHE_RESTART_PREF, "-k restart");
375 store.setDefault(MYSQL_START_BACKGROUND, "true");
376 store.setDefault(APACHE_START_BACKGROUND, "true");
377 store.setDefault(APACHE_STOP_BACKGROUND, "true");
378 store.setDefault(APACHE_RESTART_BACKGROUND, "true");
380 store.setDefault(PHP_PARSER_DEFAULT, PHP_EXTERNAL_PARSER);
381 store.setDefault(PHP_INTERNAL_PARSER, "false");
382 store.setDefault(PHP_EXTERNAL_PARSER, "true");
384 // store.setDefault(PHP_PARSE_ON_SAVE, "true");
386 // show line numbers:
387 // store.setDefault(LINE_NUMBER_RULER, "false");
388 // store.setDefault(FORMATTER_TAB_SIZE, "4");
390 // php syntax highlighting
391 store.setDefault(PHP_USERDEF_XMLFILE, "");
392 //assume there is none chooA
394 PreferenceConverter.setDefault(store, PHP_MULTILINE_COMMENT, PHPColorProvider.MULTI_LINE_COMMENT);
395 PreferenceConverter.setDefault(store, PHP_SINGLELINE_COMMENT, PHPColorProvider.SINGLE_LINE_COMMENT);
396 PreferenceConverter.setDefault(store, PHP_KEYWORD, PHPColorProvider.KEYWORD);
397 PreferenceConverter.setDefault(store, PHP_VARIABLE, PHPColorProvider.VARIABLE);
398 PreferenceConverter.setDefault(store, PHP_FUNCTIONNAME, PHPColorProvider.FUNCTION_NAME);
399 PreferenceConverter.setDefault(store, PHP_CONSTANT, PHPColorProvider.CONSTANT);
400 PreferenceConverter.setDefault(store, PHP_TYPE, PHPColorProvider.TYPE);
401 PreferenceConverter.setDefault(store, PHP_STRING, PHPColorProvider.STRING);
402 PreferenceConverter.setDefault(store, PHP_DEFAULT, PHPColorProvider.DEFAULT);
404 PreferenceConverter.setDefault(store, PHPDOC_KEYWORD, PHPColorProvider.PHPDOC_KEYWORD);
405 PreferenceConverter.setDefault(store, PHPDOC_TAG, PHPColorProvider.PHPDOC_TAG);
406 PreferenceConverter.setDefault(store, PHPDOC_LINK, PHPColorProvider.PHPDOC_LINK);
407 PreferenceConverter.setDefault(store, PHPDOC_DEFAULT, PHPColorProvider.PHPDOC_DEFAULT);
409 // PreferenceConverter.setDefault(
411 // PHP_EDITOR_BACKGROUND,
412 // PHPColorProvider.BACKGROUND);
413 // PreferenceConverter.setDefault(
415 // LINKED_POSITION_COLOR,
416 // PHPColorProvider.LINKED_POSITION_COLOR);
417 // PreferenceConverter.setDefault(
419 // LINE_NUMBER_COLOR,
420 // PHPColorProvider.LINE_NUMBER_COLOR);
422 // // set default PHPDoc colors:
423 // PreferenceConverter.setDefault(
426 // PHPColorProvider.PHPDOC_KEYWORD);
427 // PreferenceConverter.setDefault(
430 // PHPColorProvider.PHPDOC_LINK);
431 // PreferenceConverter.setDefault(
434 // PHPColorProvider.PHPDOC_DEFAULT);
435 // PreferenceConverter.setDefault(
438 // PHPColorProvider.PHPDOC_TAG);
440 // store.setDefault(PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT, "true");
441 // PreferenceConverter.setDefault(
443 // PREFERENCE_COLOR_BACKGROUND,
444 // PHPColorProvider.BACKGROUND_COLOR);
447 store.setDefault(RESOURCE_BUNDLE, LANGUAGE_DEFAULT);
448 store.setDefault(RESOURCE_BUNDLE_EN_GB, "true");
449 store.setDefault(RESOURCE_BUNDLE_DE, "false");
450 store.setDefault(RESOURCE_BUNDLE_FR, "false");
451 store.setDefault(RESOURCE_BUNDLE_ES, "false");
453 store.setDefault(PHP_OUTLINE_CLASS, "true"); //$NON-NLS-1$
454 store.setDefault(PHP_OUTLINE_FUNC, "true"); //$NON-NLS-1$
455 store.setDefault(PHP_OUTLINE_VAR, "true"); //$NON-NLS-1$
457 TemplatePreferencePage.initDefaults(store);
458 //this will initialize the static fields in the syntaxrdr class
461 PHPCore.initializeDefaultPluginPreferences();
462 PreferenceConstants.initializeDefaultValues(store);
464 externalTools.initializeDefaultPreferences(store);
467 private IWorkbenchPage internalGetActivePage() {
468 IWorkbenchWindow window = getWorkbench().getActiveWorkbenchWindow();
470 return window.getActivePage();
474 private ImageDescriptorRegistry internalGetImageDescriptorRegistry() {
475 if (fImageDescriptorRegistry == null)
476 fImageDescriptorRegistry = new ImageDescriptorRegistry();
477 return fImageDescriptorRegistry;
480 * Open a file in the Workbench that may or may not exist in the workspace.
481 * Must be run on the UI thread.
483 * @throws CoreException
485 public ITextEditor openFileInTextEditor(String filename) throws CoreException {
487 // reject directories
488 if (new File(filename).isDirectory())
491 IWorkbench workbench = PlatformUI.getWorkbench();
492 IWorkbenchWindow window = workbench.getWorkbenchWindows()[0];
493 IWorkbenchPage page = window.getActivePage();
494 IPath path = new Path(filename);
496 // If the file exists in the workspace, open it
497 IFile file = getWorkspace().getRoot().getFileForLocation(path);
499 ITextEditor textEditor;
500 if (file != null && file.exists()) {
501 editor = page.openEditor(file);
502 textEditor = (ITextEditor) editor.getAdapter(ITextEditor.class);
504 // Otherwise open the stream directly
507 FileStorage storage = new FileStorage(path);
508 IEditorRegistry registry = getWorkbench().getEditorRegistry();
509 IEditorDescriptor desc = registry.getDefaultEditor(filename);
511 desc = registry.getDefaultEditor();
513 IEditorInput input = new ExternalEditorInput(storage);
514 editor = page.openEditor(input, desc.getId());
515 textEditor = (ITextEditor) editor.getAdapter(ITextEditor.class);
517 // If the storage provider is not ours, we can't guarantee read/write.
518 if (textEditor != null) {
519 IDocumentProvider documentProvider = textEditor.getDocumentProvider();
520 if (!(documentProvider instanceof ExternalStorageDocumentProvider)) {
521 storage.setReadOnly();
528 * Open a file in the Workbench that may or may not exist in the workspace.
529 * Must be run on the UI thread.
532 * @throws CoreException
534 public void openFileAndGotoLine(String filename, int line) throws CoreException {
536 ITextEditor textEditor = openFileInTextEditor(filename);
537 if (textEditor != null) {
538 // If a line number was given, go to it
541 line--; // document is 0 based
542 IDocument document = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());
543 textEditor.selectAndReveal(document.getLineOffset(line), document.getLineLength(line));
545 } catch (BadLocationException e) {
546 // invalid text position -> do nothing
553 * Open a file in the Workbench that may or may not exist in the workspace.
554 * Must be run on the UI thread.
557 * @throws CoreException
559 public void openFileAndGotoOffset(String filename, int offset, int length) throws CoreException {
561 ITextEditor textEditor = openFileInTextEditor(filename);
562 if (textEditor != null) {
563 // If a line number was given, go to it
565 IDocument document = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());
566 textEditor.selectAndReveal(offset, length);
571 public void openFileAndFindString(String filename, String findString) throws CoreException {
573 ITextEditor textEditor = openFileInTextEditor(filename);
574 if (textEditor != null) {
575 // If a string was given, go to it
576 if (findString != null) {
578 IDocument document = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());
579 int offset = document.search(0, findString, true, false, true);
580 textEditor.selectAndReveal(offset, findString.length());
582 } catch (BadLocationException e) {
583 // invalid text position -> do nothing
589 public void setLastEditorFile(IFile textEditor) {
590 this.fLastEditorFile = textEditor;
594 * @see org.eclipse.ui.plugin.AbstractUIPlugin#shutdown()
596 public void shutdown() throws CoreException {
599 // externalTools.shutDown();
600 ColorManager.getDefault().dispose();
602 // save the information from the php index files if necessary
603 Collection collection = fIndexManagerMap.values();
604 Iterator iterator = collection.iterator();
605 IdentifierIndexManager indexManager = null;
606 while (iterator.hasNext()) {
607 indexManager = (IdentifierIndexManager) iterator.next();
608 indexManager.writeFile();
613 public void startup() throws CoreException {
615 IAdapterManager manager = Platform.getAdapterManager();
616 manager.registerAdapters(new PHPElementAdapterFactory(), PHPElement.class);
617 manager.registerAdapters(new ResourceAdapterFactory(), IResource.class);
618 // externalTools.startUp();
619 getStandardDisplay().asyncExec(new Runnable() {
621 //initialize the variable context manager
622 VariableContextManager.getDefault();