Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Platform » RCP Can anyone show me a context menu example?
RCP Can anyone show me a context menu example? [message #268491] Tue, 24 August 2004 21:13 Go to next message
dave mobley is currently offline dave mobleyFriend
Messages: 17
Registered: July 2009
Junior Member
Hi all,

So as part of my RCP learning curve I've made it to trying to take my
actions i've created in plugin.xml and now want to add them to a
context menu.

Are there any obvious examples out there or anyone have some code
snippets that might explain it?

thanks,

-Dave
Re: RCP Can anyone show me a context menu example? [message #268545 is a reply to message #268491] Wed, 25 August 2004 04:27 Go to previous messageGo to next message
dave mobley is currently offline dave mobleyFriend
Messages: 17
Registered: July 2009
Junior Member
So after some research, I've figured out a way to do this (hope it is
the best way, but at least it gets the job done)

It is a 5 step process:
1. Create a context menu in the view
2. Create a popupMenu extension in the manifest
3. Create a viewerContribution in the manifest
4. Create an action in the manifest
5. Create an IViewActionDelegate class

This assumes you have already created a view with some sort of jface
viewer in place, hooked it to the perspective, and can show it in your
application.
(if you need to know how to do this you can use the template and
create a plugin with a treeview and modify it for RCP or look at any
of the many wonderful RCP examples..build them/do them..it'll help
with the learning curve)

1. in your view class, go to the createPartControl(...) method and
add a call to hookContextMenu(TreeViewer treeViewer)

(as you can see, I used a treeviewer in my example and so passed the
viewer widget to the 'hookContextMenu()' method)

create the following code:
private void hookContextMenu(TreeViewer treeViewer) {
MenuManager menuMgr = new MenuManager("#PopupMenu",
"contextMenu");
menuMgr.setRemoveAllWhenShown(true);
menuMgr.addMenuListener(new IMenuListener() {
public void menuAboutToShow(IMenuManager manager) {
manager.add(new
GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
}
});
Menu menu =
menuMgr.createContextMenu(treeViewer.getControl());
treeViewer.getControl().setMenu(menu);
getSite().registerContextMenu(menuMgr, treeViewer);
}


2. go to the manifest editor (easy way is to double-click the
'plugin.xml' file in your project)

Choose the 'Extensions' tab (this is on the bottom of the edit window,
not the top)

click 'Add' and choose 'org.eclipse.ui.popupMenus'

make sure the new popupMenu is selected and fill in the fields as
follows:

id: org.eclipse.ui.popupMenu.my.popupMenu
point: org.eclipse.ui.popupMenus

3. now that you have the popupMenu created, now create a
viewerContribution by right-clicking on the popupMenu and choosing
'new' and then 'viewerContribution'

in id field, give it a unique name. i.e.,
org.eclipse.ui.popupMenu.my.popupMenu.myViewContribution

in the targetID this MUST be the name of your view class.

4. Now you can create an action, or menus with separators/group
markers.

right click on the viewerContribution you created and click 'new' and
choose 'action' (you could choose 'menu' for a submenu, or
'visibility' to restrict who gets the context menu).

give the action an id (some long java name probably..the class name is
good), a label (something that will display on the context menu), a
class (you'll do this in step 5, but make sure you make the class
match what you put here), and the menupath (since our example here
only used the group marker MB_ADDITIONS, then you should put
"additions" (MB_ADDITIONS and "additions" mean the same thing..so just
go with it)

5. Create an IViewActionDelegate class that matches the name you put
in the class for step 4. Here is an example:

public class ViewActionDelegate implements
org.eclipse.ui.IViewActionDelegate
{
public void init(org.eclipse.ui.IViewPart view)
{
}

public void run(org.eclipse.jface.action.IAction action)
{
org.eclipse.swt.widgets.Shell shell = new
org.eclipse.swt.widgets.Shell();

org.eclipse.jface.dialogs.MessageDialog.openInformation(
shell,
"Message Dialog Title",
"Action was clicked.");
}

public void selectionChanged(org.eclipse.jface.action.IAction
action, org.eclipse.jface.viewers.ISelection selection)
{
}
}


Now hope this clears things up for those trying to add context menus.

Also a side note on putting submenus on the context menu. If you put
a menu on your viewerContribution (just like adding an action in step
4), add a separator to it (right-click -> new -> separator or group
marker).

To add more actions to this menu, add them to the viewerContribution
just like step 4, but set their menubarpath to
"submenuID/separatorOrGroupMarkerID"

That should do it.

Long-winded, but hope it helps

--Dave

On Tue, 24 Aug 2004 16:13:07 -0500, Dave Mobley <dmobley@us.ibm.com>
wrote:

>Hi all,
>
>So as part of my RCP learning curve I've made it to trying to take my
>actions i've created in plugin.xml and now want to add them to a
>context menu.
>
>Are there any obvious examples out there or anyone have some code
>snippets that might explain it?
>
>thanks,
>
>-Dave
Re: RCP Can anyone show me a context menu example? [message #268868 is a reply to message #268545] Thu, 26 August 2004 16:23 Go to previous message
Eclipse UserFriend
Originally posted by: nick_edgar._no.spam.please_.ca.ibm.com

This will work, although it may be easier to just add actions (subclasses of
org.eclipse.jface.actions.Action) directly to the menu manager in
menuAboutToShow.

You can see how this is done by looking at the code for the resource
navigator (org.eclipse.ui.views.navigator.ResourceNavigator, in the
org.eclipse.ui.ide plug-in). This also makes use of the ActionGroup
mechanism to factor things a bit better.

The original intent is that views would add all their actions
programmatically in this way, but register the menu as you've done to allow
other plugins to add to it.

Nick


"Dave Mobley" <dmobley@us.ibm.com> wrote in message
news:hl3oi0da6o48mrme9bi9utqpcfv3hh57ki@4ax.com...
> So after some research, I've figured out a way to do this (hope it is
> the best way, but at least it gets the job done)
>
> It is a 5 step process:
> 1. Create a context menu in the view
> 2. Create a popupMenu extension in the manifest
> 3. Create a viewerContribution in the manifest
> 4. Create an action in the manifest
> 5. Create an IViewActionDelegate class
>
> This assumes you have already created a view with some sort of jface
> viewer in place, hooked it to the perspective, and can show it in your
> application.
> (if you need to know how to do this you can use the template and
> create a plugin with a treeview and modify it for RCP or look at any
> of the many wonderful RCP examples..build them/do them..it'll help
> with the learning curve)
>
> 1. in your view class, go to the createPartControl(...) method and
> add a call to hookContextMenu(TreeViewer treeViewer)
>
> (as you can see, I used a treeviewer in my example and so passed the
> viewer widget to the 'hookContextMenu()' method)
>
> create the following code:
> private void hookContextMenu(TreeViewer treeViewer) {
> MenuManager menuMgr = new MenuManager("#PopupMenu",
> "contextMenu");
> menuMgr.setRemoveAllWhenShown(true);
> menuMgr.addMenuListener(new IMenuListener() {
> public void menuAboutToShow(IMenuManager manager) {
> manager.add(new
> GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
> }
> });
> Menu menu =
> menuMgr.createContextMenu(treeViewer.getControl());
> treeViewer.getControl().setMenu(menu);
> getSite().registerContextMenu(menuMgr, treeViewer);
> }
>
>
> 2. go to the manifest editor (easy way is to double-click the
> 'plugin.xml' file in your project)
>
> Choose the 'Extensions' tab (this is on the bottom of the edit window,
> not the top)
>
> click 'Add' and choose 'org.eclipse.ui.popupMenus'
>
> make sure the new popupMenu is selected and fill in the fields as
> follows:
>
> id: org.eclipse.ui.popupMenu.my.popupMenu
> point: org.eclipse.ui.popupMenus
>
> 3. now that you have the popupMenu created, now create a
> viewerContribution by right-clicking on the popupMenu and choosing
> 'new' and then 'viewerContribution'
>
> in id field, give it a unique name. i.e.,
> org.eclipse.ui.popupMenu.my.popupMenu.myViewContribution
>
> in the targetID this MUST be the name of your view class.
>
> 4. Now you can create an action, or menus with separators/group
> markers.
>
> right click on the viewerContribution you created and click 'new' and
> choose 'action' (you could choose 'menu' for a submenu, or
> 'visibility' to restrict who gets the context menu).
>
> give the action an id (some long java name probably..the class name is
> good), a label (something that will display on the context menu), a
> class (you'll do this in step 5, but make sure you make the class
> match what you put here), and the menupath (since our example here
> only used the group marker MB_ADDITIONS, then you should put
> "additions" (MB_ADDITIONS and "additions" mean the same thing..so just
> go with it)
>
> 5. Create an IViewActionDelegate class that matches the name you put
> in the class for step 4. Here is an example:
>
> public class ViewActionDelegate implements
> org.eclipse.ui.IViewActionDelegate
> {
> public void init(org.eclipse.ui.IViewPart view)
> {
> }
>
> public void run(org.eclipse.jface.action.IAction action)
> {
> org.eclipse.swt.widgets.Shell shell = new
> org.eclipse.swt.widgets.Shell();
>
> org.eclipse.jface.dialogs.MessageDialog.openInformation(
> shell,
> "Message Dialog Title",
> "Action was clicked.");
> }
>
> public void selectionChanged(org.eclipse.jface.action.IAction
> action, org.eclipse.jface.viewers.ISelection selection)
> {
> }
> }
>
>
> Now hope this clears things up for those trying to add context menus.
>
> Also a side note on putting submenus on the context menu. If you put
> a menu on your viewerContribution (just like adding an action in step
> 4), add a separator to it (right-click -> new -> separator or group
> marker).
>
> To add more actions to this menu, add them to the viewerContribution
> just like step 4, but set their menubarpath to
> "submenuID/separatorOrGroupMarkerID"
>
> That should do it.
>
> Long-winded, but hope it helps
>
> --Dave
>
> On Tue, 24 Aug 2004 16:13:07 -0500, Dave Mobley <dmobley@us.ibm.com>
> wrote:
>
> >Hi all,
> >
> >So as part of my RCP learning curve I've made it to trying to take my
> >actions i've created in plugin.xml and now want to add them to a
> >context menu.
> >
> >Are there any obvious examples out there or anyone have some code
> >snippets that might explain it?
> >
> >thanks,
> >
> >-Dave
>
Previous Topic:Why are my annotations "invisible"?
Next Topic:Main menu fails to change focus: correct or bug?
Goto Forum:
  


Current Time: Tue Oct 01 12:19:12 GMT 2024

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

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

Back to the top