001: package com.vividsolutions.jump.workbench.ui.cursortool;
002:
003: import java.awt.Shape;
004: import java.awt.geom.NoninvertibleTransformException;
005: import java.awt.geom.Point2D;
006: import java.util.Collection;
007: import java.util.Iterator;
008: import java.util.Map;
009:
010: import com.vividsolutions.jts.geom.Coordinate;
011: import com.vividsolutions.jts.geom.GeometryFactory;
012: import com.vividsolutions.jts.geom.LineString;
013: import com.vividsolutions.jts.geom.Point;
014: import com.vividsolutions.jts.util.Assert;
015: import com.vividsolutions.jump.feature.Feature;
016: import com.vividsolutions.jump.util.Block;
017: import com.vividsolutions.jump.util.CollectionUtil;
018: import com.vividsolutions.jump.util.StringUtil;
019: import com.vividsolutions.jump.workbench.model.Layer;
020: import com.vividsolutions.jump.workbench.plugin.EnableCheckFactory;
021:
022: public abstract class AbstractClickSelectedLineStringsTool extends
023: SpecifyFeaturesTool {
024: public AbstractClickSelectedLineStringsTool() {
025: setViewClickBuffer(10);
026: }
027:
028: protected static final String NO_SELECTED_LINESTRINGS_HERE_MESSAGE = "No selected LineStrings here";
029:
030: protected void warnLayerNotEditable(Layer layer) {
031: getWorkbench().getFrame().warnUser(
032: layer.getName() + " is not editable");
033: }
034:
035: public String getName() {
036: return super .getName().replaceAll("Line String", "LineString");
037: }
038:
039: protected Coordinate getModelSource() {
040: return getModelDestination();
041: }
042:
043: protected Shape getShape(Point2D source, Point2D destination)
044: throws Exception {
045: return null;
046: }
047:
048: protected Point getModelClickPoint() {
049: return new GeometryFactory().createPoint(getModelDestination());
050: }
051:
052: protected void gestureFinished() throws Exception {
053: reportNothingToUndoYet();
054: if (!check(checkFactory()
055: .createAtLeastNLayersMustBeEditableCheck(1))) {
056: return;
057: }
058: if (!check(checkFactory()
059: .createAtLeastNItemsMustBeSelectedCheck(1))) {
060: return;
061: }
062: Collection nearbyLineStringFeatures = CollectionUtil.select(
063: CollectionUtil
064: .concatenate(layerToSpecifiedFeaturesMap()
065: .values()), new Block() {
066: public Object yield(Object feature) {
067: return getPanel().getSelectionManager()
068: .getFeaturesWithSelectedItems()
069: .contains(feature)
070: && ((Feature) feature).getGeometry() instanceof LineString ? Boolean.TRUE
071: : Boolean.FALSE;
072: }
073: });
074: if (nearbyLineStringFeatures.isEmpty()) {
075: getWorkbench().getFrame().warnUser(
076: NO_SELECTED_LINESTRINGS_HERE_MESSAGE);
077: return;
078: }
079: gestureFinished(nearbyLineStringFeatures);
080: }
081:
082: private EnableCheckFactory checkFactory() {
083: return new EnableCheckFactory(getWorkbench().getContext());
084: }
085:
086: protected abstract void gestureFinished(
087: Collection nearbyLineStringFeatures)
088: throws NoninvertibleTransformException;
089:
090: protected Layer layer(Feature feature,
091: Map layerToSpecifiedFeaturesMap) {
092: for (Iterator i = layerToSpecifiedFeaturesMap.keySet()
093: .iterator(); i.hasNext();) {
094: Layer layer = (Layer) i.next();
095: Collection features = (Collection) layerToSpecifiedFeaturesMap
096: .get(layer);
097: if (features.contains(feature)) {
098: return layer;
099: }
100: }
101: Assert.shouldNeverReachHere();
102: return null;
103: }
104:
105: }
|