KickJava   Java API By Example, From Geeks To Geeks.

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


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: ReplaceCommand.java,v 1.4 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 import java.util.Iterator JavaDoc;
23
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.EMFEditPlugin;
29 import org.eclipse.emf.edit.domain.EditingDomain;
30
31
32 /**
33  * The replace command logically acts upon an owner object that has a collection-type feature
34  * in which an object can be replaced by a collection of other objects.
35  * The static create methods delegate command creation to {@link EditingDomain#createCommand EditingDomain.createCommand},
36  * which may or may not result in the actual creation of an instance of this class.
37  *
38  * <p>
39  * The implementation of this class is low-level and EMF specific;
40  * it allows an object from a many-valued feature of an owner to be replaced by a collection of other objects.
41  * i.e., it is equivalent of the call
42  * <pre>
43  * int index = ((EList)((EObject)owner).eGet((EStructuralFeature)feature)).indexOf(value);
44  * ((EList)((EObject)owner).eGet((EStructuralFeature)feature)).remove(value);
45  * ((EList)((EObject)owner).eGet((EStructuralFeature)feature)).addAll(index, (Collection)collection);
46  * </pre>
47  *
48  * <p>
49  * It can also be used as an equivalent to the call
50  * <pre>
51  * int index = ((EList)extent).indexOf(value);
52  * ((EList)extent).remove(value);
53  * ((EList)extent).addAll(index, (Collection)collection);
54  * </pre>
55  * which is how root objects are replaced in the contents of a resource.
56  * Like all the low level comands in this package, the replace command is undoable.
57  *
58  * <p>
59  * A replace command is an {@link OverrideableCommand}.
60  */

