开发者

IPhone password field in AWT/SWT?

开发者 https://www.devze.com 2023-01-27 05:39 出处:网络
I want to create a special Password Dialog for my eclipse product, which is used with an on screen keyboard.

I want to create a special Password Dialog for my eclipse product, which is used with an on screen keyboard.

It would be very nice, if i could use a component like the IPhone Password field. In this field, the added character is shown for a second and a开发者_运维百科fter the second it is converted into the '*' character for hiding the complete password.

Did a jar/library exists, this is implemented in AWT or SWT?

Edit:

I could trying to implement it from scratch (SWT), but for these i would have to create a very special and complicated KeyListener for the password Text component. I would have to catch the keyReleased event and set the characters manually into the field.

So far i was not able to find any libraries in the web. Suggestion how this can be implemented are welcome too.


This is not really a full answer, rather than a discussion starter and I don't know of any out-of-the-box widgets which can do that.

My first idea was to inheriting the swt Text widget and overriding setEchoChar et al., but after looking at the code this doesn't really seem feasible, because this method is merely a wrapper around:

OS.SendMessage (handle, OS.EM_SETPASSWORDCHAR, echo, 0);

If anyone would know the OS specific low-level implementation, that might be helpful.

Anyway, on to a different approach. I would avoid the KeyListener and use a ModifyListener on the Text-Widget.

void addModifyListener(ModifyListener listener)

You could then build a wrapper which catches the entered text using this listener, appends it to a locally held string/stringbuffer (or e.g. the Eclipse Preferencestore) and send a modified full text to the Text widget using setText(String s), replacing all characters except the last by an echo character (e.g. *).

myText.setText((s.substring(0, s.length()-1)).replaceAll("[\\s\\S]","*")+s.charAt(s.length()-1));

This is a bit of a kludge, but it should work.

The not so straightforward bit is the 1 second timing, without stalling the whole view...


Depending on what Jules said the following code is some kind of working. The code is quick and fast and i would like to have a more thread safe solution.

originalString = new StringBuffer();

passwordField.addModifyListener(new ModifyListener() {

public void modifyText(ModifyEvent e) {

    synchronized (passwordField) {

         String s = passwordField.getText();

         String newS = s.replaceAll("[\\s\\S]", "*");
         if (newS.equals(s)) {
             while (originalString.length() > s.length()) {
                 originalString = originalString.deleteCharAt(originalString.length() - 1);
             }
             usernameField.setText(originalString.toString());
             return;
         }

        if (originalString.length() < s.length()) {
            originalString.append(s.charAt(s.length() - 1));
        }

        try {
            Thread.sleep(500);
        } catch (InterruptedException e1) {
        }
        passwordField.setText(newS);
    }

    passwordField.redraw();

    passwordField.setSelection(passwordField.getText().length());
    }

});

Key Events are cached, so you can add more characters, also when the Thread is waiting. Another Problem is the Cursor handling. the Cursor always moves to the first position, when you set the Text.

I think when this is working it is very near to the iphone solution.

0

精彩评论

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