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.AbstractCommand;
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.SetEditStateCommand;
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.EditUtils;
033: import net.refractions.udig.tools.edit.support.Point;
034: import net.refractions.udig.tools.edit.support.PrimitiveShape;
035:
036: import org.eclipse.core.runtime.IProgressMonitor;
037:
038: /**
039: * <p>Requirements:
040: * <ul>
041: * <li>EventType==Released</li>
042: * <li>CurrentShape != null</li>
043: * <li>mouse is over end vertex</li>
044: * <li>button1 was released</li>
045: * <li>no buttons or modifiers down</li>
046: * <li>mode == MODIFYING or NONE</li>
047: * </ul>
048: * </p>
049: * <p>Action:
050: * <ul>
051: * <li>mode is set to CREATING or CREATING_BACKWARD</li>
052: * </ul>
053: * </p>
054: *
055: * @author jones
056: * @since 1.1.0
057: */
058: public class ExtendLineBehaviour implements EventBehaviour {
059:
060: public boolean isValid(EditToolHandler handler, MapMouseEvent e,
061: EventType eventType) {
062: boolean legalEventType = eventType == EventType.RELEASED;
063: boolean legalState = handler.getCurrentState() == EditState.MODIFYING
064: || handler.getCurrentState() == EditState.NONE;
065: boolean shapeAndGeomNotNull = handler.getCurrentShape() != null;
066: boolean button1Released = e.button == MapMouseEvent.BUTTON1;
067:
068: if (!legalState || !legalEventType || !shapeAndGeomNotNull
069: || !button1Released || e.buttonsDown()
070: || e.modifiersDown())
071: return false;
072:
073: PrimitiveShape currentShape = handler.getCurrentShape();
074: Point point = currentShape.getEditBlackboard().overVertex(
075: Point.valueOf(e.x, e.y),
076: PreferenceUtil.instance().getVertexRadius());
077: if (currentShape.getPoint(0).equals(point)
078: || currentShape.getPoint(
079: currentShape.getNumPoints() - 1).equals(point))
080: return true;
081:
082: return false;
083: }
084:
085: public UndoableMapCommand getCommand(EditToolHandler handler,
086: MapMouseEvent e, EventType eventType) {
087: EditBlackboard editBlackboard = handler.getCurrentShape()
088: .getEditBlackboard();
089: Point point = editBlackboard.overVertex(
090: Point.valueOf(e.x, e.y), PreferenceUtil.instance()
091: .getVertexRadius());
092: if (point.equals(handler.getCurrentShape().getPoint(0))) {
093: List<UndoableMapCommand> commands = new ArrayList<UndoableMapCommand>();
094: commands.add(new ReversePointsInShapeCommand(handler,
095: handler.getCurrentShape()));
096: commands.add(new SetEditStateCommand(handler,
097: EditState.CREATING));
098: return new UndoableComposite(commands);
099: } else
100: return new SetEditStateCommand(handler, EditState.CREATING);
101: }
102:
103: public void handleError(EditToolHandler handler, Throwable error,
104: UndoableMapCommand command) {
105: EditPlugin.log("", error); //$NON-NLS-1$
106: }
107:
108: private static class ReversePointsInShapeCommand extends
109: AbstractCommand implements UndoableMapCommand {
110: PrimitiveShape shape;
111:
112: ReversePointsInShapeCommand(EditToolHandler handler,
113: PrimitiveShape shape) {
114: this .shape = shape;
115: }
116:
117: public void run(IProgressMonitor monitor) throws Exception {
118: EditUtils.instance.reverseOrder(shape);
119: }
120:
121: public String getName() {
122: return ""; //$NON-NLS-1$
123: }
124:
125: public void rollback(IProgressMonitor monitor) throws Exception {
126: run(monitor);
127: }
128:
129: }
130: }
|