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.mapgraphic;
016:
017: import java.io.IOException;
018: import java.net.MalformedURLException;
019: import java.net.URL;
020: import java.util.ArrayList;
021: import java.util.List;
022:
023: import net.refractions.udig.catalog.CatalogPlugin;
024: import net.refractions.udig.catalog.IGeoResource;
025: import net.refractions.udig.catalog.IResolve;
026: import net.refractions.udig.catalog.IService;
027: import net.refractions.udig.mapgraphic.internal.MapGraphicService;
028: import net.refractions.udig.project.ILayer;
029: import net.refractions.udig.project.command.UndoableComposite;
030: import net.refractions.udig.project.command.factory.BasicCommandFactory;
031: import net.refractions.udig.project.command.factory.EditCommandFactory;
032: import net.refractions.udig.project.internal.Layer;
033: import net.refractions.udig.project.internal.Map;
034: import net.refractions.udig.project.ui.internal.ApplicationGISInternal;
035: import net.refractions.udig.ui.ProgressManager;
036:
037: import org.eclipse.core.runtime.NullProgressMonitor;
038: import org.eclipse.jface.action.IAction;
039: import org.eclipse.ui.IWorkbenchWindow;
040: import org.eclipse.ui.IWorkbenchWindowActionDelegate;
041: import org.eclipse.ui.actions.ActionDelegate;
042:
043: /**
044: * This is a helper class for MapGraphics that should be toggled on and off. Some good candidates
045: * are: Scalebar and legend.
046: * <p>
047: * This Example is the adding a Toggle action for LegendGraphic.
048: *
049: * <p> The following xml snippet must be added to the plugin.xml</p>
050: * <p>
051: * <pre>
052: * <extension
053: * point="org.eclipse.ui.actionSets">
054: * <actionSet
055: * id="net.refractions.udig.project.ui.mapGraphic.action"
056: * label="MapGraphics"
057: * visible="true">
058: * <action
059: * class="net.refractions.udig.legend.ui.actions.LegendAction"
060: * id="net.refractions.udig.project.ui.action.addlegend"
061: * label="Legend"
062: * menubarPath="layer/mapGraphic.ext"
063: * style="push"/>
064: * </actionSet>
065: * </extension>
066: * </pre>
067: * </p>
068: * <p>The following class must be refered to by the xml snippet above(the class attribute)</p>
069: * <p>
070: * <pre>
071: * public class LegendAction extends ActionDelegate implements IWorkbenchWindowActionDelegate {
072: *
073: * protected Class<LegendGraphic> getMapGraphicClass() {
074: * return LegendGraphic.class;
075: * }
076: *
077: * protected String getExtensionID() {
078: * return "legend"; //$NON-NLS-1$
079: * }
080: *
081: * public void init( IWorkbenchWindow window ) {
082: * }
083: *
084: * }
085: * </pre>
086: * </p>
087: * @author Jesse
088: * @since 1.1.0
089: */
090: public abstract class AbstractToggleMapGraphicAction extends
091: ActionDelegate implements IWorkbenchWindowActionDelegate {
092:
093: public AbstractToggleMapGraphicAction() {
094: }
095:
096: @Override
097: public void run(IAction action) {
098: Map map = ApplicationGISInternal.getActiveMap();
099: if (map == null)
100: return;
101: List<ILayer> layers = map.getMapLayers();
102: for (ILayer layer : layers) {
103: if (layer.hasResource(getMapGraphicClass())) {
104: removeLegend();
105: return;
106: }
107: }
108: // map does not contain legend so add it.
109: addLegend();
110: }
111:
112: private void removeLegend() {
113: Map map = ApplicationGISInternal.getActiveMap();
114: if (map == null)
115: return;
116:
117: List<ILayer> layers = map.getMapLayers();
118: List<ILayer> toRemove = new ArrayList<ILayer>();
119:
120: for (ILayer layer : layers) {
121: if (layer.hasResource(getMapGraphicClass())) {
122: toRemove.add(layer);
123: }
124: }
125:
126: UndoableComposite composite = new UndoableComposite();
127: for (ILayer layer : toRemove) {
128: composite.getCommands().add(
129: EditCommandFactory.getInstance().createDeleteLayer(
130: (Layer) layer));
131: }
132:
133: map.sendCommandASync(composite);
134: }
135:
136: private void addLegend() {
137: Map map = ApplicationGISInternal.getActiveMap();
138: try {
139: IGeoResource legendResource = null;
140:
141: URL url = new URL(MapGraphicService.SERVICE_URL,
142: "#" + getExtensionID()); //$NON-NLS-1$
143: List<IResolve> matches = CatalogPlugin.getDefault()
144: .getLocalCatalog().find(url,
145: ProgressManager.instance().get());
146: if (!matches.isEmpty())
147: legendResource = (IGeoResource) matches.get(0);
148:
149: if (legendResource == null) {
150: List<IService> results = CatalogPlugin.getDefault()
151: .getServiceFactory().acquire(url);
152: for (IGeoResource resource : results.get(0).members(
153: new NullProgressMonitor())) {
154: if (resource.getIdentifier().getRef().equals(
155: url.getRef())) {
156: legendResource = resource;
157: break;
158: }
159: }
160: }
161: if (legendResource == null)
162: return;
163: Layer layer = map.getLayerFactory().createLayer(
164: legendResource);
165: map.sendCommandSync(BasicCommandFactory.getInstance()
166: .createAddLayer(layer));
167: } catch (MalformedURLException e) {
168: MapGraphicPlugin.log("", e); //$NON-NLS-1$
169: } catch (IOException e) {
170: MapGraphicPlugin.log("", e); //$NON-NLS-1$
171: }
172:
173: }
174:
175: protected abstract Class<? extends MapGraphic> getMapGraphicClass();
176:
177: protected abstract String getExtensionID();
178:
179: public void init(IWorkbenchWindow window) {
180: }
181:
182: }
|