1 17 package org.eclipse.emf.edit.command; 18 19 20 import java.util.ArrayList ; 21 import java.util.Collection ; 22 import java.util.Iterator ; 23 import java.util.List ; 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 36 public class CommandParameter 37 { 38 41 public static final int NO_INDEX = -1; 42 43 46 public Object owner; 47 48 51 public Object feature; 52 53 56 public Collection collection; 57 58 61 public Object value; 62 63 66 public int index; 67 68 71 public CommandParameter(Object owner) 72 { 73 this.owner = owner; 74 } 75 76 79 public CommandParameter(Object owner, Object feature, Object value) 80 { 81 this.owner = owner; 82 this.feature = feature; 83 this.value = value; 84 this.index = NO_INDEX; 85 } 86 87 90 public CommandParameter(Object owner, Object feature, Object value, int index) 91 { 92 this.owner = owner; 93 this.feature = feature; 94 this.value = value; 95 this.index = index; 96 } 97 98 101 public CommandParameter(Object owner, Object feature, Collection collection) 102 { 103 this.owner = owner; 104 this.feature = feature; 105 this.collection = collection; 106 this.index = NO_INDEX; 107 } 108 109 112 public CommandParameter(Object owner, Object feature, Collection collection, int index) 113 { 114 this.owner = owner; 115 this.feature = feature; 116 this.collection = collection; 117 this.index = index; 118 } 119 120 123 public CommandParameter(Object owner, Object feature, Object value, Collection 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 135 public CommandParameter(Object owner, Object feature, Object value, Collection 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 147 public Object getOwner() 148 { 149 return owner; 150 } 151 152 155 public EObject getEOwner() 156 { 157 return owner instanceof EObject ? (EObject)owner : null; 158 } 159 160 163 public void setOwner(Object owner) 164 { 165 this.owner = owner; 166 } 167 168 171 public Object getFeature() 172 { 173 return feature; 174 } 175 176 179 public EStructuralFeature getEStructuralFeature() 180 { 181 return feature instanceof EStructuralFeature ? (EStructuralFeature)feature : null; 182 } 183 184 187 public EReference getEReference() 188 { 189 return feature instanceof EReference ? (EReference)feature : null; 190 } 191 192 195 public EAttribute getEAttribute() 196 { 197 return feature instanceof EAttribute ? (EAttribute)feature : null; 198 } 199 200 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 233 public Collection getCollection() 234 { 235 return collection; 236 } 237 238 242 public List getList() 243 { 244 return 245 collection == null ? 246 null : 247 collection instanceof List ? 248 (List )collection : 249 new ArrayList (collection); 250 } 251 252 255 public Object getValue() 256 { 257 return value; 258 } 259 260 263 public EObject getEValue() 264 { 265 return value instanceof EObject ? (EObject)value : null; 266 } 267 268 271 public int getIndex() 272 { 273 return index; 274 } 275 276 279 public Collection getParameters() 280 { 281 Collection parameters = new ArrayList (); 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 collectionToString(Collection collection) 295 { 296 if (collection == null) 297 { 298 return "null"; 299 } 300 else 301 { 302 StringBuffer result = new StringBuffer (); 303 304 result.append("{ "); 305 306 for (Iterator 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 toString() 322 { 323 StringBuffer result = new StringBuffer (); 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 |