Ticket #6368: CredentialDialog Enter pressing handler.3.patch
File CredentialDialog Enter pressing handler.3.patch, 5.0 KB (added by , 14 years ago) |
---|
-
src/org/openstreetmap/josm/gui/io/CredentialDialog.java
14 14 import java.awt.event.FocusAdapter; 15 15 import java.awt.event.FocusEvent; 16 16 import java.awt.event.KeyEvent; 17 import java.awt.event.KeyListener; 17 18 import java.awt.event.WindowAdapter; 18 19 import java.awt.event.WindowEvent; 19 20 … … 103 104 104 105 public void prepareForOsmApiCredentials(String username, String password) { 105 106 setTitle(tr("Enter credentials for OSM API")); 106 getContentPane().add(pnlCredentials = new OsmApiCredentialsPanel( ), BorderLayout.CENTER);107 getContentPane().add(pnlCredentials = new OsmApiCredentialsPanel(this), BorderLayout.CENTER); 107 108 pnlCredentials.init(username, password); 108 109 validate(); 109 110 } 110 111 111 112 public void prepareForProxyCredentials(String username, String password) { 112 113 setTitle(tr("Enter credentials for HTTP proxy")); 113 getContentPane().add(pnlCredentials = new HttpProxyCredentialsPanel( ), BorderLayout.CENTER);114 getContentPane().add(pnlCredentials = new HttpProxyCredentialsPanel(this), BorderLayout.CENTER); 114 115 pnlCredentials.init(username, password); 115 116 validate(); 116 117 } … … 136 137 protected JCheckBox cbSaveCredentials; 137 138 protected JMultilineLabel lblHeading; 138 139 protected JMultilineLabel lblWarning; 140 protected CredentialDialog owner; // owner Dependency Injection to use Key listeners for username and password text fields 139 141 140 142 protected void build() { 141 143 tfUserName = new JTextField(20); 142 144 tfPassword = new JPasswordField(20); 143 145 tfUserName.addFocusListener(new SelectAllOnFocusHandler()); 144 146 tfPassword.addFocusListener(new SelectAllOnFocusHandler()); 147 tfUserName.addKeyListener(new TFKeyListener(owner, tfUserName, tfPassword)); 148 tfPassword.addKeyListener(new TFKeyListener(owner, tfPassword, tfUserName)); 145 149 cbSaveCredentials = new JCheckBox(tr("Save user and password (unencrypted)")); 146 150 147 151 setLayout(new GridBagLayout()); … … 201 205 202 206 } 203 207 204 public CredentialPanel() { 208 public CredentialPanel(CredentialDialog owner) { 209 this.owner = owner; 205 210 } 206 211 207 212 public void init(String username, String password) { … … 242 247 lblWarning.setText(tr("Warning: The password is transferred unencrypted.")); 243 248 } 244 249 245 public OsmApiCredentialsPanel() { 250 public OsmApiCredentialsPanel(CredentialDialog owner) { 251 super(owner); 246 252 build(); 247 253 } 248 254 } … … 259 265 lblWarning.setText("<html>" + tr("Warning: depending on the authentication method the proxy server uses the password may be transferred unencrypted.") + "</html>"); 260 266 } 261 267 262 public HttpProxyCredentialsPanel() { 268 public HttpProxyCredentialsPanel(CredentialDialog owner) { 269 super(owner); 263 270 build(); 264 271 } 265 272 } … … 274 281 } 275 282 } 276 283 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 277 326 class OKAction extends AbstractAction { 278 327 public OKAction() { 279 328 putValue(NAME, tr("Authenticate"));