Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Override IItemLabelProvider.getText() for all derived classes
Override IItemLabelProvider.getText() for all derived classes [message #1823910] Fri, 03 April 2020 16:27 Go to next message
Denis Nikiforov is currently offline Denis NikiforovFriend
Messages: 343
Registered: August 2013
Senior Member
Hi

There is a NamedElement class in my ecore-model. Also there are a lot of classes derived from the NamedElement class in separate ecore-models contained in separate projects.

I need to implement IItemLabelProvider.getText() for the NamedElement class. And I need to use this implementation in all derived classes. Also I'd like not to edit a generated code. Is it possible to achive it using some factory subclassing or using extension points?

Is it possible to customize labels once in the base project defining NamedElement class? Or I should customize them for each project based on the first one?

I had read this page https://wiki.eclipse.org/EMF/Recipes#Recipe:_Custom_Labels But involves edition of a generated code, what I try to avoid. Also it seems that I have to edit getText() method for each class.
Re: Override IItemLabelProvider.getText() for all derived classes [message #1823933 is a reply to message #1823910] Sat, 04 April 2020 05:26 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33136
Registered: July 2009
Senior Member
The only configurable thing in terms of affecting the getText of the generated item providers is setting the Label Feature property of each GenClass.

Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Override IItemLabelProvider.getText() for all derived classes [message #1823939 is a reply to message #1823933] Sat, 04 April 2020 08:11 Go to previous messageGo to next message
Denis Nikiforov is currently offline Denis NikiforovFriend
Messages: 343
Registered: August 2013
Senior Member
To be more precise I need to replace the default implementation:
    public String getText(Object object) {
        String label = ((NamedElement) object).getName();
        return label == null || label.length() == 0 ? getString("_UI_NamedElement_type")
                : getString("_UI_NamedElement_type") + " " + label;
    }

by this one:
    public String getText(Object object) {
        String label = ((NamedElement) object).getName();
        return label == null || label.length() == 0 ? getString("_UI_NamedElement_type")
                : label.substring(0, 1).toUpperCase() + label.substring(1);
    }


It can't be achived by changing of Label Feature property. The simplest approach is to change these methods manually and to mark them as @generated NOT.

However I try to alternate the implementation without changing the generated code.

I register a custom adapter factory:
   <extension point="org.eclipse.emf.edit.itemProviderAdapterFactories">
      <factory
            uri="..."
            class="...provider.CustomProductItemProviderAdapterFactory"
            supportedTypes="org.eclipse.emf.edit.provider.IItemLabelProvider"/>
   </extension>


Disable the default adapter factory in MyEditor.initializeEditingDomain() Because otherwise it overrides my custom adapter factory.

My custom adapter factory creates an item provider with a custom getText() method:
    public Adapter adapt(Notifier notifier, Object type) {
        if (notifier instanceof NamedElement && IItemLabelProvider.class.isAssignableFrom((Class<?>) type)) {
            return createNamedElementAdapter();
        }
        return super.adapt(notifier, this);
    }


It seems that it works. Object labels are created using this method.

But I get NPE at org.eclipse.emf.edit.ui.provider.ComposedImageDescriptor.getSize(ExtendedImageRegistry.java:407)
It seems that I broke an image provider for objects.

All this stuff is too tricky. I guess that my approach is wrong. And if I understood you write there is no a simple way to override getText()
Re: Override IItemLabelProvider.getText() for all derived classes [message #1823941 is a reply to message #1823939] Sat, 04 April 2020 09:48 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33136
Registered: July 2009
Senior Member
No, it's generally expect that generated item providers will be modified to produce nice labels. The only other approach would be to use dynamic templates and make use of the insertion point
	public String getText(Object object)
	{
<%@ include file="ItemProvider/getText.override.javajetinc" fail="alternative" %>
But that's not totally trivial to configure and you'll see that the logic you're replacing is very complex...


Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:[beginners] [Acceleo] User code blocks with @generated do not work / target file is no longer merged
Next Topic:XSD to Ecore: Group renamed to Group1
Goto Forum:
  


Current Time: Fri Apr 19 05:25:26 GMT 2024

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

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

Back to the top