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.tools.edit.commands;
016:
017: import java.util.Collections;
018: import java.util.Set;
019:
020: import net.refractions.udig.project.command.AbstractCommand;
021: import net.refractions.udig.project.command.UndoableMapCommand;
022: import net.refractions.udig.tool.edit.internal.Messages;
023: import net.refractions.udig.tools.edit.support.EditBlackboard;
024: import net.refractions.udig.tools.edit.support.Point;
025: import net.refractions.udig.tools.edit.support.Selection;
026:
027: import org.eclipse.core.runtime.IProgressMonitor;
028:
029: /**
030: * Sets the selection on the editblackboard. Depending on the Enum used
031: * the new points may be added to the selection or may replace the selection.
032: *
033: * @author jones
034: * @since 1.1.0
035: */
036: public class SelectPointCommand extends AbstractCommand implements
037: UndoableMapCommand {
038:
039: private Type type;
040: private Set<Point> points;
041: private EditBlackboard editBlackboard;
042: private Selection oldPoints;
043:
044: /**
045: * new instance
046: * @param editBlackboard blackboard to modify
047: * @param points points to add or set
048: * @param type indicates whether to add or set.
049: */
050: public SelectPointCommand(EditBlackboard editBlackboard,
051: Set<Point> points, Type type) {
052: this .editBlackboard = editBlackboard;
053: this .points = points;
054: this .type = type;
055: }
056:
057: /**
058: * new instance
059: * @param editBlackboard blackboard to modify
060: * @param point point to add or set
061: * @param type indicates whether to add or set.
062: */
063: public SelectPointCommand(EditBlackboard editBlackboard2,
064: Point point, Type type) {
065: this (editBlackboard2, Collections.singleton(point), type);
066: }
067:
068: public void run(IProgressMonitor monitor) throws Exception {
069: editBlackboard.startBatchingEvents();
070: oldPoints = new Selection(editBlackboard.getSelection());
071: oldPoints.disconnect();
072: if (type == Type.ADD)
073: editBlackboard.selectionAddAll(points);
074: else if (type == Type.SET) {
075: editBlackboard.selectionClear();
076: if (!points.isEmpty())
077: editBlackboard.selectionAddAll(points);
078: } else {
079: if (!points.isEmpty())
080: editBlackboard.selectionRemoveAll(points);
081: }
082: editBlackboard.fireBatchedEvents();
083: }
084:
085: public String getName() {
086: return Messages.SelectPointCommand_name;
087: }
088:
089: public void rollback(IProgressMonitor monitor) throws Exception {
090: Selection selection = editBlackboard.getSelection();
091: synchronized (selection) {
092: editBlackboard.selectionClear();
093: editBlackboard.selectionAddAll(oldPoints);
094: }
095: }
096:
097: public enum Type {
098: ADD, SET, REMOVE
099: };
100:
101: }
|