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

时间:2015-01-04 19:28:25

标签: java swt

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

我希望仍然有一些默认选项,如复制,剪切,粘贴,但也希望自定义操作“生成随机”以使用UUID填充文本框。

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

2 个答案:

答案 0 :(得分:2)

这是我想出的添加一些标准功能(剪切,复制,粘贴,全选)以及自定义操作(生成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); 
}    

答案 1 :(得分:1)

当我不久前必须做类似的事情时,这就是我所做的,

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

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));

}  

}

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

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

注意 - 我没有在这里提供完整的类,对于其他操作,如复制,粘贴,删除和选择所有等,你将不得不做类似Cut的操作。我使用了TextActionHandler类中定义的相同代码。