001: package net.refractions.udig.tools.internal;
002:
003: import java.util.MissingResourceException;
004: import java.util.ResourceBundle;
005:
006: import org.eclipse.core.runtime.IStatus;
007: import org.eclipse.core.runtime.Platform;
008: import org.eclipse.core.runtime.Status;
009: import org.eclipse.ui.plugin.AbstractUIPlugin;
010: import org.osgi.framework.BundleContext;
011:
012: /**
013: * The main plugin class to be used in the desktop.
014: */
015: public class ToolsPlugin extends AbstractUIPlugin {
016: //The shared instance.
017: private static ToolsPlugin plugin;
018: //Resource bundle.
019: private ResourceBundle resourceBundle;
020:
021: static private final String ID = "net.refractions.udig.tools"; //$NON-NLS-1$
022:
023: /**
024: * The constructor.
025: */
026: public ToolsPlugin() {
027: super ();
028: plugin = this ;
029: try {
030: resourceBundle = ResourceBundle.getBundle(ID
031: + ".ToolsPluginResources"); //$NON-NLS-1$
032: } catch (MissingResourceException x) {
033: resourceBundle = null;
034: }
035: }
036:
037: /**
038: * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
039: */
040: public void start(BundleContext context) throws Exception {
041: super .start(context);
042: }
043:
044: /**
045: * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
046: */
047: public void stop(BundleContext context) throws Exception {
048: super .stop(context);
049: }
050:
051: /**
052: * Returns the shared instance.
053: * @return the default plugin object
054: */
055: public static ToolsPlugin getDefault() {
056: return plugin;
057: }
058:
059: /**
060: * Returns the string from the plugin's resource bundle,
061: * or 'key' if not found.
062: * @param key a key.
063: * @return the resource indicated by the key or "key" if not found.
064: */
065: public static String getResourceString(String key) {
066: ResourceBundle bundle = ToolsPlugin.getDefault()
067: .getResourceBundle();
068: try {
069: return (bundle != null) ? bundle.getString(key) : key;
070: } catch (MissingResourceException e) {
071: return key;
072: }
073: }
074:
075: /**
076: * Returns the plugin's resource bundle,
077: * @return The resource bundle
078: */
079: public ResourceBundle getResourceBundle() {
080: return resourceBundle;
081: }
082:
083: /**
084: * Writes an info log in the plugin's log.
085: * <p>
086: * This should be used for user level messages.
087: * </p>
088: * @param message
089: * @param e
090: */
091: public static void log(String message, Throwable e) {
092: if (message == null)
093: message = ""; //$NON-NLS-1$
094: getDefault().getLog().log(
095: new Status(IStatus.INFO, ID, 0, message, e));
096: }
097:
098: /**
099: * Messages that only engage if getDefault().isDebugging()
100: * <p>
101: * It is much prefered to do this:<pre><code>
102: * private static final String RENDERING = "net.refractions.udig.project/render/trace";
103: * if( ProjectUIPlugin.getDefault().isDebugging() && "true".equalsIgnoreCase( RENDERING ) ){
104: * System.out.println( "your message here" );
105: * }
106: * @param message
107: * @param e
108: */
109: public static void trace(String message, Throwable e) {
110: if (getDefault().isDebugging()) {
111: if (message != null)
112: System.out.println(message);
113: if (e != null)
114: e.printStackTrace();
115: }
116: }
117:
118: /**
119: * Performs the Platform.getDebugOption true check on the provided trace
120: * <p>
121: * Note: ProjectUIPlugin.getDefault().isDebugging() must also be on.
122: * <ul>
123: * <li>Trace.RENDER - trace rendering progress
124: * </ul>
125: * </p>
126: * @param trace currently only RENDER is defined
127: * @return true if this plugig is in your .options file
128: */
129: public static boolean isDebugging(final String trace) {
130: return getDefault().isDebugging()
131: && "true".equalsIgnoreCase(Platform.getDebugOption(trace)); //$NON-NLS-1$
132: }
133: }
|