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.UndoableComposite;
021: import net.refractions.udig.project.command.UndoableMapCommand;
022: import net.refractions.udig.project.ui.render.displayAdapter.MapMouseEvent;
023: import net.refractions.udig.tools.edit.EditState;
024: import net.refractions.udig.tools.edit.EditToolHandler;
025: import net.refractions.udig.tools.edit.EventBehaviour;
026: import net.refractions.udig.tools.edit.EventType;
027: import net.refractions.udig.tools.edit.commands.AddVertexCommand;
028: import net.refractions.udig.tools.edit.commands.CreateAndSelectHoleCommand;
029: import net.refractions.udig.tools.edit.commands.SetEditStateCommand;
030: import net.refractions.udig.tools.edit.support.EditGeom;
031: import net.refractions.udig.tools.edit.support.Point;
032: import net.refractions.udig.tools.edit.support.PrimitiveShape;
033: import net.refractions.udig.tools.edit.support.ShapeType;
034:
035: /**
036: * <p>Requirements:
037: * <ul>
038: * <li>EventType==RELEASED</li>
039: * <li>Current State == Creating</li>
040: * <li>Current Shape != null</li>
041: * <li>Button1 is released</li>
042: * <li>no buttons are down</li>
043: * <li>no modifiers are down</li>
044: * <li>current geom is a polygon or unknown</li>
045: * </ul>
046: * </p>
047: * <p>Action:
048: * <ul>
049: * <li></li>
050: * </ul>
051: * </p>
052: * @author jones
053: * @since 1.1.0
054: */
055: public class StartHoleCuttingBehaviour implements EventBehaviour {
056:
057: public boolean isValid(EditToolHandler handler, MapMouseEvent e,
058: EventType eventType) {
059: boolean legalState = true;
060: boolean legalEventType = eventType == EventType.RELEASED;
061: boolean shapeAndGeomNotNull = handler.getCurrentShape() != null;
062: boolean button1Released = e.button == MapMouseEvent.BUTTON1;
063: if (!shapeAndGeomNotNull)
064: return false;
065: EditGeom currentGeom = handler.getCurrentGeom();
066: boolean selectedShapeIsPolygon = currentGeom.getShapeType() == ShapeType.POLYGON
067: || currentGeom.getShapeType() == ShapeType.UNKNOWN;
068:
069: if (!(legalState && legalEventType && shapeAndGeomNotNull
070: && button1Released && !e.buttonsDown()
071: && !e.modifiersDown() && selectedShapeIsPolygon))
072: return false;
073:
074: Point point = Point.valueOf(e.x, e.y);
075:
076: if (!currentGeom.getShell().contains(point, true))
077: return false;
078:
079: for (PrimitiveShape shape : currentGeom.getHoles()) {
080: if (shape.contains(point, true))
081: return false;
082: }
083: return true;
084: }
085:
086: public UndoableMapCommand getCommand(EditToolHandler handler,
087: MapMouseEvent e, EventType eventType) {
088: if (!isValid(handler, e, eventType))
089: throw new IllegalStateException(
090: "Current state is illegal this method should not be called"); //$NON-NLS-1$
091:
092: List<UndoableMapCommand> commands = new ArrayList<UndoableMapCommand>();
093: commands.add(new SetEditStateCommand(handler,
094: EditState.CREATING));
095: commands.add(new CreateAndSelectHoleCommand(handler));
096: commands.add(new AddVertexCommand(handler, handler
097: .getEditBlackboard(handler.getEditLayer()), Point
098: .valueOf(e.x, e.y)));
099: return new UndoableComposite(commands);
100: }
101:
102: public void handleError(EditToolHandler handler, Throwable error,
103: UndoableMapCommand command) {
104: }
105:
106: }
|