KickJava   Java API By Example, From Geeks To Geeks.

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


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: MoveCommand.java,v 1.3 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.Command;
24 import org.eclipse.emf.common.util.EList;
25 import org.eclipse.emf.ecore.EObject;
26 import org.eclipse.emf.ecore.EStructuralFeature;
27 import org.eclipse.emf.edit.EMFEditPlugin;
28 import org.eclipse.emf.edit.domain.EditingDomain;
29
30
31 /**
32  * The move command logically acts upon an owner object that has a collection-based feature
33  * containing an object that is to be moved to a new position within the collection.
34  * The static create method delegates command creation to {@link EditingDomain#createCommand EditingDomain.createCommand},
35  * which may or may not result in the actual creation of an instance of this class.
36  * Like all the low level comands in this package, the move command is undoable.
37  *
38  * <p>
39  * The implementation of this class is low-level and EMF specific;
40  * it allows an object to be moved to a new position within a many-valued feature of an owner,
41  * i.e., it is equivalent of the call
42  * <pre>
43  * ((EList)((EObject)owner).eGet((EStructuralFeature)feature)).move(index, object);
44  * </pre>
45  *
46  * <p>
47  * It can also be used as an equivalent to the call
48  * <pre>
49  * ((EList)extent).move(index, object);
50  * </pre>
51  * which is how root objects are moved within the contents of a resource.
52  * Like all the low-level comands in this package, the move command is undoable.
53  *
54  * <p>
55  * A move command is an {@link OverrideableCommand}.
56  */

57 public class MoveCommand extends AbstractOverrideableCommand
58 {
59   /**
60    * This creates a command to move particular value to a particular index in the specified feature of the owner.
61    * The feature will often be null because the domain will deduce it.
62    */

63   public static Command create(EditingDomain domain, Object JavaDoc owner, Object JavaDoc feature, Object JavaDoc value, int index)
64   {
65     return domain.createCommand(MoveCommand.class, new CommandParameter(owner, feature, value, index));
66   }
67
68
69   /**
70    * This caches the label.
71    */

72   protected static final String JavaDoc LABEL = EMFEditPlugin.INSTANCE.getString("_UI_MoveCommand_label");
73
74   /**
75    * This caches the description.
76    */

77   protected static final String JavaDoc DESCRIPTION = EMFEditPlugin.INSTANCE.getString("_UI_MoveCommand_description");
78
79   /**
80    * This caches the description for a list-based command.
81    */

82   protected static final String JavaDoc DESCRIPTION_FOR_LIST = EMFEditPlugin.INSTANCE.getString("_UI_MoveCommand_description_for_list");
83
84   /**
85    * This is the owner object upon which the command will act.
86    * It could be null in the case that we are dealing with an {@link org.eclipse.emf.common.util.EList}.
87    */

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

94   protected EStructuralFeature feature;
95
96   /**
97    * This is the list in which the command will move an object.
98    */

99   protected EList ownerList;
100
101   /**
102    * This is the value being moved within the owner list.
103    */

104   protected Object JavaDoc value;
105
106   /**
107    * This is the position to which the object will be moved.
108    */

109   protected int index;
110
111   /**
112    * This is the orginal position to which the object will be moved upon undo.
113    */

114   protected int oldIndex;
115
116   /**
117    * This constructs a primitive command to move a particular value to a particular index
118    * of the specified many-valued feature of the owner.
119    */

120   public MoveCommand(EditingDomain domain, EObject owner, EStructuralFeature feature, Object JavaDoc value, int index)
121   {
122     super (domain, LABEL, DESCRIPTION);
123
124     this.owner = owner;
125     this.feature = feature;
126     this.value = value;
127     this.index = index;
128
129     ownerList = getOwnerList(this.owner, feature);
130   }
131
132   /**
133    * This constructs a primitive command to move a particular value to a particular index of the specified extent.
134    */

135   public MoveCommand(EditingDomain domain, EList list, Object JavaDoc value, int index)
136   {
137     super(domain, LABEL, DESCRIPTION_FOR_LIST);
138
139     this.value = value;
140     this.index = index;
141
142     ownerList = list;
143   }
144
145   /**
146    * This returns the owner object upon which the command will act.
147    * It could be null in the case that we are dealing with an {@link org.eclipse.emf.common.util.EList}.
148    */

149   public EObject getOwner()
150   {
151     return owner;
152   }
153
154   /**
155    * This returns the feature of the owner object upon the command will act.
156    * It could be null, in the case that we are dealing with an {@link org.eclipse.emf.common.util.EList}.
157    */

158   public EStructuralFeature getFeature()
159   {
160     return feature;
161   }
162
163   /**
164    * This returns the list in which the command will move an object.
165    */

166   public EList getOwnerList()
167   {
168     return ownerList;
169   }
170
171   /**
172    * This returns the value being moved.
173    */

174   public Object JavaDoc getValue()
175   {
176     return value;
177   }
178
179   /**
180    * This returns the position to which the value will be moved.
181    */

182   public int getIndex()
183   {
184     return index;
185   }
186
187   /**
188    * This returns the orginal position to which the object will be moved upon undo.
189    */

190   public int getOldIndex()
191   {
192     return oldIndex;
193   }
194
195   protected boolean prepare()
196   {
197     // Return whether there is a list, the value is in the list, and index is in range...
198
//
199
boolean result =
200       ownerList != null &&
201          ownerList.contains(value) &&
202          index >= 0 &&
203          index < ownerList.size() &&
204          (owner == null || !domain.isReadOnly(owner.eResource()));
205
206     return result;
207   }
208
209   public void doExecute()
210   {
211     oldIndex = ownerList.indexOf(value);
212     ownerList.move(index, value);
213   }
214
215   public void doUndo()
216   {
217     ownerList.move(oldIndex, value);
218   }
219
220   public void doRedo()
221   {
222     ownerList.move(index, value);
223   }
224
225   public Collection JavaDoc doGetResult()
226   {
227     return Collections.singleton(value);
228   }
229
230   public Collection JavaDoc doGetAffectedObjects()
231   {
232     return Collections.singleton(value);
233   }
234
235   /**
236    * This gives an abbreviated name using this object's own class' name, without package qualification,
237    * followed by a space separated list of <tt>field:value</tt> pairs.
238    */

239   public String JavaDoc toString()
240   {
241     StringBuffer JavaDoc result = new StringBuffer JavaDoc(super.toString());
242     result.append(" (owner: " + owner + ")");
243     result.append(" (feature: " + feature + ")");
244     result.append(" (ownerList: " + ownerList + ")");
245     result.append(" (value: " + value + ")");
246     result.append(" (index: " + index + ")");
247     result.append(" (oldIndex: " + oldIndex + ")");
248
249     return result.toString();
250   }
251 }
252
Popular Tags