61 public class ReplaceCommand extends AbstractOverrideableCommand
62 {
63   /**
64    * This creates a command to replace an object with a collection of replacements.
65    */

66   public static Command create(EditingDomain domain, Object JavaDoc value, Collection JavaDoc collection)
67   {
68     return create(domain, null, null, value, collection);
69   }
70
71   /**
72    * This creates a command to replace a particular value in the specified feature of the owner with a collection replacements objects.
73    */

74   public static Command create(EditingDomain domain, Object JavaDoc owner, Object JavaDoc feature, Object JavaDoc value, Collection JavaDoc collection)
75   {
76     return domain.createCommand(ReplaceCommand.class, new CommandParameter(owner, feature, value, collection));
77   }
78
79   /**
80    * This caches the label.
81    */

82   protected static final String JavaDoc LABEL = EMFEditPlugin.INSTANCE.getString("_UI_ReplaceCommand_label");
83
84   /**
85    * This caches the description.
86    */

87   protected static final String JavaDoc DESCRIPTION = EMFEditPlugin.INSTANCE.getString("_UI_ReplaceCommand_description");
88
89   /**
90    * This is the owner object upon which the command will act.
91    * It could be null, in the case that we are dealing with an {@link org.eclipse.emf.common.util.EList}.
92    */

93   protected EObject owner;
94
95   /**
96    * This is the feature of the owner object upon the command will act.
97    * It could be null, in the case that we are dealing with an {@link org.eclipse.emf.common.util.EList}.
98    */

99   protected EStructuralFeature feature;
100
101   /**
102    * This is the list from which the command will replace.
103    */

104   protected EList ownerList;
105
106   /**
107    * This is value that is being replaced.
108    */

109   protected Object JavaDoc value;
110
111   /**
112    * This is the collection of replacements.
113    */

114   protected Collection JavaDoc collection;
115
116   /**
117    * This is the index at which to reinsert the replaced object during an undo so as to achieve the original list order.
118    */

119   protected int index;
120
121   /**
122    * The is the value returned by {@link Command#getAffectedObjects}.
123    * The affected objects are different after an execute than after an undo, so we record it.
124    */

125   protected Collection JavaDoc affectedObjects;
126
127   /**
128    * This constructs a primitive command to replace a particular value in the specified feature of the owner
129    * with the specified replacement.
130    */

131   public ReplaceCommand(EditingDomain domain, EObject owner, EStructuralFeature feature, Object JavaDoc value, Object JavaDoc replacement)
132   {
133     this(domain, owner, feature, value, Collections.singleton(replacement));
134   }
135
136   /**
137    * This constructs a primitive command to replace a particular value in the specified feature of the owner
138    * with the specified collection of replacements.
139    */

140   public ReplaceCommand(EditingDomain domain, EObject owner, EStructuralFeature feature, Object JavaDoc value, Collection JavaDoc collection)
141   {
142     super(domain, LABEL, DESCRIPTION);
143
144     // Initialize all the fields from the command parameter.
145
//
146
this.owner = owner;
147     this.feature = feature;
148     this.value = value;
149     this.collection = collection;
150
151     ownerList = getOwnerList(this.owner, feature);
152   }
153
154   /**
155    * This constructs a primitive command to replace a particular value in the specified extent with the given replacement.
156    */

157   public ReplaceCommand(EditingDomain domain, EList list, Object JavaDoc value, Object JavaDoc replacement)
158   {
159     this(domain, list, value, Collections.singleton(replacement));
160   }
161
162   /**
163    * This constructs a primitive command to replace a particular value in the specified extent with the given collection of replacements.
164    */

165   public ReplaceCommand(EditingDomain domain, EList list, Object JavaDoc value, Collection JavaDoc collection)
166   {
167     super(domain, LABEL, DESCRIPTION);
168
169     // Initialize all the fields from the command parameter.
170
//
171
this.value = value;
172     this.collection = collection;
173
174     ownerList = list;
175   }
176
177   /**
178    * This returns the owner object upon which the command will act.
179    * It could be null, in the case that we are dealing with an {@link org.eclipse.emf.common.util.EList}.
180    */

181   public EObject getOwner()
182   {
183     return owner;
184   }
185
186   /**
187    * This returns the feature of the owner object upon the command will act.
188    * It could be null, in the case that we are dealing with an {@link org.eclipse.emf.common.util.EList}.
189    */

190   public EStructuralFeature getFeature()
191   {
192     return feature;
193   }
194
195   /**
196    * This returns the list in which the command will replace.
197    */

198   public EList getOwnerList()
199   {
200     return ownerList;
201   }
202
203   /**
204    * This returns the value being replaced.
205    */

206   public Object JavaDoc getValue()
207   {
208     return value;
209   }
210
211   /**
212    * This returns the collection of replacement objects.
213    */

214   public Collection JavaDoc getCollection()
215   {
216     return collection;
217   }
218
219   /**
220    * This returns the index at which to reinsert the replace objecs during an undo so as to achieve the original list order.
221    */

222   public int getIndex()
223   {
224     return index;
225   }
226
227   protected boolean prepare()
228   {
229     // This can't execute if there is no owner list
230
// or the owner list doesn't contain the value being replaced or
231
// there are not replacements.
232
//
233
if (ownerList == null || !ownerList.contains(value) || collection == null || collection.isEmpty())
234     {
235       return false;
236     }
237     else if (owner != null && domain.isReadOnly(owner.eResource()))
238     {
239       return false;
240     }
241     else if (feature == null)
242     {
243       // An extent allows anything to be added.
244
//
245
return true;
246     }
247     else
248     {
249       // Make sure each object conforms to the type of the feature.
250
//
251
for (Iterator JavaDoc replacements = collection.iterator(); replacements.hasNext(); )
252       {
253         Object JavaDoc replacement = replacements.next();
254         if (!feature.getEType().isInstance(replacement))
255         {
256           return false;
257         }
258       }
259
260       return true;
261     }
262   }
263
264   public void doExecute()
265   {
266     // Record the position of the value in the onwer list.
267
//
268
index = ownerList.indexOf(value);
269   
270     // Simply remove the object from the owner list.
271
//
272
ownerList.remove(value);
273
274     // Insert the collection at the right place.
275
//
276
ownerList.addAll(index, collection);
277   
278     // We'd like the collection of replacements selected after this replace completes.
279
//
280
affectedObjects = collection;
281   }
282
283   public void doUndo()
284   {
285     // Remove the collection of replacements.
286
//
287
ownerList.removeAll(collection);
288
289     // Add the value back in the right place.
290
//
291
ownerList.add(index, value);
292
293     // We'd like the replaced selected after this undo replace completes.
294
//
295
affectedObjects = Collections.singleton(value);
296   }
297   
298   public void doRedo()
299   {
300     // Simply remove the object from the owner list.
301
//
302
ownerList.remove(value);
303
304     // Insert the collection at the right place.
305
//
306
ownerList.addAll(index, collection);
307   
308     // We'd like the collection of replacements selected after this replace completes.
309
//
310
affectedObjects = collection;
311   }
312
313   public Collection JavaDoc doGetResult()
314   {
315     return collection;
316   }
317
318   public Collection JavaDoc doGetAffectedObjects()
319   {
320     return affectedObjects;
321   }
322
323   /**
324    * This gives an abbreviated name using this object's own class' name, without package qualification,
325    * followed by a space separated list of <tt>field:value</tt> pairs.
326    */

327   public String JavaDoc toString()
328   {
329     StringBuffer JavaDoc result = new StringBuffer JavaDoc(super.toString());
330     result.append(" (owner: " + owner + ")");
331     result.append(" (feature: " + feature + ")");
332     result.append(" (ownerList: " + ownerList + ")");
333     result.append(" (value: " + value + ")");
334     result.append(" (collection: " + collection + ")");
335     result.append(" (index: " + index + ")");
336     result.append(" (affectedObjects:" + affectedObjects + ")");
337
338     return result.toString();
339   }
340 }
341
Popular Tags