如何为文本框控件添加自定义 SWT 上下文菜单

How to add custom SWT context menu for Text box control

我想修改某些 SWT 文本框的右键单击上下文菜单。

我仍然希望有一些默认选项,如复制、剪切、粘贴,但也希望有一个自定义操作 'Generate Random' 以使用 UUID 填充文本框。

如何将这样的菜单添加到控件中?

这是我添加一些标准功能(剪切、复制、粘贴,select 全部)以及自定义操作(生成 UUID)

public static void addContextMenuWithUUID(final Text control)
{
    Menu menu = new Menu(control);
    MenuItem item = new MenuItem(menu, SWT.PUSH);
    item.setText("Cut");
    item.addListener(SWT.Selection, new Listener()
    {
        @Override
        public void handleEvent(Event event)
        {
            control.cut();
        }
    });
    item = new MenuItem(menu, SWT.PUSH);
    item.setText("Copy");
    item.addListener(SWT.Selection, new Listener()
    {
        @Override
        public void handleEvent(Event event)
        {
            control.copy();
        }
    });
    item = new MenuItem(menu, SWT.PUSH);
    item.setText("Paste");
    item.addListener(SWT.Selection, new Listener()
    {
        @Override
        public void handleEvent(Event event)
        {
            control.paste();
        }
    });
    item = new MenuItem(menu, SWT.PUSH);
    item.setText("Select All");
    item.addListener(SWT.Selection, new Listener()
    {
        @Override
        public void handleEvent(Event event)
        {
            control.selectAll();
        }
    });
    item = new MenuItem(menu, SWT.PUSH);
    item.setText("Generate UUID");
    item.addListener(SWT.Selection, new Listener()
    {
        @Override
        public void handleEvent(Event event)
        {
            control.setText(UUID.randomUUID().toString());
        }
    });

   control.setMenu(menu); 
}    

前一段时间我不得不做类似的事情时,我就是这样做的,

我采用了eclipse提供的TextActionHandler class,并修改了代码以满足我的需要。

public final class TextActionHandler {

enum TextAction {

    CUT       (WorkbenchMessages.Workbench_cut, IWorkbenchCommandConstants.EDIT_CUT),
    COPY      (WorkbenchMessages.Workbench_copy, IWorkbenchCommandConstants.EDIT_COPY),
    PASTE     (WorkbenchMessages.Workbench_paste, IWorkbenchCommandConstants.EDIT_PASTE),
    DELETE    (WorkbenchMessages.Workbench_delete, null),
    SELECT_ALL(WorkbenchMessages.Workbench_selectAll, WorkbenchCommandConstants.EDIT_SELECT_ALL);

    private String text;
    private String commandId;

    private TextAction(String text, String commandId ) {
        this.text       = text;
        this.commandId = commandId;
    }

    public String getCommandId() {
        return commandId;
    }

    public String getText() {
        return text;
    }
}

public TextActionHandler(Text text) {
    addText(text);
}

public TextActionHandler() {
    super();
}

public void addText(Text textControl) {
    if (textControl == null) {
        return;
    }
    textControl.addDisposeListener(new DisposeListener() {
        @Override
        public void widgetDisposed(DisposeEvent e) {
            removeText(activeTextControl);
        }
    });
    textControl.addListener(SWT.Activate, textControlListener);
    textControl.addListener(SWT.Deactivate, textControlListener);

    textControl.addKeyListener(keyAdapter);
    textControl.addMouseListener(mouseAdapter);

    activeTextControl = textControl;
    updateActionsEnableState();
}

public void hookContextMenu() {
    final MenuManager menuMgr = new MenuManager("#PMPopupMenu");
    menuMgr.setRemoveAllWhenShown(true);
    menuMgr.addMenuListener(new IMenuListener() {
        public void menuAboutToShow(IMenuManager manager) {
            addContextMenuOptions(menuMgr);
        }
    });
    Menu menu = menuMgr.createContextMenu(activeTextControl);
    activeTextControl.setMenu(menu);
}

private void addContextMenuOptions(MenuManager manager) {
    manager.removeAll();
    manager.add(textCutAction);
    manager.add(textCopyAction);
    manager.add(textPasteAction);
    manager.add(textDeleteAction);
    manager.add(new Separator());
    manager.add(textSelectAllAction);

    // add your own action handlers here
}

...

// example.
private final class CutActionHandler extends Action {

    private CutActionHandler() {
        setProperties(this, TextAction.CUT);
        setEnabled(false);
    }

    @Override
    public void runWithEvent(Event event) {
        if (activeTextControl != null && !activeTextControl.isDisposed()) {
            activeTextControl.cut();
            updateActionsEnableState();
        }
    }

    @Override
    public boolean isEnabled() {
        return activeTextControl != null && !activeTextControl.isDisposed()
                && activeTextControl.getEditable()
                && activeTextControl.getSelectionCount() > 0;
    }

    public void updateEnabledState() {
        setEnabled(isEnabled());
    }
}

private void setProperties(Action action, TextAction actionEnum){

    action.setText(actionEnum.getText());
    action.setActionDefinitionId(actionEnum.getCommandId());
    action.setImageDescriptor(getImageDescriptor(actionEnum));
    action.setDisabledImageDescriptor(getDisabledImageDescriptor(actionEnum));

}  

}

同样,您可以添加自己的 ActionHandlers。例如,RandomGeneratorHandler。 要将此链接到您的文本框,请执行

Text text = new Text(parent, SWT.NONE);
...
TextActionHandler handler = new TextActionHandler();
handler.addText(text);
handler.hookContextMenu();

注意 - 我没有在这里提供完整的 class,对于复制、粘贴、删除和 select 等其他操作,您必须执行与剪切类似的操作。我使用了在 TextActionHandler class.

中定义的相同代码