DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations

Extending EMF ItemProviders using Google Guice – I

Nirmal Sasidharan user avatar by
Nirmal Sasidharan
·
Jun. 09, 11 · Interview
Like (0)
Save
Tweet
Share
6.64K Views

Join the DZone community and get the full member experience.

Join For Free

emf itemprovider is the most important middleman in emf framework providing all of the interfaces needed for model objects to be viewed or edited. they adapt emf objects by providing the following:

1. content and label providers which enables model objects to be viewed in eclipse viewers
2. property source for model objects to make them available in eclipse property view
3. commands for editing model objects
4. forwarding change notifications on to eclipse viewers

emf generates the itemproviders for you assuming some defaults. although the defaults work most of time, there are occasions where you would like to change these. you could make the changes directly in generated code and add @generated not annotations. however, by doing this you invite trouble from mdsd gurus. because the principle says, “don’t touch generated code!”. ideally you would like to retain the generated itemproviders as it is and extend them somehow with your changes.

the tutorial shows how to do this in an elegant way by using dependency injection (di). we will use the emf example of “extended library model” to take us through this.

setup

1. create a new eclipse workspace

2. add the “emf extended library model example” projects using “new project wizard”

3. run the example projects and create a sample library model

extending itemproviders the emf way

the generated editor by default displays the title of book in the tree editor. this is because of the default gettext() implementation in bookitemprovider .

/**
* this returns the label text for the adapted class.
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated
*/
@override
public string gettext(object object)
{
string label = ((book)object).gettitle();
return label == null || label.length() == 0 ?
getstring("_ui_book_type") : //$non-nls-1$
getstring("_ui_book_type") + " " + label; //$non-nls-1$ //$non-nls-2$
}

lets say we want to change this such that the number of pages in the book is also displayed along with its title. you could do this by changing the generated code and adding the @generated not annotation.

/**
* this returns the label text for the adapted class.
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated not
*/
@override
public string gettext(object object)
{
string label = ((book)object).gettitle() + " (" + ((book) object).getpages() + ") ";
return label == null || label.length() == 0 ?
getstring("_ui_book_type") : //$non-nls-1$
getstring("_ui_book_type") + " " + label; //$non-nls-1$ //$non-nls-2$
}
if you now run the editor, the number of pages is displayed together with the book name.

this however breaks the generation gap pattern in code generation.

extending itemproviders by extension

the cleaner approach is to extend the generated itemprovider to include your changes. now we have to also make sure that the tooling uses our customized itemprovider. this is rather easy.

emf itemproviders are in fact emf adapters. they provide some behavioural extensions to the modeled objects. the recommended way of creating adapters in emf is using adapterfactories. the itemproviders are also created in this way, using the generated itemprovideradapterfactory, extlibraryitemprovideradapterfactory in this example. you could override createxxxadapter() methods to create an instance of your custom itemprovider and register your extended itemprovideradapterfactory.

lets first do this the traditional way (without di).

1. following the generation gap pattern article by heiko, you could change the extlibrary.genmode l to output the generated code in a src-gen folder of the edit plugin and put your extensions in src folder. in this tutorial, to further isolate our changes we create a new extension plugin, org.eclipse.example.library.edit.extensio n.

2. create a new class bookitemproviderextension which extends bookitemprovider within the new plugin.

public class bookitemproviderextension extends bookitemprovider {

public bookitemproviderextension(adapterfactory adapterfactory) {
super(adapterfactory);
}

@override
public string gettext(object object) {
return super.gettext(object) + " (" + ((book) object).getpages() + ") ";
}

}

3. create extlibraryitemprovideradapterfactoryextension which extends extlibraryitemprovideradapterfactory

public class extlibraryitemprovideradapterfactoryextension extends
extlibraryitemprovideradapterfactory {

@override
public adapter createbookadapter() {
if (bookitemprovider == null)
{
bookitemprovider = new bookitemproviderextension(this);
}
return bookitemprovider;
}

}

