001: package org.tcat.citd.sim.udig.bookmarks.internal;
002:
003: import java.util.Collection;
004: import java.util.HashMap;
005:
006: import net.refractions.udig.project.IMap;
007: import net.refractions.udig.project.ui.ApplicationGIS;
008: import net.refractions.udig.project.ui.internal.MapEditor;
009: import net.refractions.udig.ui.PlatformGIS;
010:
011: import org.eclipse.jface.viewers.IStructuredContentProvider;
012: import org.eclipse.jface.viewers.ITreeContentProvider;
013: import org.eclipse.jface.viewers.TreeViewer;
014: import org.eclipse.jface.viewers.Viewer;
015: import org.eclipse.swt.widgets.Display;
016: import org.eclipse.ui.IPartListener;
017: import org.eclipse.ui.IWorkbenchPart;
018: import org.eclipse.ui.PlatformUI;
019: import org.tcat.citd.sim.udig.bookmarks.Bookmark;
020: import org.tcat.citd.sim.udig.bookmarks.BookmarkManager;
021: import org.tcat.citd.sim.udig.bookmarks.BookmarksPlugin;
022: import org.tcat.citd.sim.udig.bookmarks.internal.ui.BookmarksView;
023:
024: /**
025: * The content provider class is responsible for providing objects to the view. It can wrap existing
026: * objects in adapters or simply return objects as-is. These objects may be sensitive to the current
027: * input of the view, or ignore it and always show the same content (like Task List, for example).
028: *
029: * @author cole.markham
030: * @since 1.0.0
031: */
032: public class BookmarksContentProvider implements
033: IStructuredContentProvider, ITreeContentProvider, IPartListener {
034: private HashMap<Viewer, Object> viewers;
035:
036: private IWorkbenchPart currentPart;
037:
038: private BookmarkManager bManager;
039:
040: private MapReference currentMap;
041:
042: /**
043: * Default constructor
044: */
045: public BookmarksContentProvider() {
046: viewers = new HashMap<Viewer, Object>();
047: bManager = BookmarksPlugin.getDefault().getBookmarkManager();
048: currentPart = null;
049: currentMap = null;
050: }
051:
052: public void inputChanged(Viewer viewer, Object oldInput,
053: Object newInput) {
054: if (newInput == null)
055: viewers.remove(viewer);
056: else
057: viewers.put(viewer, newInput);
058: }
059:
060: /**
061: * returns current mapping of registered viewers and input objects Usually this involves a Viewe
062: * object
063: *
064: * @return a map of the viewers to input objects
065: */
066: protected HashMap getViewers() {
067: return viewers;
068: }
069:
070: public void dispose() {
071: PlatformUI.getWorkbench().getActiveWorkbenchWindow()
072: .getPartService().removePartListener(this );
073: viewers.clear();
074: bManager = null;
075: }
076:
077: /**
078: * Get the root element -- a single IMap object corresonding to the current map
079: *
080: * @return array of Objects
081: */
082: public Object[] getElements(Object parent) {
083: Object[] elements = new Object[1];
084: if (currentMap == null) {
085: elements[0] = Messages.BookmarksContentProvider_emptybookmarkslist;
086: } else {
087: elements[0] = currentMap;
088: }
089: return elements;
090: }
091:
092: public Object[] getChildren(Object parentElement) {
093: Object[] children = null;
094: if (parentElement instanceof MapReference) {
095: MapReference map = (MapReference) parentElement;
096: children = bManager.getBookmarks(map).toArray();
097: }
098: return children;
099: }
100:
101: public Object getParent(Object element) {
102: Object parent = null;
103: if (element instanceof Bookmark) {
104: Bookmark bookmark = (Bookmark) element;
105: parent = bookmark.getMap();
106: }
107: return parent;
108: }
109:
110: public boolean hasChildren(Object element) {
111: boolean hasChildren = false;
112: if (element instanceof MapReference) {
113: hasChildren = true;
114: }
115: return hasChildren;
116: }
117:
118: /**
119: * refreshes the given viewer in the UI thread
120: *
121: * @param asynch set to true for asynchronous refresh, false for synchronous
122: * @param v viewer to refresh
123: */
124: public void refresh(final Viewer v, boolean asynch) {
125: if (v.getControl().isDisposed())
126: return;
127: Display display = v.getControl().getDisplay();
128: if (display != null && !display.isDisposed()) {
129: Runnable r = new Runnable() {
130: public void run() {
131: if (v != null && !v.getControl().isDisposed()) {
132: v.refresh();
133: if (v instanceof TreeViewer) {
134: ((TreeViewer) v).expandToLevel(currentMap,
135: 1);
136: }
137: }
138: }
139: };
140: if (asynch)
141: display.asyncExec(r);
142: else
143: PlatformGIS.syncInDisplayThread(r);
144: }
145: }
146:
147: /**
148: * refreshes the given collection of viewers in the UI thread
149: *
150: * @param c
151: * @param asynch set to true for asynchronous refresh, false for synchronous
152: */
153: public void refresh(final Collection<Viewer> c, boolean asynch) {
154: for (Viewer v : c) {
155: refresh(v, asynch);
156: }
157: }
158:
159: /**
160: * @return Returns the currentMap.
161: */
162: public MapReference getCurrentMap() {
163: return currentMap;
164: }
165:
166: /**
167: * @param currentMap The currentMap to set.
168: */
169: public void setCurrentMap(MapReference currentMap) {
170: this .currentMap = currentMap;
171: }
172:
173: public void partActivated(IWorkbenchPart part) {
174: if (part == currentPart)
175: return;
176: if (part instanceof MapEditor) {
177: currentPart = part;
178: IMap map = ((MapEditor) part).getMap();
179: MapReference ref = bManager.getMapReference(map);
180: setCurrentMap(ref);
181: refresh(viewers.keySet(), true);
182: } else if (part instanceof BookmarksView) {
183: currentPart = part;
184: IMap map = ApplicationGIS.getActiveMap();
185: if (map != null) {
186: setCurrentMap(bManager.getMapReference(map));
187: } else
188: setCurrentMap(null);
189: refresh(viewers.keySet(), true);
190: }
191: }
192:
193: public void partBroughtToTop(IWorkbenchPart part) {
194: // nothing to do
195: }
196:
197: public void partClosed(IWorkbenchPart part) {
198: if (part == this ) {
199: dispose();
200: return;
201: }
202: if (part != currentPart)
203: return;
204: currentPart = null;
205: if (part instanceof MapEditor) {
206: setCurrentMap(null);
207: }
208: refresh(viewers.keySet(), true);
209: }
210:
211: public void partDeactivated(IWorkbenchPart part) {
212: // nothing to do
213: }
214:
215: public void partOpened(IWorkbenchPart part) {
216: // nothing to do
217: }
218: }
|