001: /*
002: * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
003: * for visualizing and manipulating spatial features with geometry and attributes.
004: *
005: * Copyright (C) 2003 Vivid Solutions
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: *
021: * For more information, contact:
022: *
023: * Vivid Solutions
024: * Suite #1A
025: * 2328 Government Street
026: * Victoria BC V8T 5G5
027: * Canada
028: *
029: * (250)385-6040
030: * www.vividsolutions.com
031: */
032:
033: package com.vividsolutions.jump.workbench.ui.cursortool;
034:
035: import java.awt.Cursor;
036: import java.awt.event.MouseEvent;
037: import java.awt.geom.NoninvertibleTransformException;
038: import java.util.ArrayList;
039: import java.util.Arrays;
040: import java.util.Collection;
041: import java.util.Iterator;
042: import java.util.Map;
043:
044: import javax.swing.Icon;
045:
046: import com.vividsolutions.jts.geom.Geometry;
047: import com.vividsolutions.jump.feature.Feature;
048: import com.vividsolutions.jump.geom.EnvelopeUtil;
049: import com.vividsolutions.jump.util.CollectionMap;
050: import com.vividsolutions.jump.workbench.model.FenceLayerFinder;
051: import com.vividsolutions.jump.workbench.model.Layer;
052: import com.vividsolutions.jump.workbench.ui.AbstractSelection;
053: import com.vividsolutions.jump.workbench.ui.LayerViewPanel;
054:
055: public abstract class SelectTool extends DragTool {
056:
057: public Cursor getCursor() {
058: return Cursor.getDefaultCursor();
059: }
060:
061: public void mouseClicked(MouseEvent e) {
062: try {
063: super .mouseClicked(e);
064: setViewSource(e.getPoint());
065: setViewDestination(e.getPoint());
066: fireGestureFinished();
067: } catch (Throwable t) {
068: getPanel().getContext().handleThrowable(t);
069: }
070: }
071:
072: protected void gestureFinished()
073: throws NoninvertibleTransformException {
074: reportNothingToUndoYet();
075:
076: if (!wasShiftPressed()) {
077: getPanel().getSelectionManager().clear();
078: }
079:
080: Map layerToFeaturesInFenceMap = getPanel()
081: .visibleLayerToFeaturesInFenceMap(
082: EnvelopeUtil
083: .toGeometry(getBoxInModelCoordinates()));
084:
085: Collection layers = layerToFeaturesInFenceMap.keySet();
086: if (selectedLayersOnly()) {
087: layers.retainAll(Arrays.asList(getTaskFrame()
088: .getLayerNamePanel().getSelectedLayers()));
089: }
090: for (Iterator i = layers.iterator(); i.hasNext();) {
091: Layer layer = (Layer) i.next();
092:
093: if (layer.getName().equals(FenceLayerFinder.LAYER_NAME)) {
094: continue;
095: }
096:
097: //Disable panel updates -- we'll manually repaint the selection and
098: //fire the selection-changed event. [Jon Aquino]
099: boolean originalPanelUpdatesEnabled = getPanel()
100: .getSelectionManager().arePanelUpdatesEnabled();
101: getPanel().getSelectionManager().setPanelUpdatesEnabled(
102: false);
103: try {
104: CollectionMap featureToItemsToSelectMap = featureToItemsInFenceMap(
105: (Collection) layerToFeaturesInFenceMap
106: .get(layer), layer, false);
107: CollectionMap featureToItemsToUnselectMap = featureToItemsInFenceMap(
108: (Collection) layerToFeaturesInFenceMap
109: .get(layer), layer, true);
110: selection.selectItems(layer, featureToItemsToSelectMap);
111: if (wasShiftPressed()) {
112: selection.unselectItems(layer,
113: featureToItemsToUnselectMap);
114: }
115: } finally {
116: getPanel().getSelectionManager()
117: .setPanelUpdatesEnabled(
118: originalPanelUpdatesEnabled);
119: }
120: }
121:
122: getPanel().getSelectionManager().updatePanel();
123: }
124:
125: protected boolean selectedLayersOnly() {
126: return wasControlPressed();
127: }
128:
129: private String rendererID;
130:
131: protected SelectTool(String rendererID) {
132: this .rendererID = rendererID;
133: }
134:
135: protected AbstractSelection selection;
136:
137: /**
138: * @param selected whether to return selected items or deselected items
139: */
140: private CollectionMap featureToItemsInFenceMap(Collection features,
141: Layer layer, boolean selected)
142: throws NoninvertibleTransformException {
143: CollectionMap featureToSelectedItemsMap = selection
144: .getFeatureToSelectedItemCollectionMap(layer);
145: CollectionMap featureToItemsInFenceMap = new CollectionMap();
146: for (Iterator i = features.iterator(); i.hasNext();) {
147: Feature feature = (Feature) i.next();
148: Collection selectedItems = featureToSelectedItemsMap
149: .getItems(feature);
150: Collection itemsToReturn = itemsInFence(feature);
151: if (selected) {
152: itemsToReturn.retainAll(selectedItems);
153: } else {
154: itemsToReturn.removeAll(selectedItems);
155: }
156: featureToItemsInFenceMap.put(feature, itemsToReturn);
157: }
158:
159: return featureToItemsInFenceMap;
160: }
161:
162: private Collection itemsInFence(Feature feature)
163: throws NoninvertibleTransformException {
164: ArrayList itemsInFence = new ArrayList();
165: Geometry fence = EnvelopeUtil
166: .toGeometry(getBoxInModelCoordinates());
167:
168: for (Iterator i = selection.items(feature.getGeometry())
169: .iterator(); i.hasNext();) {
170: Geometry selectedItem = (Geometry) i.next();
171:
172: if (LayerViewPanel.intersects(selectedItem, fence)) {
173: itemsInFence.add(selectedItem);
174: }
175: }
176:
177: return itemsInFence;
178: }
179:
180: public Icon getIcon() {
181: return null;
182: }
183:
184: }
|