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.project.internal.commands.selection;
010:
011: import java.util.HashMap;
012: import java.util.List;
013: import java.util.Map;
014: import java.util.Set;
015: import java.util.Map.Entry;
016:
017: import net.refractions.udig.project.command.AbstractCommand;
018: import net.refractions.udig.project.command.UndoableMapCommand;
019: import net.refractions.udig.project.internal.Layer;
020: import net.refractions.udig.project.internal.Messages;
021:
022: import org.eclipse.core.runtime.IProgressMonitor;
023: import org.geotools.filter.Filter;
024:
025: import com.vividsolutions.jts.geom.Envelope;
026:
027: /**
028: * A MapCommand that selects all features in the bounding box encapsulated by the class.
029: *
030: * @author jeichar
031: * @since TODO provide version
032: */
033: public class BBoxSelectionCommand extends AbstractCommand implements
034: UndoableMapCommand {
035:
036: /** Add bbox to layers' current filters */
037: public final static int ADD = 1;
038:
039: /** Replaces layers' filters */
040: public final static int NONE = 0;
041:
042: /** "and" bbox to layer's current filters */
043: public final static int SUBTRACT = -1;
044:
045: Envelope bbox = null;
046:
047: private int modifiers;
048:
049: private Map<Layer, Filter> undoState = new HashMap<Layer, Filter>();
050:
051: // private FilterQuery query=null;
052:
053: /**
054: * Creates a new instance of BBoxSelectionCommand
055: *
056: * @param bbox
057: */
058: public BBoxSelectionCommand(Envelope bbox, int modifiers) {
059: this .bbox = bbox;
060: this .modifiers = modifiers;
061: }
062:
063: /**
064: * @see net.refractions.udig.project.command.UndoableCommand#rollback()
065: */
066: public void rollback(IProgressMonitor monitor) throws Exception {
067: Set<Entry<Layer, Filter>> state = undoState.entrySet();
068: for (Entry<Layer, Filter> entry : state) {
069: entry.getKey().setFilter(entry.getValue());
070: }
071: }
072:
073: /**
074: * @see net.refractions.udig.project.command.MapCommand#run()
075: */
076: public void run(IProgressMonitor monitor) throws Exception {
077: List<Layer> layers = getMap().getLayersInternal();
078: for (Layer layer : layers) {
079: undoState.put(layer, layer.getFilter());
080: }
081: // check for key modifiers for AND/OR toggle
082: if (modifiers == NONE) {
083: getMap().select(bbox);
084: } else if (modifiers == ADD) {
085: getMap().select(bbox, true);
086: } else if (modifiers == SUBTRACT) {
087: // TODO there's no common modifier key for subtracting. This should
088: // probably be done by selecting already selected features....?
089: getMap().select(bbox, false);
090: }
091: }
092:
093: /**
094: * @see net.refractions.udig.project.command.MapCommand#getName()
095: */
096: public String getName() {
097: return Messages.BBoxSelectionCommand_boxSelection;
098: }
099:
100: }
|