001 /**
002 * <copyright>
003 *
004 * Copyright (c) 2002-2006 IBM Corporation and others.
005 * All rights reserved. This program and the accompanying materials
006 * are made available under the terms of the Eclipse Public License v1.0
007 * which accompanies this distribution, and is available at
008 * http://www.eclipse.org/legal/epl-v10.html
009 *
010 * Contributors:
011 * IBM - Initial API and implementation
012 *
013 * </copyright>
014 *
015 * $Id: CommandWrapper.java,v 1.3 2006/12/05 20:19:54 emerks Exp $
016 */
017 package org.eclipse.emf.common.command;
018
019
020 import java.util.Collection;
021 import java.util.Collections;
022
023 import org.eclipse.emf.common.CommonPlugin;
024
025
026 /**
027 * A command that wraps another command.
028 * All the {@link Command} methods are delegated to the wrapped command.
029 *
030 * <p>
031 * There are two typical usage patterns.
032 * One typical use for this command is to modify the behaviour of a command that you can't subclass, i.e., a decorator pattern:
033 *<pre>
034 * Command decoratedCommand =
035 * new CommandWrapper(someOtherCommand)
036 * {
037 * public void execute()
038 * {
039 * doSomethingBeforeExecution();
040 * super.execute();
041 * doSomethingAfterExecution();
042 * }
043 * public Collection getResult()
044 * {
045 * return someOtherResult();
046 * }
047 * };
048 *</pre>
049 * The other typical use is to act as a proxy for a command who's creation is delayed:
050 *<pre>
051 * Command proxyCommand =
052 * new CommandWrapper()
053 * {
054 * public Command createCommand()
055 * {
056 * return createACommandSomehow();
057 * }
058 * };
059 *</pre>
060 */
061 public class CommandWrapper extends AbstractCommand
062 {
063 /**
064 * The command for which this is a proxy or decorator.
065 */
066 protected Command command;
067
068 /**
069 * Creates a decorator instance for the given command.
070 * @param command the command to wrap.
071 */
072 public CommandWrapper(Command command)
073 {
074 super(command.getLabel(), command.getDescription());
075 this.command = command;
076 }
077
078 /**
079 * Creates a decorator instance with the given label for the given command.
080 * @param label the label of the wrapper
081 * @param command the command to wrap.
082 */
083 protected CommandWrapper(String label, Command command)
084 {
085 super(label, command.getDescription());
086 this.command = command;
087 }
088
089 /**
090 * Creates a decorator instance with the given label and description for the given command.
091 * @param label the label of the wrapper
092 * @param description the description of the wrapper
093 * @param command the command to wrap.
094 */
095 public CommandWrapper(String label, String description, Command command)
096 {
097 super(label, description);
098 this.command = command;
099 }
100
101 /**
102 * Creates a commandless proxy instance.
103 * The wrapped command will be created by a {@link #createCommand} callback.
104 * Since a proxy command like this is pointless unless you override some method, this constructor is protected.
105 */
106 protected CommandWrapper()
107 {
108 super();
109 }
110
111 /**
112 * Creates a commandless proxy instance, with the given label.
113 * The command will be created by a {@link #createCommand} callback.
114 * Since a proxy command like this is pointless unless you override some method, this constructor is protected.
115 * @param label the label of the wrapper
116 */
117 protected CommandWrapper(String label)
118 {
119 super(label);
120 }
121
122 /**
123 * Creates a commandless proxy instance, with the given label and description.
124 * The command will be created by a {@link #createCommand} callback.
125 * Since a proxy command like this is pointless unless you override some method, this constructor is protected.
126 * @param label the label of the wrapper
127 * @param description the description of the wrapper
128 */
129 protected CommandWrapper(String label, String description)
130 {
131 super(label, description);
132 }
133
134 /**
135 * Returns the command for which this is a proxy or decorator.
136 * This may be <code>null</code> before {@link #createCommand} is called.
137 * @return the command for which this is a proxy or decorator.
138 */
139 public Command getCommand()
140 {
141 return command;
142 }
143
144 /**
145 * Create the command being proxied.
146 * This implementation just return <code>null</code>.
147 * It is called by {@link #prepare}.
148 * @return the command being proxied.
149 */
150 protected Command createCommand()
151 {
152 return null;
153 }
154
155 /**
156 * Returns whether the command can execute.
157 * This implementation creates the command being proxied using {@link #createCommand},
158 * if the command wasn't given in the constructor.
159 * @return whether the command can execute.
160 */
161 @Override
162 protected boolean prepare()
163 {
164 if (command == null)
165 {
166 command = createCommand();
167 }
168
169 boolean result = command.canExecute();
170 return result;
171 }
172
173 /**
174 * Delegates to the execute method of the command.
175 */
176 public void execute()
177 {
178 if (command != null)
179 {
180 command.execute();
181 }
182 }
183
184 /**
185 * Delegates to the canUndo method of the command.
186 */
187 @Override
188 public boolean canUndo()
189 {
190 return command == null || command.canUndo();
191 }
192
193 /**
194 * Delegates to the undo method of the command.
195 */
196 @Override
197 public void undo()
198 {
199 if (command != null)
200 {
201 command.undo();
202 }
203 }
204
205 /**
206 * Delegates to the redo method of the command.
207 */
208 public void redo()
209 {
210 if (command != null)
211 {
212 command.redo();
213 }
214 }
215
216 /**
217 * Delegates to the getResult method of the command.
218 * @return the result.
219 */
220 @Override
221 public Collection<?> getResult()
222 {
223 return
224 command == null ?
225 Collections.EMPTY_LIST :
226 command.getResult();
227 }
228
229 /**
230 * Delegates to the getAffectedObjects method of the command.
231 * @return the result.
232 */
233 @Override
234 public Collection<?> getAffectedObjects()
235 {
236 return
237 command == null ?
238 Collections.EMPTY_LIST :
239 command.getAffectedObjects();
240 }
241
242 /**
243 * Delegates to the getLabel method of the command.
244 * @return the label.
245 */
246 @Override
247 public String getLabel()
248 {
249 return
250 label == null ?
251 command == null ?
252 CommonPlugin.INSTANCE.getString("_UI_CommandWrapper_label") :
253 command.getLabel() :
254 label;
255 }
256
257 /**
258 * Delegates to the getDescription method of the command.
259 * @return the description.
260 */
261 @Override
262 public String getDescription()
263 {
264 return
265 description == null ?
266 command == null ?
267 CommonPlugin.INSTANCE.getString("_UI_CommandWrapper_description") :
268 command.getDescription() :
269 description;
270 }
271
272 /**
273 * Delegates to the dispose method of the command.
274 */
275 @Override
276 public void dispose()
277 {
278 if (command != null)
279 {
280 command.dispose();
281 }
282 }
283
284 /*
285 * Javadoc copied from base class.
286 */
287 @Override
288 public String toString()
289 {
290 StringBuffer result = new StringBuffer(super.toString());
291 result.append(" (command: " + command + ")");
292
293 return result.toString();
294 }
295 }