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.LinkedList;
012: import java.util.List;
013: import java.util.concurrent.CopyOnWriteArrayList;
014:
015: import net.refractions.udig.project.IMap;
016: import net.refractions.udig.project.internal.Map;
017: import net.refractions.udig.project.internal.ProjectPlugin;
018:
019: import org.eclipse.core.runtime.IProgressMonitor;
020: import org.eclipse.core.runtime.SubProgressMonitor;
021:
022: /**
023: * A collection of Commands that are done as one command.
024: * See Gang of Four composite pattern.
025: * @author Jesse
026: * @since 1.0.0
027: */
028: public class CompositeCommand implements MapCommand {
029:
030: protected List<MapCommand> commands = new CopyOnWriteArrayList<MapCommand>();
031: protected List<MapCommand> finalizerCommands = new CopyOnWriteArrayList<MapCommand>();
032:
033: private Map map;
034:
035: private String name;
036:
037: /**
038: * Creates a new instance of CompositeCommand
039: *
040: * @param commands A list of commands to execute.
041: */
042: @SuppressWarnings("unchecked")//$NON-NLS-1$
043: public CompositeCommand(List commands) {
044: this .commands.addAll(commands);
045: }
046:
047: public CompositeCommand() {
048: super ();
049: }
050:
051: /**
052: * @see net.refractions.udig.project.internal.command.MapCommand#run()
053: */
054: public void run(IProgressMonitor monitor) throws Exception {
055: monitor.beginTask(getName(), 2 + 10 * commands.size() + 10
056: * finalizerCommands.size());
057: monitor.worked(2);
058: try {
059: for (MapCommand command : commands) {
060:
061: SubProgressMonitor subProgressMonitor = new SubProgressMonitor(
062: monitor, 10);
063: command.setMap(getMap());
064: command.run(subProgressMonitor);
065: subProgressMonitor.done();
066: }
067: } finally {
068: for (MapCommand c : finalizerCommands) {
069: try {
070: SubProgressMonitor subProgressMonitor = new SubProgressMonitor(
071: monitor, 10);
072: c.setMap(getMap());
073: c.run(subProgressMonitor);
074: subProgressMonitor.done();
075: } catch (Exception e) {
076: ProjectPlugin.log("", e); //$NON-NLS-1$
077: }
078: }
079: }
080: monitor.done();
081:
082: }
083:
084: /**
085: * @see net.refractions.udig.project.internal.command.MapCommand#copy()
086: */
087: public MapCommand copy() {
088: return new CompositeCommand(
089: new LinkedList<MapCommand>(commands));
090: }
091:
092: /**
093: * @see net.refractions.udig.project.command.MapCommand#getName()
094: */
095: public String getName() {
096: if (name == null)
097: return toString();
098: return name;
099: }
100:
101: public void setName(String newName) {
102: name = newName;
103: }
104:
105: /**
106: * @see net.refractions.udig.project.command.MapCommand#setMap(IMap)
107: * @uml.property name="map"
108: */
109: public void setMap(IMap map) {
110: this .map = (Map) map;
111: for (MapCommand command : commands) {
112: command.setMap(map);
113: }
114: }
115:
116: /**
117: * @see net.refractions.udig.project.command.MapCommand#getMap()
118: * @uml.property name="map"
119: */
120: public Map getMap() {
121: return map;
122: }
123:
124: @Override
125: public String toString() {
126: StringBuffer buffer = new StringBuffer("["); //$NON-NLS-1$
127:
128: for (MapCommand command : commands)
129: buffer.append(command.getName() + ","); //$NON-NLS-1$
130: buffer.replace(buffer.length() - 1, buffer.length(), "]"); //$NON-NLS-1$
131: return buffer.toString();
132: }
133:
134: /**
135: * @return Returns the commands.
136: */
137: public List<MapCommand> getCommands() {
138: return this .commands;
139: }
140:
141: /**
142: * @return Returns the commands that will always be run at the end of the command.
143: */
144: public List<MapCommand> getFinalizerCommands() {
145: return this.finalizerCommands;
146: }
147:
148: }
|