Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Platform » adding a context menu to an editor
adding a context menu to an editor [message #187957] Sun, 01 February 2004 13:09 Go to next message
Eclipse UserFriend
Originally posted by: john.heaton.net

I'm having trouble finding where/how to add a context menu to an EditorPart.
My editor is not a texteditor. I need to add specific menu items
that pertain to my editor. I can't seem to find any documentation
that discusses this. To be specific, I'm looking to add things to
the right-click pop-up menu, not to any of the menu bars.

Any help would be appreciated,

John
Re: adding a context menu to an editor [message #188103 is a reply to message #187957] Mon, 02 February 2004 08:59 Go to previous message
Jean Couillaud is currently offline Jean CouillaudFriend
Messages: 184
Registered: July 2009
Senior Member
John M. Heaton wrote:
> I'm having trouble finding where/how to add a context menu to an EditorPart.
> My editor is not a texteditor. I need to add specific menu items
> that pertain to my editor. I can't seem to find any documentation
> that discusses this. To be specific, I'm looking to add things to
> the right-click pop-up menu, not to any of the menu bars.
>
> Any help would be appreciated,

Hello,

I've add the same problem a few weeks ago.
In fact, adding a context menu is made the same way in a view or in an
editor.
First thing to know is that the context menu has to be attached to a
widget and not to the editor part, that may be why you did not succeed.
Second, I thought there was a way to create a context menu without
creating a listener, it proved to be impossible to me.

Here is how I implemented it:
(My editor is the class BeanListEditor

>>> createPartControl(Composite parent)
....
// Create the menu manager
MenuManager menuManager = new MenuManager();
menuManager.setRemoveAllWhenShown(true);
// adds the listener (called upon right click in the editor part
menuManager.addMenuListener(new IMenuListener() {
public void menuAboutToShow(IMenuManager manager) {
// the method responsible for creating the content of the context menu
BeanListEditor.this.fillContextMenu(manager);
}
});

// create a context menu using the definition of the menu above using
this widget context
Menu contextMenu = menuManager.createContextMenu(tree);
// attach it to the tree
tree.setMenu(contextMenu);
// publish it (so that it can be used by other plugins (this is not
mandatory, but it's best practice, it will allow other plugins to add
items to your context menu by letting the workbench know the existence
of this context menu)
site.registerContextMenu(menuManager, treeViewer);

....
<<< createPartControl

now, I have to mention before continuing that this.treeViewer is the
viewer contained into my editor part (thus treeViewer.getTree() returns
the main widget of the editor part, in fact, the only one)

>>> fillContextMenu(IMenuManager manager)
protected void fillContextMenu(IMenuManager manager) {

// Adds a separator (a separator determines a group of items in the
menu (it is especially useful if you want to allow some other plugins to
use your editor and add items to this context menu
manager.add(new Separator("First"));

// Create an action
OpenBeanAction action = new OpenBeanAction(treeViewer.getTree(),
getSite());
action.setText("Open all EJB classes");
action.setSelected(OpenBeanAction.OPEN_ALL);
// add the action to the menu in the "First" group defined by the
"First" separator
manager.appendToGroup("First", action);

// Adds an other separator
manager.add(new Separator("Second"));

// adds a GroupMarker. This group marker will allow
plugins using the extensions of your plugin to add items to your context
menu.
GroupMarker marker = new GroupMarker(IWorkbenchActionConstants.M
B_ADDITIONS);
manager.add(marker);
}
<<< fillContextMenu

That's it, I hope this help, if you encounter some problems with this
"tutorial", just let me know, if it's useful to a lot of people, it
could be a good idea to make a tutorial available on eclipse.org, but in
either case, it would not be a good idea to let me do this work since
english is not my native language and I have some difficulties when it
comes to writing articles ;)

Best regards,

Jean

> John
>
Previous Topic:Navigator customization
Next Topic:Refresh custom views after adding a new project
Goto Forum:
  


Current Time: Tue Sep 17 09:16:19 GMT 2024

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

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

Back to the top