001: /* uDig - User Friendly Desktop Internet GIS client
002: * http://udig.refractions.net
003: * (C) 2004, Refractions Research Inc.
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation;
008: * version 2.1 of the License.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: */
015: package net.refractions.udig.project.ui.internal;
016:
017: import java.util.ArrayList;
018: import java.util.Collections;
019: import java.util.List;
020:
021: import net.refractions.udig.project.IMap;
022: import net.refractions.udig.project.internal.Map;
023: import net.refractions.udig.project.internal.Project;
024: import net.refractions.udig.project.internal.ProjectPlugin;
025: import net.refractions.udig.project.ui.ApplicationGIS;
026: import net.refractions.udig.project.ui.internal.tool.ToolContext;
027: import net.refractions.udig.ui.PlatformGIS;
028:
029: import org.eclipse.jface.viewers.StructuredSelection;
030: import org.eclipse.swt.widgets.Display;
031: import org.eclipse.ui.IEditorPart;
032: import org.eclipse.ui.IEditorReference;
033: import org.eclipse.ui.IWorkbenchPage;
034: import org.eclipse.ui.IWorkbenchWindow;
035: import org.eclipse.ui.PlatformUI;
036: import org.geotools.feature.Feature;
037:
038: /**
039: * Allows access to the internal types such as Map.
040: * @author jones
041: * @since 1.0.0
042: */
043: public class ApplicationGISInternal {
044:
045: public static ToolContext createContext(IMap map) {
046: return (ToolContext) ApplicationGIS.createContext(map);
047: }
048:
049: /**
050: * May return null of no project is active.
051: *
052: * @return The current active project, or null if no such project exists.
053: */
054: public static Project getActiveProject() {
055: return (Project) ApplicationGIS.getActiveProject();
056: }
057:
058: /**
059: * Return all Projects. The list is unmodifiable.
060: * @return all Projects.
061: */
062: public static List<? extends Project> getProjects() {
063: return Collections.unmodifiableList(ProjectPlugin.getPlugin()
064: .getProjectRegistry().getProjects());
065: }
066:
067: /**
068: * May return null if the active editor is not a Map Editor.
069: * @return the map contained by the current MapEditor or null if the active
070: * editor is not a map editor.
071: */
072: public static Map getActiveMap() {
073:
074: return (Map) ApplicationGIS.getActiveMap();
075: }
076:
077: /**
078: * Returns all open maps.
079: * May return null if no Map Editors exist.
080: * @return a list of maps contained or null if no Map Editors exist.
081: */
082: @SuppressWarnings("unchecked")
083: public static List<? extends Map> getOpenMaps() {
084: return (List<? extends Map>) ApplicationGIS.getOpenMaps();
085: }
086:
087: /**
088: * Return a feature editor for the provided feature
089: *
090: * @return a feature editor for the provided feature
091: */
092: public static FeatureEditorLoader getFeatureEditorLoader(
093: Feature feature) {
094: if (feature == null)
095: return null;
096: return ProjectUIPlugin.getDefault().getFeatureEditProcessor()
097: .getClosestMatch(new StructuredSelection(feature));
098: }
099:
100: public static MapEditor getActiveEditor() {
101: try {
102: final ArrayList<IEditorPart> editor = new ArrayList<IEditorPart>();
103:
104: PlatformGIS.syncInDisplayThread(new Runnable() {
105: public void run() {
106: try {
107: editor.add(PlatformUI.getWorkbench()
108: .getActiveWorkbenchWindow()
109: .getActivePage().getActiveEditor());
110: } catch (Throwable t) {
111: // do nothing
112: }
113: }
114: });
115:
116: if (editor.get(0) instanceof MapEditor) {
117: return (MapEditor) editor.get(0);
118: }
119: } catch (Exception e) {
120: return null;
121: }
122:
123: return null;
124: }
125:
126: public static MapEditor findMapEditor(final IMap map) {
127: if (map == null)
128: throw new NullPointerException("Map cannot be null"); //$NON-NLS-1$
129:
130: final MapEditor[] result = new MapEditor[1];
131: PlatformGIS.syncInDisplayThread(new Runnable() {
132: public void run() {
133: IWorkbenchWindow win = PlatformUI.getWorkbench()
134: .getActiveWorkbenchWindow();
135: if (win == null)
136: return;
137: IWorkbenchPage page = win.getActivePage();
138: if (page == null)
139: return;
140: IEditorReference[] refs = page.getEditorReferences();
141: for (IEditorReference reference : refs) {
142: IEditorPart e = reference.getEditor(false);
143: if (e instanceof MapEditor) {
144: MapEditor me = (MapEditor) e;
145: if (map.equals(me.getMap())) {
146: result[0] = me;
147: return;
148: }
149: }
150: }
151: }
152: });
153: return result[0];
154: }
155:
156: }
|