Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Rich Client Platform (RCP) » Popup / Context Menu on a View without a Viewer
Popup / Context Menu on a View without a Viewer [message #532303] Fri, 07 May 2010 20:53 Go to next message
Anonymous Coward is currently offline Anonymous CowardFriend
Messages: 6
Registered: May 2010
Junior Member
Hi, I've been looking around to find how to add a popup / context menu to a View, and all the documentation seems to require a "viewer" to be able to add a popup menu. In the specific circumstance I'm attempting to solve, I don't use a Viewer in this view (The view just displays an image directly on the Canvas). Is there a way to add a popup menu to this?

Here's the code that I've been seeing (roughly):
MenuManager menuMgr = new MenuManager();
menuMgr.setRemoveAllWhenShown(true);
menuMgr.addMenuListener(new IMenuListener() {
    public void menuAboutToShow(IMenuManager mgr) {
        fillContextMenu(mgr);
    }
});
                
// Create menu.
Menu menu = 
    menuMgr.createContextMenu(viewer.getControl());
viewer.getControl().setMenu(menu);

// Register menu for extension.
getSite().registerContextMenu(menuMgr, viewer);


Now, I've attempted to do this in the 'createPartControl(Composite)' method, but have had no luck in getting it to work. Here's my approach:
public void createPartControl(Composite pParent) {

        MenuManager manager = new MenuManager();
        manager.setRemoveAllWhenShown(true);
        manager.addMenuListener(new IMenuListener() {
            public void menuAboutToShow(IMenuManager mgr) {
                fillContextMenu(mgr);
            }
        });
 
        Menu menu = manager.createContextMenu(pParent);
        pParent.setMenu(menu);

       getSite().registerContextMenu(manager, this);
}

All help is greatly appreciated.
R
Re: Popup / Context Menu on a View without a Viewer [message #532370 is a reply to message #532303] Sun, 09 May 2010 04:49 Go to previous messageGo to next message
Craig Foote is currently offline Craig FooteFriend
Messages: 217
Registered: July 2009
Senior Member
I don't think you need a Viewer but there are a couple steps here:

1. create the menu on a Control. I guess pParent will work - I've never
tried attaching a menu to a Composite.

MenuManager manager = new MenuManager();
Menu menu = manager.createContextMenu(pParent);

/*
* I'm not sure why both this and the previous line are
* required - they seem redundant to me (anyone?)
*/
pParent.setMenu(menu);

2. register an ISelectionProvider implementation with which to garner
menu contributions. As context menus are commonly attached to Viewers and
they are all SelectionProviders, they are often used in that registration:

getSite().registerContextMenu(manager, viewer);

I suspect your ViewPart, "this", is not a SelectionProvider so it
couldn't be used in that registration. That should have been a
compilation error though so I could be wrong here somewhere :(

You can instantiate an ISelectionProvider and use it in the registration.
Then, when #getSelection is called on it (upon right-click), provide
whatever object you want in a StructuredSelection, presumably something
displayed in your view. You should get all menu contributions from all
plugins (started or not) appropriate for your selection object.

Also note that I don't think you need the lines:

manager.setRemoveAllWhenShown(true);
manager.addMenuListener(new IMenuListener() {
public void menuAboutToShow(IMenuManager mgr) {
fillContextMenu(mgr);
}
});

.... if you use Commands/Handlers/Menus extensions rather than the older
IAction API. Those wizards really need updating Paul ;)

Hope that helps,
Craig Foote


On Fri, 07 May 2010 16:53:57 -0400, Anonymous Coward wrote:

> Hi, I've been looking around to find how to add a popup / context menu
> to a View, and all the documentation seems to require a "viewer" to be
> able to add a popup menu. In the specific circumstance I'm attempting
> to solve, I don't use a Viewer in this view (The view just displays an
> image directly on the Canvas). Is there a way to add a popup menu to
> this?
>
> Here's the code that I've been seeing (roughly):
>
> MenuManager menuMgr = new MenuManager();
> menuMgr.setRemoveAllWhenShown(true);
> menuMgr.addMenuListener(new IMenuListener() {
> public void menuAboutToShow(IMenuManager mgr) {
> fillContextMenu(mgr);
> }
> });
>
> // Create menu.
> Menu menu =
> menuMgr.createContextMenu(viewer.getControl());
> viewer.getControl().setMenu(menu);
>
> // Register menu for extension.
> getSite().registerContextMenu(menuMgr, viewer);
>
>
> Now, I've attempted to do this in the 'createPartControl(Composite)'
> method, but have had no luck in getting it to work. Here's my approach:
>
> public void createPartControl(Composite pParent) {
>
> MenuManager manager = new MenuManager();
> manager.setRemoveAllWhenShown(true);
> manager.addMenuListener(new IMenuListener() {
> public void menuAboutToShow(IMenuManager mgr) {
> fillContextMenu(mgr);
> }
> });
>
> Menu menu = manager.createContextMenu(pParent);
> pParent.setMenu(menu);
>
> getSite().registerContextMenu(manager, this);
> }
>
> All help is greatly appreciated.
> R
Re: Popup / Context Menu on a View without a Viewer [message #532557 is a reply to message #532303] Mon, 10 May 2010 12:55 Go to previous message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

Craig's given you some steps.

If you just want a context menu, you don't need to register it with your
site.

MenuManager menuMgr = new MenuManager();
menuMgr.setRemoveAllWhenShown(true);
menuMgr.addMenuListener(new IMenuListener() {
public void menuAboutToShow(IMenuManager mgr) {
fillContextMenu(mgr);
}
});
Menu menu = menuMgr.createContextMenu(myControl);
myControl.setMenu(menu);

You are creating the context menu on the control that you are going to
right-click on. I think your example will only work if you don't put
anything in your composite :-) although I'm not 100% sure.

You only need to register your context menu if you want
org.eclipse.ui.menus (commands) and org.eclipse.ui.popupMenus (actions)
to be able to contribute to it.

PW


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


Previous Topic:Jboss
Next Topic:How to close a empty editor area automatically?
Goto Forum:
  


Current Time: Fri Apr 26 22:12:41 GMT 2024

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

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

Back to the top