001: package net.refractions.udig.printing.model;
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.Plugin;
009: import org.eclipse.core.runtime.Status;
010: import org.osgi.framework.BundleContext;
011:
012: /**
013: * The main plugin class to be used in the desktop.
014: */
015: public class PrintingModelPlugin extends Plugin {
016: //The shared instance.
017: private static PrintingModelPlugin plugin;
018: //Resource bundle.
019: private ResourceBundle resourceBundle;
020: public static final String BOX_PRINTER_EXTENSION_ID = "net.refractions.udig.printing.ui.boxprinter"; //$NON-NLS-1$
021:
022: /**
023: * Plugin ID
024: */
025: public static final String ID = "net.refractions.udig.printing.model"; //$NON-NLS-1$
026:
027: /**
028: * The constructor.
029: */
030: public PrintingModelPlugin() {
031: super ();
032: plugin = this ;
033: }
034:
035: /**
036: * This method is called upon plug-in activation
037: */
038: public void start(BundleContext context) throws Exception {
039: super .start(context);
040: }
041:
042: /**
043: * This method is called when the plug-in is stopped
044: */
045: public void stop(BundleContext context) throws Exception {
046: resourceBundle = null;
047: plugin = null;
048: super .stop(context);
049: }
050:
051: /**
052: * Returns the shared instance.
053: * @return the PrintingModelPlugin instance
054: */
055: public static PrintingModelPlugin getDefault() {
056: return plugin;
057: }
058:
059: /**
060: * @param key string used to look up the resource
061: * @return the string from the plugin's resource bundle, or 'key' if not found.
062: */
063: public static String getResourceString(String key) {
064: ResourceBundle bundle = PrintingModelPlugin.getDefault()
065: .getResourceBundle();
066: try {
067: return (bundle != null) ? bundle.getString(key) : key;
068: } catch (MissingResourceException e) {
069: return key;
070: }
071: }
072:
073: /**
074: * Returns the plugin's resource bundle
075: * @return Returns the plugin's resource bundle
076: */
077: public ResourceBundle getResourceBundle() {
078: try {
079: if (resourceBundle == null)
080: resourceBundle = ResourceBundle
081: .getBundle("net.refractions.udig.printing.model.PrintingModelPluginResources"); //$NON-NLS-1$
082: } catch (MissingResourceException x) {
083: resourceBundle = null;
084: }
085: return resourceBundle;
086: }
087:
088: /**
089: * Logs the Throwable in the plugin's log.
090: * <p>
091: * This will be a user visable ERROR iff:
092: * <ul>
093: * <li>t is an Exception we are assuming it is human readable or if a message is provided
094: * </ul>
095: * </p>
096: * @param message
097: * @param t
098: */
099: public static void log(String message, Throwable t) {
100: if (message == null)
101: message = ""; //$NON-NLS-1$
102: int status = t instanceof Exception || message != null ? IStatus.ERROR
103: : IStatus.WARNING;
104: getDefault().getLog().log(
105: new Status(status, ID, IStatus.OK, message, t));
106: }
107:
108: /**
109: * Messages that only engage if getDefault().isDebugging()
110: * <p>
111: * It is much prefered to do this:<pre><code>
112: * private static final String RENDERING = "net.refractions.udig.project/render/trace";
113: * if( ProjectUIPlugin.getDefault().isDebugging() && "true".equalsIgnoreCase( RENDERING ) ){
114: * System.out.println( "your message here" );
115: * }
116: * </code></pre>
117: * </p>
118: * @param message
119: * @param e
120: */
121: public static void trace(String message, Throwable e) {
122: if (getDefault().isDebugging()) {
123: if (message != null)
124: System.out
125: .println("[[PrintingModelPlugin-trace]]:: " + message); //$NON-NLS-1$
126: if (e != null)
127: e.printStackTrace();
128: }
129: }
130:
131: public static void trace(String message) {
132: trace(message, null);
133: }
134:
135: /**
136: * Performs the Platform.getDebugOption true check on the provided trace
137: * <p>
138: * Note: ProjectUIPlugin.getDefault().isDebugging() must also be on.
139: * <ul>
140: * <li>Trace.RENDER - trace rendering progress
141: * </ul>
142: * </p>
143: * @param trace currently only RENDER is defined
144: * @return true if -debug is on for this plugin
145: */
146: public static boolean isDebugging(final String trace) {
147: return getDefault().isDebugging()
148: && "true".equalsIgnoreCase(Platform.getDebugOption(trace)); //$NON-NLS-1$
149: }
150: }
|