Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » DynamicMenuContribution and IEclipseContext
DynamicMenuContribution and IEclipseContext [message #1674589] Sat, 14 March 2015 20:32 Go to next message
Stefan Häusler is currently offline Stefan HäuslerFriend
Messages: 23
Registered: May 2013
Junior Member
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 #1674629 is a reply to message #1674589] Sat, 14 March 2015 20:51 Go to previous messageGo to next message
Stefan Häusler is currently offline Stefan HäuslerFriend
Messages: 23
Registered: May 2013
Junior Member
Update:

I tried to put both the @AboutToShow and @Execute method into the same class and store a reference to the eclipse context to "force" it to be the same in both methods. Looks ok now.

But is this intended? Is it the way one should implement it?
Re: DynamicMenuContribution and IEclipseContext [message #1675702 is a reply to message #1674629] Sun, 15 March 2015 06:42 Go to previous message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2902
Registered: July 2012
Senior Member
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.
Previous Topic:Expensive updates of ToolItems
Next Topic:PDE Build doest not find org.eclipse.emf.edit
Goto Forum:
  


Current Time: Tue Mar 19 02:18:23 GMT 2024

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

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

Back to the top