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:
010: package net.refractions.udig.tool.select;
011:
012: import java.awt.Color;
013: import java.awt.Point;
014: import java.awt.Rectangle;
015: import java.io.IOException;
016: import java.util.HashSet;
017: import java.util.Set;
018:
019: import net.refractions.udig.project.ILayer;
020: import net.refractions.udig.project.command.MapCommand;
021: import net.refractions.udig.project.internal.commands.selection.BBoxSelectionCommand;
022: import net.refractions.udig.project.ui.internal.commands.draw.DrawShapeCommand;
023: import net.refractions.udig.project.ui.render.displayAdapter.MapMouseEvent;
024: import net.refractions.udig.project.ui.tool.ModalTool;
025: import net.refractions.udig.project.ui.tool.SimpleTool;
026: import net.refractions.udig.ui.graphics.ViewportGraphics;
027:
028: import org.geotools.data.DefaultQuery;
029: import org.geotools.data.FeatureSource;
030: import org.geotools.data.Query;
031: import org.geotools.feature.FeatureCollection;
032: import org.geotools.feature.FeatureIterator;
033: import org.geotools.filter.FidFilter;
034: import org.geotools.filter.Filter;
035: import org.geotools.filter.FilterFactory;
036: import org.geotools.filter.FilterFactoryFinder;
037:
038: import com.vividsolutions.jts.geom.Coordinate;
039: import com.vividsolutions.jts.geom.Envelope;
040:
041: /**
042: * TODO provide type description
043: *
044: * @author jeichar
045: * @since TODO provide version
046: */
047: public class BBoxSelection extends SimpleTool implements ModalTool {
048:
049: /**
050: * Comment for <code>ID</code>
051: */
052: public static final String ID = "net.refractions.udig.tools.BBoxSelect"; //$NON-NLS-1$
053:
054: private Point start;
055:
056: private boolean selecting;
057:
058: DrawShapeCommand shapeCommand;
059:
060: Set<String> selectedFids = new HashSet<String>();
061:
062: /**
063: * Creates a new instance of BBoxSelection
064: */
065: public BBoxSelection() {
066: super (MOUSE | MOTION);
067: }
068:
069: /**
070: * @see net.refractions.udig.project.ui.tool.SimpleTool#onMouseDragged(net.refractions.udig.project.render.displayAdapter.MapMouseEvent)
071: */
072: protected void onMouseDragged(MapMouseEvent e) {
073: Point end = e.getPoint();
074: shapeCommand.setShape(new Rectangle(Math.min(start.x, end.x),
075: Math.min(start.y, end.y), Math.abs(start.x - end.x),
076: Math.abs(start.y - end.y)));
077: context.getViewportPane().repaint();
078: }
079:
080: /**
081: * @see net.refractions.udig.project.ui.tool.AbstractTool#mousePressed(net.refractions.udig.project.render.displayAdapter.MapMouseEvent)
082: */
083: public void onMousePressed(MapMouseEvent e) {
084: shapeCommand = getContext().getDrawFactory()
085: .createDrawShapeCommand(new Rectangle(0, 0, 0, 0),
086: Color.ORANGE, ViewportGraphics.LINE_DASH, 1);
087: shapeCommand.setFill(new Color(Color.ORANGE.getRed(),
088: Color.ORANGE.getGreen(), Color.ORANGE.getBlue(), 50));
089:
090: if (((e.button & MapMouseEvent.BUTTON1) != 0)) {
091: selecting = true;
092:
093: start = e.getPoint();
094: shapeCommand.setValid(true);
095: shapeCommand
096: .setShape(new Rectangle(start.x, start.y, 0, 0));
097: context.sendASyncCommand(shapeCommand);
098: }
099: }
100:
101: /**
102: * @see net.refractions.udig.project.ui.tool.AbstractTool#mouseReleased(net.refractions.udig.project.render.displayAdapter.MapMouseEvent)
103: */
104: public void onMouseReleased(MapMouseEvent e) {
105: if (selecting) {
106: Point end = e.getPoint();
107: if (start == null || start.equals(end)) {
108:
109: Envelope bounds = getContext().getBoundingBox(
110: e.getPoint(), 3);
111: sendSelectionCommand(e, bounds);
112: } else {
113: Coordinate c1 = context.getMap().getViewportModel()
114: .pixelToWorld(start.x, start.y);
115: Coordinate c2 = context.getMap().getViewportModel()
116: .pixelToWorld(end.x, end.y);
117:
118: Envelope bounds = new Envelope(c1, c2);
119: sendSelectionCommand(e, bounds);
120: }
121: }
122: }
123:
124: /**
125: * @param e
126: * @param bounds
127: */
128: protected void sendSelectionCommand(MapMouseEvent e, Envelope bounds) {
129: MapCommand command;
130: if (e.isModifierDown(MapMouseEvent.MOD2_DOWN_MASK)) {
131: command = getContext().getSelectionFactory()
132: .createBBoxSelectionCommand(bounds,
133: BBoxSelectionCommand.ADD);
134: } else if (e.isModifierDown(MapMouseEvent.MOD1_DOWN_MASK)) {
135: command = getContext().getSelectionFactory()
136: .createBBoxSelectionCommand(bounds,
137: BBoxSelectionCommand.SUBTRACT);
138: } else {
139: command = getContext().getSelectionFactory()
140: .createBBoxSelectionCommand(bounds,
141: BBoxSelectionCommand.NONE);
142: }
143:
144: getContext().sendASyncCommand(command);
145: selecting = false;
146: shapeCommand.setValid(false);
147: getContext().getViewportPane().repaint();
148: }
149: }
|