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.behaviour;
016:
017: import java.util.ArrayList;
018: import java.util.List;
019:
020: import net.refractions.udig.project.command.UndoRedoCommand;
021: import net.refractions.udig.project.command.UndoableComposite;
022: import net.refractions.udig.project.command.UndoableMapCommand;
023: import net.refractions.udig.project.ui.render.displayAdapter.MapMouseEvent;
024: import net.refractions.udig.tools.edit.EditPlugin;
025: import net.refractions.udig.tools.edit.EditState;
026: import net.refractions.udig.tools.edit.EditToolHandler;
027: import net.refractions.udig.tools.edit.EventBehaviour;
028: import net.refractions.udig.tools.edit.EventType;
029: import net.refractions.udig.tools.edit.commands.AddVertexCommand;
030: import net.refractions.udig.tools.edit.preferences.PreferenceUtil;
031: import net.refractions.udig.tools.edit.support.EditBlackboard;
032: import net.refractions.udig.tools.edit.support.EditGeom;
033: import net.refractions.udig.tools.edit.support.Point;
034:
035: import org.eclipse.core.runtime.NullProgressMonitor;
036:
037: /**
038: * <p>Requirements:
039: * <ul>
040: * <li>EventType==DOUBLE_CLICKED</li>
041: * <li>EditState==MODIFIED or CREATING</li>
042: * <li>no modifiers</li>
043: * <li>button1 clicked</li>
044: * <li>no buttons down</li> * </ul>
045: * </p>
046: * <p>Action:
047: * <ul>
048: * <li>Adds the point where double click occurs <b>if</b> addPoint is true. (Default behaviour)</li
049: * <li>Runs Accept Behaviours</li>
050: * <li>If current state is CREATING the changes state to MODIFYING</li>
051: * </ul>
052: * </p>
053: * @author jones
054: * @since 1.1.0
055: */
056: public class DoubleClickRunAcceptBehaviour implements EventBehaviour {
057:
058: boolean addPoint = true;
059:
060: public boolean isValid(EditToolHandler handler, MapMouseEvent e,
061: EventType eventType) {
062: boolean goodState = handler.getCurrentState() != EditState.NONE;
063: boolean releasedEvent = eventType == EventType.DOUBLE_CLICK;
064: boolean noModifiers = !(e.modifiersDown());
065: boolean button1 = e.button == MapMouseEvent.BUTTON1;
066: boolean onlyButton1Down = e.buttons
067: - (e.buttons & MapMouseEvent.BUTTON1) == 0;
068: boolean shapeIsSet = handler.getCurrentShape() != null;
069: if (!(shapeIsSet && goodState && releasedEvent && noModifiers
070: && button1 && onlyButton1Down))
071: return false;
072:
073: boolean changedGeom = false;
074: for (EditGeom geom : handler.getEditBlackboard(
075: handler.getEditLayer()).getGeoms()) {
076: if (geom.isChanged()) {
077: changedGeom = true;
078: break;
079: }
080: }
081: return changedGeom;
082: }
083:
084: public void handleError(EditToolHandler handler, Throwable error,
085: UndoableMapCommand command) {
086: EditPlugin.log("", error); //$NON-NLS-1$
087: }
088:
089: public UndoableMapCommand getCommand(EditToolHandler handler,
090: MapMouseEvent e, EventType eventType) {
091: List<UndoableMapCommand> commands = new ArrayList<UndoableMapCommand>();
092:
093: if (handler.getCurrentState() == EditState.CREATING && addPoint) {
094: Point clickPoint = Point.valueOf(e.x, e.y);
095: EditBlackboard editBlackboard = handler
096: .getEditBlackboard(handler.getEditLayer());
097:
098: Point destination = editBlackboard.overVertex(clickPoint,
099: PreferenceUtil.instance().getVertexRadius());
100: if (destination == null)
101: commands.add(new AddVertexCommand(handler,
102: editBlackboard, clickPoint));
103: }
104:
105: commands.add(handler.getCommand(handler.getAcceptBehaviours()));
106: UndoableComposite undoableComposite = new UndoableComposite(
107: commands);
108: try {
109: undoableComposite.setMap(handler.getContext().getMap());
110: undoableComposite.execute(new NullProgressMonitor());
111: } catch (Exception e1) {
112: throw (RuntimeException) new RuntimeException()
113: .initCause(e1);
114: }
115: return new UndoRedoCommand(undoableComposite);
116: }
117:
118: /**
119: * Returns true if a point will be added at the location of the double click.
120: * @return Returns true if a point will be added at the location of the double click.
121: */
122: public boolean isAddPoint() {
123: return this .addPoint;
124: }
125:
126: /**
127: * Sets the parameter of whether a point should be added at the location of the double click.
128: * @param addPoint true if a point should be added at the location of the double click.
129: */
130: public void setAddPoint(boolean addPoint) {
131: this.addPoint = addPoint;
132: }
133:
134: }
|