001: package net.refractions.udig.catalog.internal.shp;
002:
003: import java.util.MissingResourceException;
004: import java.util.ResourceBundle;
005: import java.util.logging.Handler;
006: import java.util.logging.Level;
007: import java.util.logging.LogRecord;
008: import java.util.logging.Logger;
009:
010: import net.refractions.udig.catalog.shp.preferences.PreferenceConstants;
011:
012: import org.eclipse.core.runtime.IStatus;
013: import org.eclipse.core.runtime.Platform;
014: import org.eclipse.core.runtime.Status;
015: import org.eclipse.ui.plugin.AbstractUIPlugin;
016: import org.geotools.data.shapefile.ShapefileDataStore;
017: import org.osgi.framework.BundleContext;
018:
019: /**
020: * The main plugin class to be used in the desktop.
021: */
022: public class ShpPlugin extends AbstractUIPlugin {
023: //The shared instance.
024: private static ShpPlugin plugin;
025: //Resource bundle.
026: private ResourceBundle resourceBundle;
027:
028: public static final String SHP_TRACE = "net.refractions.udig.catalog.shp/debug/trace"; //$NON-NLS-1$
029:
030: public static final String ID = "net.refractions.udig.catalog.shp"; //$NON-NLS-1$
031:
032: /**
033: * The constructor.
034: */
035: public ShpPlugin() {
036: super ();
037: plugin = this ;
038: }
039:
040: /**
041: * This method is called upon plug-in activation
042: */
043: public void start(BundleContext context) throws Exception {
044: super .start(context);
045: ClassLoader current = getClass().getClassLoader();
046: try {
047: Thread.currentThread().setContextClassLoader(
048: ShapefileDataStore.class.getClassLoader());
049: Logger logger = Logger
050: .getLogger("org.geotools.data.shapefile");//$NON-NLS-1$
051: if (ShpPlugin.isDebugging(SHP_TRACE)) {
052: logger.setLevel(Level.FINEST);
053: logger.addHandler(new Handler() {
054:
055: @Override
056: public void publish(LogRecord record) {
057: System.err.println(record.getMessage());
058: }
059:
060: @Override
061: public void flush() {
062: System.err.flush();
063: }
064:
065: @Override
066: public void close() throws SecurityException {
067:
068: }
069:
070: });
071: } else {
072: logger.setLevel(Level.SEVERE);
073: }
074: } finally {
075: Thread.currentThread().setContextClassLoader(current);
076: }
077: }
078:
079: /**
080: * This method is called when the plug-in is stopped
081: */
082: public void stop(BundleContext context) throws Exception {
083: plugin = null;
084: resourceBundle = null;
085: super .stop(context);
086: }
087:
088: /**
089: * Returns the shared instance.
090: * @return x
091: */
092: public static ShpPlugin getDefault() {
093: return plugin;
094: }
095:
096: /**
097: * Returns the string from the plugin's resource bundle,
098: * or 'key' if not found.
099: * @param key x
100: * @return x
101: */
102: public static String getResourceString(String key) {
103: ResourceBundle bundle = ShpPlugin.getDefault()
104: .getResourceBundle();
105: try {
106: return (bundle != null) ? bundle.getString(key) : key;
107: } catch (MissingResourceException e) {
108: return key;
109: }
110: }
111:
112: /**
113: * Returns the plugin's resource bundle,
114: * @return x
115: */
116: public ResourceBundle getResourceBundle() {
117: try {
118: if (resourceBundle == null)
119: resourceBundle = ResourceBundle
120: .getBundle("net.refractions.udig.catalog.internal.shp.ShpPluginResources"); //$NON-NLS-1$
121: } catch (MissingResourceException x) {
122: resourceBundle = null;
123: }
124: return resourceBundle;
125: }
126:
127: public static void log(String message, Throwable t) {
128: int status = t instanceof Exception || message != null ? IStatus.ERROR
129: : IStatus.WARNING;
130: getDefault().getLog().log(
131: new Status(status, ID, IStatus.OK, message, t));
132: }
133:
134: /**
135: * Messages that only engage if getDefault().isDebugging()
136: * <p>
137: * It is much prefered to do this:
138: *
139: * <pre><code>
140: * private static final String RENDERING = "net.refractions.udig.project/render/trace";
141: * if (ProjectUIPlugin.getDefault().isDebugging()
142: * && "true".equalsIgnoreCase(RENDERING)) {
143: * System.out.println("your message here");
144: * }
145: *
146: */
147: public static void trace(String message, Throwable e) {
148: if (getDefault().isDebugging()) {
149: if (message != null)
150: System.out.println(message);
151: if (e != null)
152: e.printStackTrace();
153: }
154: }
155:
156: /**
157: * Performs the Platform.getDebugOption true check on the provided trace
158: * <p>
159: * Note: ProjectUIPlugin.getDefault().isDebugging() must also be on.
160: * <ul>
161: * <li>Trace.RENDER - trace rendering progress
162: * </ul>
163: * </p>
164: *
165: * @param trace
166: * currently only RENDER is defined
167: */
168: public static boolean isDebugging(final String trace) {
169: return getDefault().isDebugging()
170: && "true".equalsIgnoreCase(Platform.getDebugOption(trace)); //$NON-NLS-1$
171: }
172:
173: /**
174: * Returns true if spatial indexes will be created by default.
175: */
176: public boolean isUseSpatialIndex() {
177: return getPreferenceStore().getBoolean(
178: PreferenceConstants.P_CREATE_INDEX);
179: }
180:
181: /**
182: * Sets the default behaviour of creating spatial indices for shapefiles.
183: *
184: * @param useSpatialIndex
185: */
186: public void setUseSpatialIndex(boolean useSpatialIndex) {
187: getPreferenceStore().setValue(
188: PreferenceConstants.P_CREATE_INDEX, useSpatialIndex);
189: }
190:
191: }
|