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