KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > edit > provider > ItemPropertyDescriptor


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2002-2005 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: ItemPropertyDescriptor.java,v 1.17 2005/06/12 13:32:37 emerks Exp $
16  */

17 package org.eclipse.emf.edit.provider;
18
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.Collection JavaDoc;
22 import java.util.Collections JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.MissingResourceException JavaDoc;
27
28 import org.eclipse.emf.common.command.Command;
29 import org.eclipse.emf.common.command.CompoundCommand;
30 import org.eclipse.emf.common.notify.AdapterFactory;
31 import org.eclipse.emf.common.util.AbstractEnumerator;
32 import org.eclipse.emf.common.util.ResourceLocator;
33 import org.eclipse.emf.common.util.TreeIterator;
34 import org.eclipse.emf.common.util.UniqueEList;
35 import org.eclipse.emf.ecore.EAttribute;
36 import org.eclipse.emf.ecore.EClass;
37 import org.eclipse.emf.ecore.EClassifier;
38 import org.eclipse.emf.ecore.EDataType;
39 import org.eclipse.emf.ecore.EEnum;
40 import org.eclipse.emf.ecore.EEnumLiteral;
41 import org.eclipse.emf.ecore.EObject;
42 import org.eclipse.emf.ecore.EReference;
43 import org.eclipse.emf.ecore.EStructuralFeature;
44 import org.eclipse.emf.ecore.EcorePackage;
45 import org.eclipse.emf.ecore.resource.Resource;
46 import org.eclipse.emf.ecore.resource.ResourceSet;
47 import org.eclipse.emf.ecore.util.EcoreUtil;
48 import org.eclipse.emf.ecore.util.ExtendedMetaData;
49 import org.eclipse.emf.ecore.util.FeatureMap;
50 import org.eclipse.emf.ecore.util.FeatureMapUtil;
51 import org.eclipse.emf.edit.EMFEditPlugin;
52 import org.eclipse.emf.edit.command.SetCommand;
53 import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
54 import org.eclipse.emf.edit.domain.EditingDomain;
55 import org.eclipse.emf.edit.domain.IEditingDomainProvider;
56 import org.eclipse.emf.edit.provider.IItemPropertyDescriptor.OverrideableCommandOwner;
57
58
59 /**
60  * This implementation of an item property descriptor supports delegating of the {@link IItemPropertySource} interface
61  * to the {@link IItemPropertyDescriptor} interface.
62  */

