1 30 package com.genimen.djeneric.tools.common; 31 32 import java.awt.BorderLayout ; 33 import java.awt.Dimension ; 34 import java.awt.Toolkit ; 35 import java.awt.event.ActionEvent ; 36 import java.awt.event.FocusEvent ; 37 import java.awt.event.KeyEvent ; 38 import java.awt.event.WindowEvent ; 39 import java.io.File ; 40 import java.io.FileInputStream ; 41 import java.io.FileOutputStream ; 42 import java.io.InputStream ; 43 import java.io.OutputStream ; 44 45 import javax.swing.BorderFactory ; 46 import javax.swing.JButton ; 47 import javax.swing.JDialog ; 48 import javax.swing.JFrame ; 49 import javax.swing.JLabel ; 50 import javax.swing.JPanel ; 51 import javax.swing.JPasswordField ; 52 import javax.swing.JTextField ; 53 import javax.swing.border.Border ; 54 55 import com.genimen.djeneric.language.Messages; 56 import com.genimen.djeneric.repository.DjRepositoryDescriptor; 57 import com.genimen.djeneric.repository.exceptions.LogonException; 58 import com.genimen.djeneric.ui.DjVerticalFlowLayout; 59 import com.genimen.djeneric.ui.Util; 60 import com.genimen.djeneric.util.DjEnvironment; 61 import com.genimen.djeneric.util.DjLogger; 62 import com.genimen.djeneric.util.DjProperties; 63 64 public class LoginDialog extends JDialog 65 { 66 private static final long serialVersionUID = 1L; 67 public static String _propertiesFileName = DjEnvironment 68 .getAbsoluteFileName("djeneric credentials.properties"); 69 private final static String USER_ID = "logon.userid"; 70 private final static String PASSWORD = "logon.password"; 71 72 DjProperties _props = new DjProperties(); 73 74 JPanel panel1 = new JPanel (); 75 BorderLayout borderLayout1 = new BorderLayout (); 76 JPanel jPanel1 = new JPanel (); 77 JPanel jPanel2 = new JPanel (); 78 JPanel jPanel3 = new JPanel (); 79 BorderLayout borderLayout2 = new BorderLayout (); 80 JPanel jPanel4 = new JPanel (); 81 JButton _butOk = new JButton (); 82 JButton _butCancel = new JButton (); 83 JLabel jLabel1 = new JLabel (); 84 JLabel jLabel2 = new JLabel (); 85 DjVerticalFlowLayout verticalFlowLayout1 = new DjVerticalFlowLayout(); 86 JTextField _edtUserId = new JTextField (); 87 DjVerticalFlowLayout verticalFlowLayout2 = new DjVerticalFlowLayout(); 88 JPasswordField _edtPassword = new JPasswordField (); 89 private boolean _wasCancelled = true; 90 Border border1; 91 JLabel jLabel3 = new JLabel (); 92 JLabel _lblRepositoryName = new JLabel (); 93 DjRepositoryDescriptor _repository; 94 95 public LoginDialog(JFrame mainFrame, DjRepositoryDescriptor repository) throws LogonException 96 { 97 super(mainFrame, Messages.getString("LoginDialog.EnterCredentials"), true); 98 try 99 { 100 _repository = repository; 101 102 jbInit(); 103 _lblRepositoryName.setText(repository.getName()); 104 pack(); 105 setSize(306, getHeight()); 106 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 107 Dimension frameSize = getSize(); 108 setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2); 109 110 loadProps(); 111 _edtUserId.setText(_props.getProperty(USER_ID + getRepositoryPrefix(), "")); 112 _edtPassword.setText(_props.getProperty(PASSWORD + getRepositoryPrefix(), "")); 113 114 setVisible(true); 115 } 116 catch (Exception ex) 117 { 118 throw new LogonException(ex); 119 } 120 if (isCancelled()) throw new LogonException(Messages.getString("LoginDialog.LoginCancelled")); 121 } 122 123 private String getRepositoryPrefix() 124 { 125 return "." + _repository.getName(); 126 } 127 128 public String getUserId() 129 { 130 return _edtUserId.getText(); 131 } 132 133 public String getPassword() 134 { 135 return new String (_edtPassword.getPassword()); 136 } 137 138 void jbInit() throws Exception 139 { 140 border1 = BorderFactory.createEmptyBorder(0, 5, 0, 0); 141 panel1.setLayout(borderLayout1); 142 jPanel3.setLayout(borderLayout2); 143 _butOk.setText(Messages.getString("global.Ok")); 144 _butOk.addKeyListener(new java.awt.event.KeyAdapter () 145 { 146 public void keyPressed(KeyEvent e) 147 { 148 _edtUserId_keyPressed(e); 149 } 150 }); 151 _butOk.addActionListener(new java.awt.event.ActionListener () 152 { 153 public void actionPerformed(ActionEvent e) 154 { 155 _butOk_actionPerformed(e); 156 } 157 }); 158 _butCancel.setText(Messages.getString("global.Cancel")); 159 _butCancel.addKeyListener(new java.awt.event.KeyAdapter () 160 { 161 public void keyPressed(KeyEvent e) 162 { 163 _edtUserId_keyPressed(e); 164 } 165 }); 166 _butCancel.addActionListener(new java.awt.event.ActionListener () 167 { 168 public void actionPerformed(ActionEvent e) 169 { 170 _butCancel_actionPerformed(e); 171 } 172 }); 173 jLabel1.setText(Messages.getString("LoginDialog.UserID")); 174 jLabel2.setText(Messages.getString("global.Password")); 175 jPanel1.setLayout(verticalFlowLayout1); 176 jPanel2.setLayout(verticalFlowLayout2); 177 _edtUserId.addKeyListener(new java.awt.event.KeyAdapter () 178 { 179 public void keyPressed(KeyEvent e) 180 { 181 _edtUserId_keyPressed(e); 182 } 183 }); 184 _edtPassword.addKeyListener(new java.awt.event.KeyAdapter () 185 { 186 public void keyPressed(KeyEvent e) 187 { 188 _edtUserId_keyPressed(e); 189 } 190 }); 191 jLabel3.setText(Messages.getString("LoginDialog.Repository")); 192 _lblRepositoryName.setText(Messages.getString("LoginDialog.RepositoryName")); 193 _edtUserId.addFocusListener(new java.awt.event.FocusAdapter () 194 { 195 public void focusGained(FocusEvent e) 196 { 197 _edtUserId_focusGained(e); 198 } 199 }); 200 _edtPassword.addFocusListener(new java.awt.event.FocusAdapter () 201 { 202 public void focusGained(FocusEvent e) 203 { 204 _edtPassword_focusGained(e); 205 } 206 }); 207 this.addWindowListener(new java.awt.event.WindowAdapter () 208 { 209 public void windowOpened(WindowEvent e) 210 { 211 this_windowOpened(e); 212 } 213 }); 214 getContentPane().add(panel1); 215 panel1.add(jPanel1, BorderLayout.WEST); 216 jPanel1.add(jLabel3, null); 217 jPanel1.add(jLabel1, null); 218 jPanel1.add(jLabel2, null); 219 panel1.add(jPanel2, BorderLayout.CENTER); 220 jPanel2.add(_lblRepositoryName, null); 221 jPanel2.add(_edtUserId, null); 222 jPanel2.add(_edtPassword, null); 223 panel1.add(jPanel3, BorderLayout.SOUTH); 224 jPanel3.add(jPanel4, BorderLayout.EAST); 225 jPanel4.add(_butCancel, null); 226 jPanel4.add(_butOk, null); 227 Util.sizeButtons(jPanel4); 228 Util.sizeLabels(getContentPane()); 229 } 230 231 void _butOk_actionPerformed(ActionEvent e) 232 { 233 setCancelled(false); 234 saveProps(); 235 setVisible(false); 236 } 237 238 void _butCancel_actionPerformed(ActionEvent e) 239 { 240 setVisible(false); 241 } 242 243 private void loadProps() 244 { 245 String absFileName = new File (_propertiesFileName).getAbsoluteFile().toString(); 246 247 try 248 { 249 InputStream ios = new FileInputStream (absFileName); 250 _props.load(ios); 251 ios.close(); 252 253 } 254 catch (Exception x) 255 { 256 DjLogger.log(x); 257 } 258 } 259 260 private void saveProps() 261 { 262 String absFileName = new File (_propertiesFileName).getAbsoluteFile().toString(); 263 264 try 265 { 266 OutputStream os = new FileOutputStream (absFileName); 267 268 _props.setProperty(USER_ID + getRepositoryPrefix(), getUserId()); 269 _props.setProperty(PASSWORD + getRepositoryPrefix(), getPassword()); 270 271 _props.store(os, Messages.getString("global.CachedLoginCredentials")); 272 os.close(); 273 } 274 catch (Exception x) 275 { 276 System.err.println(Messages.getString("LoginDialog.CouldNotWriteUnPw", absFileName)); 277 } 278 } 279 280 void _edtUserId_keyPressed(KeyEvent e) 281 { 282 if (e.getKeyCode() == KeyEvent.VK_ENTER) _butOk_actionPerformed(null); 283 if (e.getKeyCode() == KeyEvent.VK_ESCAPE) _butCancel_actionPerformed(null); 284 } 285 286 void _edtUserId_focusGained(FocusEvent e) 287 { 288 _edtUserId.selectAll(); 289 } 290 291 void _edtPassword_focusGained(FocusEvent e) 292 { 293 _edtPassword.selectAll(); 294 } 295 296 void this_windowOpened(WindowEvent e) 297 { 298 if (_edtUserId.getText().trim().length() == 0) 299 { 300 _edtUserId.requestFocus(); 301 } 302 else 303 { 304 _edtPassword.requestFocus(); 305 } 306 307 } 308 309 void setCancelled(boolean _wasCancelled) 310 { 311 this._wasCancelled = _wasCancelled; 312 } 313 314 public boolean isCancelled() 315 { 316 return _wasCancelled; 317 } 318 319 } | Popular Tags |