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;
016:
017: import java.util.ArrayList;
018: import java.util.List;
019:
020: import net.refractions.udig.project.command.UndoableMapCommand;
021: import net.refractions.udig.project.ui.render.displayAdapter.MapMouseEvent;
022:
023: /**
024: * A Composite Mode (See GOF Composite Pattern) where contained modes are ordered by priority and only the
025: * mode with the highest priority and is valid for the current context is ran.
026: *
027: * @author jones
028: * @since 1.1.0
029: */
030: public class MutualExclusiveEventBehavior implements EventBehaviour,
031: LockingBehaviour {
032:
033: private List<EventBehaviour> behaviours = new ArrayList<EventBehaviour>();
034: private EventBehaviour current;
035:
036: public MutualExclusiveEventBehavior(List<EventBehaviour> modes) {
037: this .behaviours = modes;
038: }
039:
040: /**
041: * @param behaviour
042: */
043: public MutualExclusiveEventBehavior(EventBehaviour behaviour) {
044: behaviours.add(behaviour);
045: }
046:
047: public boolean isValid(EditToolHandler handler, MapMouseEvent e,
048: EventType eventType) {
049: for (EventBehaviour mode : behaviours) {
050: if (mode.isValid(handler, e, eventType)) {
051: current = mode;
052: return true;
053: }
054: }
055: return false;
056: }
057:
058: public UndoableMapCommand getCommand(EditToolHandler handler,
059: MapMouseEvent e, EventType eventType) {
060: return current.getCommand(handler, e, eventType);
061:
062: }
063:
064: public void handleError(EditToolHandler handler, Throwable error,
065: UndoableMapCommand command) {
066: EditPlugin
067: .log(
068: "Very Strange I don't know how this happenned...", error); //$NON-NLS-1$
069: }
070:
071: @Override
072: public String toString() {
073: StringBuffer buffer = new StringBuffer("["); //$NON-NLS-1$
074: for (EventBehaviour behaviour : this .behaviours) {
075: buffer.append(behaviour.toString());
076: buffer.append("||"); //$NON-NLS-1$
077: }
078: buffer.reverse();
079: buffer.append(" "); //$NON-NLS-1$
080: buffer.reverse();
081: buffer.append("]"); //$NON-NLS-1$
082: return null;
083: }
084:
085: /**
086: * @return Returns the behaviours.
087: */
088: public List<EventBehaviour> getBehaviours() {
089: return this .behaviours;
090: }
091:
092: public Object getKey(EditToolHandler handler) {
093: for (EventBehaviour behaviour : behaviours) {
094: if (behaviour instanceof LockingBehaviour) {
095: LockingBehaviour locker = (LockingBehaviour) behaviour;
096: if (handler.isLockOwner(locker))
097: return locker.getKey(handler);
098: }
099: }
100: return NULL_KEY;
101: }
102:
103: private static final Object NULL_KEY = new Object();
104: }
|