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.tool.select;
016:
017: import java.awt.Point;
018: import java.io.IOException;
019: import java.lang.reflect.InvocationTargetException;
020: import java.util.Iterator;
021:
022: import net.refractions.udig.project.ILayer;
023: import net.refractions.udig.project.ui.render.displayAdapter.MapMouseEvent;
024: import net.refractions.udig.project.ui.tool.AbstractModalTool;
025: import net.refractions.udig.project.ui.tool.ModalTool;
026: import net.refractions.udig.tool.select.internal.Messages;
027: import net.refractions.udig.ui.PlatformGIS;
028:
029: import org.eclipse.core.runtime.IProgressMonitor;
030: import org.eclipse.core.runtime.SubProgressMonitor;
031: import org.eclipse.jface.operation.IRunnableWithProgress;
032: import org.geotools.data.FeatureSource;
033: import org.geotools.feature.Feature;
034: import org.geotools.feature.FeatureCollection;
035: import org.geotools.geometry.jts.ReferencedEnvelope;
036:
037: /**
038: * Selects and drags single features.
039: *
040: * @author jones
041: * @since 1.0.0
042: */
043: public class ArrowSelection extends AbstractModalTool implements
044: ModalTool {
045:
046: private int x;
047: private int y;
048:
049: public ArrowSelection() {
050: super (DRAG_DROP | MOUSE);
051: }
052:
053: @Override
054: public void mousePressed(MapMouseEvent e) {
055: x = e.x;
056: y = e.y;
057: }
058:
059: @Override
060: public void mouseReleased(final MapMouseEvent e) {
061: if (e.x == x && e.y == y) {
062: PlatformGIS.run(new IRunnableWithProgress() {
063:
064: @SuppressWarnings("unchecked")
065: public void run(IProgressMonitor monitor)
066: throws InvocationTargetException,
067: InterruptedException {
068: monitor.beginTask(Messages.ArrowSelection_0, 5);
069: ReferencedEnvelope bbox = getContext()
070: .getBoundingBox(new Point(x, y), 5);
071: FeatureCollection collection = null;
072: Iterator<Feature> iter = null;
073: try {
074: ILayer selectedLayer = getContext()
075: .getSelectedLayer();
076: FeatureSource source = selectedLayer
077: .getResource(FeatureSource.class,
078: new SubProgressMonitor(monitor,
079: 1));
080: if (source == null)
081: return;
082: collection = source.getFeatures(selectedLayer
083: .createBBoxFilter(bbox,
084: new SubProgressMonitor(monitor,
085: 1)));
086: iter = collection.iterator();
087: if (!iter.hasNext()) {
088: if (!e.buttonsDown()) {
089: getContext()
090: .sendASyncCommand(
091: getContext()
092: .getEditFactory()
093: .createNullEditFeatureCommand());
094: }
095: getContext().sendASyncCommand(
096: getContext().getSelectionFactory()
097: .createNoSelectCommand());
098: return;
099: }
100: Feature feature = iter.next();
101: getContext()
102: .sendASyncCommand(
103: getContext()
104: .getEditFactory()
105: .createSetEditFeatureCommand(
106: feature,
107: selectedLayer));
108: getContext()
109: .sendASyncCommand(
110: getContext()
111: .getSelectionFactory()
112: .createFIDSelectCommand(
113: selectedLayer,
114: feature));
115: } catch (IOException e) {
116:
117: // return;
118: } finally {
119: monitor.done();
120: if (collection != null && iter != null)
121: collection.close(iter);
122: }
123: }
124:
125: });
126: }
127: }
128: }
|