Source code: com/port80/swt/SwtPlugin.java
1 package com.port80.swt;
2
3 import java.util.MissingResourceException;
4 import java.util.ResourceBundle;
5
6 import org.eclipse.core.resources.IWorkspace;
7 import org.eclipse.core.resources.ResourcesPlugin;
8 import org.eclipse.core.runtime.IPluginDescriptor;
9 import org.eclipse.core.runtime.IStatus;
10 import org.eclipse.core.runtime.Status;
11 import org.eclipse.ui.plugin.AbstractUIPlugin;
12
13 /**
14 * The main plugin class to be used in the desktop.
15 */
16 public class SwtPlugin extends AbstractUIPlugin {
17
18 //The shared instance.
19 private static SwtPlugin plugin;
20
21 private ResourceBundle resourceBundle;
22
23 /**
24 * The constructor.
25 */
26 public SwtPlugin(IPluginDescriptor descriptor) {
27 super(descriptor);
28 plugin = this;
29 try {
30 resourceBundle = ResourceBundle.getBundle("com.port80.swt.messages");
31 } catch (MissingResourceException x) {
32 resourceBundle = null;
33 }
34 // This is required to make sure preference store is loaded and initialized.
35 getPreferenceStore();
36 savePluginPreferences();
37 }
38
39 /**
40 * Returns the shared instance.
41 */
42 public static SwtPlugin getDefault() {
43 return plugin;
44 }
45
46 /**
47 * Returns the workspace instance.
48 */
49 public static IWorkspace getWorkspace() {
50 return ResourcesPlugin.getWorkspace();
51 }
52
53 /**
54 * Returns the string from the plugin's resource bundle,
55 * or 'key' if not found.
56 */
57 public static String getResourceString(String key) {
58 ResourceBundle bundle = SwtPlugin.getDefault().getResourceBundle();
59 try {
60 return bundle.getString(key);
61 } catch (MissingResourceException e) {
62 return key;
63 }
64 }
65
66 /**
67 * Returns the plugin's resource bundle,
68 */
69 public ResourceBundle getResourceBundle() {
70 return resourceBundle;
71 }
72
73 // Messages ////////////////////////////////////////////////////////////
74 //
75
76 public static void log(IStatus status) {
77 getDefault().getLog().log(status);
78 }
79
80 public static void log(String message) {
81 log(message, 1, null);
82 }
83
84 public static void log(String message, Exception e) {
85 log(message, 1, e);
86 }
87
88 public static void log(String message, int code, Exception e) {
89 log(new Status(IStatus.ERROR, getDefault().getDescriptor().getUniqueIdentifier(), code, message, e));
90 }
91
92 ////////////////////////////////////////////////////////////////////////
93 }