gef 上下文菜单的应用

  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;

}

 

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值