01: /* uDig - User Friendly Desktop Internet GIS client
02: * http://udig.refractions.net
03: * (C) 2004, Refractions Research Inc.
04: *
05: * This library is free software; you can redistribute it and/or
06: * modify it under the terms of the GNU Lesser General Public
07: * License as published by the Free Software Foundation;
08: * version 2.1 of the License.
09: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: */
15: package net.refractions.udig.tools.edit.behaviour;
16:
17: import java.util.ArrayList;
18: import java.util.List;
19:
20: import net.refractions.udig.project.command.UndoRedoCommand;
21: import net.refractions.udig.project.command.UndoableComposite;
22: import net.refractions.udig.project.command.UndoableMapCommand;
23: import net.refractions.udig.project.ui.render.displayAdapter.MapMouseEvent;
24: import net.refractions.udig.tools.edit.EditPlugin;
25: import net.refractions.udig.tools.edit.EditState;
26: import net.refractions.udig.tools.edit.EditToolHandler;
27: import net.refractions.udig.tools.edit.EventBehaviour;
28: import net.refractions.udig.tools.edit.EventType;
29: import net.refractions.udig.tools.edit.commands.SetEditStateCommand;
30:
31: import org.eclipse.core.runtime.NullProgressMonitor;
32:
33: /**
34: * <p>Requirements:
35: * <ul>
36: * <li>EventType==RELEASED</li>
37: * <li>Current State == Creating</li>
38: * <li>Current Shape != null</li>
39: * <li>Button1 is released</li>
40: * <li>no buttons are down</li>
41: * <li>no modifiers are down</li>
42: * </ul>
43: * </p>
44: * <p>Action:
45: * <ul>
46: * <li>Will run accept behaviours</li>
47: *</ul>
48: * </p>
49: * @author jones
50: * @since 1.1.0
51: */
52: public class AcceptBehaviour implements EventBehaviour {
53:
54: public boolean isValid(EditToolHandler handler, MapMouseEvent e,
55: EventType eventType) {
56: boolean legalState = handler.getCurrentState() == EditState.CREATING;
57: boolean legalEventType = eventType == EventType.RELEASED;
58: boolean shapeAndGeomNotNull = handler.getCurrentShape() != null;
59: boolean button1Released = e.button == MapMouseEvent.BUTTON1;
60:
61: return legalState && legalEventType && shapeAndGeomNotNull
62: && button1Released && !e.buttonsDown()
63: && !e.modifiersDown();
64: }
65:
66: public UndoableMapCommand getCommand(EditToolHandler handler,
67: MapMouseEvent e, EventType eventType) {
68: if (!isValid(handler, e, eventType))
69: throw new IllegalArgumentException(
70: "Current state is not legal"); //$NON-NLS-1$
71: List<UndoableMapCommand> commands = new ArrayList<UndoableMapCommand>();
72:
73: commands.add(handler.getCommand(handler.getAcceptBehaviours()));
74: if (handler.getCurrentState() == EditState.CREATING)
75: commands.add(new SetEditStateCommand(handler,
76: EditState.MODIFYING));
77: UndoableComposite undoableComposite = new UndoableComposite(
78: commands);
79: undoableComposite.setMap(handler.getContext().getMap());
80: try {
81: undoableComposite.run(new NullProgressMonitor());
82: } catch (Exception e1) {
83: throw new RuntimeException(e1);
84: }
85: return new UndoRedoCommand(undoableComposite);
86: }
87:
88: public void handleError(EditToolHandler handler, Throwable error,
89: UndoableMapCommand command) {
90: EditPlugin.log("", error); //$NON-NLS-1$
91: }
92:
93: }
|