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.tool.edit.internal.Messages;
024:
025: import org.eclipse.core.runtime.IProgressMonitor;
026: import org.eclipse.core.runtime.SubProgressMonitor;
027:
028: /**
029: * Command used by (@link net.refractions.udig.tools.edit.EditToolHandler} and
030: * {@link net.refractions.udig.tools.edit.OrderedCompositeEventBehavior} for executing the
031: * behaviours in a tool.
032: *
033: * @author jones
034: * @since 1.1.0
035: */
036: public class BehaviourCommand extends AbstractCommand implements
037: PostDeterminedEffectCommand {
038:
039: private List<Behaviour> behaviours;
040: private List<UndoableMapCommand> commandsRan = new LinkedList<UndoableMapCommand>();
041: private EditToolHandler handler;
042:
043: public BehaviourCommand(List<Behaviour> behaviours,
044: EditToolHandler handler) {
045: this .behaviours = behaviours;
046: this .handler = handler;
047: }
048:
049: public boolean execute(IProgressMonitor monitor) throws Exception {
050: if (commandsRan.isEmpty()) {
051: monitor.beginTask(getName(), commandsRan.size() * 12);
052: monitor.worked(2);
053: for (Behaviour behaviour : behaviours) {
054: monitor.beginTask(getName(), behaviours.size() * 12);
055: monitor.worked(2);
056: if (behaviour.isValid(handler)) {
057: UndoableMapCommand c = null;
058: try {
059: c = behaviour.getCommand(handler);
060: if (c == null)
061: continue;
062: c.setMap(getMap());
063: IProgressMonitor submonitor = new SubProgressMonitor(
064: monitor, 10);
065: if (c instanceof PostDeterminedEffectCommand) {
066: PostDeterminedEffectCommand command = (PostDeterminedEffectCommand) c;
067: if (command.execute(submonitor))
068: commandsRan.add(command);
069: } else {
070: c.run(submonitor);
071: commandsRan.add(c);
072: }
073: submonitor.done();
074: } catch (Exception e) {
075: behaviour.handleError(handler, e, c);
076: }
077: }
078: }
079: } else {
080: monitor.beginTask(getName(), commandsRan.size() * 12);
081: monitor.worked(2);
082: for (UndoableMapCommand command : commandsRan) {
083: command.setMap(getMap());
084: IProgressMonitor submonitor = new SubProgressMonitor(
085: monitor, 10);
086: if (command instanceof PostDeterminedEffectCommand) {
087: ((PostDeterminedEffectCommand) command)
088: .execute(submonitor);
089: } else {
090: command.run(submonitor);
091: }
092: submonitor.done();
093: }
094: }
095: return !commandsRan.isEmpty();
096: }
097:
098: String name = Messages.EventBehaviourCommand_name;
099:
100: public String getName() {
101: return name;
102: }
103:
104: public void setName(String name) {
105: this .name = name;
106: }
107:
108: public void rollback(IProgressMonitor monitor) throws Exception {
109: monitor.beginTask(getName(), commandsRan.size() * 12);
110: monitor.worked(2);
111: for (UndoableMapCommand command : commandsRan) {
112: command.setMap(getMap());
113: IProgressMonitor submonitor = new SubProgressMonitor(
114: monitor, 10);
115: command.rollback(submonitor);
116: submonitor.done();
117: }
118: }
119:
120: public void run(IProgressMonitor monitor) throws Exception {
121: throw new UnsupportedOperationException(
122: "PostDeterminedEffectCommands do not use the run method"); //$NON-NLS-1$
123: }
124:
125: }
|