1 18 19 package com.Ostermiller.util; 20 21 import javax.swing.*; 22 import java.awt.*; 23 import java.awt.event.*; 24 import java.util.ResourceBundle ; 25 import java.util.Locale ; 26 27 47 public class PasswordDialog extends JDialog { 48 49 54 protected ResourceBundle labels; 55 56 64 public void setLocale(Locale locale){ 65 labels = ResourceBundle.getBundle("com.Ostermiller.util.PasswordDialog", locale); 66 } 67 68 73 protected JTextField name; 74 79 protected JPasswordField pass; 80 85 protected JButton okButton; 86 91 protected JButton cancelButton; 92 97 protected JLabel nameLabel; 98 103 protected JLabel passLabel; 104 105 114 public void setName(String name){ 115 this.name.setText(name); 116 } 117 118 127 public void setPass(String pass){ 128 this.pass.setText(pass); 129 } 130 131 139 public void setOKText(String ok){ 140 this.okButton.setText(ok); 141 pack(); 142 } 143 144 152 public void setCancelText(String cancel){ 153 this.cancelButton.setText(cancel); 154 pack(); 155 } 156 157 165 public void setNameLabel(String name){ 166 this.nameLabel.setText(name); 167 pack(); 168 } 169 170 178 public void setPassLabel(String pass){ 179 this.passLabel.setText(pass); 180 pack(); 181 } 182 183 191 public String getName(){ 192 return name.getText(); 193 } 194 195 203 public String getPass(){ 204 return new String (pass.getPassword()); 205 } 206 207 218 public boolean okPressed(){ 219 return pressed_OK; 220 } 221 222 227 private boolean pressed_OK = false; 228 229 237 public PasswordDialog(Frame parent, String title) { 238 239 super(parent, title, true); 240 241 setLocale(Locale.getDefault()); 242 243 if (title==null){ 244 setTitle(labels.getString("dialog.title")); 245 } 246 if (parent != null){ 247 setLocationRelativeTo(parent); 248 } 249 } 251 252 259 public PasswordDialog(Frame parent) { 260 this(parent, null); 261 } 262 263 268 public PasswordDialog() { 269 this(null, null); 270 } 271 272 277 protected void dialogInit(){ 278 279 if (labels == null){ 280 setLocale(Locale.getDefault()); 281 } 282 283 name = new JTextField("", 20); 284 pass = new JPasswordField("", 20); 285 okButton = new JButton(labels.getString("dialog.ok")); 286 cancelButton = new JButton(labels.getString("dialog.cancel")); 287 nameLabel = new JLabel(labels.getString("dialog.name") + " "); 288 passLabel = new JLabel(labels.getString("dialog.pass") + " "); 289 290 super.dialogInit(); 291 292 KeyListener keyListener = (new KeyAdapter() { 293 public void keyPressed(KeyEvent e){ 294 if (e.getKeyCode() == KeyEvent.VK_ESCAPE || 295 (e.getSource() == cancelButton 296 && e.getKeyCode() == KeyEvent.VK_ENTER)){ 297 pressed_OK = false; 298 PasswordDialog.this.setVisible(false); 299 } 300 if (e.getSource() == okButton && 301 e.getKeyCode() == KeyEvent.VK_ENTER){ 302 pressed_OK = true; 303 PasswordDialog.this.setVisible(false); 304 } 305 } 306 }); 307 addKeyListener(keyListener); 308 309 ActionListener actionListener = new ActionListener() { 310 public void actionPerformed(ActionEvent e){ 311 Object source = e.getSource(); 312 if (source == name){ 313 name.transferFocus(); 315 } else { 316 pressed_OK = (source == pass || source == okButton); 318 PasswordDialog.this.setVisible(false); 319 } 320 } 321 }; 322 323 GridBagLayout gridbag = new GridBagLayout(); 324 GridBagConstraints c = new GridBagConstraints(); 325 c.insets.top = 5; 326 c.insets.bottom = 5; 327 JPanel pane = new JPanel(gridbag); 328 pane.setBorder(BorderFactory.createEmptyBorder(10, 20, 5, 20)); 329 JLabel label; 330 331 c.anchor = GridBagConstraints.EAST; 332 gridbag.setConstraints(nameLabel, c); 333 pane.add(nameLabel); 334 335 gridbag.setConstraints(name, c); 336 name.addActionListener(actionListener); 337 name.addKeyListener(keyListener); 338 pane.add(name); 339 340 c.gridy = 1; 341 gridbag.setConstraints(passLabel, c); 342 pane.add(passLabel); 343 344 gridbag.setConstraints(pass, c); 345 pass.addActionListener(actionListener); 346 pass.addKeyListener(keyListener); 347 pane.add(pass); 348 349 c.gridy = 2; 350 c.gridwidth = GridBagConstraints.REMAINDER; 351 c.anchor = GridBagConstraints.CENTER; 352 JPanel panel = new JPanel(); 353 okButton.addActionListener(actionListener); 354 okButton.addKeyListener(keyListener); 355 panel.add(okButton); 356 cancelButton.addActionListener(actionListener); 357 cancelButton.addKeyListener(keyListener); 358 panel.add(cancelButton); 359 gridbag.setConstraints(panel, c); 360 pane.add(panel); 361 362 getContentPane().add(pane); 363 364 pack(); 365 } 366 367 374 public boolean showDialog(){ 375 setVisible(true); 376 return okPressed(); 377 } 378 379 388 private static void main(String [] args){ 389 PasswordDialog p = new PasswordDialog(); 390 if(args.length > 0){ 391 p.setName(args[0]); 392 } 393 if(args.length > 1){ 394 p.setPass(args[1]); 395 } 396 if(p.showDialog()){ 397 System.out.println("Name: " + p.getName()); 398 System.out.println("Pass: " + p.getPass()); 399 } else { 400 System.out.println("User selected cancel"); 401 } 402 p.dispose(); 403 p = null; 404 System.exit(0); 405 } 406 } 407 | Popular Tags |