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;
16:
17: import java.util.ArrayList;
18: import java.util.List;
19:
20: import net.refractions.udig.project.command.UndoableComposite;
21: import net.refractions.udig.project.command.UndoableMapCommand;
22: import net.refractions.udig.project.ui.render.displayAdapter.MapMouseEvent;
23:
24: /**
25: * A Composite Mode (See GOF Composite Pattern) where contained modes are ordered and are executed in order.
26: * Each mode can see the state changes affected by the prior mode.
27: *
28: * @author jones
29: * @since 1.1.0
30: */
31: public class OrderedCompositeEventBehavior implements EventBehaviour,
32: LockingBehaviour {
33: private List<EventBehaviour> modes = new ArrayList<EventBehaviour>();
34: private boolean processAsCommand;
35:
36: public OrderedCompositeEventBehavior(List<EventBehaviour> modes,
37: boolean processAsCommand) {
38: this .modes = modes;
39: this .processAsCommand = processAsCommand;
40: }
41:
42: public boolean isValid(EditToolHandler handler, MapMouseEvent e,
43: EventType eventType) {
44: if (processAsCommand)
45: return true;
46:
47: for (EventBehaviour mode : modes) {
48: if (mode.isValid(handler, e, eventType)) {
49: return true;
50: }
51: }
52: return false;
53: }
54:
55: public UndoableMapCommand getCommand(EditToolHandler handler,
56: MapMouseEvent e, EventType eventType) {
57: if (processAsCommand)
58: return new EventBehaviourCommand(modes, handler, e,
59: eventType);
60:
61: UndoableComposite command = new UndoableComposite();
62: for (EventBehaviour mode : modes) {
63: if (mode.isValid(handler, e, eventType)) {
64: UndoableMapCommand command2 = mode.getCommand(handler,
65: e, eventType);
66: if (command2 != null)
67: command.getCommands().add(command2);
68: }
69: }
70: return command;
71: }
72:
73: public void handleError(EditToolHandler handler, Throwable error,
74: UndoableMapCommand command) {
75: EditPlugin
76: .log(
77: "Very Strange I don't know how this happenned...", error); //$NON-NLS-1$
78: }
79:
80: public Object getKey(EditToolHandler handler) {
81: for (EventBehaviour behaviour : modes) {
82: if (behaviour instanceof LockingBehaviour) {
83: LockingBehaviour locker = (LockingBehaviour) behaviour;
84: if (handler.isLockOwner(locker))
85: return locker.getKey(handler);
86: }
87: }
88: return NULL_KEY;
89: }
90:
91: private static final Object NULL_KEY = new Object();
92:
93: @Override
94: public String toString() {
95: return this.modes.toString();
96: }
97: }
|