在gef的编辑器视图中添加相关的上下文菜单,经常需要是实现org.eclipse.gef.ContextMenuProvider的类。在gef编辑器中注册。操作类似公共的快捷键设置。在编辑器视图的protected void configureGraphicalViewer()方法中添加相关的配置:
ContextMenuProvider provider = new LogicContextMenuProvider(viewer, getActionRegistry());
viewer.setContextMenu(provider);
getSite().registerContextMenu(
"org.eclipse.gef.examples.logic.editor.contextmenu",
provider, viewer);
viewer.setKeyHandler(new GraphicalViewerKeyHandler(viewer)
.setParent(getCommonKeyHandler()));
关于ContextMenuProvider的实现类LogicContextMenuProvider代码如下:
package com.easyway.plugin.gef.diagram.logicdesigner;
import org.eclipse.gef.EditPartViewer;
import org.eclipse.gef.ui.actions.ActionRegistry;
import org.eclipse.gef.ui.actions.GEFActionConstants;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.ui.IWorkbenchActionConstants;
import com.easyway.plugin.gef.diagram.logicdesigner.actions.IncrementDecrementAction;
/**
* 上下文菜单提供者的使用
* @author longgangbai
*
*/
publicclass LogicContextMenuProvider extends org.eclipse.gef.ContextMenuProvider
{
private ActionRegistry actionRegistry;
public LogicContextMenuProvider(EditPartViewer viewer, ActionRegistry registry) {
super(viewer);
setActionRegistry(registry);
}
/**
* 创建上下文菜单
*/
publicvoid buildContextMenu(IMenuManager manager) {
GEFActionConstants.addStandardActionGroups(manager);
IAction action;
action = getActionRegistry().getAction(GEFActionConstants.UNDO);
manager.appendToGroup(GEFActionConstants.GROUP_UNDO, action);
action = getActionRegistry().getAction(GEFActionConstants.REDO);
manager.appendToGroup(GEFActionConstants.GROUP_UNDO, action);
action = getActionRegistry().getAction(IWorkbenchActionConstants.PASTE);
if (action.isEnabled())
manager.appendToGroup(GEFActionConstants.GROUP_EDIT, action);
action = getActionRegistry().getAction(IWorkbenchActionConstants.DELETE);
if (action.isEnabled())
manager.appendToGroup(GEFActionConstants.GROUP_EDIT, action);
action = getActionRegistry().getAction(GEFActionConstants.DIRECT_EDIT);
if (action.isEnabled())
manager.appendToGroup(GEFActionConstants.GROUP_EDIT, action);
action = getActionRegistry().getAction(IncrementDecrementAction.INCREMENT);
if (action.isEnabled())
manager.appendToGroup(GEFActionConstants.GROUP_REST, action);
action = getActionRegistry().getAction(IncrementDecrementAction.DECREMENT);
if (action.isEnabled())
manager.appendToGroup(GEFActionConstants.GROUP_REST, action);
// Alignment Actions
MenuManager submenu = new MenuManager(LogicMessages.AlignmentAction_AlignSubmenu_ActionLabelText);
action = getActionRegistry().getAction(GEFActionConstants.ALIGN_LEFT);
if (action.isEnabled())
submenu.add(action);
action = getActionRegistry().getAction(GEFActionConstants.ALIGN_CENTER);
if (action.isEnabled())
submenu.add(action);
action = getActionRegistry().getAction(GEFActionConstants.ALIGN_RIGHT);
if (action.isEnabled())
submenu.add(action);
submenu.add(new Separator());
action = getActionRegistry().getAction(GEFActionConstants.ALIGN_TOP);
if (action.isEnabled())
submenu.add(action);
action = getActionRegistry().getAction(GEFActionConstants.ALIGN_MIDDLE);
if (action.isEnabled())
submenu.add(action);
action = getActionRegistry().getAction(GEFActionConstants.ALIGN_BOTTOM);
if (action.isEnabled())
submenu.add(action);
if (!submenu.isEmpty())
manager.appendToGroup(GEFActionConstants.GROUP_REST, submenu);
action = getActionRegistry().getAction(IWorkbenchActionConstants.SAVE);
manager.appendToGroup(GEFActionConstants.GROUP_SAVE, action);
}
private ActionRegistry getActionRegistry() {
returnactionRegistry;
}
privatevoid setActionRegistry(ActionRegistry registry) {
actionRegistry = registry;
}
}