001: /*
002: * uDig - User Friendly Desktop Internet GIS client http://udig.refractions.net (C) 2004,
003: * Refractions Research Inc. This library is free software; you can redistribute it and/or modify it
004: * under the terms of the GNU Lesser General Public License as published by the Free Software
005: * Foundation; version 2.1 of the License. This library is distributed in the hope that it will be
006: * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
007: * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
008: */
009: package net.refractions.udig.tools.edit.impl;
010:
011: import java.awt.Point;
012:
013: import net.refractions.udig.project.ILayer;
014: import net.refractions.udig.project.command.MapCommand;
015: import net.refractions.udig.project.ui.internal.commands.draw.DrawShapeCommand;
016: import net.refractions.udig.project.ui.render.displayAdapter.MapMouseEvent;
017: import net.refractions.udig.project.ui.tool.AbstractModalTool;
018: import net.refractions.udig.project.ui.tool.ModalTool;
019: import net.refractions.udig.tool.edit.internal.Messages;
020: import net.refractions.udig.tools.edit.EditPlugin;
021:
022: import org.eclipse.jface.action.IStatusLineManager;
023: import org.geotools.feature.Feature;
024: import org.geotools.feature.FeatureCollection;
025: import org.geotools.feature.FeatureIterator;
026: import org.geotools.geometry.jts.ReferencedEnvelope;
027:
028: import com.vividsolutions.jts.geom.Envelope;
029:
030: /**
031: * Deletes a feature from the currently selected layer or the top layer.
032: *
033: * @author Jesse
034: * @since 0.8.1
035: */
036: public class DeleteTool extends AbstractModalTool implements ModalTool {
037:
038: /**
039: * Construct <code>DeleteTool</code>.
040: *
041: */
042: public DeleteTool() {
043: super (MOUSE | MOTION);
044: }
045:
046: @Override
047: public void setActive(final boolean active) {
048: super .setActive(active);
049: setStatusBarMessage(active);
050: }
051:
052: private void setStatusBarMessage(final boolean active) {
053: getContext().updateUI(new Runnable() {
054: public void run() {
055: if (getContext().getActionBars() == null)
056: return;
057: IStatusLineManager bar = getContext().getActionBars()
058: .getStatusLineManager();
059: if (bar != null) {
060: if (active) {
061: if (getContext().getMapLayers().size() > 0)
062: bar.setMessage(Messages.DeleteTool_status);
063: } else
064: bar.setMessage(""); //$NON-NLS-1$
065: bar.setErrorMessage(null);
066: }
067: }
068: });
069: }
070:
071: @Override
072: public void mousePressed(MapMouseEvent e) {
073: draw.setValid(true); // make sure context.getViewportPane().repaint() knows about us
074: context.sendASyncCommand(draw); // should of isValided us
075: feedback(e);
076:
077: }
078:
079: @Override
080: public void mouseDragged(MapMouseEvent e) {
081: feedback(e);
082:
083: }
084:
085: DrawShapeCommand draw = new DrawShapeCommand();
086:
087: /**
088: * Provides user feedback
089: * @param e
090: */
091: public void feedback(MapMouseEvent e) {
092: ReferencedEnvelope box = context.getBoundingBox(new Point(
093: e.x - 3, e.y - 3), 7);
094: draw.setShape(context.toShape(box));
095: context.getViewportPane().repaint();
096:
097: super .mouseDragged(e);
098: }
099:
100: public void mouseReleased(MapMouseEvent e) {
101: if (getContext().getMapLayers().size() == 0)
102: return;
103: FeatureIterator reader = null;
104: try {
105:
106: ILayer layer = getContext().getEditManager()
107: .getSelectedLayer();
108: if (layer == null)
109: layer = getContext().getMapLayers().get(0);
110:
111: if (layer == null)
112: throw new Exception("No layers in map"); //$NON-NLS-1$
113:
114: Envelope env = getContext().getBoundingBox(e.getPoint(), 6);
115: FeatureCollection results = getContext().getFeaturesInBbox(
116: layer, env);
117:
118: reader = results.features();
119:
120: final boolean found = !reader.hasNext();
121: getContext().updateUI(new Runnable() {
122: public void run() {
123: if (getContext().getActionBars() == null)
124: return;
125: IStatusLineManager bar = getContext()
126: .getActionBars().getStatusLineManager();
127: if (bar != null) {
128: if (found)
129: bar
130: .setErrorMessage(Messages.DeleteTool_warning);
131: else
132: bar.setErrorMessage(null);
133: }
134: }
135: });
136:
137: if (found)
138: return;
139:
140: Feature feature = reader.next();
141:
142: MapCommand deleteFeatureCommand = getContext()
143: .getEditFactory().createDeleteFeature(feature,
144: layer);
145: getContext().sendASyncCommand(deleteFeatureCommand);
146:
147: getContext().getViewportPane().repaint();
148: } catch (Exception e1) {
149: EditPlugin.log(null, e1);
150: } finally {
151: try {
152: if (reader != null)
153: reader.close();
154: } catch (Exception e2) {
155: EditPlugin.log(null, e2);
156: }
157: draw.setValid(false); // get us off the draw stack for context.getViewportPane().repaint();
158: getContext().getViewportPane().repaint();
159: }
160: }
161:
162: }
|