4. modify the editor code to use the new itemprovideradapterfactoryextension

public class extlibraryeditor extends multipageeditorpart
implements
ieditingdomainprovider,
iselectionprovider,
imenulistener,
iviewerprovider,
igotomarker
{
...
protected void initializeeditingdomain()
{
// create an adapter factory that yields item providers.
//
adapterfactory = new composedadapterfactory(composedadapterfactory.descriptor.registry.instance);
adapterfactory.addadapterfactory(new resourceitemprovideradapterfactory());
adapterfactory.addadapterfactory(new extlibraryitemprovideradapterfactoryextension());
adapterfactory.addadapterfactory(new reflectiveitemprovideradapterfactory());
...
}
...
}

if you run the editor again, by including the new extension plugin you would get the same result as before.  with this step, we managed to isolate our changes to a new plugin.

extending itemproviders using google guice

guice although, we managed to isolate our changes without changing the generated code, this might not be enough in the long run. what if

1. you need multiple itemprovider implementations for the same emf object and want to switch between them
2. you want to extend many itemproviders in the inheritance hierarchy. for example, you need to change personitemprovider and writeritemprovider (which extends personitemprovider) .

although, you don’t need to use di to solve these problems, di would do it for you in a simpler, cleaner way. in this tutorial we will use google guice to achieve this. google guice is cool light weight dependency injection framework. you could inject your dependencies just by writing few lines of code and some annotations. if you don’t like annotations, you could even use guice without them. if you are not familiar with google guice read, getting started .

lets go ahead and “guicify” our earlier example. we start with simple modifications and go on to detailed ones in later steps.

1. firstly, you need to add a dependency to google guice from our org.eclipse.example.library.edit.extension plugin.

google guice is currently not publicly available as an eclipse plugin. there is a bugzilla reques t to add it to the orbit bundle. it is however available with xtext as an eclipse plugin. since i have xtext in my target, i use this in my tutorial. if you don’t have this, you need to add google guice as an external jar to your project.

2. the next step would be to get rid of the “new” statements in the extended itemproviderfactory. this is what binds the itemprovideradapterfactory to a specific itemprovider implementation. we use google guice field injection to inject bookitemprovider .

public class extlibraryitemprovideradapterfactoryextension extends
extlibraryitemprovideradapterfactory {

@inject
protected bookitemprovider bookitemprovider;

@override
public adapter createbookadapter() {
return bookitemprovider;
}

}

3. we now need to create a google guice module to bind the extended itemprovider. so go ahead and create a module as follows:

public class librarymodule extends abstractmodule implements module{
@override
protected void configure() {
bind(bookitemprovider.class).to(bookitemproviderextension.class).in(scopes.singleton);
}

}

4. you could also inject the extended itemprovideradapterfactory into our editor. since we don’t want the editor to have a google guice dependency, we make the following changes to the module and extended itemprovider.


public class librarymodule extends abstractmodule implements module{

private final adapterfactory adapterfactory;

public librarymodule(adapterfactory adapterfactory) {
this.adapterfactory = adapterfactory;
}

@override
protected void configure() {
bind(adapterfactory.class).toinstance(adapterfactory);
bind(bookitemprovider.class).to(bookitemproviderextension.class).in(scopes.singleton);
}

}
public class bookitemproviderextension extends bookitemprovider {

@inject
public bookitemproviderextension(adapterfactory adapterfactory) {
super(adapterfactory);
}

@override
public string gettext(object object) {
return super.gettext(object) + " (" + ((book) object).getpages() + ") ";
}

}


5. now we need to create a guice injector.

public class extlibraryitemprovideradapterfactoryextension extends
extlibraryitemprovideradapterfactory {

public libraryitemprovideradapterfactoryextension() {
guice.createinjector(new librarymodule(this));
}

@inject
protected bookitemprovider bookitemprovider;

@override
public adapter createbookadapter() {
return bookitemprovider;
}

}

6. run it and you get the same result as before.

you could now inject a different implementation of the itemprovider by only creating a new binding in the module file.

