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.ILayer;
021: import net.refractions.udig.project.command.UndoRedoCommand;
022: import net.refractions.udig.project.command.UndoableComposite;
023: import net.refractions.udig.project.command.UndoableMapCommand;
024: import net.refractions.udig.project.ui.render.displayAdapter.MapMouseEvent;
025: import net.refractions.udig.tools.edit.EditPlugin;
026: import net.refractions.udig.tools.edit.EditState;
027: import net.refractions.udig.tools.edit.EditToolHandler;
028: import net.refractions.udig.tools.edit.EventBehaviour;
029: import net.refractions.udig.tools.edit.EventType;
030: import net.refractions.udig.tools.edit.commands.RemoveEditGeomCommand;
031: import net.refractions.udig.tools.edit.commands.StartEditingCommand;
032: import net.refractions.udig.tools.edit.support.EditBlackboard;
033: import net.refractions.udig.tools.edit.support.ShapeType;
034:
035: import org.eclipse.core.runtime.NullProgressMonitor;
036:
037: /**
038: * Creates a new Geometry and feature
039: * <p>Requirements:
040: * <ul>
041: * <li>current state is NONE</li>
042: * <li>eventType is RELEASED</li>
043: * <li>no modifiers</li>
044: * <li>button1 released</li>
045: * <li>no buttons down</li>
046: * </ul>
047: * </p>
048: * <p>Action:
049: * <ul>
050: * <li>Sets the currentGeom to be the default geom on the black board</li>
051: * <li>Sets the state to CREATING</li>
052: * <li>Adds a point to the geom</li>
053: * </ul>
054: * </p>
055: * @author jones
056: * @since 1.1.0
057: */
058: public class StartEditingBehaviour implements EventBehaviour {
059:
060: private ShapeType type;
061:
062: public StartEditingBehaviour(ShapeType type) {
063: this .type = type;
064: }
065:
066: public boolean isValid(EditToolHandler handler, MapMouseEvent e,
067: EventType eventType) {
068: boolean goodState = handler.getCurrentState() != EditState.NONE
069: && handler.getCurrentState() == EditState.MODIFYING
070: || handler.getCurrentState() == EditState.NONE
071: && handler.getCurrentState() != EditState.MODIFYING;
072: boolean releasedEvent = eventType == EventType.RELEASED;
073: boolean noModifiers = !(e.modifiersDown());
074: boolean button1 = e.button == MapMouseEvent.BUTTON1;
075: boolean noButtonsDown = !e.buttonsDown();
076: return goodState && releasedEvent && noButtonsDown
077: && noModifiers && button1;
078: }
079:
080: public UndoableMapCommand getCommand(EditToolHandler handler,
081: MapMouseEvent e, EventType eventType) {
082: if (!isValid(handler, e, eventType))
083: throw new IllegalArgumentException(
084: "Current State is not valid for behaviour"); //$NON-NLS-1$
085: List<UndoableMapCommand> commands = new ArrayList<UndoableMapCommand>();
086: commands.add(handler.getContext().getEditFactory()
087: .createNullEditFeatureCommand());
088: ILayer editLayer = handler.getEditLayer();
089: EditBlackboard bb = handler.getEditBlackboard(editLayer);
090: commands.add(new RemoveEditGeomCommand(handler, bb.getGeoms()));
091: commands.add(new StartEditingCommand(handler, e, type));
092:
093: UndoableComposite undoableComposite = new UndoableComposite(
094: commands);
095: undoableComposite.setMap(handler.getContext().getMap());
096: try {
097: undoableComposite.run(new NullProgressMonitor());
098: } catch (Exception e1) {
099: throw new RuntimeException(e1);
100: }
101: return new UndoRedoCommand(undoableComposite);
102:
103: }
104:
105: public void handleError(EditToolHandler handler, Throwable error,
106: UndoableMapCommand command) {
107: EditPlugin.log("" + handler, error); //$NON-NLS-1$
108: }
109:
110: }
|