开发者

"paste" listener on a SWT widget

开发者 https://www.devze.com 2023-01-01 11:13 出处:网络
I have an application with a SWT widget, say a org.eclipse.swt.widgets.Text, and want to add some control to the paste function.

I have an application with a SWT widget, say a org.eclipse.swt.widgets.Text, and want to add some control to the paste function.

The idea is that if the user can paste a string of IDs, I detect that, run some code and paste the object that corresponds to the IDs.

So I'm looking for some "ClipBoardListener" of some sort开发者_StackOverflow to add to my widget, but that doesnt seem to exist. A keylistener would only trap the pastes done by key and then you would have to deal with different key combos for pasting in different OS's.

Based on this java 1.2 question I tried subclassing the text class and override the inser method, but that didnt work

Exception in thread "main" org.eclipse.swt.SWTException: Subclassing not allowed

Seemed like an ugly solution anyway.


Fredrik, Handling ModifyListner will not solve your problem, as it will get invoked every time you type something . As I believe you are interested to handle "PASTE" event only. As in ModifyListner you will not come to know if this is triggered because of someone actually typed data or data comes via "PASTE" event ( this paste can be CTRL-V, context menu "Paste" operation or other OS specific keys)


There's a ModifyListener which you can listen to. It doesn't distinguish between typed text and pasted text, but based on what it seems you want to do, that might work as well.


the answer from dplass is right. in my project i have no change to override the paste-mehtod from swt-text, because the textfield that based on this comes from another project and there is the paste-method not implement. I need also a another workaround to detect paste-operations to a textfield that comes from a key-combination or from the clipboard or eg.

Also, what if:

We assume that a normal user with a normal keyboard can only type one character at a time. So all other changes with more than one character at a time must represent an insert operation. (or not ;D)

    Text swtText = new Text(composite, 1);

    // Security-Listener
    swtText.addModifyListener(new ModifyListener() {

        private boolean hasChanged  = false;
        private int charCountBefore = 0;
        private int charCountNow    = 0;

        public void modifyText(ModifyEvent e) {

            charCountNow = swtText.getCharCount();

            // We assume that a normal user with a normal keyboard can only type one character at a time
            // So all other changes with more than one character at a time must represent an insert operation
            if (charCountNow > (charCountBefore + 1)) {

                // MY CODE TO DISABLE THE EYE-BUTTON FOR DEMASK THE TEXTFIELD
                hasChanged = true;
            }
            // The eye button is otherwise only available when you start typing, i.e. something is in the input field
            else if ((swtText.getCharCount() > 0) && (hasChanged == false)) {

                // MY CODE TO ENABLE THE EYE-BUTTON FOR DEMASK THE TEXTFIELD
                hasChanged = true;
            }
            // Reset after all characters have been removed, so we start over
            else if ((swtText.getCharCount() <= 0) && (hasChanged == true)) {

                // MY CODE TO DISABLE THE EYE-BUTTON FOR DEMASK THE TEXTFIELD
                hasChanged = false;
            }

            charCountBefore = swtText.getCharCount();
        }
    });
0

精彩评论

暂无评论...
验证码 换一张
取 消