Eclipse Plug-in 视图菜单(四)

首先申明下,本文为笔者学习《Eclipse插件开发学习笔记》的笔记,并加入笔者自己的理解和归纳总结。

1. 视图菜单

扩展点【org.eclipse.ui.viewActions】

在【Extensions】中添加【org.eclipse.ui.viewActions】。
在【org.eclipse.ui.viewActions】中添加【viewContribution】,【targetID】指定对应的视图DemoViewPart
在【viewContribution】中添加相应的【menu】和【action】。

<extension
      point="org.eclipse.ui.viewActions">
    <viewContribution
          id="com.plugin.blog.demo.viewContribution.DemoViewPart"
          targetID="com.plugin.blog.demo.view.DemoViewPart">
        <menu
              id="com.plugin.blog.demo.DemoViewPart.menu"
              label="下拉菜单"
              path="additions">
            <separator
                  name="DemoViewPart.menu.separator">
            </separator>
        </menu>
        <action
              class="com.plugin.blog.demo.actions.UserInfoAction"
              icon="icons/icon_selection.png"
              id="com.plugin.blog.demo.actions.UserInfoAction"
              label="用户信息"
              menubarPath="com.plugin.blog.demo.DemoViewPart.menu/DemoViewPart.menu.separator"
              style="push"
              toolbarPath="additions">
        </action>
    </viewContribution>
</extension>

【action】配置
在【class】中选择对应的Action,必须继承IViewActionDelegate类。
在【menubarPath】和【toolbarPath】中分别定义了下拉菜单和工具栏菜单的路径。
UserSelectionAction类,显示被选择列表信息

public class UserInfoAction implements IViewActionDelegate {
    private IViewPart mView;
    private User mUser;

    @Override
    public void run(IAction action) {
        if (mView != null && mUser != null) {
            MessageBox mb = new MessageBox(mView.getSite().getShell());
            mb.setText("消息");
            mb.setMessage("用户" + mUser.getName() + "被选择!");
            mb.open();
        }
    }

    @Override
    public void selectionChanged(IAction action, ISelection selection) {
        if (selection != null) {
            IStructuredSelection ss = (IStructuredSelection) selection;
            if (!ss.isEmpty()) {
                mUser = (User) ss.getFirstElement();
                action.setEnabled(true);
            } else {
                action.setEnabled(false);
            }
        } else {
            action.setEnabled(false);
        }
    }

    @Override
    public void init(IViewPart view) {
        this.mView = view;
    }

}

显示如下
在这里插入图片描述

2. 视图Popup菜单

扩展点【org.eclipse.ui.popupMenus】
在【Extensions】中添加【org.eclipse.ui.popupMenus】
在【org.eclipse.ui.popupMenus】中添加【viewContribution】,【targetID】指定对应的视图
在【viewContribution】中添加相应的【menu】和【action】。

<extension
      point="org.eclipse.ui.popupMenus">
    <viewerContribution
          id="com.plugin.blog.demo.viewerContribution.DemoViewPart"
          targetID="com.plugin.blog.demo.view.DemoViewPart">
        <menu
              id="com.plugin.blog.demo.popup.view.menu"
              label="PopupViewMenu"
              path="additions">
        </menu>
        <action
              class="com.plugin.blog.demo.actions.AddUserAction"
              icon="icons/icon_add.png"
              id="com.plugin.blog.demo.actions.AddUserAction"
              label="新增用户"
              menubarPath="com.plugin.blog.demo.popup.view.menu">
        </action>
    </viewerContribution>
</extension>

【action】配置
在【class】中选择对应的Action,必须继承IWorkbenchWindowActionDelegate类。
AddUserAction类,添加一个新的用户,使用InputDialog来输入用户名。

public class AddUserAction implements IViewActionDelegate {
    private IViewPart mViewPart;

    @Override
    public void run(IAction action) {
        if (mViewPart != null) {
            InputDialog dialog = new InputDialog(mViewPart.getViewSite().getShell(),
                "添加用户", "输入用户名:", "", null);
            dialog.open();
            String value = dialog.getValue();
            if (value != null && value.length() > 0) {
                UserManager.getInstance().addObject(
                    new User(value, User.CATEGORY_ARRAY[0]));
            }
        }
    }

