KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > common > command > CommandWrapper


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2002-2004 IBM Corporation and others.
5  * All rights reserved. This program and the accompanying materials
6  * are made available under the terms of the Eclipse Public License v1.0
7  * which accompanies this distribution, and is available at
8  * http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Contributors:
11  * IBM - Initial API and implementation
12  *
13  * </copyright>
14  *
15  * $Id: CommandWrapper.java,v 1.2 2005/06/08 05:44:08 nickb Exp $
16  */

17 package org.eclipse.emf.common.command;
18
19
20 import java.util.Collection JavaDoc;
21 import java.util.Collections JavaDoc;
22
23 import org.eclipse.emf.common.CommonPlugin;
24
25
26 /**
27  * A command that wraps another command.
28  * All the {@link Command} methods are delegated to the wrapped command.
29  *
30  * <p>
31  * There are two typical usage patterns.
32  * One typical use for this command is to modify the behaviour of a command that you can't subclass, i.e., a decorator pattern:
33  *<pre>
34  * Command decoratedCommand =
35  * new CommandWrapper(someOtherCommand)
36  * {
37  * public void execute()
38  * {
39  * doSomethingBeforeExecution();
40  * super.execute();
41  * doSomethingAfterExecution();
42  * }
43  * public Collection getResult()
44  * {
45  * return someOtherResult();
46  * }
47  * };
48  *</pre>
49  * The other typical use is to act as a proxy for a command who's creation is delayed:
50  *<pre>
51  * Command proxyCommand =
52  * new CommandWrapper()
53  * {
54  * public Command createCommand()
55  * {
56  * return createACommandSomehow();
57  * }
58  * };
59  *</pre>
60  */

61 public class CommandWrapper extends AbstractCommand
62 {
63   /**
64    * The command for which this is a proxy or decorator.
65    */

66   protected Command command;
67
68   /**
69    * Creates a decorator instance for the given command.
70    * @param command the command to wrap.
71    */

72   public CommandWrapper(Command command)
73   {
74     super(command.getLabel(), command.getDescription());
75     this.command = command;
76   }
77
78   /**
79    * Creates a decorator instance with the given label for the given command.
80    * @param label the label of the wrapper
81    * @param command the command to wrap.
82    */

83   protected CommandWrapper(String JavaDoc label, Command command)
84   {
85     super(label, command.getDescription());
86     this.command = command;
87   }
88
89   /**
90    * Creates a decorator instance with the given label and description for the given command.
91    * @param label the label of the wrapper
92    * @param description the description of the wrapper
93    * @param command the command to wrap.
94    */

95   public CommandWrapper(String JavaDoc label, String JavaDoc description, Command command)
96   {
97     super(label, description);
98     this.command = command;
99   }
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 JavaDoc 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 JavaDoc label, String JavaDoc 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   protected boolean prepare()
162   {
163     if (command == null)
164     {
165       command = createCommand();
166     }
167
168     boolean result = command.canExecute();
169     return result;
170   }
171
172   /**
173    * Delegates to the execute method of the command.
174    */

175   public void execute()
176   {
177     if (command != null)
178     {
179       command.execute();
180     }
181   }
182
183   /**
184    * Delegates to the canUndo method of the command.
185    */

186   public boolean canUndo()
187   {
188     return command == null || command.canUndo();
189   }
190
191   /**
192    * Delegates to the undo method of the command.
193    */

194   public void undo()
195   {
196     if (command != null)
197     {
198       command.undo();
199     }
200   }
201
202   /**
203    * Delegates to the redo method of the command.
204    */

205   public void redo()
206   {
207     if (command != null)
208     {
209       command.redo();
210     }
211   }
212
213   /**
214    * Delegates to the getResult method of the command.
215    * @return the result.
216    */

217   public Collection JavaDoc getResult()
218   {
219     return
220       command == null ?
221         Collections.EMPTY_LIST :
222         command.getResult();
223   }
224
225   /**
226    * Delegates to the getAffectedObjects method of the command.
227    * @return the result.
228    */

229   public Collection JavaDoc getAffectedObjects()
230   {
231     return
232       command == null ?
233         Collections.EMPTY_LIST :
234         command.getAffectedObjects();
235   }
236
237   /**
238    * Delegates to the getLabel method of the command.
239    * @return the label.
240    */

241   public String JavaDoc getLabel()
242   {
243     return
244       label == null ?
245         command == null ?
246           CommonPlugin.INSTANCE.getString("_UI_CommandWrapper_label") :
247           command.getLabel() :
248         label;
249   }
250
251   /**
252    * Delegates to the getDescription method of the command.
253    * @return the description.
254    */

255   public String JavaDoc getDescription()
256   {
257     return
258       description == null ?
259         command == null ?
260           CommonPlugin.INSTANCE.getString("_UI_CommandWrapper_description") :
261           command.getDescription() :
262         description;
263   }
264
265   /**
266    * Delegates to the dispose method of the command.
267    */

268   public void dispose()
269   {
270     if (command != null)
271     {
272       command.dispose();
273     }
274   }
275
276   /*
277    * Javadoc copied from base class.
278    */

279   public String JavaDoc toString()
280   {
281     StringBuffer JavaDoc result = new StringBuffer JavaDoc(super.toString());
282     result.append(" (command: " + command + ")");
283
284     return result.toString();
285   }
286 }
287
Popular Tags