Source code: com/port80/eclipse/jdt/JdtPlugin.java
1 package com.port80.eclipse.jdt;
2
3 import java.util.MissingResourceException;
4 import java.util.ResourceBundle;
5
6 import org.eclipse.core.internal.plugins.PluginDescriptor;
7 import org.eclipse.core.resources.IWorkspace;
8 import org.eclipse.core.resources.ResourcesPlugin;
9 import org.eclipse.core.runtime.CoreException;
10 import org.eclipse.core.runtime.IPluginDescriptor;
11 import org.eclipse.core.runtime.IStatus;
12 import org.eclipse.core.runtime.Status;
13 import org.eclipse.jdt.core.IJavaElement;
14 import org.eclipse.jdt.internal.ui.IJavaStatusConstants;
15 import org.eclipse.jface.action.IAction;
16 import org.eclipse.jface.preference.IPreferenceStore;
17 import org.eclipse.swt.dnd.Clipboard;
18 import org.eclipse.swt.graphics.Color;
19 import org.eclipse.swt.graphics.Cursor;
20 import org.eclipse.swt.graphics.Font;
21 import org.eclipse.swt.widgets.Shell;
22 import org.eclipse.ui.IMemento;
23 import org.eclipse.ui.IPartListener;
24 import org.eclipse.ui.IWorkbenchWindow;
25 import org.eclipse.ui.XMLMemento;
26 import org.eclipse.ui.plugin.AbstractUIPlugin;
27
28 import com.port80.eclipse.jdt.annotation.AnnotationManager;
29 import com.port80.eclipse.jdt.history.EditorHistory;
30
31 /**
32 * The main plugin class to be used in the desktop.
33 */
34 public class JdtPlugin extends AbstractUIPlugin {
35
36 // Static fields ///////////////////////////////////////////////////////
37
38 private static final String NAME = "JdtPlugin";
39 private static final String ID = "com.port80.eclipse.jdt.JdtPlugin";
40 private static final String TAG_ROOT = "JdtPlugin";
41 private static final String TAG_VERSION = "JdtPluginVersion";
42 private static final String[] VERSIONS = new String[] { "1.0" };
43
44 private static final boolean DEBUG = false;
45 private static final boolean TRACE = false;
46 private static final boolean VERBOSE = true;
47
48 public static final int RET_FAIL = -1;
49 public static final int RET_OK = 0;
50 public static final int RET_CANCEL = 1;
51 public static final int RET_RESET = 2;
52
53 private static JdtPlugin plugin;
54
55 // Instance fields ////////////////////////////////////////////////////
56
57 // Shared resources to be used by other packages for the plugin.
58 private ResourceBundle resourceBundle;
59 private String fPluginPath;
60 private Clipboard fClipboard;
61 private ThemeManager fThemeManager;
62 private AnnotationManager fAnnotationManager;
63 private IAction fForwardAction;
64 private IAction fBackwardAction;
65 private EditorHistory fEditorHistory;
66 private IPartListener fPartListener;
67
68 // Constructors ////////////////////////////////////////////////////////
69 //
70
71 public JdtPlugin(IPluginDescriptor descriptor) {
72 super(descriptor);
73 plugin = this;
74 try {
75 resourceBundle = ResourceBundle.getBundle("com.port80.eclipse.jdt.messages");
76 } catch (MissingResourceException x) {
77 resourceBundle = null;
78 }
79 fClipboard = new Clipboard(getWorkbench().getActiveWorkbenchWindow().getShell().getDisplay());
80 fThemeManager = new ThemeManager();
81 IPreferenceStore prefs = getPreferenceStore();
82 fEditorHistory = new EditorHistory(prefs);
83 restoreXMLState();
84 }
85
86 /**
87 * @see org.eclipse.ui.plugin.AbstractUIPlugin#initializeDefaultPreferences(IPreferenceStore)
88 */
89 protected void initializeDefaultPreferences(IPreferenceStore store) {
90 if (TRACE)
91 System.err.println(NAME + ".initializeDefaultPreferences(store)");
92 store.setDefault(
93 IConstants.PREF_METHODVIEW_SYNC_FROM_EDITOR,
94 IConstants.DEF_METHODVIEW_SYNC_FROM_EDITOR);
95 store.setDefault(IConstants.PREF_ANNOTATIONVIEW_INBOX_LIMIT, IConstants.DEF_ANNOTATIONVIEW_INBOX_LIMIT);
96 store.setDefault(IConstants.PREF_EDITOR_HISTORY_SIZE, IConstants.DEF_EDITOR_HISTORY_SIZE);
97 store.setDefault(IConstants.PREF_EDITOR_HISTORY_VISITED_SIZE, IConstants.DEF_EDITOR_HISTORY_VISITED_SIZE);
98 }
99
100 /**
101 * @see org.eclipse.core.runtime.Plugin#startup()
102 */
103 public void startup() throws CoreException {
104 super.startup();
105 fEditorHistory.startup();
106 }
107
108 /**
109 * @see org.eclipse.core.runtime.Plugin#shutdown()
110 */
111 public void shutdown() throws CoreException {
112 super.shutdown();
113 saveXMLState();
114 fEditorHistory.shutdown();
115 fEditorHistory = null;
116 fThemeManager.dispose();
117 fThemeManager = null;
118 }
119
120 public void enableEditorHistory() {
121 fEditorHistory.initActions();
122 }
123
124 ////////////////////////////////////////////////////////////////////////
125
126 /**
127 * Restore state from state file in metadata area.
128 */
129 public boolean restoreXMLState() {
130 if (DEBUG)
131 System.err.println(NAME + ".restoreXMLState()");
132 IMemento memento = Util.restoreXMLState(IConstants.JDTPLUGIN_STATE_FILENAME);
133 return restoreState(memento);
134 }
135
136 private boolean restoreState(IMemento memento) {
137 if (memento == null || !validVersion(memento))
138 return false;
139 // Restore the saved state
140 boolean ok = true;
141 IMemento editorhistory = memento.getChild("Visited");
142 if (editorhistory != null)
143 ok &= fEditorHistory.restoreState(editorhistory);
144 return ok;
145 }
146
147 ////////////////////////////////////////////////////////////////////////
148
149 /** Save Plugin state to state file in metadata area.*/
150 public boolean saveXMLState() {
151 if (DEBUG)
152 System.err.println(NAME + ".saveXMLState()");
153 XMLMemento memento = XMLMemento.createWriteRoot(ID);
154 memento.putString(TAG_VERSION, VERSIONS[0]);
155 saveState(memento);
156 return Util.saveXMLState(IConstants.JDTPLUGIN_STATE_FILENAME, memento);
157 }
158
159 private void saveState(IMemento memento) {
160 // Save navigate history visited set.
161 fEditorHistory.saveState(memento);
162 }
163
164 ////////////////////////////////////////////////////////////////////////
165
166 /** Validate Memento is in valid version format. */
167 boolean validVersion(IMemento memento) {
168 String version = memento.getString(TAG_VERSION);
169 for (int i = 0; i < VERSIONS.length; i++) {
170 if (VERSIONS[i].equals(version)) {
171 return true;
172 }
173 }
174 log(getString(IConstants.INVALID_VERSION) + ": version=" + version);
175 return false;
176 }
177
178 // Standard plugin methods /////////////////////////////////////////
179 //
180
181 /**
182 * Returns the shared instance.
183 */
184 public static JdtPlugin getDefault() {
185 return plugin;
186 }
187
188 public String getPluginPath() {
189 if (fPluginPath == null)
190 fPluginPath = ((PluginDescriptor) getDescriptor()).getInstallURLInternal().getFile();
191 return fPluginPath;
192 }
193
194 /**
195 * Returns the workspace instance.
196 */
197 public static IWorkspace getWorkspace() {
198 return ResourcesPlugin.getWorkspace();
199 }
200
201 /**
202 * Returns the plugin's resource bundle,
203 */
204 public ResourceBundle getResourceBundle() {
205 return resourceBundle;
206 }
207
208 /**
209 * Returns the string from the plugin's resource bundle,
210 * or 'key' if not found.
211 */
212 public static String getString(String key) {
213 ResourceBundle bundle = JdtPlugin.getDefault().getResourceBundle();
214 try {
215 return bundle.getString(key);
216 } catch (MissingResourceException e) {
217 return key;
218 }
219 }
220
221 public static String getPluginId() {
222 return getDefault().getDescriptor().getUniqueIdentifier();
223 }
224
225 public static IWorkbenchWindow getActiveWorkbenchWindow() {
226 return getDefault().getWorkbench().getActiveWorkbenchWindow();
227 }
228
229 public static Shell getActiveWorkbenchShell() {
230 return getActiveWorkbenchWindow().getShell();
231 }
232
233 // Some static utility methods /////////////////////////////////////////
234 //
235
236 public static EditorHistory getEditorHistory() {
237 return getDefault().fEditorHistory;
238 }
239
240 public static AnnotationManager getAnnotationManager() {
241 JdtPlugin plugin = getDefault();
242 if (plugin.fAnnotationManager == null)
243 plugin.fAnnotationManager = new AnnotationManager();
244 return plugin.fAnnotationManager;
245 }
246
247 public static Clipboard getClipboard() {
248 return getDefault().fClipboard;
249 }
250
251 // ThemeManager /////////////////////////////////////////////////////
252 //
253
254 public static ThemeManager getThemeManager() {
255 return getDefault().fThemeManager;
256 }
257
258 public static Color getColor(String name) {
259 return getDefault().fThemeManager.getColor(name);
260 }
261
262 public static Font getFont(String name) {
263 return getDefault().fThemeManager.getFont(name);
264 }
265
266 public static Cursor getCursor(String name) {
267 return getDefault().fThemeManager.getCursor(name);
268 }
269
270 // Messages ////////////////////////////////////////////////////////////
271 //
272
273 public static void log(IStatus status) {
274 getDefault().getLog().log(status);
275 }
276
277 public static void log(String message) {
278 log(message, null);
279 }
280
281 public static void log(String message, Exception e) {
282 //For 2.1.0
283 log(new Status(IStatus.ERROR, getPluginId(), IJavaStatusConstants.INTERNAL_ERROR, message, e));
284 //For 2.0.2
285 //log(new Status(IStatus.ERROR, getPluginId(), JavaStatusConstants.INTERNAL_ERROR, message, e));
286 }
287
288 public static void log(String prefix, IJavaElement element, Exception e) {
289 if (element != null)
290 prefix =
291 prefix + ": element=" + element.getElementType() + ", type=" + element.getElementType();
292 log(new Status(IStatus.ERROR, ID, 1, prefix, e));
293 }
294
295 /** @return A composed message with the given Java element.*/
296 public static String msg(String prefix, IJavaElement e) {
297 String ret = prefix;
298 String name = null;
299 int type = 0;
300 if (e != null) {
301 name = e.getElementName();
302 type = e.getElementType();
303 }
304 if (name != null)
305 return ret + ": element=" + name + ", type=" + type;
306 else
307 return ret;
308 }
309
310 /**
311 * @return A composited error message of form: "classname.method(): translated(classname.key)"
312 * @param classname The class name.
313 * @param method The method name.
314 * @param key The key to lookup the locale specific message.
315 */
316 public static final String getString(String classname, String method, String key) {
317 return classname + "." + method + "(): " + getString(classname + "." + key);
318 }
319
320 // IMemento helper ///////////////////////////////////////////////
321 //
322 public static int getIntDef(IMemento m, String key, int def) {
323 if (m == null)
324 return def;
325 if (key == null)
326 return def;
327 Integer n = m.getInteger(key);
328 if (n == null)
329 return def;
330 return n.intValue();
331 }
332
333 ////////////////////////////////////////////////////////////////////////
334
335 }