001: package org.tcat.citd.sim.udig.bookmarks;
002:
003: import org.eclipse.core.runtime.Platform;
004: import org.eclipse.core.runtime.preferences.IEclipsePreferences;
005: import org.eclipse.core.runtime.preferences.IPreferencesService;
006: import org.eclipse.core.runtime.preferences.InstanceScope;
007: import org.eclipse.emf.common.util.URI;
008: import org.eclipse.jface.resource.ImageDescriptor;
009: import org.eclipse.ui.plugin.AbstractUIPlugin;
010: import org.geotools.geometry.jts.ReferencedEnvelope;
011: import org.geotools.referencing.CRS;
012: import org.geotools.referencing.crs.DefaultGeographicCRS;
013: import org.opengis.referencing.NoSuchAuthorityCodeException;
014: import org.opengis.referencing.crs.CoordinateReferenceSystem;
015: import org.osgi.framework.BundleContext;
016: import org.osgi.service.prefs.BackingStoreException;
017: import org.osgi.service.prefs.Preferences;
018: import org.tcat.citd.sim.udig.bookmarks.internal.MapReference;
019:
020: import com.vividsolutions.jts.geom.Envelope;
021:
022: /**
023: * The main plugin class to be used in the desktop.
024: */
025: public class BookmarksPlugin extends AbstractUIPlugin {
026:
027: private static final String KEY_NAME = "name"; //$NON-NLS-1$
028: private static final String KEY_MINX = "minx"; //$NON-NLS-1$
029: private static final String KEY_MINY = "miny"; //$NON-NLS-1$
030: private static final String KEY_MAXX = "maxx"; //$NON-NLS-1$
031: private static final String KEY_MAXY = "maxy"; //$NON-NLS-1$
032: private static final String KEY_CRS = "crs"; //$NON-NLS-1$
033:
034: // The shared instance.
035: private static BookmarksPlugin plugin;
036:
037: private BookmarkManager bmManager;
038:
039: /**
040: * The constructor.
041: */
042: public BookmarksPlugin() {
043: plugin = this ;
044: }
045:
046: /**
047: * This method is called upon plug-in activation
048: */
049: @Override
050: public void start(BundleContext context) throws Exception {
051: super .start(context);
052: restoreFromPreferences();
053: }
054:
055: /**
056: * This method is called when the plug-in is stopped
057: */
058: @Override
059: public void stop(BundleContext context) throws Exception {
060: storeToPreferences();
061: super .stop(context);
062: plugin = null;
063: }
064:
065: /**
066: * Returns the shared instance.
067: *
068: * @return The instance of this plugin
069: */
070: public static BookmarksPlugin getDefault() {
071: return plugin;
072: }
073:
074: /**
075: * Returns an image descriptor for the image file at the given plug-in relative path.
076: *
077: * @param path the path
078: * @return the image descriptor
079: */
080: public ImageDescriptor getImageDescriptor(String path) {
081: return AbstractUIPlugin.imageDescriptorFromPlugin(plugin
082: .getBundle().getSymbolicName(), path);
083: }
084:
085: /**
086: * Returns the bookmark manager for this plug-in.
087: *
088: * @return the bookmark manager
089: */
090: public BookmarkManager getBookmarkManager() {
091: if (bmManager == null) {
092: bmManager = new BookmarkManager();
093: }
094: return bmManager;
095: }
096:
097: /**
098: * Restore the bookmarks from the plugin's preference store
099: * @throws BackingStoreException
100: */
101: public void restoreFromPreferences() throws BackingStoreException {
102: IPreferencesService prefs = Platform.getPreferencesService();
103: IEclipsePreferences root = prefs.getRootNode();
104: Preferences node = root.node(InstanceScope.SCOPE).node(
105: getBundle().getSymbolicName() + ".bookmarks"); //$NON-NLS-1$
106:
107: for (String projectId : node.childrenNames()) {
108: URI projectURI = URI.createURI(URI.decode(projectId));
109: Preferences projectNode = node.node(projectId);
110: for (String mapId : projectNode.childrenNames()) {
111: URI mapURI = URI.createURI(URI.decode(mapId));
112: Preferences mapNode = projectNode.node(mapId);
113: String mapName = mapNode.get(KEY_NAME, null);
114: for (String bmarkName : mapNode.childrenNames()) {
115: Preferences bmarkNode = mapNode.node(bmarkName);
116: double minx = bmarkNode.getDouble(KEY_MINX, 0.0);
117: double miny = bmarkNode.getDouble(KEY_MINY, 0.0);
118: double maxx = bmarkNode.getDouble(KEY_MAXX, 0.0);
119: double maxy = bmarkNode.getDouble(KEY_MAXY, 0.0);
120: Envelope env = new Envelope(minx, maxx, miny, maxy);
121: CoordinateReferenceSystem crs;
122: String crsString = bmarkNode.get(KEY_CRS, ""); //$NON-NLS-1$
123: try {
124: crs = CRS.decode(crsString);
125: } catch (NoSuchAuthorityCodeException e) {
126: // TODO Handle NoSuchAuthorityCodeException
127: crs = DefaultGeographicCRS.WGS84;
128: }
129: ReferencedEnvelope bounds = new ReferencedEnvelope(
130: env, crs);
131: Bookmark bmark = new Bookmark(bounds,
132: new MapReference(mapURI, projectURI,
133: mapName), URI.decode(bmarkName));
134: getBookmarkManager().addBookmark(bmark);
135: }
136: }
137: }
138:
139: }
140:
141: /**
142: * Stores the bookmarks to the plugin's preference store
143: *
144: * @throws BackingStoreException
145: */
146: public void storeToPreferences() throws BackingStoreException {
147: IPreferencesService prefs = Platform.getPreferencesService();
148: IEclipsePreferences root = prefs.getRootNode();
149: Preferences node = root.node(InstanceScope.SCOPE).node(
150: getBundle().getSymbolicName() + ".bookmarks"); //$NON-NLS-1$
151: clearPreferences(node);
152: BookmarkManager mgr = getBookmarkManager();
153: for (URI project : mgr.getProjects()) {
154: String projectString = project.toString();
155: String encPStr = URI.encodeSegment(projectString, true);
156: Preferences projectNode = node.node(encPStr);
157: for (MapReference map : mgr.getMaps(project)) {
158: Preferences mapNode = projectNode
159: .node(URI.encodeSegment(map.getMapID()
160: .toString(), true));
161: mapNode.put(KEY_NAME, map.getName());
162: for (Bookmark bookmark : mgr.getBookmarks(map)) {
163: Preferences bmarkNode = mapNode.node(URI
164: .encodeSegment(bookmark.getName(), true));
165: ReferencedEnvelope bounds = bookmark.getEnvelope();
166: bmarkNode.putDouble(KEY_MINX, bounds.getMinX());
167: bmarkNode.putDouble(KEY_MINY, bounds.getMinY());
168: bmarkNode.putDouble(KEY_MAXX, bounds.getMaxX());
169: bmarkNode.putDouble(KEY_MAXY, bounds.getMaxY());
170: bmarkNode.put(KEY_CRS, bounds
171: .getCoordinateReferenceSystem().toWKT());
172: }
173: mapNode.flush();
174: }
175: projectNode.flush();
176: }
177: node.flush();
178: }
179:
180: private void clearPreferences(Preferences node)
181: throws BackingStoreException {
182: for (String name : node.childrenNames()) {
183: Preferences child = node.node(name);
184: child.removeNode();
185: }
186: }
187: }
|