Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » How to find out reference and references of a EClass?
How to find out reference and references of a EClass? [message #1833137] Sat, 03 October 2020 10:30 Go to next message
Dimg Cim is currently offline Dimg CimFriend
Messages: 59
Registered: December 2015
Member
Hello all,

I tried to implement a PropertyView for my own use. And when the EMF TreeViewer changed this PropertyView will updated by the attributes, this should be worked, but I am not sure how to display the Reference and References in my custom view.

E.g.: I have an xcore model for

class Addressboo{
   contains Entry[0..*] entries
  contains Group[0..*] groups
}

class Group{
  String name 
   refers Entry[0..*] associatedEntries
}

class Entry{
  String name
}


The following method will update my view on selection changed.

	@Override
	public void changed(ObservableValue<? extends Object> observable, Object oldValue, Object newValue) {
		if (newValue instanceof TreeItem) {
			TreeItem<Object> item = (TreeItem) newValue;
			Object object = item.getValue();
			
			if (object instanceof EObject) {
				properties.clear();
				
				// Displaying attributes works!
				cuurentSelection = (EObject) object;
				for (EAttribute attr : cuurentSelection.eClass().getEAllAttributes()) {
					properties.add(attr);
				}

				// How to get the associated entries references of the Group
				IItemPropertySource itemPropertySource = (IItemPropertySource) adapterFactory.adapt(cuurentSelection, IItemPropertySource.class);
				List<IItemPropertyDescriptor> descriptors = itemPropertySource.getPropertyDescriptors(cuurentSelection);
				for (IItemPropertyDescriptor iItemPropertyDescriptor : descriptors) {
					Object feature = iItemPropertyDescriptor.getFeature(cuurentSelection);
					if (feature instanceof EReference) {
						System.out.println(feature + " : "+iItemPropertyDescriptor+" : "+iItemPropertyDescriptor.getChoiceOfValues(object));
					}
				}
			}
		}
	}


How can I get the propertyId of the associatedEntries of the Group to get the right ItemPropertyDescriptor. I can get all the descriptors, but for only one the propertyId is required
IItemPropertySource itemPropertySource = (IItemPropertySource) adapterFactory.adapt(cuurentSelection, IItemPropertySource.class);
				List<IItemPropertyDescriptor> descriptors = itemPropertySource.getPropertyDescriptor(cuurentSelection, propertyId);


how can I find out whether the attribute associatedEntries can set one or more references? I took a look into the PropertyDescriptor, maybe this is the right way itemPropertyDescriptor.isMany(object)?

best regards
dim

[Updated on: Sat, 03 October 2020 10:31]

Report message to a moderator

Re: How to find out reference and references of a EClass? [message #1833138 is a reply to message #1833137] Sat, 03 October 2020 10:58 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33113
Registered: July 2009
Senior Member
As the Javadoc of org.eclipse.emf.edit.provider.IItemPropertySource.getPropertyDescriptor(Object, Object) suggests:
  /**
   * This returns the property descriptor
   * with an {@link IItemPropertyDescriptor#getId(Object) ID} or {@link IItemPropertyDescriptor#getFeature(Object) feature} 
   * that matches the given ID.
   */
  IItemPropertyDescriptor getPropertyDescriptor(Object object, Object propertyID);
you can also use the feature as the second argument.

Also, if you look at the implementation of org.eclipse.emf.edit.provider.ItemProviderAdapter.getPropertyDescriptor(Object, Object) you can see that it loops to find the property descriptor testing against both the ID and the feature:
  public IItemPropertyDescriptor getPropertyDescriptor(Object object, Object propertyId)
  {
    for (IItemPropertyDescriptor itemPropertyDescriptor : getPropertyDescriptors(object))
    {
      if (propertyId.equals(itemPropertyDescriptor.getId(object)) || propertyId.equals(itemPropertyDescriptor.getFeature(object)))
      {
        return itemPropertyDescriptor;
      }
    }

    return null;
  }
And yes, org.eclipse.emf.edit.provider.IItemPropertyDescriptor.isMany(Object) is generally true for multi-valued values (unless the code has been specialized in some funky way).


Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:CDO - Issue with partial commit
Next Topic:Ecore containment property.
Goto Forum:
  


Current Time: Thu Mar 28 09:32:18 GMT 2024

Powered by FUDForum. Page generated in 0.06182 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top