001 package com.croftsoft.core.gui; 002 003 import java.awt.*; 004 import java.awt.event.*; 005 import javax.swing.*; 006 007 import com.croftsoft.core.lang.StringLib; 008 import com.croftsoft.core.security.Authentication; 009 import com.croftsoft.core.security.Identifier; 010 011 /********************************************************************* 012 * Prompts the user for a username/password pair. 013 * 014 * <p /> 015 * 016 * @version 017 * 2001-08-16 018 * @since 019 * 2001-07-30 020 * @author 021 * <a href="https://www.croftsoft.com/">David Wallace Croft</a> 022 *********************************************************************/ 023 024 public final class IdentifierDialog 025 extends JDialog 026 implements ActionListener, Identifier, KeyListener, Runnable 027 ////////////////////////////////////////////////////////////////////// 028 ////////////////////////////////////////////////////////////////////// 029 { 030 031 private static final int FIELD_WIDTH = 10; 032 033 // 034 035 private JTextField usernameField; 036 037 private JPasswordField passwordField; 038 039 private final JButton okButton; 040 041 private boolean isOk; 042 043 ////////////////////////////////////////////////////////////////////// 044 ////////////////////////////////////////////////////////////////////// 045 046 public static void main ( String [ ] args ) 047 ////////////////////////////////////////////////////////////////////// 048 { 049 JFrame jFrame = new JFrame ( "frame" ); 050 051 WindowLib.centerOnScreen ( jFrame, 0.8 ); 052 053 jFrame.setVisible ( true ); 054 055 IdentifierDialog identifierDialog = new IdentifierDialog ( 056 jFrame, 057 "title", 058 "", 059 null, 060 null ); 061 062 System.out.println ( identifierDialog.getAuthentication ( ) ); 063 064 System.exit ( 0 ); 065 } 066 067 ////////////////////////////////////////////////////////////////////// 068 ////////////////////////////////////////////////////////////////////// 069 070 public IdentifierDialog ( 071 Frame frame, 072 String title, 073 String username, 074 Color panelBackgroundColor, 075 Color textFieldBackgroundColor ) 076 ////////////////////////////////////////////////////////////////////// 077 { 078 super ( frame, title, true ); // modal 079 080 if ( panelBackgroundColor != null ) 081 { 082 setBackground ( panelBackgroundColor ); 083 } 084 085 Container contentPane = getContentPane ( ); 086 087 contentPane.setLayout ( new GridBagLayout ( ) ); 088 089 GridBagConstraints gridBagConstraints = new GridBagConstraints ( ); 090 091 gridBagConstraints.fill = GridBagConstraints.HORIZONTAL; 092 093 // gridBagConstraints.ipadx = 10; 094 095 // gridBagConstraints.ipady = 10; 096 097 gridBagConstraints.insets = new Insets ( 10, 10, 10, 10 ); 098 099 // 100 101 if ( StringLib.trimToNull ( username ) == null ) 102 { 103 addUsernameField ( username, 104 contentPane, gridBagConstraints, textFieldBackgroundColor ); 105 106 addPasswordField ( 107 contentPane, gridBagConstraints, textFieldBackgroundColor ); 108 } 109 else 110 { 111 addPasswordField ( 112 contentPane, gridBagConstraints, textFieldBackgroundColor ); 113 114 addUsernameField ( username, 115 contentPane, gridBagConstraints, textFieldBackgroundColor ); 116 } 117 118 // 119 120 gridBagConstraints.gridx = 0; 121 122 gridBagConstraints.gridy = 2; 123 124 gridBagConstraints.weightx = 0.0; 125 126 JButton cancelButton = new JButton ( "Cancel" ); 127 128 cancelButton.addActionListener ( this ); 129 130 contentPane.add ( cancelButton, gridBagConstraints ); 131 132 gridBagConstraints.gridx = 1; 133 134 gridBagConstraints.gridy = 2; 135 136 okButton = new JButton ( "OK" ); 137 138 okButton.addActionListener ( this ); 139 140 contentPane.add ( okButton, gridBagConstraints ); 141 142 // 143 144 pack ( ); 145 } 146 147 ////////////////////////////////////////////////////////////////////// 148 ////////////////////////////////////////////////////////////////////// 149 150 public Authentication getAuthentication ( ) 151 ////////////////////////////////////////////////////////////////////// 152 { 153 WindowLib.centerAboveParent ( this ); 154 155 okButton.setEnabled ( false ); 156 157 isOk = false; 158 159 passwordField.setText ( "" ); 160 161 setVisible ( true ); 162 163 if ( !isOk ) 164 { 165 return null; 166 } 167 168 String username = usernameField.getText ( ); 169 170 char [ ] chars = passwordField.getPassword ( ); 171 172 String password = new String ( chars ); 173 174 passwordField.setText ( "" ); 175 176 for ( int i = 0; i < chars.length; i++ ) 177 { 178 chars [ i ] = 0; 179 } 180 181 return new Authentication ( username, password ); 182 } 183 184 public void actionPerformed ( ActionEvent actionEvent ) 185 ////////////////////////////////////////////////////////////////////// 186 { 187 Object source = actionEvent.getSource ( ); 188 189 if ( source == okButton ) 190 { 191 isOk = true; 192 } 193 194 SwingUtilities.invokeLater ( this ); 195 } 196 197 public void run ( ) 198 ////////////////////////////////////////////////////////////////////// 199 { 200 setVisible ( false ); 201 } 202 203 ////////////////////////////////////////////////////////////////////// 204 ////////////////////////////////////////////////////////////////////// 205 206 public void keyPressed ( KeyEvent keyEvent ) 207 ////////////////////////////////////////////////////////////////////// 208 { 209 } 210 211 public void keyReleased ( KeyEvent keyEvent ) 212 ////////////////////////////////////////////////////////////////////// 213 { 214 } 215 216 public synchronized void keyTyped ( KeyEvent keyEvent ) 217 ////////////////////////////////////////////////////////////////////// 218 { 219 SwingUtilities.invokeLater ( 220 new Runnable ( ) 221 { 222 public void run ( ) { checkFields ( ); } 223 } ); 224 } 225 226 ////////////////////////////////////////////////////////////////////// 227 ////////////////////////////////////////////////////////////////////// 228 229 private void addUsernameField ( 230 String username, 231 Container contentPane, 232 GridBagConstraints gridBagConstraints, 233 Color textFieldBackgroundColor ) 234 ////////////////////////////////////////////////////////////////////// 235 { 236 gridBagConstraints.gridx = 0; 237 238 gridBagConstraints.gridy = 0; 239 240 gridBagConstraints.weightx = 0.0; 241 242 gridBagConstraints.fill = GridBagConstraints.NONE; 243 244 contentPane.add ( new JLabel ( "Username" ), gridBagConstraints ); 245 246 usernameField = new JTextField ( FIELD_WIDTH ); 247 248 usernameField.addKeyListener ( this ); 249 250 if ( username != null ) 251 { 252 usernameField.setText ( username ); 253 } 254 255 gridBagConstraints.gridx = 1; 256 257 gridBagConstraints.weightx = 1.0; 258 259 gridBagConstraints.fill = GridBagConstraints.HORIZONTAL; 260 261 if ( textFieldBackgroundColor != null ) 262 { 263 usernameField.setBackground ( textFieldBackgroundColor ); 264 } 265 266 contentPane.add ( usernameField, gridBagConstraints ); 267 } 268 269 private void addPasswordField ( 270 Container contentPane, 271 GridBagConstraints gridBagConstraints, 272 Color textFieldBackgroundColor ) 273 ////////////////////////////////////////////////////////////////////// 274 { 275 gridBagConstraints.gridx = 0; 276 277 gridBagConstraints.gridy = 1; 278 279 gridBagConstraints.weightx = 0.0; 280 281 gridBagConstraints.fill = GridBagConstraints.NONE; 282 283 contentPane.add ( new JLabel ( "Password" ), gridBagConstraints ); 284 285 passwordField = new JPasswordField ( FIELD_WIDTH ); 286 287 passwordField.addKeyListener ( this ); 288 289 gridBagConstraints.gridx = 1; 290 291 gridBagConstraints.weightx = 1.0; 292 293 gridBagConstraints.fill = GridBagConstraints.HORIZONTAL; 294 295 if ( textFieldBackgroundColor != null ) 296 { 297 passwordField.setBackground ( textFieldBackgroundColor ); 298 } 299 300 contentPane.add ( passwordField, gridBagConstraints ); 301 } 302 303 private synchronized void checkFields ( ) 304 ////////////////////////////////////////////////////////////////////// 305 { 306 String username = usernameField.getText ( ); 307 308 char [ ] password = passwordField.getPassword ( ); 309 310 okButton.setEnabled ( 311 ( username != null ) 312 && !"".equals ( username.trim ( ) ) 313 && ( password != null ) 314 && ( password.length > 0 ) ); 315 } 316 317 ////////////////////////////////////////////////////////////////////// 318 ////////////////////////////////////////////////////////////////////// 319 }