    @Override
    public void selectionChanged(IAction action, ISelection selection) {
    }

    @Override
    public void init(IViewPart view) {
        mViewPart = view;
    }
}

DemoViewPart类中注册右键菜单,添加到createPartControl()方法中。

private void addMenu() {
    MenuManager menuManager = new MenuManager();
    menuManager.setRemoveAllWhenShown(true);

    Menu menu = menuManager.createContextMenu(mTable);
    mTable.setMenu(menu);
    getSite().registerContextMenu(menuManager, mTableViewer);
}

显示如下
在这里插入图片描述

3. 对象Popup菜单

扩展点【org.eclipse.ui.popupMenus】
在【org.eclipse.ui.popupMenus】中添加【objectContribution】,在【objectClass】中指定对象User
在【objectContribution】中添加相应的【menu】和【action】

<extension
      point="org.eclipse.ui.popupMenus">
    <objectContribution
          adaptable="false"
          id="com.plugin.blog.demo.objectContribution.User"
          objectClass="com.plugin.blog.demo.data.User">
        <menu
              id="com.plugin.blog.demo.popup.object.menu"
              label="PopupUserMenu"
              path="additions">
        </menu>
        <action
              class="com.plugin.blog.demo.actions.RemoveUserAction"
              icon="icons/icon_remove.png"
              id="com.plugin.blog.demo.actions.RemoveUserAction"
              label="删除用户"
              menubarPath="com.plugin.blog.demo.popup.object.menu">
        </action>
    </objectContribution>
</extension>

RemoveUserAction

public class RemoveUserAction implements IObjectActionDelegate {
    private IWorkbenchPart mWorkbenchPart;
    private ISelection mSelection;

    @Override
    public void run(IAction action) {
        if (mWorkbenchPart != null && mSelection != null) {
            IStructuredSelection ss = (IStructuredSelection) mSelection;
            if (!ss.isEmpty()) {
                User user = (User) ss.getFirstElement();
                boolean result = MessageDialog.openConfirm(
                        mWorkbenchPart.getSite().getShell(), 
                        "删除用户", "您是否确认删除" + user.getName() + "?");
                if (result) {
                    UserManager.getInstance().removeObject(user);
                }
            }
        }
    }

    @Override
    public void selectionChanged(IAction action, ISelection selection) {
        this.mSelection = selection;
    }

    @Override
    public void setActivePart(IAction action, IWorkbenchPart targetPart) {
        this.mWorkbenchPart = targetPart;
    }

}

显示如下
在这里插入图片描述

4. 视图代码中添加菜单

MessageAction类,继承Action类,实现功能

public class WarnAction extends Action {
    private IViewPart mPart;

    public WarnAction(IViewPart targetPart) {
        super("警告", ImageKeys.getImageDescriptor(ImageKeys.IMAGE_WARN));
        this.mPart = targetPart;
    }

    @Override
    public void run() {
        MessageBox mb = new MessageBox(mPart.getSite().getShell());
        mb.setText("警告");
        mb.setMessage("警告");
        mb.open();
    }

}

ImageKeys类,加载图片

public class ImageKeys {
    public static final String IMAGE_WARN = "icons/icon_warn.png";

    public static ImageDescriptor getImageDescriptor(String path) {
        return Activator.imageDescriptorFromPlugin(Activator.PLUGIN_ID, path);
    }

}

DemoViewPart类,把MessageAction操作添加到视图的菜单栏、工具栏和右键菜单中。

private WarnAction mWarnAction;

private void addMenu() {
    mWarnAction = new WarnAction(this);

    MenuManager menuManager = new MenuManager();
    menuManager.setRemoveAllWhenShown(true);
    menuManager.addMenuListener(new IMenuListener() {
        @Override
        public void menuAboutToShow(IMenuManager manager) {
            addMenu(manager);				
        }
    });

    Menu menu = menuManager.createContextMenu(mTable);
    mTable.setMenu(menu);
    getSite().registerContextMenu(menuManager, mTableViewer);

    IActionBars actionBar = getViewSite().getActionBars();
    addMenu(actionBar.getMenuManager());
    addMenu(actionBar.getToolBarManager());
}

private void addMenu(IContributionManager manager) {
    manager.add(mWarnAction);
}

显示如下
在这里插入图片描述 在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值