1 17 package org.eclipse.emf.ecore.provider; 18 19 20 import java.util.ArrayList ; 21 import java.util.Collection ; 22 import java.util.Collections ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 26 import org.eclipse.emf.common.command.CompoundCommand; 27 import org.eclipse.emf.common.notify.AdapterFactory; 28 import org.eclipse.emf.common.notify.Notification; 29 import org.eclipse.emf.common.util.ResourceLocator; 30 import org.eclipse.emf.ecore.EClass; 31 import org.eclipse.emf.ecore.EReference; 32 import org.eclipse.emf.ecore.EcorePackage; 33 import org.eclipse.emf.edit.command.SetCommand; 34 import org.eclipse.emf.edit.domain.EditingDomain; 35 import org.eclipse.emf.edit.provider.ComposeableAdapterFactory; 36 import org.eclipse.emf.edit.provider.IEditingDomainItemProvider; 37 import org.eclipse.emf.edit.provider.IItemLabelProvider; 38 import org.eclipse.emf.edit.provider.IItemPropertySource; 39 import org.eclipse.emf.edit.provider.IStructuredItemContentProvider; 40 import org.eclipse.emf.edit.provider.ITreeItemContentProvider; 41 import org.eclipse.emf.edit.provider.ItemPropertyDescriptor; 42 43 44 import org.eclipse.emf.edit.provider.ViewerNotification; 45 46 52 public class EReferenceItemProvider 53 extends EStructuralFeatureItemProvider 54 implements 55 IEditingDomainItemProvider, IStructuredItemContentProvider, ITreeItemContentProvider, IItemLabelProvider, IItemPropertySource 56 { 57 63 public EReferenceItemProvider(AdapterFactory adapterFactory) 64 { 65 super(adapterFactory); 66 } 67 68 74 public List getPropertyDescriptors(Object object) 75 { 76 if (itemPropertyDescriptors == null) 77 { 78 super.getPropertyDescriptors(object); 79 80 addContainmentPropertyDescriptor(object); 81 addContainerPropertyDescriptor(object); 82 addResolveProxiesPropertyDescriptor(object); 83 addEOppositePropertyDescriptor(object); 84 addEReferenceTypePropertyDescriptor(object); 85 } 86 return itemPropertyDescriptors; 87 } 88 89 95 protected void addContainmentPropertyDescriptor(Object object) 96 { 97 itemPropertyDescriptors.add 98 (createItemPropertyDescriptor 99 (((ComposeableAdapterFactory)adapterFactory).getRootAdapterFactory(), 100 getResourceLocator(), 101 getString("_UI_EReference_containment_feature"), 102 getString("_UI_PropertyDescriptor_description", "_UI_EReference_containment_feature", "_UI_EReference_type"), 103 EcorePackage.eINSTANCE.getEReference_Containment(), 104 true, 105 ItemPropertyDescriptor.BOOLEAN_VALUE_IMAGE, 106 null, 107 null)); 108 } 109 110 116 protected void addContainerPropertyDescriptor(Object object) 117 { 118 itemPropertyDescriptors.add 119 (createItemPropertyDescriptor 120 (((ComposeableAdapterFactory)adapterFactory).getRootAdapterFactory(), 121 getResourceLocator(), 122 getString("_UI_EReference_container_feature"), 123 getString("_UI_PropertyDescriptor_description", "_UI_EReference_container_feature", "_UI_EReference_type"), 124 EcorePackage.eINSTANCE.getEReference_Container(), 125 false, 126 ItemPropertyDescriptor.BOOLEAN_VALUE_IMAGE, 127 null, 128 null)); 129 } 130 131 137 protected void addResolveProxiesPropertyDescriptor(Object object) 138 { 139 itemPropertyDescriptors.add 140 (createItemPropertyDescriptor 141 (((ComposeableAdapterFactory)adapterFactory).getRootAdapterFactory(), 142 getResourceLocator(), 143 getString("_UI_EReference_resolveProxies_feature"), 144 getString("_UI_PropertyDescriptor_description", "_UI_EReference_resolveProxies_feature", "_UI_EReference_type"), 145 EcorePackage.eINSTANCE.getEReference_ResolveProxies(), 146 true, 147 ItemPropertyDescriptor.BOOLEAN_VALUE_IMAGE, 148 null, 149 null)); 150 } 151 152 158 protected void addEOppositePropertyDescriptor(Object object) 159 { 160 itemPropertyDescriptors.add 161 (new ItemPropertyDescriptor 162 (((ComposeableAdapterFactory)adapterFactory).getRootAdapterFactory(), 163 getResourceLocator(), 164 getString("_UI_EReference_eOpposite_feature"), 165 getString("_UI_PropertyDescriptor_description", "_UI_EReference_eOpposite_feature", "_UI_EReference_type"), 166 EcorePackage.eINSTANCE.getEReference_EOpposite(), 167 true) 168 { 169 public Collection getChoiceOfValues(Object object) 170 { 171 EReference eReference = (EReference)object; 172 EClass eContainingClass = eReference.getEContainingClass(); 173 EClass eReferenceType = eReference.getEReferenceType(); 174 if (eContainingClass == null || eReferenceType == null) 175 { 176 return Collections.EMPTY_LIST; 177 } 178 Collection result = new ArrayList (super.getChoiceOfValues(object)); 179 for (Iterator i = result.iterator(); i.hasNext(); ) 180 { 181 EReference eOpposite = (EReference)i.next(); 182 if (eOpposite != null) 183 { 184 if (eOpposite == eReference) 185 { 186 i.remove(); 187 } 188 else 189 { 190 EClass eOppositeContainingClass = eOpposite.getEContainingClass(); 191 EClass eOppositeReferenceType = eOpposite.getEReferenceType(); 192 if (eOppositeContainingClass == null || 193 !eOppositeContainingClass.isSuperTypeOf(eReferenceType) || 194 !eContainingClass.isSuperTypeOf(eOppositeReferenceType)) 195 { 196 i.remove(); 197 } 198 } 199 } 200 } 201 return result; 202 } 203 204 public void setPropertyValue(Object object, Object value) 205 { 206 EReference eReference = (EReference)object; 207 EReference eOpposite = (EReference)value; 208 EditingDomain editingDomain = getEditingDomain(eReference); 209 if (editingDomain == null) 210 { 211 EReference oldReferenceOpposite = eReference.getEOpposite(); 212 if (oldReferenceOpposite != null) 213 { 214 oldReferenceOpposite.setEOpposite(null); 215 } 216 if (eOpposite != null) 217 { 218 EReference oldOppositeOpposite = eOpposite.getEOpposite(); 219 if (oldOppositeOpposite != null) 220 { 221 oldOppositeOpposite.setEOpposite(null); 222 } 223 eOpposite.setEOpposite(eReference); 224 } 225 eReference.setEOpposite(eOpposite); 226 } 227 else 228 { 229 CompoundCommand compoundCommand = new CompoundCommand(CompoundCommand.LAST_COMMAND_ALL); 230 EReference oldReferenceOpposite = eReference.getEOpposite(); 231 if (oldReferenceOpposite != null) 232 { 233 compoundCommand.append(SetCommand.create(editingDomain, getCommandOwner(oldReferenceOpposite), feature, null)); 234 } 235 if (eOpposite != null) 236 { 237 EReference oldOppositeOpposite = eOpposite.getEOpposite(); 238 if (oldOppositeOpposite != null) 239 { 240 compoundCommand.append(SetCommand.create(editingDomain, getCommandOwner(oldOppositeOpposite), feature, null)); 241 } 242 compoundCommand.append(SetCommand.create(editingDomain, getCommandOwner(eOpposite), feature, eReference)); 243 } 244 compoundCommand.append(SetCommand.create(editingDomain, getCommandOwner(eReference), feature, eOpposite)); 245 editingDomain.getCommandStack().execute(compoundCommand); 246 } 247 } 248 }); 249 } 250 251 257 protected void addEReferenceTypePropertyDescriptor(Object object) 258 { 259 itemPropertyDescriptors.add 260 (createItemPropertyDescriptor 261 (((ComposeableAdapterFactory)adapterFactory).getRootAdapterFactory(), 262 getResourceLocator(), 263 getString("_UI_EReference_eReferenceType_feature"), 264 getString("_UI_PropertyDescriptor_description", "_UI_EReference_eReferenceType_feature", "_UI_EReference_type"), 265 EcorePackage.eINSTANCE.getEReference_EReferenceType(), 266 false, 267 null, 268 null, 269 null)); 270 } 271 272 278 public Object getImage(Object object) 279 { 280 return getComposedImage(object, getResourceLocator().getImage("full/obj16/EReference")); 281 } 282 283 289 public String getText(Object object) 290 { 291 EReference eReference = (EReference)object; 292 StringBuffer result = new StringBuffer (); 293 result.append(eReference.getName()); 294 if (eReference.getEType() != null) 295 { 296 result.append(" : "); 297 result.append(eReference.getEType().getName()); 298 } 299 return result.toString(); 300 } 301 302 309 public void notifyChanged(Notification notification) 310 { 311 updateChildren(notification); 312 313 switch (notification.getFeatureID(EReference.class)) 314 { 315 case EcorePackage.EREFERENCE__CONTAINMENT: 316 case EcorePackage.EREFERENCE__CONTAINER: 317 case EcorePackage.EREFERENCE__RESOLVE_PROXIES: 318 case EcorePackage.EREFERENCE__EOPPOSITE: 319 case EcorePackage.EREFERENCE__EREFERENCE_TYPE: 320 fireNotifyChanged(new ViewerNotification(notification, notification.getNotifier(), false, true)); 321 return; 322 } 323 super.notifyChanged(notification); 324 } 325 326 333 protected void collectNewChildDescriptors(Collection newChildDescriptors, Object object) 334 { 335 super.collectNewChildDescriptors(newChildDescriptors, object); 336 } 337 338 344 public ResourceLocator getResourceLocator() 345 { 346 return EcoreEditPlugin.INSTANCE; 347 } 348 349 } 350 | Popular Tags |