Ticket #6368: CredentialDialog Enter pressing handler.3.patch

File CredentialDialog Enter pressing handler.3.patch, 5.0 KB (added by Kachkaev, 2 years ago)

Forgot trim() on line 308 :)

  • src/org/openstreetmap/josm/gui/io/CredentialDialog.java

     
    1414import java.awt.event.FocusAdapter; 
    1515import java.awt.event.FocusEvent; 
    1616import java.awt.event.KeyEvent; 
     17import java.awt.event.KeyListener; 
    1718import java.awt.event.WindowAdapter; 
    1819import java.awt.event.WindowEvent; 
    1920 
     
    103104 
    104105    public void prepareForOsmApiCredentials(String username, String password) { 
    105106        setTitle(tr("Enter credentials for OSM API")); 
    106         getContentPane().add(pnlCredentials = new OsmApiCredentialsPanel(), BorderLayout.CENTER); 
     107        getContentPane().add(pnlCredentials = new OsmApiCredentialsPanel(this), BorderLayout.CENTER); 
    107108        pnlCredentials.init(username, password); 
    108109        validate(); 
    109110    } 
    110111 
    111112    public void prepareForProxyCredentials(String username, String password) { 
    112113        setTitle(tr("Enter credentials for HTTP proxy")); 
    113         getContentPane().add(pnlCredentials = new HttpProxyCredentialsPanel(), BorderLayout.CENTER); 
     114        getContentPane().add(pnlCredentials = new HttpProxyCredentialsPanel(this), BorderLayout.CENTER); 
    114115        pnlCredentials.init(username, password); 
    115116        validate(); 
    116117    } 
     
    136137        protected JCheckBox cbSaveCredentials; 
    137138        protected JMultilineLabel lblHeading; 
    138139        protected JMultilineLabel lblWarning; 
     140        protected CredentialDialog owner; // owner Dependency Injection to use Key listeners for username and password text fields 
    139141 
    140142        protected void build() { 
    141143            tfUserName = new JTextField(20); 
    142144            tfPassword = new JPasswordField(20); 
    143145            tfUserName.addFocusListener(new SelectAllOnFocusHandler()); 
    144146            tfPassword.addFocusListener(new SelectAllOnFocusHandler()); 
     147            tfUserName.addKeyListener(new TFKeyListener(owner, tfUserName, tfPassword)); 
     148            tfPassword.addKeyListener(new TFKeyListener(owner, tfPassword, tfUserName)); 
    145149            cbSaveCredentials =  new JCheckBox(tr("Save user and password (unencrypted)")); 
    146150 
    147151            setLayout(new GridBagLayout()); 
     
    201205 
    202206        } 
    203207 
    204         public CredentialPanel() { 
     208        public CredentialPanel(CredentialDialog owner) { 
     209            this.owner = owner; 
    205210        } 
    206211 
    207212        public void init(String username, String password) { 
     
    242247            lblWarning.setText(tr("Warning: The password is transferred unencrypted.")); 
    243248        } 
    244249 
    245         public OsmApiCredentialsPanel() { 
     250        public OsmApiCredentialsPanel(CredentialDialog owner) { 
     251            super(owner); 
    246252            build(); 
    247253        } 
    248254    } 
     
    259265            lblWarning.setText("<html>" + tr("Warning: depending on the authentication method the proxy server uses the password may be transferred unencrypted.") + "</html>"); 
    260266        } 
    261267 
    262         public HttpProxyCredentialsPanel() { 
     268        public HttpProxyCredentialsPanel(CredentialDialog owner) { 
     269            super(owner); 
    263270            build(); 
    264271        } 
    265272    } 
     
    274281        } 
    275282    } 
    276283 
     284    /** 
     285     * Listener for username and password text fields key events. 
     286     * When user presses Enter: 
     287     *   If current text field is empty (or just contains a sequence of spaces), nothing happens (or all spaces become selected). 
     288     *   If current text field is not empty, but the next one is (or just contains a sequence of spaces), focuses the next text field. 
     289     *   If both text fields contain characters, submits the form by calling owner's {@link OKAction}. 
     290     */ 
     291    static private class TFKeyListener implements KeyListener{ 
     292        protected CredentialDialog owner; // owner Dependency Injection to call OKAction 
     293        protected JTextField currentTF; 
     294        protected JTextField nextTF; 
     295 
     296        public TFKeyListener (CredentialDialog owner, JTextField currentTF, JTextField nextTF) 
     297        { 
     298            this.owner = owner; 
     299            this.currentTF = currentTF; 
     300            this.nextTF = nextTF; 
     301        } 
     302 
     303        public void keyPressed(KeyEvent e) { 
     304            if(e.getKeyChar() == KeyEvent.VK_ENTER) { 
     305                if (currentTF.getText().trim().isEmpty()) { 
     306                    currentTF.selectAll(); 
     307                    return; 
     308                } else if (nextTF.getText().trim().isEmpty()) { 
     309                    nextTF.requestFocusInWindow(); 
     310                    nextTF.selectAll(); 
     311                    return; 
     312                } else { 
     313                    OKAction okAction = owner.new OKAction(); 
     314                    okAction.actionPerformed(null); 
     315                } 
     316            } 
     317        } 
     318 
     319        public void keyReleased ( KeyEvent e ){ 
     320        } 
     321 
     322        public void keyTyped ( KeyEvent e ){ 
     323        } 
     324    } 
     325 
    277326    class OKAction extends AbstractAction { 
    278327        public OKAction() { 
    279328            putValue(NAME, tr("Authenticate"));