Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Rich Client Platform (RCP) » Popup menu, objectContribution
Popup menu, objectContribution [message #816172] Thu, 08 March 2012 14:06 Go to next message
ModelGeek Mising name is currently offline ModelGeek Mising nameFriend
Messages: 550
Registered: June 2011
Senior Member
Hi,

i am trying to attach popup menu with some object... so in my application GUI, where ever any control backed by specific object is right clicked then popup menu should appear. For example, i have view containg table control which contains hellomenumodel.Test objects.
hellomenutest.Action2 is also added to project but when i right click the table objects then popup menu does not appear...

<extension
point="org.eclipse.ui.popupMenus">
<objectContribution
id="HelloMenuTest.objectContribution1"
objectClass="hellomenumodel.Test">
<action
class="hellomenutest.Action2"
enablesFor="1"
id="HelloMenuTest.action2"
label="test action"
menubarPath="HelloMenuTest.menu2">
</action>
<menu
id="HelloMenuTest.menu2"
label="test menu">
</menu>
</objectContribution>
</extension>

is there anything missing?

cheers,
Re: Popup menu, objectContribution [message #816194 is a reply to message #816172] Thu, 08 March 2012 14:39 Go to previous messageGo to next message
Thorsten Schlathölter is currently offline Thorsten SchlathölterFriend
Messages: 312
Registered: February 2012
Location: Düsseldorf
Senior Member
In order to have your context menu be extended by workbench contributions you need to register your viewer with the part site of your view. Have you done that?

So something like this should be called within your view:
private void createContextMenu(TableViewer viewer) {
        // Create menu manager.
     MenuManager menuMgr = new MenuManager();
        menuMgr.setRemoveAllWhenShown(true);

        // Optionally register a listener to fill the menu
        menuMgr.addMenuListener(new IMenuListener() {
                public void menuAboutToShow(IMenuManager mgr) {
                        fillContextMenu(mgr);
                }
        });
        
        // Create menu.
     Menu menu = menuMgr.createContextMenu(viewer.getControl());
        viewer.getControl().setMenu(menu);
        
        // Here you register the menu for menu extensions.
     getSite().registerContextMenu(menuMgr, viewer);



You may also want to have a look at the org.eclipse.ui.menus extension point which is a newer strategy that separates the location of contributions from their visibility and enablement state. Its a more generic approach than using old style org.eclipse.ui.popupMenus
Re: Popup menu, objectContribution [message #816226 is a reply to message #816194] Thu, 08 March 2012 15:18 Go to previous messageGo to next message
ModelGeek Mising name is currently offline ModelGeek Mising nameFriend
Messages: 550
Registered: June 2011
Senior Member
Thanks alot.... it worked..

As i am looking to bind popup menu with a specific object so i want to know can i achieve the same phenomena with org.eclipse.ui.menus extension point?

Cheers,
Re: Popup menu, objectContribution [message #816259 is a reply to message #816226] Thu, 08 March 2012 16:13 Go to previous messageGo to next message
Thorsten Schlathölter is currently offline Thorsten SchlathölterFriend
Messages: 312
Registered: February 2012
Location: Düsseldorf
Senior Member
Yes you can. There a several tutorials out there. You may want to look into one of them to get a detailed understanding. But basically you use the visible when tag and build up an expression similar to the following one. This expression uses adapt but in your case an instanceof would do just fine. The org.eclipse.ui.menu extension decouples the menu contribution from the actual action that handles it. This is done using commands. So in order to get this running you need to declare a command and a handler for this command. In the beginning it is a little bit more tricky but in the end it is more powerfull.

 <command
                  commandId="editMarketingOperations"
                  icon="icons/marketingOperation/marketingOperation_edit.gif"
                  id="editMarketingOperations"
                  label="%editMarketingOperations">
               <visibleWhen>
                  <with
                        variable="activeMenuSelection">
                     <iterate
                           ifEmpty="false"
                           operator="and">
                        <adapt
                              type="common.popup.IMarketingOperation">
                           <and>
                              <with
                                    variable="activeMenuSelection">
                                 <count
                                       value="1">
                                 </count>
                              </with>
                           </and>
                        </adapt>
                     </iterate>
                  </with>
               </visibleWhen>
            </command>
Re: Popup menu, objectContribution [message #816795 is a reply to message #816259] Fri, 09 March 2012 09:05 Go to previous messageGo to next message
ModelGeek Mising name is currently offline ModelGeek Mising nameFriend
Messages: 550
Registered: June 2011
Senior Member
Thanka alot... its been very helpful.
I am looking for book/tutorials/resources which covers all those things... Can you refer me some... thanks alot

One more question, i have tried commands/handlers... they work quite well. But all the handlers are used with default constructors, i would like to use constructors with parameters for handlers... Can i do it?

thanks!

cheers,
Re: Popup menu, objectContribution [message #816800 is a reply to message #816795] Fri, 09 March 2012 09:15 Go to previous messageGo to next message
Daniel Krügler is currently offline Daniel KrüglerFriend
Messages: 853
Registered: July 2009
Senior Member
On 2012-03-09 10:05, ModelGeek Mising name wrote:
> One more question, i have tried commands/handlers... they work quite
> well. But all the handlers are used with default constructors, i would
> like to use constructors with parameters for handlers... Can i do it?

Sure, but these handlers presumably need programmatic creation because
you have to provide the parameters. One example from Eclipse is the
CollapseAllHandler.

HTH & Greetings from Bremen,

Daniel Krügler
Re: Popup menu, objectContribution [message #816840 is a reply to message #816795] Fri, 09 March 2012 10:22 Go to previous messageGo to next message
Thorsten Schlathölter is currently offline Thorsten SchlathölterFriend
Messages: 312
Registered: February 2012
Location: Düsseldorf
Senior Member
Yes u can. But as Daniel states you have to register the handler programatically via the Platform IHandlerService:

see here:
wiki.eclipse.org/Platform_Command_Framework

IHandlerService handlerService = (IHandlerService) getSite()
    .getService(IHandlerService.class);
IHandler handler = new AbstractHandler() {
  public Object execute(ExecutionEvent event)
          throws ExecutionException {
    System.out.println("Eat that Taco");
    return null;
  }
};
handlerService
    .activateHandler("z.ex.view.keybindings.eatTaco", handler);
Re: Popup menu, objectContribution [message #816888 is a reply to message #816259] Fri, 09 March 2012 11:44 Go to previous messageGo to next message
ModelGeek Mising name is currently offline ModelGeek Mising nameFriend
Messages: 550
Registered: June 2011
Senior Member
Hi,

i have been trying visiblewhen but eventhough i select the object in table viewer but command never becomes visible. Any idea?

<menuContribution
locationURI="toolbar:org.eclipse.ui.main.toolbar">
<toolbar
id="HelloMenuTest.toolbar3"
label="maintoolbar">
<command
commandId="HelloMenuTest.HelloCommand"
icon="icons/alt_window_16.gif"
label="say hello"
style="push">
</command>
<command
commandId="HelloMenuTest.EnableCommand"
icon="icons/alt_window_16.gif"
label="enable"
style="push">
<visibleWhen
checkEnabled="true">
<with
variable="selection">
<instanceof
value="hellomenutest.Test">
</instanceof>
</with>
</visibleWhen>
</command>
</toolbar>
</menuContribution>

cheers,
Re: Popup menu, objectContribution [message #816918 is a reply to message #816888] Fri, 09 March 2012 12:22 Go to previous messageGo to next message
Thorsten Schlathölter is currently offline Thorsten SchlathölterFriend
Messages: 312
Registered: February 2012
Location: Düsseldorf
Senior Member
First of all please put your source code in a Code Block (Formatting Tools right Button). That makes it better readable.

And to answer your question let me start with a question myself:Is the above definition all you have? I am missing the handler definition. The command is just an abstraction. It doesn't do anything for you. You need a handler for that. A handler may or may not become active for a given command under certain circumstances. That is the power of this approach. You can have different handlers active in different contexts. Anyway, an UI Element only becomes visible if the related command has an active handler. Like I said: In the beginning it is a little tricky. My advice is to add a defaultHandler to your command.
Furthermore the "selection" source you are refering to, may not contain what you think it contains. I have not used that source, yet, but I would expect it to contain a variable of type ISelection. That is not your selected element. The element you are refering to is inside the selection. In your case you have an IStructuredSelection which is iterable. The iterables are the elements you are looking for. Check the command of my first post and replace the adapt expression by an instanceof expression. In general that should do it for you.
Re: Popup menu, objectContribution [message #816938 is a reply to message #816918] Fri, 09 March 2012 12:46 Go to previous message
ModelGeek Mising name is currently offline ModelGeek Mising nameFriend
Messages: 550
Registered: June 2011
Senior Member
Thanks!

I got it working now
Previous Topic:RCP application on network drive (Slow start)
Next Topic:Why "Switch workspace" fail?
Goto Forum:
  


Current Time: Thu Mar 28 14:26:24 GMT 2024

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

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

Back to the top