SWT 文本中上下文菜单的子菜单

Submenu for Context Menu in SWT Text

我想要一个带有简单子菜单的弹出菜单。右键单击 SWT Text (commandText)。我想要实现的是:

A -> D
     E
     F
B
C

所以动作"A"下应该有动作"D,E,F"。 "B" 和 "C" 是顶层的操作,就像 "A" 一样。我的尝试是:

private void addCommandTextContextMenu() {
    MenuManager popupMenu = new MenuManager("#PopupMenu");
    popupMenu.setRemoveAllWhenShown(true);

    popupMenu.addMenuListener(new IMenuListener() {
        public void menuAboutToShow(IMenuManager manager) {
            Action aAction = new Action("A") {};
            Action bAction = new Action("B") {};
            Action cAction = new Action("C") {};

            manager.add(aAction);
            manager.add(bAction);
            manager.add(cAction);
        }
    });

    MenuManager subMenu = new MenuManager("#SubMenu");
    subMenu.setRemoveAllWhenShown(true);
    subMenu.addMenuListener(new IMenuListener() {
        public void menuAboutToShow(IMenuManager manager) {
            Action dAction = new Action("D") {};
            Action eAction = new Action("E") {};
            Action fAction = new Action("F") {};

            manager.add(dAction);
            manager.add(eAction);
            manager.add(fAction);
        }
    });

    popupMenu.add(subMenu);

    final Menu menu2 = popupMenu.createContextMenu(commandText);
    commandText.setMenu(menu2);
}

我只能看到A、B、C

我尝试使用 Java 为 eclipse 插件添加这个弹出菜单只是因为我认为它应该比使用命令和处理程序在 plugin.xml 中定义菜单更容易。

只需创建子菜单并将子菜单操作目录添加到子菜单:

    public void menuAboutToShow(final IMenuManager manager) {

        final Action bAction = new Action("B") {};
        final Action cAction = new Action("C") {};
        final Action dAction = new Action("D") {};
        final Action eAction = new Action("E") {};
        final Action fAction = new Action("F") {};

        final MenuManager subMenu = new MenuManager("A");

        subMenu.add(dAction);
        subMenu.add(eAction);
        subMenu.add(fAction);

        manager.add(subMenu);

        manager.add(bAction);
        manager.add(cAction);
    }

将子菜单管理器添加到顶级管理器。子菜单管理器的名称用于顶级菜单项。