|
Re: Popup / Context Menu on a View without a Viewer [message #532370 is a reply to message #532303] |
Sun, 09 May 2010 04:49 |
|
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
|
|
|
|
Powered by
FUDForum. Page generated in 0.02828 seconds