811948eaf5293f715160ce63b6c604ed66a5c043
[phpeclipse.git] / net.sourceforge.phpeclipse.externaltools / src / net / sourceforge / phpeclipse / externaltools / ExternalToolsPlugin.java
1 package net.sourceforge.phpeclipse.externaltools;
2
3 /**********************************************************************
4  Copyright (c) 2002 IBM Corp. and others. All rights reserved.
5  This file is made available under the terms of the Common Public License v1.0
6  which accompanies this distribution, and is available at
7  http://www.eclipse.org/legal/cpl-v10.html
8  �
9  Contributors:
10  **********************************************************************/
11
12 import java.net.MalformedURLException;
13 import java.net.URL;
14
15 import net.sourceforge.phpdt.externaltools.internal.model.ColorManager;
16 import net.sourceforge.phpdt.externaltools.internal.model.ExternalToolsImages;
17 import net.sourceforge.phpdt.externaltools.internal.model.IPreferenceConstants;
18 import net.sourceforge.phpdt.externaltools.internal.model.VariableContextManager;
19 import net.sourceforge.phpdt.externaltools.internal.registry.ArgumentVariableRegistry;
20 import net.sourceforge.phpdt.externaltools.internal.registry.PathLocationVariableRegistry;
21 import net.sourceforge.phpdt.externaltools.internal.registry.RefreshScopeVariableRegistry;
22 import net.sourceforge.phpdt.externaltools.model.IExternalToolConstants;
23 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
24
25 import org.eclipse.core.runtime.CoreException;
26 import org.eclipse.core.runtime.IStatus;
27 import org.eclipse.core.runtime.Status;
28 import org.eclipse.jface.preference.IPreferenceStore;
29 import org.eclipse.jface.preference.PreferenceConverter;
30 import org.eclipse.jface.resource.ImageDescriptor;
31 import org.eclipse.jface.resource.ImageRegistry;
32 import org.eclipse.swt.graphics.Color;
33 import org.eclipse.swt.graphics.RGB;
34 import org.eclipse.swt.widgets.Display;
35 import org.eclipse.ui.IWorkbenchWindow;
36 import org.eclipse.ui.plugin.AbstractUIPlugin;
37 import org.osgi.framework.Bundle;
38
39 /**
40  * External tools plug-in class
41  */
42 public final class ExternalToolsPlugin extends AbstractUIPlugin {
43   /**
44    * Status representing no problems encountered during operation.
45    */
46   public static final IStatus OK_STATUS = new Status(IStatus.OK, IExternalToolConstants.PLUGIN_ID, 0, "", null); //$NON-NLS-1$
47
48   private static ExternalToolsPlugin plugin;
49
50   private RefreshScopeVariableRegistry refreshVarRegistry;
51
52   private PathLocationVariableRegistry fileLocVarRegistry;
53
54   private PathLocationVariableRegistry dirLocVarRegistry;
55
56   private ArgumentVariableRegistry argumentVarRegistry;
57
58   /**
59    * Create an instance of the External Tools plug-in.
60    */
61   public ExternalToolsPlugin() { // IPluginDescriptor descriptor) {
62     // super(descriptor);
63     plugin = this;
64   }
65
66   /**
67    * Returns the default instance of the receiver. This represents the runtime plugin.
68    */
69   public static ExternalToolsPlugin getDefault() {
70     return plugin;
71   }
72
73   /**
74    * Returns a new <code>IStatus</code> for this plug-in
75    */
76   public static IStatus newErrorStatus(String message, Throwable exception) {
77     return new Status(Status.ERROR, IExternalToolConstants.PLUGIN_ID, 0, message, exception);
78   }
79
80   /**
81    * Returns a new <code>CoreException</code> for this plug-in
82    */
83   public static CoreException newError(String message, Throwable exception) {
84     return new CoreException(new Status(Status.ERROR, IExternalToolConstants.PLUGIN_ID, 0, message, exception));
85   }
86
87   /**
88    * Returns the registry of refresh scope variables.
89    */
90   public ArgumentVariableRegistry getArgumentVariableRegistry() {
91     if (argumentVarRegistry == null)
92       argumentVarRegistry = new ArgumentVariableRegistry();
93     return argumentVarRegistry;
94   }
95
96   /**
97    * Returns the registry of directory location variables.
98    */
99   public PathLocationVariableRegistry getDirectoryLocationVariableRegistry() {
100     if (dirLocVarRegistry == null)
101       dirLocVarRegistry = new PathLocationVariableRegistry(IExternalToolConstants.EXTENSION_POINT_DIRECTORY_VARIABLES);
102     return dirLocVarRegistry;
103   }
104
105   /**
106    * Returns the registry of file location variables.
107    */
108   public PathLocationVariableRegistry getFileLocationVariableRegistry() {
109     if (fileLocVarRegistry == null)
110       fileLocVarRegistry = new PathLocationVariableRegistry(IExternalToolConstants.EXTENSION_POINT_FILE_VARIABLES);
111     return fileLocVarRegistry;
112   }
113
114   /**
115    * Returns the registry of refresh scope variables.
116    */
117   public RefreshScopeVariableRegistry getRefreshVariableRegistry() {
118     if (refreshVarRegistry == null)
119       refreshVarRegistry = new RefreshScopeVariableRegistry();
120     return refreshVarRegistry;
121   }
122
123   /**
124    * Writes the message to the plug-in's log
125    * 
126    * @param message
127    *          the text to write to the log
128    */
129   public void log(String message, Throwable exception) {
130     IStatus status = newErrorStatus(message, exception);
131     //  getLog().log(status);
132     PHPeclipsePlugin.log(status);
133   }
134
135   /**
136    * Returns the ImageDescriptor for the icon with the given path
137    * 
138    * @return the ImageDescriptor object
139    */
140   public ImageDescriptor getImageDescriptor(String path) {
141     try {
142       Bundle bundle = PHPeclipsePlugin.getDefault().getBundle();
143       URL installURL = bundle.getEntry("/"); //$NON-NLS-1$
144       URL url = new URL(installURL, path);
145       return ImageDescriptor.createFromURL(url);
146     } catch (MalformedURLException e) {
147       return null;
148     }
149   }
150
151   /*
152    * (non-Javadoc) Method declared in AbstractUIPlugin.
153    */
154   public void initializeDefaultPreferences(IPreferenceStore prefs) {
155     prefs.setDefault(IPreferenceConstants.PROMPT_FOR_MIGRATION, true);
156
157     PreferenceConverter.setDefault(prefs, IPreferenceConstants.CONSOLE_ERROR_RGB, new RGB(255, 0, 0)); // red - exactly the same as
158                                                                                                        // debug Consol
159     PreferenceConverter.setDefault(prefs, IPreferenceConstants.CONSOLE_WARNING_RGB, new RGB(255, 100, 0)); // orange
160     PreferenceConverter.setDefault(prefs, IPreferenceConstants.CONSOLE_INFO_RGB, new RGB(0, 0, 255)); // blue
161     PreferenceConverter.setDefault(prefs, IPreferenceConstants.CONSOLE_VERBOSE_RGB, new RGB(0, 200, 125)); // green
162     PreferenceConverter.setDefault(prefs, IPreferenceConstants.CONSOLE_DEBUG_RGB, new RGB(0, 0, 0)); // black
163   }
164
165   public static IWorkbenchWindow getActiveWorkbenchWindow() {
166     return PHPeclipsePlugin.getDefault().getWorkbench().getActiveWorkbenchWindow();
167   }
168
169   /**
170    * Returns the standard display to be used. The method first checks, if the thread calling this method has an associated display.
171    * If so, this display is returned. Otherwise the method returns the default display.
172    */
173   public static Display getStandardDisplay() {
174     Display display = Display.getCurrent();
175     if (display == null) {
176       display = Display.getDefault();
177     }
178     return display;
179   }
180
181   /**
182    * @see org.eclipse.ui.plugin.AbstractUIPlugin#createImageRegistry()
183    */
184   protected ImageRegistry createImageRegistry() {
185     return ExternalToolsImages.initializeImageRegistry();
186   }
187
188   /**
189    * @see org.eclipse.core.runtime.Plugin#startup()
190    */
191   public void startup() throws CoreException {
192 //    super.startup();
193     getStandardDisplay().asyncExec(new Runnable() {
194       public void run() {
195         //initialize the variable context manager
196         VariableContextManager.getDefault();
197       }
198     });
199   }
200
201   /**
202    * @see org.eclipse.core.runtime.Plugin#shutdown()
203    */
204   public void shutdown() throws CoreException {
205 //      super.shutdown();
206     ColorManager.getDefault().dispose();
207   }
208
209   /**
210    * Returns the preference color, identified by the given preference.
211    */
212   public static Color getPreferenceColor(String pref) {
213     return ColorManager.getDefault().getColor(
214         PreferenceConverter.getColor(PHPeclipsePlugin.getDefault().getPreferenceStore(), pref));
215   }
216 }