63 public class ItemPropertyDescriptor implements IItemPropertyDescriptor, OverrideableCommandOwner
64 {
65   public static final Object JavaDoc BOOLEAN_VALUE_IMAGE = EMFEditPlugin.INSTANCE.getImage("full/obj16/BooleanValue");
66   public static final Object JavaDoc GENERIC_VALUE_IMAGE = EMFEditPlugin.INSTANCE.getImage("full/obj16/GenericValue");
67   public static final Object JavaDoc INTEGRAL_VALUE_IMAGE = EMFEditPlugin.INSTANCE.getImage("full/obj16/IntegralValue");
68   public static final Object JavaDoc REAL_VALUE_IMAGE = EMFEditPlugin.INSTANCE.getImage("full/obj16/RealValue");
69   public static final Object JavaDoc TEXT_VALUE_IMAGE = EMFEditPlugin.INSTANCE.getImage("full/obj16/TextValue");
70
71   /**
72    * For now we need to keep track of the adapter factory, because we need it to provide a correct label provider.
73    */

74   protected AdapterFactory adapterFactory;
75
76   /**
77    * This is used to locate resources for translated values like enumeration literals.
78    */

79   protected ResourceLocator resourceLocator;
80
81   /**
82    * This is a convenient wrapper of the {@link #adapterFactory}.
83    */

84   protected AdapterFactoryItemDelegator itemDelegator;
85
86   /**
87    * This is returned by {@link #canSetProperty}.
88    */

89   protected boolean isSettable;
90
91   /**
92    * This is the name that is displayed in the property sheet.
93    */

94   protected String JavaDoc displayName;
95
96   /**
97    * This is the description shown in the property sheet when this property is selected.
98    */

99   protected String JavaDoc description;
100
101   /**
102    * This is the structural feature that provides the values for this property.
103    * This is mutually exclusive with {@link #parentReferences}.
104    */

105   protected EStructuralFeature feature;
106
107   /**
108    * This is the set of single-valued references that act as a parent, only one can have a non null value at a time.
109    * This is mutually exclusive with {@link #feature}.
110    */

111   protected EReference [] parentReferences;
112
113   /**
114    * This represents the group of properties into which this one should be placed.
115    */

116   protected String JavaDoc category;
117
118   /**
119    * These are the flags used as filters in the property sheet.
120    */

121   protected String JavaDoc [] filterFlags;
122
123   /**
124    * This is the label provider used to render property values.
125    */

126   // protected Object labelProvider;
127

128   /**
129    * This is the image that will be used with the value no matter what type of object it is.
130    */

131   protected Object JavaDoc staticImage;
132
133   /**
134    * If non-null, this object will be the owner of commands created to set the property's value.
135    */

136   protected Object JavaDoc commandOwner;
137
138   /**
139    * This class uses a static image
140    */

141   protected class ItemDelegator extends AdapterFactoryItemDelegator
142   {
143     protected ResourceLocator resourceLocator;
144
145     public ItemDelegator(AdapterFactory adapterFactory)
146     {
147       super(adapterFactory);
148     }
149
150     public ItemDelegator(AdapterFactory adapterFactory, ResourceLocator resourceLocator)
151     {
152       super(adapterFactory);
153       this.resourceLocator = resourceLocator;
154     }
155
156     public String JavaDoc getText(Object JavaDoc object)
157     {
158       if (feature instanceof EAttribute)
159       {
160         EDataType eDataType = ((EAttribute)feature).getEAttributeType();
161         if (eDataType.isSerializable())
162         {
163           if (isMany(object))
164           {
165             if (object instanceof List JavaDoc)
166             {
167               StringBuffer JavaDoc result = new StringBuffer JavaDoc();
168               for (Iterator JavaDoc i = ((List JavaDoc)object).iterator(); i.hasNext(); )
169               {
170                 Object JavaDoc value = i.next();
171                 result.append(convert(eDataType, value));
172                 if (i.hasNext())
173                 {
174                   result.append(", ");
175                 }
176               }
177               return result.toString();
178             }
179           }
180           else if (eDataType.isInstance(object))
181           {
182             return convert(eDataType, object);
183           }
184         }
185       }
186
187       return super.getText(object);
188     }
189
190     protected String JavaDoc convert(EDataType eDataType, Object JavaDoc value)
191     {
192       if (resourceLocator != null)
193       {
194         if (eDataType instanceof EEnum)
195         {
196           try
197           {
198             return
199               resourceLocator.getString
200                 ("_UI_" + eDataType.getName() + "_" + ((AbstractEnumerator)value).getName() + "_literal");
201           }
202           catch (MissingResourceException JavaDoc exception)
203           {
204           }
205         }
206         else if (value instanceof Boolean JavaDoc)
207         {
208           try
209           {
210             return
211               resourceLocator.getString
212                 (Boolean.TRUE.equals(value) ? "_UI_Boolean_true_literal" : "_UI_Boolean_false_literal");
213           }
214           catch (MissingResourceException JavaDoc exception)
215           {
216           }
217         }
218       }
219       return EcoreUtil.convertToString(eDataType, value);
220     }
221
222     public Object JavaDoc getImage(Object JavaDoc object)
223     {
224       return staticImage == null ? super.getImage(object) : staticImage;
225     }
226   }
227
228   /**
229    * This creates an instance that does not use a resource locator and determines the cell editor from the type of the
230    * structural feature. It assumed that the feature should be setable from this property.
231    *
232    * <p>To reduce the number of constructors for this class, this one will soon be deprecated. For new code, please
233    * use {@link #ItemPropertyDescriptor(AdapterFactory, ResourceLocator, String, String, EStructuralFeature, boolean)
234    * this} form, instead.
235    */

236   public ItemPropertyDescriptor
237      (AdapterFactory adapterFactory,
238       String JavaDoc displayName,
239       String JavaDoc description,
240       EStructuralFeature feature)
241   {
242     this(adapterFactory, null, displayName, description, feature, true, null, null, null);
243   }
244
245   /**
246    * This creates an instance that uses a resource locator and determines the cell editor from the type of the
247    * structural feature. It assumed that the feature should be setable from this property.
248    *
249    * <p>To reduce the number of constructors for this class, this one will soon be deprecated. For new code, please
250    * use {@link #ItemPropertyDescriptor(AdapterFactory, ResourceLocator, String, String, EStructuralFeature, boolean)
251    * this} form, instead.
252    */

253   public ItemPropertyDescriptor
254      (AdapterFactory adapterFactory,
255       ResourceLocator resourceLocator,
256       String JavaDoc displayName,
257       String JavaDoc description,
258       EStructuralFeature feature)
259   {
260     this(adapterFactory, resourceLocator, displayName, description, feature, true, null, null, null);
261   }
262
263   /**
264    * This creates an instance that does not use a resource locator and determines the cell editor from the type of the
265    * structural feature.
266    *
267    * <p>To reduce the number of constructors for this class, this ony may be deprecated in the future. For new code, please
268    * use {@link #ItemPropertyDescriptor(AdapterFactory, ResourceLocator, String, String, EStructuralFeature, boolean)
269    * this} form, instead.
270    */

271   public ItemPropertyDescriptor
272      (AdapterFactory adapterFactory,
273       String JavaDoc displayName,
274       String JavaDoc description,
275       EStructuralFeature feature,
276       boolean isSettable)
277   {
278     this(adapterFactory, null, displayName, description, feature, isSettable, null, null, null);
279   }
280
281   /**
282    * This creates an instance that uses a resource locator and determines the cell editor from the type of the
283    * structural feature.
284    */

285   public ItemPropertyDescriptor
286      (AdapterFactory adapterFactory,
287       ResourceLocator resourceLocator,
288       String JavaDoc displayName,
289       String JavaDoc description,
290       EStructuralFeature feature,
291       boolean isSettable)
292   {
293     this(adapterFactory, resourceLocator, displayName, description, feature, isSettable, null, null, null);
294   }
295
296   /**
297    * This creates an instance that does not use a resource locator, specifies a static image, and determines the cell
298    * editor from the type of the structural feature.
299    *
300    * <p>To reduce the number of constructors for this class, this one may be deprecated in the future. For new code, please
301    * use {@link #ItemPropertyDescriptor(AdapterFactory, ResourceLocator, String, String, EStructuralFeature, boolean, Object)
302    * this} form, instead.
303    */

304   public ItemPropertyDescriptor
305      (AdapterFactory adapterFactory,
306       String JavaDoc displayName,
307       String JavaDoc description,
308       EStructuralFeature feature,
309       boolean isSettable,
310       Object JavaDoc staticImage)
311   {
312     this(adapterFactory, null, displayName, description, feature, isSettable, staticImage, null, null);
313   }
314
315   /**
316    * This creates an instance that uses a resource locator, specifies a static image, and determines the cell editor
317    * from the type of the structural feature.
318    */

319   public ItemPropertyDescriptor
320      (AdapterFactory adapterFactory,
321       ResourceLocator resourceLocator,
322       String JavaDoc displayName,
323       String JavaDoc description,
324       EStructuralFeature feature,
325       boolean isSettable,
326       Object JavaDoc staticImage)
327   {
328     this(adapterFactory, resourceLocator, displayName, description, feature, isSettable, staticImage, null, null);
329   }
330
331   /**
332    * This creates an instance that does not use a resource locator, specifies a category, and determines the cell
333    * editor from the type of the structural feature.
334    *
335    * <p>To reduce the number of constructors for this class, this one will soon be deprecated. For new code, please
336    * use {@link #ItemPropertyDescriptor(AdapterFactory, ResourceLocator, String, String, EStructuralFeature, boolean, String, String[])
337    * this} form, instead.
338    */

339   public ItemPropertyDescriptor
340      (AdapterFactory adapterFactory,
341       String JavaDoc displayName,
342       String JavaDoc description,
343       EStructuralFeature feature,
344       boolean isSettable,
345       String JavaDoc category)
346   {
347     this(adapterFactory, null, displayName, description, feature, isSettable, null, category, null);
348   }
349
350   /**
351    * This creates an instance that uses a resource locator, specifies a category, and determines the cell editor from
352    * the type of the structural feature.
353    *
354    * <p>To reduce the number of constructors for this class, this one will soon be deprecated. For new code, please
355    * use {@link #ItemPropertyDescriptor(AdapterFactory, ResourceLocator, String, String, EStructuralFeature, boolean, String, String[])
356    * this} form, instead.
357    */

358   public ItemPropertyDescriptor
359      (AdapterFactory adapterFactory,
360       ResourceLocator resourceLocator,
361       String JavaDoc displayName,
362       String JavaDoc description,
363       EStructuralFeature feature,
364       boolean isSettable,
365       String JavaDoc category)
366   {
367     this(adapterFactory, resourceLocator, displayName, description, feature, isSettable, null, category, null);
368   }
369
370   /**
371    * This creates an instance that does not use a resource locator, specifies a static image and category, and
372    * determines the cell editor from the type of the structural feature.
373    *
374    * <p>To reduce the number of constructors for this class, this one will soon be deprecated. For new code, please
375    * use {@link #ItemPropertyDescriptor(AdapterFactory, ResourceLocator, String, String, EStructuralFeature, boolean, Object, String, String[])
376    * this} form, instead.
377    */

378   public ItemPropertyDescriptor
379      (AdapterFactory adapterFactory,
380       String JavaDoc displayName,
381       String JavaDoc description,
382       EStructuralFeature feature,
383       boolean isSettable,
384       Object JavaDoc staticImage,
385       String JavaDoc category)
386   {
387     this(adapterFactory, null, displayName, description, feature, isSettable, staticImage, category, null);
388   }
389
390   /**
391    * This creates an instance that uses a resource locator, specifies a static image and category, and determines the
392    * cell editor from the type of the structural feature.
393    *
394    * <p>To reduce the number of constructors for this class, this one will soon be deprecated. For new code, please
395    * use {@link #ItemPropertyDescriptor(AdapterFactory, ResourceLocator, String, String, EStructuralFeature, boolean, Object, String, String[])
396    * this} form, instead.
397    */

398   public ItemPropertyDescriptor
399      (AdapterFactory adapterFactory,
400       ResourceLocator resourceLocator,
401       String JavaDoc displayName,
402       String JavaDoc description,
403       EStructuralFeature feature,
404       boolean isSettable,
405       Object JavaDoc staticImage,
406       String JavaDoc category)
407   {
408     this(adapterFactory, resourceLocator, displayName, description, feature, isSettable, staticImage, category, null);
409   }
410
411   /**
412    * This creates an instance that does not use a resource locator, specifies a category and filter flags, and
413    * determines the cell editor from the type of the structural feature.
414    *
415    * <p>To reduce the number of constructors for this class, this one may be deprecated in the future. For new code, please
416    * use {@link #ItemPropertyDescriptor(AdapterFactory, ResourceLocator, String, String, EStructuralFeature, boolean, String, String[])
417    * this} form, instead.
418    */

419   public ItemPropertyDescriptor
420      (AdapterFactory adapterFactory,
421       String JavaDoc displayName,
422       String JavaDoc description,
423       EStructuralFeature feature,
424       boolean isSettable,
425       String JavaDoc category,
426       String JavaDoc [] filterFlags)
427   {
428     this(adapterFactory, null, displayName, description, feature, isSettable, null, category, filterFlags);
429   }
430
431   /**
432    * This creates an instance that uses a resource locator, specifies a category and filter flags, and determines the
433    * cell editor from the type of the structural feature.
434    */

435   public ItemPropertyDescriptor
436      (AdapterFactory adapterFactory,
437       ResourceLocator resourceLocator,
438       String JavaDoc displayName,
439       String JavaDoc description,
440       EStructuralFeature feature,
441       boolean isSettable,
442       String JavaDoc category,
443       String JavaDoc [] filterFlags)
444   {
445     this(adapterFactory, resourceLocator, displayName, description, feature, isSettable, null, category, filterFlags);
446   }
447
448   /**
449    * This creates an instance that does not use a resource locator; specifies a static image, a category, and filter
450    * flags; and determines the cell editor from the type of the structural feature.
451    *
452    * <p>To reduce the number of constructors for this class, this one may be deprecated in the future. For new code, please
453    * use {@link #ItemPropertyDescriptor(AdapterFactory, ResourceLocator, String, String, EStructuralFeature, boolean, Object, String, String[])
454    * this} form, instead.
455    */

456   public ItemPropertyDescriptor
457      (AdapterFactory adapterFactory,
458       String JavaDoc displayName,
459       String JavaDoc description,
460       EStructuralFeature feature,
461       boolean isSettable,
462       Object JavaDoc staticImage,
463       String JavaDoc category,
464       String JavaDoc [] filterFlags)
465   {
466     this(adapterFactory, null, displayName, description, feature, isSettable, staticImage, category, filterFlags);
467   }
468
469   /**
470    * This creates an instance that uses a resource locator; specifies a static image, a category, and filter flags;
471    * and determines the cell editor from the type of the structural feature.
472    */

473   public ItemPropertyDescriptor
474      (AdapterFactory adapterFactory,
475       ResourceLocator resourceLocator,
476       String JavaDoc displayName,
477       String JavaDoc description,
478       EStructuralFeature feature,
479       boolean isSettable,
480       Object JavaDoc staticImage,
481       String JavaDoc category,
482       String JavaDoc [] filterFlags)
483   {
484     this.adapterFactory = adapterFactory;
485     this.resourceLocator = resourceLocator;
486     this.itemDelegator = new ItemDelegator(adapterFactory, resourceLocator);
487     this.displayName = displayName;
488     this.description = description;
489     this.feature = feature;
490     this.isSettable = isSettable;
491     this.staticImage = staticImage;
492     this.category = category;
493     this.filterFlags = filterFlags;
494   }
495
496   /**
497    * This creates an instance that does not use a resource locator and determines the cell editor from the parent
498    * references. It assumed that the feature should be setable from this property.
499    *
500    * <p>To reduce the number of constructors for this class, this one will soon be deprecated. For new code, please
501    * use {@link #ItemPropertyDescriptor(AdapterFactory, ResourceLocator, String, String, EReference[], boolean)
502    * this} form, instead.
503    */

504   public ItemPropertyDescriptor
505      (AdapterFactory adapterFactory,
506       String JavaDoc displayName,
507       String JavaDoc description,
508       EReference [] parentReferences)
509   {
510     this(adapterFactory, null, displayName, description, parentReferences, true, null, null);
511   }
512
513   /**
514    * This creates an instance that uses a resource locator and determines the cell editor from the parent references.
515    * It assumed that the feature should be setable from this property.
516    *
517    * <p>To reduce the number of constructors for this class, this one will soon be deprecated. For new code, please
518    * use {@link #ItemPropertyDescriptor(AdapterFactory, ResourceLocator, String, String, EReference[], boolean)
519    * this} form, instead.
520    */

521   public ItemPropertyDescriptor
522      (AdapterFactory adapterFactory,
523       ResourceLocator resourceLocator,
524       String JavaDoc displayName,
525       String JavaDoc description,
526       EReference [] parentReferences)
527   {
528     this(adapterFactory, resourceLocator, displayName, description, parentReferences, true, null, null);
529   }
530
531   /**
532    * This creates an instance that does not use a resource locator and determines the cell editor from the parent
533    * references.
534    *
535    * <p>To reduce the number of constructors for this class, this one may be deprecated in the future. For new code, please
536    * use {@link #ItemPropertyDescriptor(AdapterFactory, ResourceLocator, String, String, EReference[], boolean)
537    * this} form, instead.
538    */

539   public ItemPropertyDescriptor
540      (AdapterFactory adapterFactory,
541       String JavaDoc displayName,
542       String JavaDoc description,
543       EReference [] parentReferences,
544       boolean isSettable)
545   {
546     this(adapterFactory, null, displayName, description, parentReferences, isSettable, null, null);
547   }
548
549   /**
550    * This creates an instance that uses a resource locator and determines the cell editor from the parent references.
551    */

552   public ItemPropertyDescriptor
553      (AdapterFactory adapterFactory,
554       ResourceLocator resourceLocator,
555       String JavaDoc displayName,
556       String JavaDoc description,
557       EReference [] parentReferences,
558       boolean isSettable)
559   {
560     this(adapterFactory, resourceLocator, displayName, description, parentReferences, isSettable, null, null);
561   }
562
563   /**
564    * This creates an instance that does not use a resource locator, specifies a category, and determines the cell
565    * editor from the parent references.
566    *
567    * <p>To reduce the number of constructors for this class, this one will soon be deprecated. For new code, please
568    * use {@link #ItemPropertyDescriptor(AdapterFactory, ResourceLocator, String, String, EReference[], boolean, String, String[])
569    * this} form, instead.
570    */

571   public ItemPropertyDescriptor
572      (AdapterFactory adapterFactory,
573       String JavaDoc displayName,
574       String JavaDoc description,
575       EReference [] parentReferences,
576       boolean isSettable,
577       String JavaDoc category)
578   {
579     this(adapterFactory, null, displayName, description, parentReferences, isSettable, category, null);
580   }
581
582   /**
583    * This creates an instance that uses a resource locator, specifies a category, and determines the cell editor from
584    * the parent references.
585    *
586    * <p>To reduce the number of constructors for this class, this one will soon be deprecated. For new code, please
587    * use {@link #ItemPropertyDescriptor(AdapterFactory, ResourceLocator, String, String, EReference[], boolean, String, String[])
588    * this} form, instead.
589    */

590   public ItemPropertyDescriptor
591      (AdapterFactory adapterFactory,
592       ResourceLocator resourceLocator,
593       String JavaDoc displayName,
594       String JavaDoc description,
595       EReference [] parentReferences,
596       boolean isSettable,
597       String JavaDoc category)
598   {
599     this(adapterFactory, resourceLocator, displayName, description, parentReferences, isSettable, category, null);
600   }
601
602   /**
603    * This creates an instance that does not use a resource locator, specifies a category and filter flags, and
604    * determines the cell editor from the parent references.
605    *
606    * <p>To reduce the number of constructors for this class, this one may be deprecated in the future. For new code, please
607    * use {@link #ItemPropertyDescriptor(AdapterFactory, ResourceLocator, String, String, EReference[], boolean, String, String[])
608    * this} form, instead.
609    */

610   public ItemPropertyDescriptor
611      (AdapterFactory adapterFactory,
612       String JavaDoc displayName,
613       String JavaDoc description,
614       EReference [] parentReferences,
615       boolean isSettable,
616       String JavaDoc category,
617       String JavaDoc [] filterFlags)
618   {
619     this(adapterFactory, null, displayName, description, parentReferences, isSettable, category, filterFlags);
620   }
621
622   /**
623    * This creates an instance that uses a resource locator, specifies a category and filter flags, and determines the
624    * cell editor from the parent references.
625    */

626   public ItemPropertyDescriptor
627      (AdapterFactory adapterFactory,
628       ResourceLocator resourceLocator,
629       String JavaDoc displayName,
630       String JavaDoc description,
631       EReference [] parentReferences,
632       boolean isSettable,
633       String JavaDoc category,
634       String JavaDoc [] filterFlags)
635   {
636     this.adapterFactory = adapterFactory;
637     this.resourceLocator = resourceLocator;
638     this.itemDelegator = new ItemDelegator(adapterFactory, resourceLocator);
639     this.displayName = displayName;
640     this.description = description;
641     this.parentReferences = parentReferences;
642     this.isSettable = isSettable;
643     this.category = category;
644     this.filterFlags = filterFlags;
645   }
646
647   /**
648    * This returns the group of propertiesk into which this one should be placed.
649    */

650   public String JavaDoc getCategory(Object JavaDoc object)
651   {
652     return category;
653   }
654
655   /**
656    * This returns the description to be displayed in the property sheet when this property is selected.
657    */

658   public String JavaDoc getDescription(Object JavaDoc object)
659   {
660     return description;
661   }
662
663   /**
664    * This returns the name of the property to be displayed in the property sheet.
665    */

666   public String JavaDoc getDisplayName(Object JavaDoc object)
667   {
668     return displayName;
669   }
670
671   /**
672    * This returns the flags used as filters in the property sheet.
673    */

674   public String JavaDoc[] getFilterFlags(Object JavaDoc object)
675   {
676     return filterFlags;
677   }
678
679   /**
680    * This returns the interface name of this property.
681    * This is the key that is passed around and must uniquely identifiy this descriptor.
682    */

683   public String JavaDoc getId(Object JavaDoc object)
684   {
685     // System.out.println("getName " + feature.eClass().getEID());
686
// return feature.eClass().getEID().toString();
687

688     return displayName;
689   }
690
691   public Object JavaDoc getHelpContextIds(Object JavaDoc object)
692   {
693     return new String JavaDoc [] {};
694   }
695
696   protected static final EcorePackage ecorePackage = EcorePackage.eINSTANCE;
697
698   /**
699    * This will be called to populate a list of choices.
700    * The label provider will be used to determine the labels for the objects this returns.
701    * This default implementation uses {@link #getReachableObjectsOfType getReachableObjectsOfType}.
702    */

703   protected Collection JavaDoc getComboBoxObjects(Object JavaDoc object)
704   {
705     if (object instanceof EObject)
706     {
707       if (parentReferences != null)
708       {
709         Collection JavaDoc result = new UniqueEList();
710         for (int i = 0; i < parentReferences.length; ++i)
711         {
712           result.addAll(getReachableObjectsOfType((EObject)object, parentReferences[i].getEType()));
713         }
714         return result;
715       }
716       else if (feature != null)
717       {
718         if (feature instanceof EReference)
719         {
720           Collection JavaDoc result = getReachableObjectsOfType((EObject)object, feature.getEType());
721           if (!feature.isMany() && !result.contains(null))
722           {
723             result.add(null);
724           }
725           return result;
726         }
727         else if (feature.getEType() instanceof EEnum)
728         {
729           EEnum eEnum = (EEnum)feature.getEType();
730           List JavaDoc enumerators = new ArrayList JavaDoc();
731           for (Iterator JavaDoc iter = eEnum.getELiterals().iterator(); iter.hasNext(); )
732           {
733             enumerators.add(((EEnumLiteral)iter.next()).getInstance());
734           }
735           return enumerators;
736         }
737         else
738         {
739           EDataType eDataType = (EDataType)feature.getEType();
740           List JavaDoc enumeration = ExtendedMetaData.INSTANCE.getEnumerationFacet(eDataType);
741           if (!enumeration.isEmpty())
742           {
743             List JavaDoc enumerators = new ArrayList JavaDoc();
744             for (Iterator JavaDoc i = enumeration.iterator(); i.hasNext();)
745             {
746               enumerators.add(EcoreUtil.createFromString(eDataType, (String JavaDoc)i.next()));
747             }
748             return enumerators;
749           }
750         }
751       }
752     }
753
754     return null;
755   }
756
757   /**
758    * This yields all reachable references from object with a meta object which indicates that it is a subtype of type.
759    */

760   static public Collection JavaDoc getReachableObjectsOfType(EObject object, EClassifier type)
761   {
762     Collection JavaDoc visited = new HashSet JavaDoc();
763     Collection JavaDoc result = new ArrayList JavaDoc();
764     Resource resource = object.eResource();
765     if (resource != null)
766     {
767       ResourceSet resourceSet = resource.getResourceSet();
768       if (resourceSet != null)
769       {
770         for (TreeIterator i = resourceSet.getAllContents(); i.hasNext(); )
771         {
772           Object JavaDoc child = i.next();
773           if (child instanceof EObject)
774           {
775             collectReachableObjectsOfType(visited, result, (EObject)child, type);
776             i.prune();
777           }
778         }
779       }
780       else
781       {
782         for (Iterator JavaDoc i = resource.getContents().iterator(); i.hasNext(); )
783         {
784           collectReachableObjectsOfType(visited, result, (EObject)i.next(), type);
785         }
786       }
787     }
788     else
789     {
790       collectReachableObjectsOfType(visited, result, EcoreUtil.getRootContainer(object), type);
791     }
792     return result;
793   }
794
795   /**
796    * This will visit all reachable references from object except those in visited;
797    * it updates visited and adds to result any object with a meta object that indicates that it is a subtype of type.
798    */

799   static public void collectReachableObjectsOfType(Collection JavaDoc visited, Collection JavaDoc result, EObject object, EClassifier type)
800   {
801     if (visited.add(object))
802     {
803       if (type.isInstance(object))
804       {
805         result.add(object);
806       }
807
808       EClass eClass = object.eClass();
809       for (Iterator JavaDoc i = eClass.getEAllStructuralFeatures().iterator(); i.hasNext(); )
810       {
811         EStructuralFeature eStructuralFeature = (EStructuralFeature)i.next();
812         if (!eStructuralFeature.isDerived())
813         {
814           if (eStructuralFeature instanceof EReference)
815           {
816             EReference eReference = (EReference)eStructuralFeature;
817             if (eReference.isMany())
818             {
819               for (Iterator JavaDoc referencedObjects = ((List JavaDoc)object.eGet(eReference)).iterator(); referencedObjects.hasNext(); )
820               {
821                 Object JavaDoc referencedObject = referencedObjects.next();
822                 if (referencedObject instanceof EObject)
823                 {
824                   collectReachableObjectsOfType(visited, result, (EObject)referencedObject, type);
825                 }
826               }
827             }
828             else
829             {
830               Object JavaDoc referencedObject = object.eGet(eReference);
831               if (referencedObject instanceof EObject)
832               {
833                 collectReachableObjectsOfType(visited, result, (EObject)referencedObject, type);
834               }
835             }
836           }
837           else if (FeatureMapUtil.isFeatureMap(eStructuralFeature))
838           {
839             for (Iterator JavaDoc j = ((List JavaDoc)object.eGet(eStructuralFeature)).iterator(); j.hasNext(); )
840             {
841               FeatureMap.Entry entry = (FeatureMap.Entry)j.next();
842               if (entry.getEStructuralFeature() instanceof EReference && entry.getValue() != null)
843               {
844                 collectReachableObjectsOfType(visited, result, (EObject)entry.getValue(), type);
845               }
846             }
847           }
848         }
849       }
850     }
851   }
852
853   /**
854    * This returns the label provider that will be used to render the value of this property.
855    * The implementation here just creates an {@link AdapterFactoryItemDelegator}.
856    */

857   public IItemLabelProvider getLabelProvider(Object JavaDoc object)
858   {
859     return itemDelegator;
860   }
861
862   /**
863    * This indicates whether these two property descriptors are equal.
864    * It's not really clear to me how this is meant to be used,
865    * but it's a little bit like an equals test.
866    */

867   public boolean isCompatibleWith(Object JavaDoc object, Object JavaDoc anotherObject, IItemPropertyDescriptor anotherItemPropertyDescriptor)
868   {
869 /*
870     if (propertyDescriptor == this)
871     {
872       return true;
873     }
874     else if (propertyDescriptor instanceof ItemPropertyDescriptor)
875     {
876       ItemPropertyDescriptor itemPropertyDescriptor = (ItemPropertyDescriptor)propertyDescriptor;
877       if (adapterFactory == itemPropertyDescriptor.adapterFactory &&
878             displayName.equals(itemPropertyDescriptor.displayName) &&
879             (category == null && itemPropertyDescriptor.category == null || category.equals(itemPropertyDescriptor.category)))
880       {
881         return true;
882       }
883     }
884 */

885
886     return false;
887   }
888
889   static public class PropertyValueWrapper implements IItemLabelProvider, IItemPropertySource
890   {
891     protected Object JavaDoc object;
892     protected Object JavaDoc propertyValue;
893     protected Object JavaDoc nestedPropertySource;
894     protected AdapterFactoryItemDelegator itemDelegator;
895
896     public PropertyValueWrapper(AdapterFactory adapterFactory, Object JavaDoc object, Object JavaDoc propertyValue, Object JavaDoc nestedPropertySource)
897     {
898       this.object = object;
899       this.propertyValue = propertyValue;
900       this.nestedPropertySource = nestedPropertySource;
901       this.itemDelegator = new AdapterFactoryItemDelegator(adapterFactory);
902     }
903
904     public String JavaDoc getText(Object JavaDoc thisObject)
905     {
906       return itemDelegator.getText(propertyValue);
907     }
908
909     public Object JavaDoc getImage(Object JavaDoc thisObject)
910     {
911       return itemDelegator.getImage(propertyValue);
912     }
913
914     public List JavaDoc getPropertyDescriptors(Object JavaDoc thisObject)
915     {
916       // This guards the switch.
917
//
918
List JavaDoc list = itemDelegator.getPropertyDescriptors(nestedPropertySource);
919       if (list != null)
920       {
921         List JavaDoc result = new ArrayList JavaDoc(list.size());
922         for (Iterator JavaDoc i = list.iterator(); i.hasNext(); )
923         {
924           IItemPropertyDescriptor itemPropertyDescriptor = (IItemPropertyDescriptor)i.next();
925           result.add(createPropertyDescriptorDecorator(nestedPropertySource, itemPropertyDescriptor));
926         }
927         return result;
928       }
929   
930       return Collections.EMPTY_LIST;
931     }
932
933     public IItemPropertyDescriptor getPropertyDescriptor(Object JavaDoc thisObject, Object JavaDoc propertyId)
934     {
935       return
936         createPropertyDescriptorDecorator(nestedPropertySource, itemDelegator.getPropertyDescriptor(nestedPropertySource, propertyId));
937     }
938
939     public Object JavaDoc getEditableValue(Object JavaDoc thisObject)
940     {
941       return propertyValue;
942     }
943
944     protected IItemPropertyDescriptor createPropertyDescriptorDecorator(Object JavaDoc object, IItemPropertyDescriptor itemPropertyDescriptor)
945     {
946       return new ItemPropertyDescriptorDecorator(object, itemPropertyDescriptor);
947     }
948   }
949
950   protected Object JavaDoc createPropertyValueWrapper(Object JavaDoc object, Object JavaDoc propertyValue)
951   {
952     return new PropertyValueWrapper(adapterFactory, object, propertyValue, null);
953   }
954
955   public static Object JavaDoc getDefaultValue(EClassifier eType)
956   {
957     if (eType.getEPackage() == EcorePackage.eINSTANCE)
958     {
959       switch (eType.getClassifierID())
960       {
961         case EcorePackage.EBOOLEAN:
962         case EcorePackage.EBOOLEAN_OBJECT:
963         {
964           return Boolean.FALSE;
965         }
966         case EcorePackage.EBYTE:
967         case EcorePackage.EBYTE_OBJECT:
968         {
969           return new Byte JavaDoc((byte)0);
970         }
971         case EcorePackage.ECHAR:
972         case EcorePackage.ECHARACTER_OBJECT:
973         {
974           return new Character JavaDoc(' ');
975         }
976         case EcorePackage.EDOUBLE:
977         case EcorePackage.EDOUBLE_OBJECT:
978         {
979           return new Double JavaDoc(0.0);
980         }
981         case EcorePackage.EFLOAT:
982         case EcorePackage.EFLOAT_OBJECT:
983         {
984           return new Float JavaDoc(0.0);
985         }
986         case EcorePackage.EINT:
987         case EcorePackage.EINTEGER_OBJECT:
988         {
989           return new Integer JavaDoc(0);
990         }
991         case EcorePackage.ELONG:
992         case EcorePackage.ELONG_OBJECT:
993         {
994           return new Long JavaDoc(0);
995         }
996         case EcorePackage.ESHORT:
997         case EcorePackage.ESHORT_OBJECT:
998         {
999           return new Short JavaDoc((short)0);
1000        }
1001        case EcorePackage.ESTRING:
1002        {
1003          return "";
1004        }
1005      }
1006    }
1007    else if (eType instanceof EEnum)
1008    {
1009      return ((EEnumLiteral)((EEnum)eType).getELiterals().get(0)).getInstance();
1010    }
1011    else if (eType instanceof EDataType)
1012    {
1013      EDataType eDataType = (EDataType)eType;
1014      List JavaDoc enumeration = ExtendedMetaData.INSTANCE.getEnumerationFacet(eDataType);
1015      if (!enumeration.isEmpty())
1016      {
1017        return EcoreUtil.createFromString(eDataType, (String JavaDoc)enumeration.get(0));
1018      }
1019    }
1020
1021    return null;
1022  }
1023
1024  /**
1025   * This is called by {@link #getPropertyValue getPropertyValue} to reflectively obtain the value of a feature
1026   * from an object. It can be overridden by a subclass to provide additional processing of the value.
1027   */

1028  protected Object JavaDoc getValue(EObject object, EStructuralFeature feature)
1029  {
1030    try
1031    {
1032      return object.eGet(feature);
1033    }
1034    catch (Throwable JavaDoc exception)
1035    {
1036      return null;
1037    }
1038  }
1039
1040  /**
1041   * This does the delegated job of getting the property value from the given object;
1042   * and it sets object, which is necessary if {@link #getComboBoxObjects getComboBoxObjects} is called.
1043   * It is implemented in a generic way using the structural feature or parent references.
1044   */

1045  public Object JavaDoc getPropertyValue(Object JavaDoc object)
1046  {
1047    EObject eObject = (EObject)object;
1048    if (feature instanceof EAttribute)
1049    {
1050      EAttribute attribute = (EAttribute)feature;
1051      Object JavaDoc result = getValue(eObject, attribute);
1052      if (result == null)
1053      {
1054        return getDefaultValue(attribute.getEType());
1055      }
1056      else
1057      {
1058        return createPropertyValueWrapper(object, result);
1059      }
1060    }
1061    else if (parentReferences != null)
1062    {
1063      for (int i = 0; i < parentReferences.length; ++i)
1064      {
1065        Object JavaDoc result = getValue(eObject, parentReferences[i]);
1066        if (result != null)
1067        {
1068          return createPropertyValueWrapper(object, result);
1069        }
1070      }
1071      return "";
1072    }
1073    else
1074    {
1075      return createPropertyValueWrapper(object, getValue(eObject, feature));
1076    }
1077  }
1078
1079  /**
1080   * This does the delegated job of determine whether the property value from the given object is set.
1081   * It is implemented in a generic way using the structural feature.
1082   */

1083  public boolean isPropertySet(Object JavaDoc object)
1084  {
1085    // System.out.println("isPropertySet " + object);
1086
EObject eObject = (EObject)object;
1087
1088    if (parentReferences != null)
1089    {
1090      for (int i = 0; i < parentReferences.length; ++i)
1091      {
1092        if (eObject.eGet(parentReferences[i]) != null)
1093        {
1094          return true;
1095        }
1096      }
1097      return false;
1098    }
1099    else
1100    {
1101      try
1102      {
1103        return
1104          feature instanceof EAttribute ?
1105            feature.isMany() ?
1106              !((List JavaDoc)eObject.eGet(feature)).isEmpty() :
1107              eObject.eIsSet(feature) :
1108            eObject.eGet(feature) != null;
1109      }
1110      catch (Throwable JavaDoc exception)
1111      {
1112        return false;
1113      }
1114    }
1115  }
1116
1117  /**
1118   * This determines whether this descriptor's property for the object supports set (and reset).
1119   */

1120  public boolean canSetProperty(Object JavaDoc object)
1121  {
1122    return isSettable;
1123  }
1124
1125  /**
1126   * Sets the object to use as the owner of commands created to set the property's value.
1127   */

1128  public void setCommandOwner(Object JavaDoc commandOwner)
1129  {
1130    this.commandOwner = commandOwner;
1131  }
1132
1133  /**
1134   * Returns the override command owner set via {@link setCommandOwner setCommandOwner}.
1135   */

1136  public Object JavaDoc getCommandOwner()
1137  {
1138    return commandOwner;
1139  }
1140
1141  /**
1142   * Returns either the override command owner set via {@link #setCommandOwner setCommandOwner} or, if that is null, the
1143   * fallback object provided.
1144   */

1145  protected Object JavaDoc getCommandOwner(Object JavaDoc fallback)
1146  {
1147    return commandOwner != null ? commandOwner : fallback;
1148  }
1149
1150  /**
1151   * This does the delegated job of resetting property value back to it's default value.
1152   */

1153  public void resetPropertyValue(Object JavaDoc object)
1154  {
1155    EObject eObject = (EObject)object;
1156    EditingDomain editingDomain = getEditingDomain(object);
1157
1158    if (parentReferences != null)
1159    {
1160      for (int i = 0; i < parentReferences.length; ++i)
1161      {
1162        final EReference parentReference = parentReferences[i];
1163        if (eObject.eIsSet(parentReference))
1164        {
1165          if (editingDomain == null)
1166          {
1167            eObject.eUnset(parentReferences[i]);
1168          }
1169          else
1170          {
1171            editingDomain.getCommandStack().execute(SetCommand.create(editingDomain, getCommandOwner(eObject), parentReference, null));
1172          }
1173          break;
1174        }
1175      }
1176    }
1177    else
1178    {
1179      if (editingDomain == null)
1180      {
1181        eObject.eUnset(feature);
1182      }
1183      else if (feature.isMany())
1184      {
1185        editingDomain.getCommandStack().execute(SetCommand.create(editingDomain, getCommandOwner(eObject), feature, Collections.EMPTY_LIST));
1186      }
1187      else
1188      {
1189        editingDomain.getCommandStack().execute(SetCommand.create(editingDomain, getCommandOwner(eObject), feature, null));
1190      }
1191    }
1192  }
1193
1194  public EditingDomain getEditingDomain(Object JavaDoc object)
1195  {
1196    EObject eObject = (EObject)object;
1197    EditingDomain result = AdapterFactoryEditingDomain.getEditingDomainFor(eObject);
1198    if (result == null)
1199    {
1200      if (adapterFactory instanceof IEditingDomainProvider)
1201      {
1202        result = ((IEditingDomainProvider)adapterFactory).getEditingDomain();
1203      }
1204
1205      if (result == null && adapterFactory instanceof ComposeableAdapterFactory)
1206      {
1207        AdapterFactory rootAdapterFactory = ((ComposeableAdapterFactory)adapterFactory).getRootAdapterFactory();
1208        if (rootAdapterFactory instanceof IEditingDomainProvider)
1209        {
1210          result = ((IEditingDomainProvider)rootAdapterFactory).getEditingDomain();
1211        }
1212      }
1213    }
1214    return result;
1215  }
1216
1217  /**
1218   * This does the delegated job of setting the property to the given value.
1219   * It is implemented in a generic way using the structural feature.
1220   */

1221  public void setPropertyValue(Object JavaDoc object, Object JavaDoc value)
1222  {
1223    EObject eObject = (EObject)object;
1224    EditingDomain editingDomain = getEditingDomain(object);
1225
1226    if (parentReferences != null)
1227    {
1228      Command removeCommand = null;
1229      for (int i = 0; i < parentReferences.length; ++i)
1230      {
1231        Object JavaDoc oldValue = eObject.eGet(parentReferences[i]);
1232        if (oldValue != null)
1233        {
1234          final EReference parentReference = parentReferences[i];
1235          if (oldValue == value)
1236          {
1237            return;
1238          }
1239          else if (parentReference.getEType().isInstance(value))
1240          {
1241            if (editingDomain == null)
1242            {
1243              eObject.eSet(parentReference, value);
1244            }
1245            else
1246            {
1247              editingDomain.getCommandStack().execute(SetCommand.create(editingDomain, getCommandOwner(eObject), parentReference, value));
1248            }
1249            return;
1250          }
1251          else
1252          {
1253            if (editingDomain == null)
1254            {
1255              eObject.eSet(parentReference, null);
1256            }
1257            else
1258            {
1259              removeCommand = SetCommand.create(editingDomain, getCommandOwner(eObject), parentReference, null);
1260            }
1261            break;
1262          }
1263        }
1264      }
1265
1266      for (int i = 0; i < parentReferences.length; ++i)
1267      {
1268        final EReference parentReference = parentReferences[i];
1269        if (parentReference.getEType().isInstance(value))
1270        {
1271          if (editingDomain == null)
1272          {
1273            eObject.eSet(parentReferences[i], value);
1274          }
1275          else
1276          {
1277            if (removeCommand != null)
1278            {
1279              final CompoundCommand compoundCommand = new CompoundCommand(CompoundCommand.LAST_COMMAND_ALL);
1280              compoundCommand.append(removeCommand);
1281              compoundCommand.append(SetCommand.create(editingDomain, getCommandOwner(eObject), parentReference, value));
1282              editingDomain.getCommandStack().execute(compoundCommand);
1283            }
1284            else
1285            {
1286              editingDomain.getCommandStack().execute(SetCommand.create(editingDomain, getCommandOwner(eObject), parentReference, value));
1287            }
1288          }
1289          break;
1290        }
1291      }
1292    }
1293    else
1294    {
1295      if (editingDomain == null)
1296      {
1297        eObject.eSet(feature, value);
1298      }
1299      else
1300      {
1301        editingDomain.getCommandStack().execute(SetCommand.create(editingDomain, getCommandOwner(eObject), feature, value));
1302      }
1303    }
1304  }
1305
1306  public Object JavaDoc getFeature(Object JavaDoc object)
1307  {
1308    if (feature != null)
1309    {
1310      return feature;
1311    }
1312    else if (parentReferences != null)
1313    {
1314      return parentReferences;
1315    }
1316    else
1317    {
1318      return null;
1319    }
1320  }
1321
1322  /**
1323   * Returns whether this property represents multiple values. This is true only if we're using a {@link #feature
1324   * structural feature} to provide the values for this property, and if that feature is multi-valued.
1325   */

1326  public boolean isMany(Object JavaDoc object)
1327  {
1328    return parentReferences == null && feature != null && feature.isMany();
1329  }
1330
1331  public Collection JavaDoc getChoiceOfValues(Object JavaDoc object)
1332  {
1333    return getComboBoxObjects(object);
1334  }
1335}
1336
Popular Tags