KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > edit > command > AbstractOverrideableCommand


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: AbstractOverrideableCommand.java,v 1.2 2005/06/08 06:17:05 nickb Exp $
16  */

17 package org.eclipse.emf.edit.command;
18
19
20 import java.util.Collection JavaDoc;
21 import java.util.Collections JavaDoc;
22
23 import org.eclipse.emf.common.command.AbstractCommand;
24 import org.eclipse.emf.common.command.Command;
25 import org.eclipse.emf.common.util.EList;
26 import org.eclipse.emf.ecore.EObject;
27 import org.eclipse.emf.ecore.EStructuralFeature;
28 import org.eclipse.emf.edit.domain.EditingDomain;
29
30
31 /**
32  * This is a convenient base class for classes that implement {@link OverrideableCommand}.
33  * Subclasses of AbstractOverrideableCommand should provide implementations of the doXxx methods
34  * (e.g., doExecute) from OverrideableCommand instead of the base command methods (e.g.,
35  * execute), which have final implementations here.
36  */

37 public abstract class AbstractOverrideableCommand extends AbstractCommand implements OverrideableCommand
38 {
39   /**
40    * This is the editing doman in which this command operates.
41    */

42   protected EditingDomain domain;
43
44   /**
45    * This is the command that overrides this command.
46    */

47   protected Command overrideCommand;
48
49   /**
50    * This constructs an instance in this editing domain.
51    */

52   protected AbstractOverrideableCommand(EditingDomain domain)
53   {
54     this(domain, null, null);
55   }
56
57   /**
58    * This constructs an instance with the given label and in this editing domain.
59    */

60   protected AbstractOverrideableCommand(EditingDomain domain, String JavaDoc label)
61   {
62     this(domain, label, null);
63   }
64
65   /**
66    * This constructs an instance with the given label and description, in this editing domain.
67    */

68   protected AbstractOverrideableCommand(EditingDomain domain, String JavaDoc label, String JavaDoc description)
69   {
70     super(label, description);
71
72     this.domain = domain;
73   }
74
75   /**
76    * This returns the editing domain that contains this.
77    */

78   public EditingDomain getDomain()
79   {
80     return domain;
81   }
82
83   /**
84    * This returns the command that overrides this command.
85    */

86   public Command getOverride()
87   {
88     return overrideCommand;
89   }
90
91   /**
92    * This sets the command that overrides this command.
93    */

94   public void setOverride(Command overrideCommand)
95   {
96     this.overrideCommand = overrideCommand;
97   }
98
99   public final boolean canExecute()
100   {
101     if (domain != null && !isPrepared)
102     {
103       Command newOverrideCommand = domain.createOverrideCommand(this);
104       setOverride(newOverrideCommand);
105     }
106
107     boolean result =
108       overrideCommand != null ?
109         overrideCommand.canExecute() :
110         doCanExecute();
111     
112     return result;
113   }
114
115   public boolean doCanExecute()
116   {
117     return super.canExecute();
118   }
119
120   public final void execute()
121   {
122     if (overrideCommand != null)
123     {
124       overrideCommand.execute();
125     }
126     else
127     {
128       doExecute();
129     }
130   }
131
132   public abstract void doExecute();
133
134   public final boolean canUndo()
135   {
136     boolean result =
137       overrideCommand != null ?
138         overrideCommand.canUndo() :
139         doCanUndo();
140
141     return result;
142   }
143
144   public boolean doCanUndo()
145   {
146     return super.canUndo();
147   }
148
149   public final void undo()
150   {
151     if (overrideCommand != null)
152     {
153       overrideCommand.undo();
154     }
155     else
156     {
157       doUndo();
158     }
159   }
160
161   public abstract void doUndo();
162
163   public final void redo()
164   {
165     if (overrideCommand != null)
166     {
167       overrideCommand.redo();
168     }
169     else
170     {
171       doRedo();
172     }
173   }
174
175   public abstract void doRedo();
176
177   public final Collection JavaDoc getResult()
178   {
179     return
180       overrideCommand != null ?
181         overrideCommand.getResult() :
182         doGetResult();
183   }
184
185   public Collection JavaDoc doGetResult()
186   {
187     return super.getResult();
188   }
189
190   public final Collection JavaDoc getAffectedObjects()
191   {
192     return
193       overrideCommand != null ?
194         overrideCommand.getAffectedObjects() :
195         doGetAffectedObjects();
196   }
197
198   public Collection JavaDoc doGetAffectedObjects()
199   {
200     return super.getAffectedObjects();
201   }
202
203   public final String JavaDoc getLabel()
204   {
205     return
206       overrideCommand != null ?
207         overrideCommand.getLabel() :
208         doGetLabel();
209   }
210
211   public String JavaDoc doGetLabel()
212   {
213     return super.getLabel();
214   }
215
216   public final String JavaDoc getDescription()
217   {
218     return
219       overrideCommand != null ?
220         overrideCommand.getDescription() :
221         doGetDescription();
222   }
223
224   public String JavaDoc doGetDescription()
225   {
226     return super.getDescription();
227   }
228
229   public final void dispose()
230   {
231     if (overrideCommand != null)
232     {
233       overrideCommand.dispose();
234     }
235     else
236     {
237       doDispose();
238     }
239   }
240
241   public void doDispose()
242   {
243     super.dispose();
244   }
245
246   public final Collection JavaDoc getChildrenToCopy()
247   {
248     Collection JavaDoc result =
249       overrideCommand instanceof ChildrenToCopyProvider ?
250         ((ChildrenToCopyProvider)overrideCommand).getChildrenToCopy() :
251         doGetChildrenToCopy();
252
253     return result;
254   }
255
256   public Collection JavaDoc doGetChildrenToCopy()
257   {
258     return Collections.EMPTY_LIST;
259   }
260
261   public static EList getOwnerList(EObject owner, EStructuralFeature feature)
262   {
263     return
264       owner.eClass().getEAllStructuralFeatures().contains(feature) && feature.isMany() ?
265         (EList)owner.eGet(feature) :
266         null;
267   }
268
269   /**
270    * This gives an abbreviated name using this object's own class' name, without package qualification,
271    * followed by a space separated list of <tt>field:value</tt> pairs.
272    */

273   public String JavaDoc toString()
274   {
275     StringBuffer JavaDoc result = new StringBuffer JavaDoc(super.toString());
276     result.append(" (domain: " + domain + ")");
277     result.append(" (overrideCommand: " + overrideCommand + ")");
278     return result.toString();
279   }
280 }
281
Popular Tags