How to set a reference with ecore reflection? [message #1836518] |
Wed, 06 January 2021 16:28 |
Dimg Cim Messages: 59 Registered: December 2015 |
Member |
|
|
Hello all,
here I have defned my model with Xcore. Now I want to find out how to programmatically set the referencing of the persons (associatedPerson) to the group via reflection. I tried a lot but maybe this is not the right way.
class Person {
String firstname
}
class Group {
refers Person[] associatedPerson
String name
contains ExtendedGroup[] extendedgroup
}
class Manager {
contains Group[] groups
contains Person[] persons
}
class ExtendedGroup {
}
here is the corresponding test class to that model with trying to set the reference from Person to Group.
public static void main(String[] args) {
ComposedAdapterFactory adapterFactory = new ComposedAdapterFactory(ComposedAdapterFactory.Descriptor.Registry.INSTANCE);
adapterFactory.addAdapterFactory(new ExampleItemProviderAdapterFactory());
adapterFactory.addAdapterFactory(new ResourceItemProviderAdapterFactory());
adapterFactory.addAdapterFactory(new ReflectiveItemProviderAdapterFactory());
Manager manager = ExampleFactory.eINSTANCE.createManager();
Group group = ExampleFactory.eINSTANCE.createGroup();
manager.getGroups().add(group);
Person person1 = ExampleFactory.eINSTANCE.createPerson();
Person person2 = ExampleFactory.eINSTANCE.createPerson();
group.getAssociatedPerson().add(person1);
group.getAssociatedPerson().add(person2);
ExtendedGroup extendedGroup = ExampleFactory.eINSTANCE.createExtendedGroup();
manager.getGroups().add(group);
manager.getPersons().add(person1);
manager.getPersons().add(person2);
// From here on I would like to do the setting of the associated persons via Reflection
EClass eClass = group.eClass();
EList<EReference> references = eClass.getEAllReferences();
for (EReference r : references) {
if (r.isContainment()) {
// Maybe something like this should be done?
// Add person1 and person2 to group
r.eSet()
// Here the way without Reflections
group.getAssicatedPersons().add(person1);
group.getAssicatedPersons().add(person2);
}
}
best regards
Dimg
[Updated on: Wed, 06 January 2021 16:28] Report message to a moderator
|
|
|
|
|
|
Re: How to set a reference with ecore reflection? [message #1836566 is a reply to message #1836563] |
Thu, 07 January 2021 15:41 |
Dimg Cim Messages: 59 Registered: December 2015 |
Member |
|
|
Just another question. How can I find out which propertyId is used for the EReference of the object. I tried to implement something like the FeatureEditorDialog logic for References. I find this why to get the chosen values
[code][IItemPropertySource itemPropertySource = ...;
IItemPropertyDescriptor propertyDescriptors = itemPropertySource.getPropertyDescriptor(object, propertyID)
propertyDescriptors.getChoiceOfValues(object);
/code]
but where can I get the property id?
And how can I get all the values of the FeatureEditorDialog to set the choices?
I know that a Group has the reference associatedPerson and in the ExamplePackage I can get the GROUP__ASSOCIATED_PERSON with the index 0, but there should be a way to get the propertyId, aren't there? How do the FeatureEditorDialog do this?
[Updated on: Thu, 07 January 2021 15:44] Report message to a moderator
|
|
|
Re: How to set a reference with ecore reflection? [message #1836573 is a reply to message #1836566] |
Thu, 07 January 2021 18:28 |
Ed Merks Messages: 33113 Registered: July 2009 |
Senior Member |
|
|
You can use the feature itself as the propertID.
The org.eclipse.emf.edit.provider.ItemPropertyDescriptor.getComboBoxObjects(Object) already works reflectively to populate all the choices, so I don't know why you're asking this question.
You should use the debugger to try to answer these kinds of questions by looking at how the reflective editor works...
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
|
Powered by
FUDForum. Page generated in 0.04784 seconds