001: package org.tcat.citd.sim.udig.bookmarks.internal.actions;
002:
003: import java.util.Collection;
004: import java.util.List;
005:
006: import net.refractions.udig.project.IMap;
007: import net.refractions.udig.project.command.MapCommand;
008: import net.refractions.udig.project.render.IViewportModel;
009: import net.refractions.udig.project.ui.ApplicationGIS;
010:
011: import org.eclipse.jface.action.Action;
012: import org.eclipse.jface.action.IAction;
013: import org.eclipse.jface.dialogs.InputDialog;
014: import org.eclipse.jface.dialogs.MessageDialog;
015: import org.eclipse.jface.viewers.DoubleClickEvent;
016: import org.eclipse.jface.viewers.IDoubleClickListener;
017: import org.eclipse.jface.viewers.ISelection;
018: import org.eclipse.jface.viewers.IStructuredSelection;
019: import org.eclipse.jface.viewers.StructuredSelection;
020: import org.eclipse.jface.window.Window;
021: import org.eclipse.swt.widgets.Display;
022: import org.eclipse.ui.IObjectActionDelegate;
023: import org.eclipse.ui.IViewActionDelegate;
024: import org.eclipse.ui.IViewPart;
025: import org.eclipse.ui.IWorkbenchPart;
026: import org.geotools.geometry.jts.ReferencedEnvelope;
027: import org.tcat.citd.sim.udig.bookmarks.Bookmark;
028: import org.tcat.citd.sim.udig.bookmarks.BookmarkCommandFactory;
029: import org.tcat.citd.sim.udig.bookmarks.BookmarkManager;
030: import org.tcat.citd.sim.udig.bookmarks.BookmarksPlugin;
031: import org.tcat.citd.sim.udig.bookmarks.internal.MapReference;
032: import org.tcat.citd.sim.udig.bookmarks.internal.MapWrapper;
033: import org.tcat.citd.sim.udig.bookmarks.internal.Messages;
034: import org.tcat.citd.sim.udig.bookmarks.internal.ui.BookmarksView;
035:
036: import com.vividsolutions.jts.geom.Envelope;
037:
038: /**
039: * The action delegate the provides all of the actions for working with bookmarks.
040: * <p>
041: * </p>
042: *
043: * @author cole.markham
044: * @since 1.0.0
045: */
046: public class BookmarkAction extends Action implements
047: IObjectActionDelegate, IViewActionDelegate,
048: IDoubleClickListener {
049: /**
050: * id for action to remove a bookmark
051: */
052: public static final String REMOVE_BOOKMARK_ACTION_ID = Messages.BookmarkAction_removebookmarkaction;
053:
054: /**
055: * id for action to remove a bookmark
056: */
057: public static final String REMOVE_MAP_ACTION_ID = Messages.BookmarkAction_removemapaction;
058:
059: /**
060: * id for action to remove a bookmark
061: */
062: public static final String REMOVE_PROJECT_ACTION_ID = Messages.BookmarkAction_removeprojectaction;
063:
064: /**
065: * id for action to remove all bookmarks
066: */
067: public static final String REMOVE_ALL_ACTION_ID = Messages.BookmarkAction_removeallbookmarksaction;
068:
069: /**
070: * id for action to go to a bookmark
071: */
072: public static final String GOTO_BOOKMARK_ACTION_ID = Messages.BookmarkAction_gotobookmarkaction;
073:
074: /**
075: * id for action to add a bookmark
076: */
077: public static final String ADD_BOOKMARK_ACTION_ID = Messages.BookmarkAction_addbookmarkaction;
078:
079: /**
080: * id for action to rename a bookmark
081: */
082: public static final String RENAME_BOOKMARK_ACTION_ID = Messages.BookmarkAction_renamebookmarkaction;
083:
084: /**
085: * id for action to save the bookmarks
086: */
087: public static final String SAVE_BOOKMARKS_ACTION_ID = Messages.BookmarkAction_savebookmarksaction;
088:
089: /**
090: * id for action to restore the bookmarks
091: */
092: public static final String RESTORE_BOOKMARKS_ACTION_ID = Messages.BookmarkAction_restorebookmarksaction;
093:
094: @SuppressWarnings("unused")
095: private IViewPart view;
096: private IStructuredSelection selection;
097: private BookmarkManager bmManager;
098:
099: /**
100: * Default Constructor
101: */
102: public BookmarkAction() {
103: // nothing to do
104: }
105:
106: // public void run() {
107: // ViewportModel v = (ViewportModel)(PlatformGIS.getActiveMap().getViewportModel());
108: // IStructuredSelection selection = (IStructuredSelection)this.view.viewer.getSelection();
109: // Bookmark bookmark = (Bookmark)selection.getFirstElement();
110: // v.setBounds(bookmark.getEnvelope());
111: // }
112:
113: public void setActivePart(IAction action, IWorkbenchPart targetPart) {
114: if (targetPart != null && targetPart instanceof IViewPart) {
115: view = (IViewPart) targetPart;
116: }
117: }
118:
119: public void run(IAction action) {
120: try {
121: if (REMOVE_ALL_ACTION_ID.equals(action.getId())) {
122: if (MessageDialog
123: .openConfirm(
124: Display.getCurrent().getActiveShell(),
125: Messages.BookmarkAction_dialogtitle_removebookmarks,
126: Messages.BookmarkAction_dialogprompt_removeallbookmarks)) {
127: if (bmManager == null) {
128: bmManager = BookmarksPlugin.getDefault()
129: .getBookmarkManager();
130: }
131: bmManager.empty();
132: refreshView();
133: }
134: } else if (REMOVE_MAP_ACTION_ID.equals(action.getId())) {
135: int size = selection.size();
136: if (size > 0) {
137: if (size > 1) {
138: if (MessageDialog
139: .openConfirm(
140: Display.getCurrent()
141: .getActiveShell(),
142: Messages.BookmarkAction_dialogtitle_removebookmarks,
143: Messages.BookmarkAction_dialogprompt_removemapbookmarks)) {
144: MapReference map = (MapReference) selection
145: .getFirstElement();
146: if (bmManager == null) {
147: bmManager = BookmarksPlugin
148: .getDefault()
149: .getBookmarkManager();
150: }
151: bmManager.removeMap(map);
152: }
153: } else {
154: if (MessageDialog
155: .openConfirm(
156: Display.getCurrent()
157: .getActiveShell(),
158: Messages.BookmarkAction_dialogtitle_removebookmarks,
159: Messages.BookmarkAction_dialogprompt_removeselectedmaps)) {
160: if (bmManager == null) {
161: bmManager = BookmarksPlugin
162: .getDefault()
163: .getBookmarkManager();
164: }
165: List wrappedMaps = selection.toList();
166: Collection maps = MapWrapper
167: .unwrap(wrappedMaps);
168: bmManager.removeMaps(maps);
169: }
170: }
171: refreshView();
172: }
173: // }else if(REMOVE_PROJECT_ACTION_ID.equals(action.getId())){
174: // int size = selection.size();
175: // if(size > 0){
176: // if(size > 1){
177: // if( MessageDialog.openConfirm(
178: // Display.getCurrent().getActiveShell(),
179: // Messages..BOOKMARK_ACTION_DIALOGTITLE_REMOVEBOOKMARKS,
180: // //$NON-NLS-1$
181: // Messages..BOOKMARK_ACTION_DIALOGPROMPT_REMOVEPROJECTBOOKMARKS)
182: // ){ //$NON-NLS-1$
183: // ProjectWrapper wrapper = (ProjectWrapper)selection.getFirstElement();
184: // if(bmManager == null){
185: // bmManager = BookmarksPlugin.getDefault().getBookmarkManager();
186: // }
187: // // bmManager.removeProject(wrapper.getProject());
188: // }
189: // }else {
190: // if( MessageDialog.openConfirm(
191: // Display.getCurrent().getActiveShell(),
192: // Messages..BOOKMARK_ACTION_DIALOGTITLE_REMOVEBOOKMARKS,
193: // //$NON-NLS-1$
194: // Messages..BOOKMARK_ACTION_DIALOGPROMPT_REMOVESELECTEDPROJECTS)
195: // ){ //$NON-NLS-1$
196: // if(bmManager == null){
197: // bmManager = BookmarksPlugin.getDefault().getBookmarkManager();
198: // }
199: // List wrappedProjects = selection.toList();
200: // Collection<IProject> projects = ProjectWrapper.unwrap(wrappedProjects);
201: // bmManager.removeProjects(projects);
202: // }
203: // }
204: // refreshView();
205: // }
206: } else if (REMOVE_BOOKMARK_ACTION_ID.equals(action.getId())) {
207: int size = selection.size();
208: if (size > 0) {
209: if (size > 1) {
210: if (MessageDialog
211: .openConfirm(
212: Display.getCurrent()
213: .getActiveShell(),
214: Messages.BookmarkAction_dialogtitle_removebookmarks,
215: Messages.BookmarkAction_dialogprompt_removeselectedbookmarks)) {
216: List bookmarks = selection.toList();
217: if (bmManager == null) {
218: bmManager = BookmarksPlugin
219: .getDefault()
220: .getBookmarkManager();
221: }
222: bmManager.removeBookmarks(bookmarks);
223: }
224: } else {
225: if (MessageDialog
226: .openConfirm(
227: Display.getCurrent()
228: .getActiveShell(),
229: Messages.BookmarkAction_dialogtitle_removebookmark,
230: Messages.BookmarkAction_dialogprompt_removebookmark)) {
231: Bookmark bookmark = (Bookmark) selection
232: .getFirstElement();
233: if (bmManager == null) {
234: bmManager = BookmarksPlugin
235: .getDefault()
236: .getBookmarkManager();
237: }
238: bmManager.removeBookmark(bookmark);
239: }
240: }
241: refreshView();
242: }
243: } else if (GOTO_BOOKMARK_ACTION_ID.equals(action.getId())) {
244: Bookmark bookmark = (Bookmark) selection
245: .getFirstElement();
246: gotoBookmark(bookmark);
247: } else if (ADD_BOOKMARK_ACTION_ID.equals(action.getId())) {
248: IMap map = ApplicationGIS.getActiveMap();
249: if (map != null) {
250: IViewportModel v = map.getViewportModel();
251: Envelope env = v.getBounds();
252: ReferencedEnvelope bounds;
253: if (env instanceof ReferencedEnvelope) {
254: bounds = (ReferencedEnvelope) env;
255: } else {
256: bounds = new ReferencedEnvelope(env, v.getCRS());
257: }
258: MapReference ref = bmManager.getMapReference(map);
259: Bookmark bookmark = new Bookmark(bounds, ref, null);
260: InputDialog dialog = new InputDialog(
261: Display.getCurrent().getActiveShell(),
262: Messages.BookmarkAction_dialogtitle_bookmarklocation,
263: Messages.BookmarkAction_dialogprompt_enterbookmarkname,
264: bookmark.getName(), null);
265: dialog.open();
266: if (dialog.getReturnCode() == Window.OK) {
267: String name = dialog.getValue();
268: bookmark.setName(name);
269: bmManager = BookmarksPlugin.getDefault()
270: .getBookmarkManager();
271: bmManager.addBookmark(bookmark);
272: refreshView();
273: }
274: ((BookmarksView) view)
275: .selectReveal(new StructuredSelection(
276: bookmark));
277: }
278: } else if (RENAME_BOOKMARK_ACTION_ID.equals(action.getId())) {
279: Bookmark bookmark = (Bookmark) selection
280: .getFirstElement();
281: InputDialog dialog = new InputDialog(
282: Display.getCurrent().getActiveShell(),
283: Messages.BookmarkAction_dialogtitle_renamebookmark,
284: Messages.BookmarkAction_dialogprompt_enterbookmarkname,
285: bookmark.getName(), null);
286: dialog.open();
287: if (dialog.getReturnCode() == Window.OK) {
288: String name = dialog.getValue();
289: bookmark.setName(name);
290: refreshView();
291: }
292: } else if (SAVE_BOOKMARKS_ACTION_ID.equals(action.getId())) {
293: BookmarksPlugin.getDefault().storeToPreferences();
294: } else if (RESTORE_BOOKMARKS_ACTION_ID.equals(action
295: .getId())) {
296: BookmarksPlugin.getDefault().restoreFromPreferences();
297: refreshView();
298: }
299: } catch (Exception e) {
300: e.printStackTrace();
301: }
302: }
303:
304: private void refreshView() {
305: if (view != null && view instanceof BookmarksView) {
306: BookmarksView bView = ((BookmarksView) view);
307: bView.refresh();
308: }
309: }
310:
311: /**
312: * Go to the given bookmark
313: *
314: * @param bookmark The bookmark to go to
315: */
316: private void gotoBookmark(Bookmark bookmark) {
317: BookmarkCommandFactory factory = BookmarkCommandFactory
318: .getInstance();
319: MapCommand cmd = factory.createGotoBookmarkCommand(bookmark);
320: ApplicationGIS.getActiveMap().sendCommandASync(cmd);
321: }
322:
323: public void init(IViewPart viewPart) {
324: if (viewPart != null) {
325: this .view = viewPart;
326: }
327: if (bmManager == null) {
328: bmManager = BookmarksPlugin.getDefault()
329: .getBookmarkManager();
330: }
331: }
332:
333: public void selectionChanged(IAction action, ISelection newSelection) {
334: this .selection = (IStructuredSelection) newSelection;
335: }
336:
337: public void doubleClick(DoubleClickEvent event) {
338: final IStructuredSelection eventSelection = (IStructuredSelection) event
339: .getSelection();
340: if (eventSelection.size() > 0
341: && eventSelection.getFirstElement() instanceof Bookmark) {
342: Bookmark bookmark = (Bookmark) eventSelection
343: .getFirstElement();
344: gotoBookmark(bookmark);
345: }
346: }
347:
348: }
|