001: package org.tcat.citd.sim.udig.bookmarks;
002:
003: import java.util.Collection;
004: import java.util.HashMap;
005: import java.util.Vector;
006:
007: import net.refractions.udig.project.IMap;
008: import net.refractions.udig.project.internal.Project;
009:
010: import org.eclipse.emf.common.util.URI;
011: import org.tcat.citd.sim.udig.bookmarks.internal.MapReference;
012: import org.tcat.citd.sim.udig.bookmarks.internal.Messages;
013:
014: /**
015: * This class provides a bookmark repository and associated management functions.
016: * <p>
017: * </p>
018: *
019: * @author cole.markham
020: * @since 1.0.0
021: */
022: public class BookmarkManager {
023: private HashMap<URI, Vector<MapReference>> projectsHash;
024: private HashMap<URI, Vector<Bookmark>> mapsHash;
025: private HashMap<URI, MapReference> mapReferences;
026: private int count = 0;
027:
028: /**
029: *
030: */
031: public BookmarkManager() {
032: projectsHash = new HashMap<URI, Vector<MapReference>>();
033: mapsHash = new HashMap<URI, Vector<Bookmark>>();
034: mapReferences = new HashMap<URI, MapReference>();
035: }
036:
037: /**
038: * Add the given bookmark.
039: *
040: * @param bookmark
041: */
042: public void addBookmark(Bookmark bookmark) {
043: if (bookmark.getName() == null || bookmark.getName() == "") { //$NON-NLS-1$
044: bookmark
045: .setName(Messages.BookmarkManager_bookmarkdefaultname
046: + (++count));
047: }
048: MapReference map = bookmark.getMap();
049: URI project = map.getProjectID();
050: if (!projectsHash.containsKey(project)) {
051: Vector<MapReference> projectMaps = new Vector<MapReference>();
052: projectMaps.add(map);
053: projectsHash.put(project, projectMaps);
054: } else {
055: Vector<MapReference> projectMaps = projectsHash
056: .get(project);
057: if (!projectMaps.contains(map)) {
058: projectMaps.add(map);
059: }
060: }
061: if (!mapsHash.containsKey(map.getMapID())) {
062: Vector<Bookmark> bookmarks = new Vector<Bookmark>();
063: bookmarks.add(bookmark);
064: mapsHash.put(map.getMapID(), bookmarks);
065: } else {
066: Vector<Bookmark> bmarks = mapsHash.get(map.getMapID());
067: if (!bmarks.contains(bookmark)) {
068: bmarks.add(bookmark);
069: }
070: }
071: }
072:
073: /**
074: * Empties the list of bookmarks
075: */
076: public void empty() {
077: projectsHash.clear();
078: mapsHash.clear();
079: }
080:
081: /**
082: * Returns whether the list is empty
083: *
084: * @return whether this list is empty
085: */
086: public boolean isEmpty() {
087: boolean isEmpty = mapsHash.isEmpty();
088: return isEmpty;
089: }
090:
091: /**
092: * Returns the list of projects as an array of objects
093: *
094: * @return array of IProject objects
095: */
096: public Collection<URI> getProjects() {
097: Vector<URI> projects = new Vector<URI>(this .projectsHash
098: .keySet());
099: return projects;
100: }
101:
102: /**
103: * Returns the list of maps which are contained in the specified project
104: *
105: * @param project The project for which the maps will be returned
106: * @return array of MapReference objects
107: */
108: public Collection<MapReference> getMaps(URI project) {
109: Vector<MapReference> maps = projectsHash.get(project);
110: return maps;
111: }
112:
113: /**
114: * Return the list of bookmarks associated with the specified map
115: *
116: * @param map The map for which the bookmarks will be returned
117: * @return A vector of Bookmark objects
118: */
119: public Collection<Bookmark> getBookmarks(MapReference map) {
120: if (!mapsHash.containsKey(map.getMapID())) {
121: mapsHash.put(map.getMapID(), new Vector<Bookmark>());
122: }
123: Vector<Bookmark> bookmarks = mapsHash.get(map.getMapID());
124: return bookmarks;
125: }
126:
127: /**
128: * Get the name of this bookmark manager for display It's just a static string for now
129: *
130: * @return the name
131: */
132: public String getName() {
133: return Messages.BookmarkManager_name_bookmarkmanager;
134: }
135:
136: @Override
137: public String toString() {
138: return this .getName();
139: }
140:
141: /**
142: * Remove the given bookmark.
143: *
144: * @param bookmark
145: */
146: public void removeBookmark(Bookmark bookmark) {
147: MapReference map = bookmark.getMap();
148: mapsHash.get(map.getMapID()).remove(bookmark);
149: if (mapsHash.get(map.getMapID()).isEmpty()) {
150: URI projectID = map.getProjectID();
151: mapsHash.remove(map.getMapID());
152: projectsHash.get(projectID).remove(map);
153: if (projectsHash.get(projectID).isEmpty()) {
154: projectsHash.remove(projectID);
155: }
156: }
157: }
158:
159: /**
160: * Remove all of the bookmarks in the given list.
161: *
162: * @param elements
163: */
164: public void removeBookmarks(Collection elements) {
165: for (Object element : elements) {
166: if (element instanceof Bookmark) {
167: Bookmark bmark = (Bookmark) element;
168: this .removeBookmark(bmark);
169: }
170: }
171: }
172:
173: /**
174: * Remove the map and all it's associated bookmarks
175: *
176: * @param map
177: */
178: public void removeMap(MapReference map) {
179: mapsHash.remove(map.getMapID());
180: URI projectID = map.getProjectID();
181: Vector<MapReference> maps = projectsHash.get(projectID);
182: if (maps != null && maps.size() > 0) {
183: maps.remove(map);
184: if (maps.isEmpty()) {
185: projectsHash.remove(projectID);
186: }
187: }
188: }
189:
190: /**
191: * Remove all of the maps in the given list and their associated bookmarks.
192: *
193: * @param elements
194: */
195: public void removeMaps(Collection elements) {
196: for (Object element : elements) {
197: if (element instanceof MapReference) {
198: MapReference map = (MapReference) element;
199: this .removeMap(map);
200: }
201: }
202: }
203:
204: /**
205: * Remove the project and all it's associated maps and bookmarks
206: *
207: * @param project
208: */
209: public void removeProject(URI project) {
210: Vector<MapReference> maps = projectsHash.get(project);
211: projectsHash.remove(project);
212: for (MapReference map : maps) {
213: maps.remove(map);
214: }
215: }
216:
217: /**
218: * Remove all of the projects in the given list and their associated maps and bookmarks.
219: *
220: * @param elements
221: */
222: public void removeProjects(Collection elements) {
223: for (Object element : elements) {
224: if (element instanceof URI) {
225: URI project = (URI) element;
226: this .removeProject(project);
227: }
228: }
229: }
230:
231: /**
232: * @param map
233: * @return the MapReference singleton for the given IMap
234: */
235: public MapReference getMapReference(IMap map) {
236: MapReference ref = null;
237: if (!mapReferences.containsKey(map.getID())) {
238: // HACK: fix this when IProject has a getID() method
239: Project project = (Project) map.getProject();
240: URI projectURI = project.eResource().getURI();
241: ref = new MapReference(map.getID(), projectURI, map
242: .getName());
243: mapReferences.put(map.getID(), ref);
244: } else {
245: ref = mapReferences.get(map.getID());
246: }
247: return ref;
248: }
249: }
|