001: /*
002: * uDig - User Friendly Desktop Internet GIS client http://udig.refractions.net (C) 2004,
003: * Refractions Research Inc. This library is free software; you can redistribute it and/or modify it
004: * under the terms of the GNU Lesser General Public License as published by the Free Software
005: * Foundation; version 2.1 of the License. This library is distributed in the hope that it will be
006: * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
007: * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
008: */
009: package net.refractions.udig.project.command;
010:
011: import java.util.List;
012:
013: import org.eclipse.core.runtime.IProgressMonitor;
014: import org.eclipse.core.runtime.SubProgressMonitor;
015:
016: /**
017: * A UndoableCommand composed of multiple UndoableCommands. Executes and rollsback as a atomic
018: * command. See Composite Pattern.
019: *
020: * @author jeichar
021: * @since 0.3
022: * @see CompositeCommand
023: * @see UndoableCommand
024: */
025: public class UndoableComposite extends CompositeCommand implements
026: UndoableMapCommand, PostDeterminedEffectCommand {
027:
028: /**
029: * Creates a new instance of UndoableComposite
030: *
031: * @param undoableCommands an ordered list of UndoableCommands
032: */
033: public UndoableComposite() {
034: super ();
035: }
036:
037: /**
038: * Creates a new instance of UndoableComposite
039: *
040: * API List<UndoableCommand>
041: *
042: * @param undoableCommands an ordered list of UndoableCommands
043: */
044: public UndoableComposite(List undoableCommands) {
045: super (undoableCommands);
046: }
047:
048: @Override
049: public void run(IProgressMonitor monitor) throws Exception {
050: execute(monitor);
051: }
052:
053: /**
054: * @see net.refractions.udig.project.internal.command.UndoableCommand#rollback()
055: */
056: public void rollback(IProgressMonitor monitor) throws Exception {
057:
058: for (int i = finalizerCommands.size() - 1; i > -1; i--) {
059: UndoableCommand command = (UndoableCommand) finalizerCommands
060: .get(i);
061: command.rollback(monitor);
062: }
063:
064: for (int i = commands.size() - 1; i > -1; i--) {
065: UndoableCommand command = (UndoableCommand) commands.get(i);
066: command.rollback(monitor);
067: }
068: }
069:
070: public boolean execute(IProgressMonitor monitor) throws Exception {
071: monitor.beginTask(getName(), 2 + 10 * commands.size() + 10
072: * finalizerCommands.size());
073: monitor.worked(2);
074: boolean changedState = false;
075: try {
076: for (MapCommand command : commands) {
077: command.setMap(getMap());
078: SubProgressMonitor subProgressMonitor = new SubProgressMonitor(
079: monitor, 10);
080: if (command instanceof PostDeterminedEffectCommand) {
081: boolean change = ((PostDeterminedEffectCommand) command)
082: .execute(subProgressMonitor);
083: changedState = changedState || change;
084: } else {
085: command.run(subProgressMonitor);
086: changedState = true;
087: }
088: subProgressMonitor.done();
089: }
090: } finally {
091: for (MapCommand command : finalizerCommands) {
092: command.setMap(getMap());
093: SubProgressMonitor subProgressMonitor = new SubProgressMonitor(
094: monitor, 10);
095: if (command instanceof PostDeterminedEffectCommand) {
096: boolean change = ((PostDeterminedEffectCommand) command)
097: .execute(subProgressMonitor);
098: changedState = changedState || change;
099: } else {
100: command.run(subProgressMonitor);
101: changedState = true;
102: }
103: subProgressMonitor.done();
104: }
105:
106: }
107: monitor.done();
108:
109: return changedState;
110: }
111:
112: }
|