KickJava   Java API By Example, From Geeks To Geeks.

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


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: CommandParameter.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.ArrayList JavaDoc;
21 import java.util.Collection JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24
25 import org.eclipse.emf.common.util.EList;
26 import org.eclipse.emf.ecore.EAttribute;
27 import org.eclipse.emf.ecore.EObject;
28 import org.eclipse.emf.ecore.EReference;
29 import org.eclipse.emf.ecore.EStructuralFeature;
30
31
32 /**
33  * This is a convenient common base class for all the command parameters need by the various types of commands.
34  * It provides particular support for the encodings need by the basic EMF-based command implementations.
35  */

36 public class CommandParameter
37 {
38   /**
39    * This value is used to indicate that the optional positional index indicator is unspecified.
40    */

41   public static final int NO_INDEX = -1;
42
43   /**
44    * This is the object that is the target or subject of the command.
45    */

46   public Object JavaDoc owner;
47
48   /**
49    * This is the aspect of the owner that will be affected.
50    */

51   public Object JavaDoc feature;
52
53   /**
54    * This is the collection of values involved in the command.
55    */

56   public Collection JavaDoc collection;
57
58   /**
59    * This is the single value involved in the command.
60    */

61   public Object JavaDoc value;
62
63   /**
64    * This the index (ususally the position indicator) of the command.
65    */

66   public int index;
67
68   /**
69    * This creates an instance specifying only an owner.
70    */

71   public CommandParameter(Object JavaDoc owner)
72   {
73     this.owner = owner;
74   }
75
76   /**
77    * This creates an instance specifying an owner, a feature, and a value.
78    */

79   public CommandParameter(Object JavaDoc owner, Object JavaDoc feature, Object JavaDoc value)
80   {
81     this.owner = owner;
82     this.feature = feature;
83     this.value = value;
84     this.index = NO_INDEX;
85   }
86
87   /**
88    * This creates an instance specifying an owner, a feature, a value, and an index.
89    */

90   public CommandParameter(Object JavaDoc owner, Object JavaDoc feature, Object JavaDoc value, int index)
91   {
92     this.owner = owner;
93     this.feature = feature;
94     this.value = value;
95     this.index = index;
96   }
97
98   /**
99    * This creates an instance specifying an owner, a feature, and a collection of values.
100    */

101   public CommandParameter(Object JavaDoc owner, Object JavaDoc feature, Collection JavaDoc collection)
102   {
103     this.owner = owner;
104     this.feature = feature;
105     this.collection = collection;
106     this.index = NO_INDEX;
107   }
108
109   /**
110    * This creates an instance specifying an owner, a feature, a collection of values, and an index.
111    */

112   public CommandParameter(Object JavaDoc owner, Object JavaDoc feature, Collection JavaDoc collection, int index)
113   {
114     this.owner = owner;
115     this.feature = feature;
116     this.collection = collection;
117     this.index = index;
118   }
119
120   /**
121    * This creates an instance specifying an owner, a feature, and a value, and a collection.
122    */

123   public CommandParameter(Object JavaDoc owner, Object JavaDoc feature, Object JavaDoc value, Collection JavaDoc collection)
124   {
125     this.owner = owner;
126     this.feature = feature;
127     this.value = value;
128     this.collection = collection;
129     this.index = NO_INDEX;
130   }
131
132   /**
133    * This creates an instance specifying an owner, a feature, a value, a collection, and an index.
134    */

135   public CommandParameter(Object JavaDoc owner, Object JavaDoc feature, Object JavaDoc value, Collection JavaDoc collection, int index)
136   {
137     this.owner = owner;
138     this.feature = feature;
139     this.value = value;
140     this.collection = collection;
141     this.index = index;
142   }
143
144   /**
145    * This returns the specified owner.
146    */

147   public Object JavaDoc getOwner()
148   {
149     return owner;
150   }
151
152   /**
153    * This returns the specified owner as a EObject, if it is one.
154    */

155   public EObject getEOwner()
156   {
157     return owner instanceof EObject ? (EObject)owner : null;
158   }
159
160   /**
161    * This sets the owner to the specified value.
162    */

163   public void setOwner(Object JavaDoc owner)
164   {
165     this.owner = owner;
166   }
167
168   /**
169    * This returns the specified feature.
170    */

171   public Object JavaDoc getFeature()
172   {
173     return feature;
174   }
175
176   /**
177    * This returns the specified feature as a EStructuralFeature, if it is one.
178    */

179   public EStructuralFeature getEStructuralFeature()
180   {
181     return feature instanceof EStructuralFeature ? (EStructuralFeature)feature : null;
182   }
183
184   /**
185    * This returns the specified feature as a EReference, if it is one.
186    */

187   public EReference getEReference()
188   {
189     return feature instanceof EReference ? (EReference)feature : null;
190   }
191
192   /**
193    * This returns the specified feature as a EReference, if it is one.
194    */

195   public EAttribute getEAttribute()
196   {
197     return feature instanceof EAttribute ? (EAttribute)feature : null;
198   }
199
200   /**
201    * This is a safe way to get the list affected by the parameters for an add or remove specification.
202    * It can return either the {@link org.eclipse.emf.common.util.EList}, if the owner is one,
203    * or it tries to get the EList specified by the feature of the owner.
204    *
205    * <p>
206    * It works as an excellent guard for poorly formed parameters.
207    */

208   public EList getOwnerList()
209   {
210     if (owner instanceof EObject)
211     {
212       EObject eOwner = (EObject)owner;
213       if (eOwner.eClass().getEAllStructuralFeatures().contains(feature))
214       {
215         EStructuralFeature eStructuralFeature = (EStructuralFeature)feature;
216         if (eStructuralFeature.isMany())
217         {
218           return (EList)eOwner.eGet(eStructuralFeature);
219         }
220       }
221     }
222     else if (owner instanceof EList)
223     {
224       return (EList)owner;
225     }
226
227     return null;
228   }
229
230   /**
231    * This returns the specified collection.
232    */

233   public Collection JavaDoc getCollection()
234   {
235     return collection;
236   }
237
238   /**
239    * This returns the specified collection as a list.
240    * If the collection isn't a list, a new copy is created.
241    */

242   public List JavaDoc getList()
243   {
244     return
245       collection == null ?
246         null :
247         collection instanceof List JavaDoc ?
248           (List JavaDoc)collection :
249           new ArrayList JavaDoc(collection);
250   }
251
252   /**
253    * This returns the specified value.
254    */

255   public Object JavaDoc getValue()
256   {
257     return value;
258   }
259
260   /**
261    * This returns the specified value as a EObject, if it is one.
262    */

263   public EObject getEValue()
264   {
265     return value instanceof EObject ? (EObject)value : null;
266   }
267
268   /**
269    * This returns the specified index.
270    */

271   public int getIndex()
272   {
273     return index;
274   }
275
276   /**
277    * This yields an encoding of the owner-child relation.
278    */

279   public Collection JavaDoc /* of String */ getParameters()
280   {
281     Collection JavaDoc parameters = new ArrayList JavaDoc();
282
283     EObject eObject = getEOwner();
284     EStructuralFeature eStructuralFeature = getEStructuralFeature();
285     if (eObject != null && eStructuralFeature != null)
286     {
287       parameters.add(eObject.eClass().getName());
288       parameters.add(eStructuralFeature.getEType().getName());
289     }
290
291     return parameters;
292   }
293  
294   public static String JavaDoc collectionToString(Collection JavaDoc collection)
295   {
296     if (collection == null)
297     {
298       return "null";
299     }
300     else
301     {
302       StringBuffer JavaDoc result = new StringBuffer JavaDoc();
303
304       result.append("{ ");
305
306       for (Iterator JavaDoc objects = collection.iterator(); objects.hasNext(); )
307       {
308         result.append(objects.next());
309         if (objects.hasNext())
310         {
311           result.append(", ");
312         }
313       }
314
315       result.append(" }");
316
317       return result.toString();
318     }
319   }
320
321   public String JavaDoc toString()
322   {
323     StringBuffer JavaDoc result = new StringBuffer JavaDoc();
324
325     result.append("CommandParameter");
326
327     result.append("\n owner = ");
328     result.append(owner);
329
330     result.append("\n feature = ");
331     result.append(feature);
332
333     if (getOwnerList() != null)
334     {
335       result.append("\n ownerList = ");
336       result.append(collectionToString(getOwnerList()));
337     }
338
339     if (collection != null)
340     {
341       result.append("\n collection = ");
342       result.append(collectionToString(collection));
343     }
344
345     if (value != null)
346     {
347       result.append("\n value = ");
348       result.append(value);
349     }
350
351     if (index != NO_INDEX)
352     {
353       result.append("\n index = ");
354       result.append(index);
355     }
356
357     return result.toString();
358   }
359 }
360
Popular Tags