Programmatically access element IDs [message #1723625] |
Tue, 16 February 2016 20:50 |
|
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 = "com.foo.perspective.foo";
String BAR = "com.foo.perspective.bar";
}
public interface Window {
String FOO = "com.foo.trimmedwindow.foo";
String BAR = "com.foo.trimmedwindow.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
|
|
|
|
Powered by
FUDForum. Page generated in 0.02905 seconds