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);
399 // PreferenceConverter.setDefault(
401 // PHP_EDITOR_BACKGROUND,
402 // PHPColorProvider.BACKGROUND);
403 // PreferenceConverter.setDefault(
405 // LINKED_POSITION_COLOR,
406 // PHPColorProvider.LINKED_POSITION_COLOR);
407 // PreferenceConverter.setDefault(
409 // LINE_NUMBER_COLOR,
410 // PHPColorProvider.LINE_NUMBER_COLOR);
412 // // set default PHPDoc colors:
413 // PreferenceConverter.setDefault(
416 // PHPColorProvider.PHPDOC_KEYWORD);
417 // PreferenceConverter.setDefault(
420 // PHPColorProvider.PHPDOC_LINK);
421 // PreferenceConverter.setDefault(
424 // PHPColorProvider.PHPDOC_DEFAULT);
425 // PreferenceConverter.setDefault(
428 // PHPColorProvider.PHPDOC_TAG);
430 // store.setDefault(PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT, "true");
431 // PreferenceConverter.setDefault(
433 // PREFERENCE_COLOR_BACKGROUND,
434 // PHPColorProvider.BACKGROUND_COLOR);
437 store.setDefault(RESOURCE_BUNDLE, LANGUAGE_DEFAULT);
438 store.setDefault(RESOURCE_BUNDLE_EN_GB, "true");
439 store.setDefault(RESOURCE_BUNDLE_DE, "false");
440 store.setDefault(RESOURCE_BUNDLE_FR, "false");
441 store.setDefault(RESOURCE_BUNDLE_ES, "false");
443 store.setDefault(PHP_OUTLINE_CLASS, "true"); //$NON-NLS-1$
444 store.setDefault(PHP_OUTLINE_FUNC, "true"); //$NON-NLS-1$
445 store.setDefault(PHP_OUTLINE_VAR, "true"); //$NON-NLS-1$
447 TemplatePreferencePage.initDefaults(store);
448 //this will initialize the static fields in the syntaxrdr class
451 PHPCore.initializeDefaultPluginPreferences();
452 PreferenceConstants.initializeDefaultValues(store);
454 externalTools.initializeDefaultPreferences(store);
457 private IWorkbenchPage internalGetActivePage() {
458 IWorkbenchWindow window = getWorkbench().getActiveWorkbenchWindow();
460 return window.getActivePage();
464 private ImageDescriptorRegistry internalGetImageDescriptorRegistry() {
465 if (fImageDescriptorRegistry == null)
466 fImageDescriptorRegistry = new ImageDescriptorRegistry();
467 return fImageDescriptorRegistry;
471 * Open a file in the Workbench that may or may not exist in the workspace.
472 * Must be run on the UI thread.
475 * @throws CoreException
477 public void openFileInTextEditor(String filename, int line, String findString) throws CoreException {
479 // reject directories
480 if (new File(filename).isDirectory())
483 IWorkbench workbench = PlatformUI.getWorkbench();
484 IWorkbenchWindow window = workbench.getWorkbenchWindows()[0];
485 IWorkbenchPage page = window.getActivePage();
486 IPath path = new Path(filename);
488 // If the file exists in the workspace, open it
489 IFile file = getWorkspace().getRoot().getFileForLocation(path);
491 ITextEditor textEditor;
492 if (file != null && file.exists()) {
493 editor = page.openEditor(file);
494 textEditor = (ITextEditor) editor.getAdapter(ITextEditor.class);
496 // Otherwise open the stream directly
499 FileStorage storage = new FileStorage(path);
500 IEditorRegistry registry = getWorkbench().getEditorRegistry();
501 IEditorDescriptor desc = registry.getDefaultEditor(filename);
503 desc = registry.getDefaultEditor();
505 IEditorInput input = new ExternalEditorInput(storage);
506 editor = page.openEditor(input, desc.getId());
507 textEditor = (ITextEditor) editor.getAdapter(ITextEditor.class);
509 // If the storage provider is not ours, we can't guarantee read/write.
510 if (textEditor != null) {
511 IDocumentProvider documentProvider = textEditor.getDocumentProvider();
512 if (!(documentProvider instanceof ExternalStorageDocumentProvider)) {
513 storage.setReadOnly();
517 if (textEditor != null) {
518 // If a line number was given, go to it
521 line--; // document is 0 based
522 IDocument document = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());
523 textEditor.selectAndReveal(document.getLineOffset(line), document.getLineLength(line));
525 } catch (BadLocationException e) {
526 // invalid text position -> do nothing
529 // If a string was given, go to it
530 if (findString != null) {
532 IDocument document = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());
533 int offset = document.search(0, findString, true, false, true);
534 textEditor.selectAndReveal(offset, findString.length());
536 } catch (BadLocationException e) {
537 // invalid text position -> do nothing
544 public void setLastEditorFile(IFile textEditor) {
545 this.fLastEditorFile = textEditor;
549 * @see org.eclipse.ui.plugin.AbstractUIPlugin#shutdown()
551 public void shutdown() throws CoreException {
554 // externalTools.shutDown();
555 ColorManager.getDefault().dispose();
557 // save the information from the php index files if necessary
558 Collection collection = fIndexManagerMap.values();
559 Iterator iterator = collection.iterator();
560 IdentifierIndexManager indexManager = null;
561 while (iterator.hasNext()) {
562 indexManager = (IdentifierIndexManager) iterator.next();
563 indexManager.writeFile();
568 public void startup() throws CoreException {
570 IAdapterManager manager = Platform.getAdapterManager();
571 manager.registerAdapters(new PHPElementAdapterFactory(), PHPElement.class);
572 manager.registerAdapters(new ResourceAdapterFactory(), IResource.class);
573 // externalTools.startUp();
574 getStandardDisplay().asyncExec(new Runnable() {
576 //initialize the variable context manager
577 VariableContextManager.getDefault();