Ignore:
Timestamp:
2011-06-01T21:15:09+02:00 (13 years ago)
Author:
stoecker
Message:

see 6368 - patch by Kachkaev - Enter key pressing handler for text fields in Credentials dialog

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/io/CredentialDialog.java

    r3770 r4110  
    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;
     
    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();
     
    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();
     
    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() {
     
    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
     
    202206        }
    203207
    204         public CredentialPanel() {
     208        public CredentialPanel(CredentialDialog owner) {
     209            this.owner = owner;
    205210        }
    206211
     
    243248        }
    244249
    245         public OsmApiCredentialsPanel() {
     250        public OsmApiCredentialsPanel(CredentialDialog owner) {
     251            super(owner);
    246252            build();
    247253        }
     
    260266        }
    261267
    262         public HttpProxyCredentialsPanel() {
     268        public HttpProxyCredentialsPanel(CredentialDialog owner) {
     269            super(owner);
    263270            build();
    264271        }
     
    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() {
Note: See TracChangeset for help on using the changeset viewer.