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.LinkedList;
018: import java.util.List;
019:
020: import net.refractions.udig.project.command.AbstractCommand;
021: import net.refractions.udig.project.command.PostDeterminedEffectCommand;
022: import net.refractions.udig.project.command.UndoableMapCommand;
023: import net.refractions.udig.project.ui.render.displayAdapter.MapMouseEvent;
024: import net.refractions.udig.tool.edit.internal.Messages;
025:
026: import org.eclipse.core.runtime.IProgressMonitor;
027: import org.eclipse.core.runtime.SubProgressMonitor;
028:
029: /**
030: * Command used by (@link net.refractions.udig.tools.edit.EditToolHandler} and
031: * {@link net.refractions.udig.tools.edit.OrderedCompositeEventBehavior} for executing the
032: * behaviours in a tool.
033: *
034: * @author jones
035: * @since 1.1.0
036: */
037: public class EventBehaviourCommand extends AbstractCommand implements
038: PostDeterminedEffectCommand {
039:
040: private List<EventBehaviour> behaviours;
041: private List<UndoableMapCommand> commandsRan = new LinkedList<UndoableMapCommand>();
042: private EditToolHandler handler;
043: private MapMouseEvent event;
044: private EventType eventType;
045:
046: public EventBehaviourCommand(List<EventBehaviour> behaviours,
047: EditToolHandler handler, MapMouseEvent event,
048: EventType eventType) {
049: this .behaviours = behaviours;
050: this .handler = handler;
051: this .event = event;
052: this .eventType = eventType;
053: }
054:
055: public boolean execute(IProgressMonitor monitor) throws Exception {
056: if (commandsRan.isEmpty()) {
057: monitor.beginTask(getName(), commandsRan.size() * 12);
058: monitor.worked(2);
059: for (EventBehaviour behaviour : behaviours) {
060:
061: if (canUnlock(behaviour)
062: && behaviour.isValid(handler, event, eventType)) {
063: UndoableMapCommand c = null;
064: try {
065: c = behaviour.getCommand(handler, event,
066: eventType);
067: if (c == null)
068: continue;
069: IProgressMonitor submonitor = new SubProgressMonitor(
070: monitor, 10);
071: c.setMap(getMap());
072: if (c instanceof PostDeterminedEffectCommand) {
073: PostDeterminedEffectCommand command = (PostDeterminedEffectCommand) c;
074: if (command.execute(submonitor))
075: commandsRan.add(command);
076: } else {
077: c.run(submonitor);
078: commandsRan.add(c);
079: }
080: submonitor.done();
081: } catch (Exception e) {
082: behaviour.handleError(handler, e, c);
083: }
084: }
085: }
086: } else {
087: monitor.beginTask(getName(), commandsRan.size() * 12);
088: monitor.worked(2);
089: for (UndoableMapCommand command : commandsRan) {
090: command.setMap(getMap());
091: IProgressMonitor submonitor = new SubProgressMonitor(
092: monitor, 10);
093: if (command instanceof PostDeterminedEffectCommand) {
094: ((PostDeterminedEffectCommand) command)
095: .execute(submonitor);
096: } else {
097: command.run(submonitor);
098: }
099: submonitor.done();
100: }
101: }
102: monitor.done();
103: return !commandsRan.isEmpty();
104: }
105:
106: /**
107: * Returns true if the handler is unlocked or the behaviour has the correct key.
108: *
109: * @param behaviour trying to run
110: * @return Returns true if the handler is unlocked or the behaviour has the correct key.
111: */
112: private boolean canUnlock(EventBehaviour behaviour) {
113: if (!handler.isLocked())
114: return true;
115: if (behaviour instanceof LockingBehaviour) {
116: LockingBehaviour locker = (LockingBehaviour) behaviour;
117:
118: EditPlugin
119: .trace(
120: EditPlugin.HANDLER_LOCK,
121: "Can unlock: " + (handler.behaviourLock == locker.getKey(handler)), null); //$NON-NLS-1$
122: return handler.behaviourLock == locker.getKey(handler);
123: }
124: return false;
125: }
126:
127: String name = Messages.EventBehaviourCommand_name;
128:
129: public String getName() {
130: return name;
131: }
132:
133: public void setName(String name) {
134: this .name = name;
135: }
136:
137: public void rollback(IProgressMonitor monitor) throws Exception {
138: monitor.beginTask(getName(), commandsRan.size() * 12);
139: monitor.worked(2);
140: for (UndoableMapCommand command : commandsRan) {
141: command.setMap(getMap());
142: IProgressMonitor submonitor = new SubProgressMonitor(
143: monitor, 10);
144: command.rollback(submonitor);
145: submonitor.done();
146: }
147: }
148:
149: public void run(IProgressMonitor monitor) throws Exception {
150: throw new UnsupportedOperationException(
151: "PostDeterminedEffectCommands do not use the run method"); //$NON-NLS-1$
152: }
153: }
|