KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 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: AttributeValueWrapperItemProvider.java,v 1.3 2005/06/08 06:17:05 nickb Exp $
16  */

17 package org.eclipse.emf.edit.provider;
18
19
20 import java.util.Collections JavaDoc;
21 import java.util.List JavaDoc;
22
23 import org.eclipse.emf.common.command.Command;
24 import org.eclipse.emf.common.notify.AdapterFactory;
25 import org.eclipse.emf.common.util.ResourceLocator;
26 import org.eclipse.emf.ecore.EAttribute;
27 import org.eclipse.emf.ecore.EDataType;
28 import org.eclipse.emf.ecore.EObject;
29 import org.eclipse.emf.ecore.util.EcoreUtil;
30 import org.eclipse.emf.edit.command.CommandParameter;
31 import org.eclipse.emf.edit.command.CopyCommand;
32 import org.eclipse.emf.edit.command.SetCommand;
33 import org.eclipse.emf.edit.domain.EditingDomain;
34
35
36 /**
37  * A wrapper implementation for simple attribute values.
38  */

39 public class AttributeValueWrapperItemProvider extends WrapperItemProvider
40   implements
41     IStructuredItemContentProvider,
42     ITreeItemContentProvider,
43     IItemLabelProvider,
44     IItemPropertySource,
45     IEditingDomainItemProvider
46 {
47   /**
48    * The resource locator from the owner's item provider.
49    */

50   protected ResourceLocator resourceLocator;
51
52   /**
53    * The single property descriptor for the value is cached here as a singleton list.
54    */

55   protected List JavaDoc propertyDescriptors;
56
57   /**
58    * Creates an instance for a single-valued attribute.
59    */

60   public AttributeValueWrapperItemProvider(Object JavaDoc value, EObject owner, EAttribute attribute, AdapterFactory adapterFactory, ResourceLocator resourceLocator)
61   {
62     super(value, owner, attribute, CommandParameter.NO_INDEX, adapterFactory);
63     this.resourceLocator = resourceLocator;
64   }
65
66   /**
67    * Creates an instance for a value within a multi-valued attribute.
68    */

69   public AttributeValueWrapperItemProvider(Object JavaDoc value, EObject owner, EAttribute attribute, int index, AdapterFactory adapterFactory, ResourceLocator resourceLocator)
70   {
71     super(value, owner, attribute, index, adapterFactory);
72     this.resourceLocator = resourceLocator;
73   }
74
75   /**
76    * Creates an instance for a single-valued attribute. Because the item property descriptor that will be created for
77    * the value should get a resource locator, this constructor has been deprecated.
78    *
79    * @deprecated As of EMF 2.0.1, replaced by {@link #AttributeValueWrapperItemProvider(Object, EObject, EAttribute, AdapterFactory, ResourceLocator)
80    * this form}.
81    */

82   public AttributeValueWrapperItemProvider(Object JavaDoc value, EObject owner, EAttribute attribute, AdapterFactory adapterFactory)
83   {
84     this(value, owner, attribute, adapterFactory, null);
85   }
86
87   /**
88    * Creates an instance for a value within a multi-valued attribute. Because the item property descriptor that will be
89    * created for the value should get a resource locator, this constructor has been deprecated.
90    *
91    * @deprecated As of EMF 2.0.1, replaced by {@link #AttributeValueWrapperItemProvider(Object, EObject, EAttribute, int, AdapterFactory, ResourceLocator)
92    * this form}.
93    */

94   public AttributeValueWrapperItemProvider(Object JavaDoc value, EObject owner, EAttribute attribute, int index, AdapterFactory adapterFactory)
95   {
96     this(value, owner, attribute, index, adapterFactory, null);
97   }
98
99   /**
100    * If non-null, the value is converted to a string, using the type of its attribute and the appropriate factory.
101    */

102   public String JavaDoc getText(Object JavaDoc object)
103   {
104     return value != null ? EcoreUtil.convertToString(((EAttribute)feature).getEAttributeType(), value) : "null";
105   }
106
107   /**
108    * Creates, caches and returns an item property descriptor for the value.
109    */

110   public List JavaDoc getPropertyDescriptors(Object JavaDoc object)
111   {
112     if (propertyDescriptors == null)
113     {
114       propertyDescriptors = Collections.singletonList(new WrapperItemPropertyDescriptor(resourceLocator, feature));
115     }
116     return propertyDescriptors;
117   }
118
119   /**
120    * Returns a wrapped set command that returns as its affected object the replacement wrapper for the value.
121    */

122   protected Command createSetCommand(EditingDomain domain, Object JavaDoc owner, Object JavaDoc feature, Object JavaDoc value, int index)
123   {
124     return new ReplacementAffectedObjectCommand(SetCommand.create(domain, this.owner, this.feature, value, this.index));
125   }
126
127   /**
128    * Returns a {@link WrapperItemProvider.SimpleCopyCommand} that copies the value by converting it into a string and
129    * back, using the factory methods.
130    */

131   protected Command createCopyCommand(EditingDomain domain, Object JavaDoc owner, CopyCommand.Helper helper)
132   {
133     return new SimpleCopyCommand(domain)
134     {
135       public IWrapperItemProvider copy()
136       {
137         Object JavaDoc valueCopy = null;
138         
139         if (value != null)
140         {
141           EDataType dataType = ((EAttribute)feature).getEAttributeType();
142           String JavaDoc serialization = EcoreUtil.convertToString(dataType, value);
143           valueCopy = EcoreUtil.createFromString(dataType, serialization);
144           if (serialization == value && serialization == valueCopy)
145           {
146             valueCopy = new String JavaDoc((String JavaDoc)value);
147           }
148         }
149         return new AttributeValueWrapperItemProvider(
150           valueCopy, (EObject)AttributeValueWrapperItemProvider.this.owner, (EAttribute)feature, index, adapterFactory);
151       }
152     };
153   }
154 }
155
Popular Tags