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:
022: /**
023: * A Composite Mode (See GOF Composite Pattern) where contained modes are ordered by priority and only the
024: * mode with the highest priority and is valid for the current context is ran.
025: *
026: * @author jones
027: * @since 1.1.0
028: */
029: public class MutualExclusiveBehavior implements Behaviour {
030:
031: private List<Behaviour> behaviours = new ArrayList<Behaviour>();
032:
033: public MutualExclusiveBehavior(List<Behaviour> modes) {
034: this .behaviours = modes;
035: }
036:
037: /**
038: * @param behaviour
039: */
040: public MutualExclusiveBehavior(Behaviour behaviour) {
041: behaviours.add(behaviour);
042: }
043:
044: /**
045: * Create an empty behaviour. Behaviours must be added via the {@link #getBehaviours()} method
046: */
047: public MutualExclusiveBehavior() {
048: }
049:
050: public boolean isValid(EditToolHandler handler) {
051: for (Behaviour mode : behaviours) {
052: EditPlugin
053: .trace(
054: EditPlugin.BEHAVIOUR,
055: " Validating mode: " + mode.getClass().getName(), null); //$NON-NLS-1$
056: if (mode.isValid(handler))
057: return true;
058: }
059: return false;
060: }
061:
062: public UndoableMapCommand getCommand(EditToolHandler handler) {
063: List<Behaviour> behaviours = new ArrayList<Behaviour>(
064: this .behaviours);
065: for (Behaviour mode : behaviours) {
066: if (mode.isValid(handler)) {
067: UndoableMapCommand c = null;
068: try {
069: c = mode.getCommand(handler);
070: EditPlugin
071: .trace(
072: EditPlugin.BEHAVIOUR,
073: " Running mode: " + mode.getClass().getName(), null); //$NON-NLS-1$
074:
075: return c;
076: } catch (Throwable error) {
077: EditPlugin
078: .trace(
079: EditPlugin.BEHAVIOUR,
080: "Error running mode: " + mode.getClass().getName(), null); //$NON-NLS-1$
081: mode.handleError(handler, error, c);
082: }
083: }
084: }
085: return null;
086:
087: }
088:
089: public void handleError(EditToolHandler handler, Throwable error,
090: UndoableMapCommand command) {
091: EditPlugin
092: .log(
093: "Very Strange I don't know how this happenned...", error); //$NON-NLS-1$
094: }
095:
096: @Override
097: public String toString() {
098: StringBuffer buffer = new StringBuffer("["); //$NON-NLS-1$
099: for (Behaviour behaviour : this .behaviours) {
100: buffer.append(behaviour.toString());
101: buffer.append("||"); //$NON-NLS-1$
102: }
103: buffer.reverse();
104: buffer.append(" "); //$NON-NLS-1$
105: buffer.reverse();
106: buffer.append("]"); //$NON-NLS-1$
107: return buffer.toString();
108: }
109:
110: /**
111: * @return Returns the behaviours.
112: */
113: public List<Behaviour> getBehaviours() {
114: return this.behaviours;
115: }
116:
117: }
|