that was a rather trivial example. lets take a more significant example where we need to make changes to personitemprovider and writeritemprovider ( which extends personitemprovider) .

the extended library model example, by default displays the lastname of the writer for the attribute name . this comes from the following lines of code in personitemprovider , the superclass of writeritemprovider .

@override
public string gettext(object object)
{
string label = ((person)object).getlastname();
return label == null || label.length() == 0 ?
getstring("_ui_person_type") : //$non-nls-1$
getstring("_ui_person_type") + " " + label; //$non-nls-1$ //$non-nls-2$
}

lets change this to display the firstname instead of lastname .

1. create a new extension itemprovider for person , personitemproviderextension and override the gettext() method as follows

public class personitemproviderextension extends personitemprovider {

@inject
public personitemproviderextension(adapterfactory adapterfactory) {
super(adapterfactory);
}

@override
public string gettext(object object) {
string label = ((person) object).getfirstname();
return label == null || label.length() == 0 ? getstring("_ui_person_type") : //$non-nls-1$
getstring("_ui_person_type") + " " + label; //$non-nls-1$ //$non-nls-2$

}

}

2. inject the extended personitemproviderextension into the itemprovideradapterfactory extension.

public class extlibraryitemprovideradapterfactoryextension extends
extlibraryitemprovideradapterfactory {
...

@inject
protected personitemprovider personitemprovider;

@override
public adapter createpersonadapter() {
return personitemprovider;
}

}

3. update the google guice module

@override
protected void configure() {
bind(adapterfactory.class).toinstance(adapterfactory);
bind(bookitemprovider.class).to(bookitemproviderextension.class).in(scopes.singleton);
bind(personitemprovider.class).to(personitemproviderextension.class).in(scopes.singleton);
}

if you run the code now, you will see that we haven’t got the expected results yet. this is because, the writeritemprovider still extends personitemprovider and not personitemproviderextension , where we integrated the changes. we could go ahead and create a new writeritemproviderextension which extends personitemproviderextension . but in this way we would tie the writeritemproviderextension to personitemproviderextension implementation. we would like to inject each of these extensions without creating any inter-dependency between any of them.

4. we can change inheritance to delegation and use injection again here, that is, inject personitemproviderextension into writeritemproviderextension and delegate the gettext() call.

changing inheritance to delegation however comes at the cost of some java specific issues which i will talk about in a later part of my article.

public class writeritemproviderextension extends writeritemprovider {

@inject
private personitemprovider personitemprovider;

@inject
public writeritemproviderextension(adapterfactory adapterfactory) {
super(adapterfactory);
}

@override
public string gettext(object object) {
return personitemprovider.gettext(object);
}

}

5. don’t forget to update your extlibraryitemprovideradapterfactoryextension and guice module to bind writeritemproviderextension .


public class extlibraryitemprovideradapterfactoryextension extends
extlibraryitemprovideradapterfactory {

...

@inject
protected writeritemprovider writeritemprovider;

@override
public adapter createwriteradapter() {
return writeritemprovider;
}

}
@override
protected void configure() {
bind(adapterfactory.class).toinstance(adapterfactory);
bind(bookitemprovider.class).to(bookitemproviderextension.class).in(scopes.singleton);
bind(personitemprovider.class).to(personitemproviderextension.class).in(scopes.singleton);
bind(writeritemprovider.class).to(writeritemproviderextension.class).in(scopes.singleton);
}

if you run the code now, you will see that firstname is displayed as name attribute of writer , instead of lastname .

i will cover this in the second part of the tutorial. hang on!

from http://nirmalsasidharan.wordpress.com/2011/05/18/extending-emf-itemproviders-using-google-guice-i/

Google Guice Google (verb) Eclipse Modeling Framework

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 5 Steps for Getting Started in Deep Learning
  • Unlock the Power of Terragrunt’s Hierarchy
  • Collaborative Development of New Features With Microservices
  • Keep Your Application Secrets Secret

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: