Overriding Cut/Copy/Paste in SWT Text control

我只是一个虾纸丫 提交于 2019-12-25 03:05:11

问题


What is the correct way to override the cut(), copy(), and paste() methods of the Text control? What triggers the execution of these methods?

I have created an example application with a custom class that overrides these methods. Unfortunately, nothing seems to execute these overridden methods, including the act of using Ctrl+X / Ctrl+C / Ctrl+V or selecting cut/copy/paste from the context menu.

Custom Text Class:

import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;

public class TextCopyable extends Text{

    public TextCopyable(Composite parent, int style) {
        super(parent, style);
    }

    @Override
    public void checkSubclass() {
    }

    @Override
    public void cut() {
        System.out.println("Cut!");
    }

    @Override
    public void copy() {
        System.out.println("Copy!");
    }

    @Override
    public void paste() {
        System.out.println("Paste!");
    }

}

Test Shell:

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.layout.GridData;

public class CopyPasteTest extends Shell {

    private TextCopyable text;

    public static void main(String args[]) {
        try {
            Display display = Display.getDefault();
            CopyPasteTest shell = new CopyPasteTest(display);
            shell.open();
            shell.layout();
            while (!shell.isDisposed()) {
                if (!display.readAndDispatch()) {
                    display.sleep();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public CopyPasteTest(Display display) {
        super(display, SWT.SHELL_TRIM);
        createContents();
    }

    protected void createContents() {
        setText("SWT Application");
        setSize(450, 300);
        GridLayout gridLayout = new GridLayout();
        setLayout(gridLayout);

        text = new TextCopyable(this, SWT.BORDER);
        text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
    }

    @Override
    protected void checkSubclass() {
        // Disable the check that prevents subclassing of SWT components
    }

}

My expectation was that the copy() method would be called any time that I use the Ctrl+C command to copy text from the textbox. However, the methods do not trigger at all. Is my assumption faulty?

来源:https://stackoverflow.com/questions/22050899/overriding-cut-copy-paste-in-swt-text-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!