001: /*
002: * uDig - User Friendly Desktop Internet GIS client
003: * http://udig.refractions.net
004: * (C) 2004, Refractions Research Inc.
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: */
017: package net.refractions.udig.project.ui;
018:
019: import java.util.ArrayList;
020: import java.util.LinkedList;
021: import java.util.List;
022:
023: import net.refractions.udig.catalog.IGeoResource;
024: import net.refractions.udig.project.IMap;
025: import net.refractions.udig.project.IProject;
026: import net.refractions.udig.project.internal.Project;
027: import net.refractions.udig.project.internal.ProjectPlugin;
028: import net.refractions.udig.project.ui.internal.MapEditor;
029: import net.refractions.udig.project.ui.internal.MapFactory;
030: import net.refractions.udig.project.ui.internal.ProjectExplorer;
031: import net.refractions.udig.project.ui.internal.ProjectUIPlugin;
032:
033: import org.eclipse.core.runtime.IProgressMonitor;
034: import org.eclipse.core.runtime.ISafeRunnable;
035: import org.eclipse.core.runtime.IStatus;
036: import org.eclipse.core.runtime.Status;
037: import org.eclipse.core.runtime.jobs.Job;
038: import org.eclipse.ui.IEditorPart;
039: import org.eclipse.ui.IEditorReference;
040: import org.eclipse.ui.IViewPart;
041: import org.eclipse.ui.IViewReference;
042: import org.eclipse.ui.IWorkbenchPage;
043: import org.eclipse.ui.IWorkbenchWindow;
044: import org.eclipse.ui.PartInitException;
045: import org.eclipse.ui.PlatformUI;
046:
047: /**
048: * A facade into udig to simplify operations such as getting the active map
049: * and openning a map editor.
050: *
051: * @author jeichar
052: * @since 0.9.0
053: * @deprecated - use to {@link ApplicationGIS}
054: */
055: public class PlatformGIS {
056: /**
057: * May return null of no project is active.
058: *
059: * @return The current active project, or null if no such project exists.
060: * @deprecated - use to {@link ApplicationGIS#getActiveProject()}
061: */
062: public static IProject getActiveProject() {
063: Project project = ProjectPlugin.getPlugin()
064: .getProjectRegistry().getCurrentProject();
065:
066: if (project != null)
067: return project;
068:
069: return ProjectPlugin.getPlugin().getProjectRegistry()
070: .getCurrentProject();
071: }
072:
073: /**
074: * May return null if the active editor is not a Map Editor.
075: * @return the map contained by the current MapEditor or null if the active
076: * editor is not a map editor.
077: * @deprecated - use to {@link ApplicationGIS#getActiveMap()()}
078: */
079: public static IMap getActiveMap() {
080:
081: //need to be in an event thread
082: final ArrayList<IMap> l = new ArrayList<IMap>();
083: net.refractions.udig.ui.PlatformGIS
084: .syncInDisplayThread(new Runnable() {
085:
086: public void run() {
087: try {
088: IEditorPart editor = PlatformUI
089: .getWorkbench()
090: .getActiveWorkbenchWindow()
091: .getActivePage().getActiveEditor();
092: if (editor instanceof MapEditor) {
093: l.add(((MapEditor) editor).getMap());
094: }
095: } catch (NullPointerException e) {
096: //do nothing
097: }
098: }
099: });
100:
101: if (!l.isEmpty())
102: return l.get(0);
103:
104: return null;
105: }
106:
107: /**
108: * May return null if no Map Editors exist.
109: * @return a list of maps contained or null if no Map Editors exist.
110: * @deprecated - use to {@link ApplicationGIS#getOpenMaps()}
111: */
112: public static List<IMap> getMaps() {
113: try {
114: //For some reason, getting the active workbench doesn't seem to work??!! So looping
115: // through all the workbenches and all their pages was the slow way to go...
116: //IEditorReference[] editors = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getEditorReferences();
117: IWorkbenchWindow[] wWindows = PlatformUI.getWorkbench()
118: .getWorkbenchWindows();
119:
120: List<IMap> maps = new ArrayList<IMap>();
121: for (IWorkbenchWindow wWindow : wWindows) {
122: IWorkbenchPage[] wPages = wWindow.getPages();
123: for (IWorkbenchPage wPage : wPages) {
124: IEditorReference[] editors = wPage
125: .getEditorReferences();
126: for (IEditorReference editor : editors) {
127: if (editor.getEditor(false) instanceof MapEditor) {
128: maps.add(((MapEditor) editor
129: .getEditor(false)).getMap());
130: }
131: }
132: }
133: }
134: if (maps.size() == 0)
135: return null;
136: return maps;
137: } catch (NullPointerException e) {
138: return null;
139: }
140: }
141:
142: /**
143: * Opens a Map editor for the provided map.
144: * @param map the map to open. Must be an instance of Map.
145: * @deprecated - use to {@link ApplicationGIS#openMap(IMap)}
146: */
147: public static void openMap(IMap map) {
148: ProjectExplorer.getProjectExplorer().open(map, false);
149: }
150:
151: /**
152: * creates a map and opens an editor for the map.
153: * @param a list of IGeoResources. Each resource will be a layer in the created
154: * map.
155: * @deprecated - use to {@link ApplicationGIS#createAndOpenMap(List))}
156: */
157: public static void createAndOpenMap(List<IGeoResource> resources) {
158: MapFactory.instance().process(null, resources, true);
159: }
160:
161: /**
162: * creates a map and opens an editor for the map.
163: * @param a list of IGeoResources. Each resource will be a layer in the created
164: * map.
165: * @param owner the project that will contain the map. owner must be an instance of Project. If it
166: * is obtained using the framework then this will always be the case.
167: * @deprecated - use to {@link ApplicationGIS#createAndOpenMap(List, IProject))}
168: */
169: public static void createAndOpenMap(List<IGeoResource> resources,
170: IProject owner) {
171: MapFactory.instance().process((Project) owner, resources, true);
172: }
173:
174: /**
175: * If an active map exists the layers will be added to that map. Otherwise
176: * an IllegalStateException will be thrown.
177: * @param a list of IGeoResources. Each resource will be a layer in the active
178: * map.
179: * @deprecated - use to {@link ApplicationGIS#addLayersToMap(IMap, List, int, Project))}
180: */
181: public static void addLayersToActiveMap(List<IGeoResource> resources)
182: throws IllegalStateException {
183: if (getActiveMap() == null)
184: throw new IllegalStateException("No active map exists"); //$NON-NLS-1$
185: MapFactory.instance().process(null, resources, false);
186: }
187:
188: /**
189: * Gets a reference to a view. If the view has not been opened previously then the view will be
190: * opened.
191: *
192: * @param show whether to show the view or not.
193: * @param id the id of the view to show.
194: * @return returns the view or null if the view does not exist
195: * @deprecated - use to {@link ApplicationGIS#getView(boolean, String))}
196: */
197: public static IViewPart getView(boolean show, String id) {
198: IWorkbenchPage page = PlatformUI.getWorkbench()
199: .getActiveWorkbenchWindow().getActivePage();
200: IViewReference[] view = page.getViewReferences();
201: IViewReference infoRef = null;
202: for (IViewReference reference : view) {
203: if (reference.getId().equals(id)) {
204: infoRef = reference;
205: break;
206: }
207: }
208: // JONES: need to get the part and set the selection to null so that the last selected feature
209: // will not flash (because it will not be in list any more).
210: IViewPart infoView = null;
211: if (infoRef == null) {
212: try {
213: infoView = page.showView(id);
214: } catch (PartInitException e1) {
215: return null;
216: }
217: if (infoView == null) {
218: return null;
219: }
220: }
221: if (infoRef != null)
222: return (IViewPart) infoRef.getPart(show);
223:
224: return null;
225:
226: }
227:
228: /**
229: * Runs the given runnable in a protected mode. Exceptions
230: * thrown in the runnable are logged and passed to the runnable's
231: * exception handler. Such exceptions are not rethrown by this method.
232: * @deprecated - use to {@link net.refractions.udig.ui.PlatformGIS#run(ISafeRunnable)}
233: */
234: public static void run(ISafeRunnable request) {
235: runner.addRequest(request);
236: }
237:
238: private static Runner runner = new Runner();
239:
240: private static class Runner extends Job {
241: /**
242: * @param name
243: */
244: public Runner() {
245: super (""); //$NON-NLS-1$
246: setPriority(LONG);
247: setSystem(true);
248: }
249:
250: List<ISafeRunnable> requests = new LinkedList<ISafeRunnable>();
251: IProgressMonitor current;
252:
253: /**
254: * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
255: */
256: @Override
257: protected IStatus run(IProgressMonitor monitor) {
258: current = monitor;
259: while (requests.size() > 0) {
260: ISafeRunnable runnable = requests.get(0);
261: requests.remove(0);
262: run(runnable);
263: }
264: return Status.OK_STATUS;
265: }
266:
267: /**
268: * Add a runnable object to be run.
269: * @param runnable
270: */
271: public void addRequest(ISafeRunnable runnable) {
272: if (getThread() == Thread.currentThread()) {
273: if (current.isCanceled())
274: return;
275: run(runnable);
276: } else {
277: requests.add(runnable);
278: schedule();
279: }
280: }
281:
282: private void run(ISafeRunnable runnable) {
283: try {
284: runnable.run();
285: } catch (Throwable e) {
286: // be extra careful. Maybe the handler is crazy. If so log the exception.
287: try {
288: runnable.handleException(e);
289: } catch (Throwable e2) {
290: ProjectUIPlugin.log("", e); //$NON-NLS-1$
291: }
292: }
293: }
294: }
295: }
|