DynamicMenuContribution and IEclipseContext [message #1674589] |
Sat, 14 March 2015 16:32  |
Eclipse User |
|
|
|
Hello all,
i currently have some problems within the following scenario:
I have a file menu entry that is populated with dynamic menu contribution, each of the created direct menu items has a handler that sets a global object into the eclipse context (this context objects does influence the whole UI). Furthermore, the menu item representing the currently selected context object shall be selected. Therefore, i also ask the eclipse object for the current value.
Interestingly, most of the time, the selection is not detected correctly. Also sometimes the change of the context object has no influence to the views listening to the named object.
When i went into debugging, i observed the following facts:
- The injected IEclipseContext within the @AboutToShow method always points to the TrimmedWindow context. Looks ok to me, as it is in the context of the MainMenu
- The injected IEclipseContext wthin the handlers @Execute method always points to the Context of a part. Only that part reacts to a change within the named object. This also leads to the conclusion (to my understanding) why a call in the other method always return null, when i want to retrieve the named object value again (as it is the trimmed window context).
Am i doing something wrong here?
Dynamic Menu Contribution
@AboutToShow
public void createEntries(List<MMenuElement> items) {
Object currentMaster = context.get(Constant.SOME_GLOBAL_SELECTION);
List<SomeObject> allObjects= service.getAllObjects();
for (SomeObject obj: allObjects) {
MDirectMenuItem dynamicItem = MMenuFactory.INSTANCE.createDirectMenuItem();
dynamicItem.setLabel(obj.getName());
dynamicItem.setType(ItemType.CHECK);
dynamicItem.getTransientData().put(OBJECTKEY, obj);
dynamicItem.setContributorURI(CONTRIBUTOR);
dynamicItem.setContributionURI(HANDLER_CLASS);
//Set the dynamic item as selected, if it represents the architecture that was already
//selected previously
dynamicItem.setSelected(currentMaster != null && obj.equals(currentMaster));
items.add(dynamicItem);
}
}
The handler looks as follows
@Execute
public void elementSelected(MDirectMenuItem selectedItem, IEclipseContext context) {
Object object = selectedItem.getTransientData().get(OBJECTKEY);
context.set(CONTEXTOBJECT, object);
}
|
|
|
|
Re: DynamicMenuContribution and IEclipseContext [message #1675702 is a reply to message #1674629] |
Sun, 15 March 2015 02:42  |
Eclipse User |
|
|
|
You always get the instance injected that is found in the current active context or if there is no instance in the current context you get an instance that is found in the context hierarchy. That's why you get the application context for the menu contribution (field injection performed at creation time) and the part context on handler execution.
So the behaviour you explain matches the code you posted.
If you need the application context you should use a way to get it. For example get MApplication injected and call getContext() on it.
|
|
|
Powered by
FUDForum. Page generated in 0.05251 seconds