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.tools.edit.activator;
016:
017: import java.io.IOException;
018: import java.util.List;
019:
020: import net.refractions.udig.catalog.CatalogPlugin;
021: import net.refractions.udig.catalog.IGeoResource;
022: import net.refractions.udig.mapgraphic.grid.GridMapGraphic;
023: import net.refractions.udig.mapgraphic.internal.MapGraphicService;
024: import net.refractions.udig.project.ILayer;
025: import net.refractions.udig.project.IMap;
026: import net.refractions.udig.project.command.SetLayerVisibilityCommand;
027: import net.refractions.udig.project.internal.Layer;
028: import net.refractions.udig.project.internal.LayerFactory;
029: import net.refractions.udig.project.internal.commands.AddLayerCommand;
030: import net.refractions.udig.tools.edit.Activator;
031: import net.refractions.udig.tools.edit.EditPlugin;
032: import net.refractions.udig.tools.edit.EditToolHandler;
033: import net.refractions.udig.tools.edit.preferences.PreferenceUtil;
034: import net.refractions.udig.tools.edit.support.SnapBehaviour;
035: import net.refractions.udig.ui.ProgressManager;
036:
037: /**
038: * If the snap behaviour is GRID then enables the grid map graphic on activation and
039: * disables the layer if this activator originally added the layer (or sets it to visible).
040: *
041: * @author Jesse
042: * @since 1.1.0
043: */
044: public class GridActivator implements Activator {
045:
046: private static final String KEY = "GRID_ACTIVATOR_INDICATOR";
047:
048: /**
049: * Hides the grid layer (visibility=false) if the layer was added or set visible by this class.
050: */
051: public void hideGrid(IMap map) {
052: ILayer gridLayer = findGridLayer(map);
053: if (gridLayer != null
054: && gridLayer.getBlackboard().get(KEY) != null) {
055: setGridLayerVisibility(gridLayer, false);
056: }
057: }
058:
059: /**
060: * Shows the grid.
061: *
062: * @param map map to add the grid to.
063: */
064: public void showGrid(IMap map) {
065: ILayer gridLayer = findGridLayer(map);
066:
067: if (gridLayer != null) {
068: setGridLayerVisibility(gridLayer, true);
069: } else {
070: addLayer(map);
071: }
072: }
073:
074: public void activate(EditToolHandler handler) {
075: if (PreferenceUtil.instance().getSnapBehaviour() == SnapBehaviour.GRID) {
076: IMap map = handler.getContext().getMap();
077: showGrid(map);
078: }
079: }
080:
081: public void handleDeactivateError(EditToolHandler handler,
082: Throwable error) {
083: EditPlugin.log("", error);
084: }
085:
086: public void handleActivateError(EditToolHandler handler,
087: Throwable error) {
088: EditPlugin.log("", error);
089: }
090:
091: public void deactivate(EditToolHandler handler) {
092: if (PreferenceUtil.instance().getSnapBehaviour() == SnapBehaviour.GRID) {
093: IMap map = handler.getContext().getMap();
094: hideGrid(map);
095: }
096: }
097:
098: private void addLayer(IMap map) {
099: MapGraphicService service = CatalogPlugin.getDefault()
100: .getLocalCatalog().getById(MapGraphicService.class,
101: MapGraphicService.SERVICE_URL,
102: ProgressManager.instance().get());
103: try {
104: List<? extends IGeoResource> members = service
105: .members(ProgressManager.instance().get());
106: for (IGeoResource resource : members) {
107: if (resource.canResolve(GridMapGraphic.class)) {
108: LayerFactory factory = map.getLayerFactory();
109: Layer newLayer = factory.createLayer(resource);
110: newLayer.getBlackboard().put(KEY, KEY);
111:
112: AddLayerCommand command = new AddLayerCommand(
113: newLayer);
114: map.sendCommandASync(command);
115: }
116: }
117:
118: } catch (IOException e) {
119: throw (RuntimeException) new RuntimeException()
120: .initCause(e);
121: }
122: }
123:
124: private void setGridLayerVisibility(ILayer gridLayer,
125: boolean enabled) {
126: if (enabled != gridLayer.isVisible()) {
127: if (enabled) {
128: gridLayer.getBlackboard().putString(KEY, KEY);
129: }
130: gridLayer.getMap().sendCommandASync(
131: new SetLayerVisibilityCommand(gridLayer, enabled));
132: }
133: }
134:
135: private ILayer findGridLayer(IMap map) {
136: List<ILayer> layers = map.getMapLayers();
137: ILayer graphicLayer = null;
138: for (ILayer layer : layers) {
139: if (layer.hasResource(GridMapGraphic.class)) {
140: graphicLayer = layer;
141: break;
142: }
143: }
144: return graphicLayer;
145: }
146:
147: }
|