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