1 45 package org.exolab.jms.tools.admin; 46 47 import java.awt.BorderLayout ; 48 import java.awt.FlowLayout ; 49 import java.awt.event.ActionEvent ; 50 import java.awt.event.ActionListener ; 51 import java.awt.event.KeyEvent ; 52 import java.awt.event.WindowAdapter ; 53 import java.awt.event.WindowEvent ; 54 55 import javax.swing.BorderFactory ; 56 import javax.swing.JButton ; 57 import javax.swing.JFrame ; 58 import javax.swing.JLabel ; 59 import javax.swing.JOptionPane ; 60 import javax.swing.JPanel ; 61 import javax.swing.JPasswordField ; 62 import javax.swing.JSeparator ; 63 import javax.swing.JTextField ; 64 import javax.swing.KeyStroke ; 65 import javax.swing.SwingConstants ; 66 import javax.swing.SwingUtilities ; 67 import javax.swing.border.Border ; 68 import javax.swing.text.Keymap ; 69 70 71 78 public class ChangePasswordDialog extends BaseDialog { 79 80 private JPanel jPanel1; 82 private JButton okButton; 83 private JButton cancelButton; 84 private JPanel jPanel2; 85 private JSeparator jSeparator2; 86 private JLabel jLabel1; 87 private JPanel jPanel3; 88 private JPanel jPanel4; 89 private JPanel jPanel5; 90 private JLabel jLabel2; 91 private JLabel jLabel3; 92 private JPasswordField jPasswordField1; 93 private JPasswordField jPasswordField2; 94 95 96 protected String password; 97 protected String confirmedPassword; 98 99 static private ChangePasswordDialog instance_; 101 102 103 108 public ChangePasswordDialog(JFrame parent) { 109 super(parent); 110 } 111 112 116 protected void initComponents() { 117 jPanel1 = new JPanel (); 118 okButton = new JButton (); 119 cancelButton = new JButton (); 120 jPanel2 = new JPanel (); 121 jPanel3 = new JPanel (); 122 jPanel4 = new JPanel (); 123 jPanel5 = new JPanel (); 124 jLabel2 = new JLabel (); 125 jLabel2.setText("Enter password"); 126 jLabel3 = new JLabel (); 127 jLabel3.setText("Confirm password"); 128 jPasswordField1 = new JPasswordField (); 129 jPasswordField2 = new JPasswordField (); 130 131 132 jLabel1 = new JLabel (); 133 jLabel1.setText("Username"); 134 displayText = new JTextField (); 135 jSeparator2 = new JSeparator (); 136 setTitle("Change password"); 137 setModal(true); 138 setResizable(true); 139 addWindowListener(new WindowAdapter () { 140 141 public void windowClosing(WindowEvent evt) { 142 closeDialog(evt); 143 } 144 } 145 ); 146 147 jPanel1.setLayout(new FlowLayout (1, 50, 10)); 148 okButton.setToolTipText("Press to confirm Action"); 149 okButton.setText("OK"); 150 getRootPane().setDefaultButton(okButton); 151 jPanel1.add(okButton); 152 cancelButton.setToolTipText("Press to abort Action"); 153 cancelButton.setText("Cancel"); 154 jPanel1.add(cancelButton); 155 getContentPane().add(jPanel1, BorderLayout.SOUTH); 156 157 jPanel2.setLayout(new BorderLayout (0, 15)); 158 jPanel2.add(jPanel3, BorderLayout.NORTH); 159 jPanel2.add(jPanel4, BorderLayout.CENTER); 160 jPanel2.add(jPanel5, BorderLayout.SOUTH); 161 getContentPane().add(jPanel2, BorderLayout.CENTER); 162 163 jPanel3.setLayout(new BorderLayout (0, 15)); 165 Border loweredbevel = BorderFactory.createLoweredBevelBorder(); 166 displayText.setBorder(loweredbevel); 167 displayText.setEditable(false); 168 displayText.setEnabled(false); 169 displayText.setHorizontalAlignment(SwingConstants.LEFT); 170 jPanel3.add(jLabel1, BorderLayout.NORTH); 171 jPanel3.add(displayText, BorderLayout.CENTER); 172 jPanel3.add(jSeparator2, BorderLayout.SOUTH); 173 175 jPanel4.setLayout(new BorderLayout (0, 15)); 177 jPasswordField1.setBorder(loweredbevel); 178 jPasswordField1.setEditable(true); 179 jPasswordField1.setText(""); 180 jPasswordField1.setHorizontalAlignment(SwingConstants.LEFT); 181 jPanel4.add(jLabel2, BorderLayout.NORTH); 182 jPanel4.add(jPasswordField1, BorderLayout.CENTER); 183 jPanel4.add(jSeparator2, BorderLayout.SOUTH); 184 185 jPanel5.setLayout(new BorderLayout (0, 15)); 187 jPasswordField2.setBorder(loweredbevel); 188 jPasswordField2.setEditable(true); 189 jPasswordField2.setText(""); 190 jPasswordField2.setHorizontalAlignment(SwingConstants.LEFT); 191 jPanel5.add(jLabel3, BorderLayout.NORTH); 192 jPanel5.add(jPasswordField2, BorderLayout.CENTER); 193 jPanel5.add(jSeparator2, BorderLayout.SOUTH); 194 195 okButton.addActionListener(new ActionListener () { 196 197 public void actionPerformed(ActionEvent evt) { 198 confirm(); 199 } 200 } 201 ); 202 203 cancelButton.addActionListener(new ActionListener () { 204 205 public void actionPerformed(ActionEvent evt) { 206 cancel(); 207 } 208 } 209 ); 210 211 KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0); 214 Keymap km = displayText.getKeymap(); 215 km.removeKeyStrokeBinding(enter); 216 } 217 218 221 public void displayChangePassword(String username) { 222 clearPasswords(); 223 displayText.setText(username); 224 225 setLocationRelativeTo(getParent()); 226 status_ = CANCELED; 227 setVisible(true); 228 229 SwingUtilities.invokeLater(new Runnable () { 230 231 public void run() { 232 cancelButton.requestFocus(); 233 } 234 }); 235 } 236 237 244 public static ChangePasswordDialog instance() { 245 return instance_; 246 } 247 248 255 public static ChangePasswordDialog create(JFrame parent) { 256 if (instance_ == null) { 257 instance_ = new ChangePasswordDialog(parent); 258 } 259 return instance_; 260 } 261 262 267 public String getPassword() { 268 return password; 269 } 270 271 275 protected void confirm() { 276 password = String.valueOf(jPasswordField1.getPassword()); 277 confirmedPassword = String.valueOf(jPasswordField2.getPassword()); 278 279 if (password == null || password.equals("")) { 280 clearPasswords(); 281 JOptionPane.showMessageDialog 282 (this, "A password must be suplied", "Create Error", 283 JOptionPane.ERROR_MESSAGE); 284 } else if (confirmedPassword == null || confirmedPassword.equals("")) { 285 clearPasswords(); 286 JOptionPane.showMessageDialog 287 (this, "A confirmed password must be suplied", "Create Error", 288 JOptionPane.ERROR_MESSAGE); 289 } else if (!password.equals(confirmedPassword)) { 290 clearPasswords(); 291 JOptionPane.showMessageDialog 292 (this, "Confirmed password don't match password", "Create Error", 293 JOptionPane.ERROR_MESSAGE); 294 } else { 295 JOptionPane.showMessageDialog 296 (this, "Password has changed", "Info", 297 JOptionPane.INFORMATION_MESSAGE); 298 status_ = CONFIRMED; 299 setVisible(false); 300 dispose(); 301 } 302 } 303 304 private void clearPasswords() { 305 jPasswordField1.setText(""); 306 jPasswordField2.setText(""); 307 } 308 309 } 310 311 | Popular Tags |