How to get an Ecore feature disply name without an object instance?

350 Views Asked by At

I'd like to create a GUI table to display a given list of features of an EObject sub-class. To do this I have to get the display names of the features for the column header.

How do I get the feature display names in the best way?

One solution that seems a bit like a hack:

If I have an instance of the class then I can use the adaptor factory to get a IItemPropertySource that can do this:

SomeEntity e = ...
String displayName = adaptorFactory.adapt(e, IItemPropertySource.class)
    .getPropertyDescriptor(null, feature).getDisplayName(null));

But when the table is empty there is no SomeEntity object handy to use to get the IItemPropertySource.

I can create a dummy object using the EFactory in this way:

EClass containingClass = feature.getEContainingClass();
SomeEntity dummy = containingClass.getEPackage().getEFactoryInstance()
    .create(containingClass));

... and then use that object the get the IItemPropertySource. But this seem a bit like a hack. Is there no better solution?

1

There are 1 best solutions below

4
Markus On

If you know the class at compile time, you can create the ItemProviderAdapter yourself:

MyClassItemProvider provider = new MyClassItemProvider(adaptorFactory);
String name = provider.getPropertyDescriptor(null, property).getDisplayName(null);

If you do not know the class at compile time, but only have an EClass instance at runtime, things are more complicated, because the necessary methods are protected. You have to "make" them public first.

I would add respective methods to the generated MyPackageSwitch and MyPackageAdapterFactory classes (in myPackage.util).

In MyPackageAdapterFactory:

/**
 * @generated NOT
 */
public MyPackageSwitch<Adapter> getModelSwitch() {
    return modelSwitch;
}

In MyPackageSwitch:

/**
 * generated NOT
 */
public T doPublicSwitch(EClass theEClass, EObject theEObject) {
    return doSwitch(theEClass, theEObject);
}

Now you can create an ItemProviderAdapter for an EClass theEClass like this:

provider = (ItemProviderAdapter) adapterFactory.getModelSwitch()
        .doPublicSwitch(theEClass, null);

EMF was obviously not made for this. Keep in mind that this all is only working if you do not have any custom provider implementations that uses the EObject values.