Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » Access Menu at runtime(How to search Context for a MMenuItem)
Access Menu at runtime [message #1062477] Fri, 07 June 2013 18:25 Go to next message
Don Smyth is currently offline Don Smyth
Messages: 27
Registered: April 2013
Junior Member
I would like to disable/enable a HandledMenuItem based on user interaction.

Using the EModelService I can find MParts, MTrimmedWindows etc, but cannot find a MMenu (see Eclipse Bug 383403 EModelService.findElement() does not find all elements).

There is a reference in the Bug 383403 Comment 25 to "using the EModel service to get my main window, and using that window as the context in find(...) to get the menu item".

I am having trouble searching the context for the menu item via the menu item id.

private void findObjectById(MApplication application,
EModelService service) {
List<MTrimmedWindow> findElements = service.findElements(application,
"com.test.ui.mainwindow",MTrimmedWindow.class, null);

//This window is found OK
System.out.println("Found part by id: " + findElements.size());
if (findElements.size() == 1){
MTrimmedWindow window = findElements.get(0);
IEclipseContext windowContext = window.getContext();

//doesn't contain key
boolean hasKey = windowContext.containsKey("com.test.ui.filesubmenu");
if (hasKey){
System.out.println("Key found");
}

//not found in the context
Object menuItem = windowContext.get("com.test.ui.filesubmenu");
if (menuItem !=null){
System.out.println("Found in context: "+ menuItem.toString());
}
}
}

Has anyone found a way to access menu items at runtime?
Re: Access Menu at runtime [message #1062651 is a reply to message #1062477] Mon, 10 June 2013 05:06 Go to previous messageGo to next message
Sopot Cela is currently offline Sopot Cela
Messages: 589
Registered: December 2010
Senior Member

Use MWindow.getMainMenu as the search root in the EModelService.find method.
Re: Access Menu at runtime [message #1063169 is a reply to message #1062477] Wed, 12 June 2013 10:14 Go to previous messageGo to next message
Paul Webster is currently offline Paul Webster
Messages: 6850
Registered: July 2009
Location: Ottawa
Senior Member

On 06/07/2013 06:25 PM, Don Smyth wrote:
> I would like to disable/enable a HandledMenuItem based on user interaction.

HandledMenuItems call their handler @CanExecute method repeatedly (on
every SWT.Show event, for instance). If you want the menu item
disabled, your handler should be disabled at that time.


Later,
PW


--
Paul Webster
http://wiki.eclipse.org/Platform_Command_Framework
http://wiki.eclipse.org/Command_Core_Expressions
http://wiki.eclipse.org/Platform_Expression_Framework
http://wiki.eclipse.org/Menu_Contributions
http://wiki.eclipse.org/Menus_Extension_Mapping
http://help.eclipse.org/helios/index.jsp?topic=/org.eclipse.platform.doc.isv/guide/workbench.htm


Re: Access Menu at runtime [message #1064052 is a reply to message #1063169] Mon, 17 June 2013 08:42 Go to previous messageGo to next message
Don Smyth is currently offline Don Smyth
Messages: 27
Registered: April 2013
Junior Member
Thanks Guys,

I can access the menu and all it's childern now. Will test the @CanExecute functionality next.

@Execute
public void execute(MWindow window, EModelService modelService)
{
findDoSomethingItem(window, modelService);
...
}

private void findDoSomethingItem(MWindow window,
EModelService service) {

MUIElement findElement = service.find("menu:com.test.filesubmenu", window.getMainMenu());
MUIElement doSomethingElement = service.find("com.test.handledmenuitem.dosomething", window.getMainMenu());
logger.debug("Found submenu " + findElement);
logger.debug("Found do something element " + doSomethingElement);
}

@CanExecute
private void testCanExecute(){
//set my doSomethingElement enabled to false here as well?
}
Re: Access Menu at runtime [message #1064064 is a reply to message #1064052] Mon, 17 June 2013 09:28 Go to previous messageGo to next message
Paul Webster is currently offline Paul Webster
Messages: 6850
Registered: July 2009
Location: Ottawa
Senior Member

On 06/17/2013 08:42 AM, Don Smyth wrote:
> Thanks Guys,
> @CanExecute
> private void testCanExecute(){
> //set my doSomethingElement enabled to false here as well?
> }


Sorry, what I meant was that:

1) com.test.handledmenuitem.dosomething points to a command

2) that command must have a handler somewhere

3) that command's handler has a @CanExecute, and it should return false
when it determines it cannot run

Later,
PW

--
Paul Webster
http://wiki.eclipse.org/Platform_Command_Framework
http://wiki.eclipse.org/Command_Core_Expressions
http://wiki.eclipse.org/Platform_Expression_Framework
http://wiki.eclipse.org/Menu_Contributions
http://wiki.eclipse.org/Menus_Extension_Mapping
http://help.eclipse.org/helios/index.jsp?topic=/org.eclipse.platform.doc.isv/guide/workbench.htm


Re: Access Menu at runtime [message #1064160 is a reply to message #1064064] Mon, 17 June 2013 17:20 Go to previous message
Don Smyth is currently offline Don Smyth
Messages: 27
Registered: April 2013
Junior Member
Now all working,

Thanks

Previous Topic:Show part title when added directly to sash container
Next Topic:Invalid Thread Access on EclipseContext.dispose()
Goto Forum:
  


Current Time: Fri Apr 25 02:47:38 EDT 2014

Powered by FUDForum. Page generated in 0.07535 seconds