001: package org.tcat.citd.sim.udig.bookmarks.internal.ui;
002:
003: import org.eclipse.jface.action.GroupMarker;
004: import org.eclipse.jface.action.IMenuListener;
005: import org.eclipse.jface.action.IMenuManager;
006: import org.eclipse.jface.action.MenuManager;
007: import org.eclipse.jface.viewers.DoubleClickEvent;
008: import org.eclipse.jface.viewers.IDoubleClickListener;
009: import org.eclipse.jface.viewers.ISelection;
010: import org.eclipse.jface.viewers.IStructuredSelection;
011: import org.eclipse.jface.viewers.StructuredSelection;
012: import org.eclipse.jface.viewers.TreeViewer;
013: import org.eclipse.jface.viewers.ViewerSorter;
014: import org.eclipse.swt.widgets.Composite;
015: import org.eclipse.swt.widgets.Display;
016: import org.eclipse.swt.widgets.Menu;
017: import org.eclipse.ui.IWorkbenchActionConstants;
018: import org.eclipse.ui.part.ISetSelectionTarget;
019: import org.eclipse.ui.part.ViewPart;
020: import org.tcat.citd.sim.udig.bookmarks.Bookmark;
021: import org.tcat.citd.sim.udig.bookmarks.internal.BookmarksContentProvider;
022: import org.tcat.citd.sim.udig.bookmarks.internal.BookmarksLabelProvider;
023: import org.tcat.citd.sim.udig.bookmarks.internal.actions.BookmarkAction;
024:
025: /**
026: * This is the view that displays the <code>Bookmark</code>s. The content provider connects to
027: * the <code>BookmarkManager</code> which is the model.
028: * <p>
029: *
030: * @author cole.markham
031: * @since 1.0.0
032: */
033:
034: public class BookmarksView extends ViewPart implements
035: ISetSelectionTarget {
036: private TreeViewer bookmarksTree;
037: private MenuManager menuMgr;
038: private BookmarksLabelProvider labelProvider;
039: private BookmarksContentProvider bookmarksProvider;
040:
041: /**
042: * The constructor.
043: */
044: public BookmarksView() {
045: // nothing to do
046: }
047:
048: @Override
049: public void dispose() {
050: menuMgr.dispose();
051: labelProvider.dispose();
052: bookmarksProvider.dispose();
053: bookmarksTree = null;
054: super .dispose();
055: }
056:
057: private void initPopup() {
058: menuMgr = new MenuManager("#PopupMenu"); //$NON-NLS-1$
059: menuMgr.setRemoveAllWhenShown(true);
060: menuMgr.addMenuListener(new IMenuListener() {
061: public void menuAboutToShow(IMenuManager manager) {
062: fillContextMenu(manager);
063: }
064: });
065: Menu m = menuMgr.createContextMenu(bookmarksTree.getTree());
066: bookmarksTree.getTree().setMenu(m);
067: getViewSite().registerContextMenu(menuMgr, bookmarksTree);
068: }
069:
070: private void fillContextMenu(IMenuManager mm) {
071: mm.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
072: }
073:
074: /**
075: * This is a callback that will allow us to create the viewer and initialize it.
076: */
077: @Override
078: public void createPartControl(Composite parent) {
079: bookmarksTree = new TreeViewer(parent);
080: bookmarksProvider = new BookmarksContentProvider();
081: bookmarksTree.setContentProvider(bookmarksProvider);
082: getSite().getWorkbenchWindow().getPartService()
083: .addPartListener(bookmarksProvider);
084: labelProvider = new BookmarksLabelProvider();
085: bookmarksTree.setLabelProvider(labelProvider);
086: bookmarksTree.setSorter(new ViewerSorter());
087: bookmarksTree.setInput(bookmarksProvider);
088: bookmarksTree.setAutoExpandLevel(2);
089: bookmarksTree.addDoubleClickListener(new BookmarkAction());
090: bookmarksTree
091: .addDoubleClickListener(new IDoubleClickListener() {
092:
093: public void doubleClick(DoubleClickEvent event) {
094: final Object obj = ((IStructuredSelection) bookmarksTree
095: .getSelection()).getFirstElement();
096: if (!(obj instanceof Bookmark)) {
097: Display.getCurrent().asyncExec(
098: new Runnable() {
099:
100: public void run() {
101: bookmarksTree
102: .setExpandedState(
103: obj,
104: !bookmarksTree
105: .getExpandedState(obj));
106: }
107: });
108: return;
109: }
110: }
111:
112: });
113: initPopup();
114: }
115:
116: /**
117: * Passing the focus request to the viewer's control.
118: */
119: @Override
120: public void setFocus() {
121: bookmarksTree.getControl().setFocus();
122: }
123:
124: /**
125: * Refresh the view
126: */
127: public void refresh() {
128: ISelection selection = bookmarksTree.getSelection();
129: bookmarksTree.refresh();
130: if (selection.isEmpty()) {
131: bookmarksTree.setSelection(new StructuredSelection(
132: bookmarksProvider.getCurrentMap()));
133: } else {
134: bookmarksTree.setSelection(selection);
135: }
136: }
137:
138: /*
139: * @see org.eclipse.ui.part.ISetSelectionTarget#selectReveal(org.eclipse.jface.viewers.ISelection)
140: */
141: public void selectReveal(ISelection selection) {
142: bookmarksTree.setSelection(selection);
143: }
144:
145: }
|