Skip to main content


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 22:25 Go to next message
Don Smyth is currently offline Don SmythFriend
Messages: 35
Registered: April 2013
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 09:06 Go to previous messageGo to next message
Eclipse UserFriend
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 14:14 Go to previous messageGo to next message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
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 12:42 Go to previous messageGo to next message
Don Smyth is currently offline Don SmythFriend
Messages: 35
Registered: April 2013
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 13:28 Go to previous messageGo to next message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
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 21:20 Go to previous message
Don Smyth is currently offline Don SmythFriend
Messages: 35
Registered: April 2013
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: Thu Mar 28 11:38:28 GMT 2024

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

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

Back to the top