Skip to main content

Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » Programmatically access element IDs
Programmatically access element IDs [message #1723625] Tue, 16 February 2016 20:50 Go to next message
Colin Sharples is currently offline Colin SharplesFriend
Messages: 90
Registered: July 2009
Location: Wellington, New Zealand

There are several times when you need to programmatically access a model element, which you typically find from EModelService or EPartService by referencing the element ID. Examples include switching between perspectives, opening or bringing to the top a different window, setting focus on a part etc.

I can't see any way at present to avoid hardcoding the element IDs that you pass to the services to retrieve a model element. This is pretty fragile, as changing the element ID in the application model will break code that still has the old ID hardcoded.

One way to reduce the fragility is to pull all hardcoded element IDs out into a common class or interface, but this still requires a manual step to keep the interface in synch with the model.

It would be great if the model editor could generate this interface when you save the model, so that you would be able to refer to a constant from your code. I'm thinking something along the lines of the R class in Android, with the elements grouped by type, something like this:

public interface Resources {

public interface Perspective {
String FOO = "";
String BAR = "";

public interface Window {
String FOO = "";
String BAR = "";

The constant names would be taken from the label or name of the element, and the value would be the ID. This would make code more readable, as an ID would be accessed by something like:
MPart fooWindow = ePartService.find(Resources.Window.FOO);

Using the name or label would also make the code independent of the actual ID. Ideally, the model would do a refactor on the generated interface whenever the name or label was changed as well.

Are there any plans for such a feature, or should I put in an RFE?

Colin Sharples
CTG Games Ltd
Wellington, New Zealand
Re: Programmatically access element IDs [message #1723775 is a reply to message #1723625] Wed, 17 February 2016 19:33 Go to previous message
Peter Szanto is currently offline Peter SzantoFriend
Messages: 4
Registered: December 2015
Junior Member

I think the closest thing to this right now is if you open your Application.e4xmi in Eclipse and select the root item (Application) and from the popup menu select "Export Element Ids".
It brings up a Dialog with all the ids in Application.e4xmi and if you press OK, then it generates into your source folder a java file with name "" in a package with a name of your product.
(Right now in my Eclipse Neon M5 I was not able to press OK on this Dialog so I checked the function in Eclipse Neon M1.)

Previous Topic:Create a toolbar programmatically and attach to an ID...
Next Topic:Build Product Problem
Goto Forum:

Current Time: Wed Jun 29 10:37:29 GMT 2022

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

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

Back to the top