1 6 package com.nightlabs.ipanema.base.login; 7 8 import java.awt.BorderLayout ; 9 import java.awt.Component ; 10 import java.awt.FlowLayout ; 11 import java.awt.Font ; 12 import java.awt.event.ActionEvent ; 13 import java.awt.event.ActionListener ; 14 15 import javax.swing.JButton ; 16 import javax.swing.JCheckBox ; 17 import javax.swing.JComponent ; 18 import javax.swing.JLabel ; 19 import javax.swing.JPanel ; 20 import javax.swing.JPasswordField ; 21 import javax.swing.JTextField ; 22 import javax.swing.JToggleButton ; 23 import javax.swing.SpringLayout ; 24 25 import org.apache.commons.beanutils.BeanUtils; 26 import org.apache.log4j.Logger; 27 28 import com.nightlabs.config.Config; 29 import com.nightlabs.config.ConfigException; 30 import com.nightlabs.rcp.splash.SplashScreen; 31 import com.nightlabs.util.SpringUtilities; 32 33 37 public class SplashLoginPanel extends JPanel { 38 39 private static Logger LOGGER = Logger.getLogger(SplashLoginPanel.class); 40 41 private IpanemaLoginContext loginContext; 42 private LoginConfigModule loginConfigModule; 43 44 private JPanel fieldWrapperPanel; 45 46 private JPanel messagePanel; 47 private JLabel labelMessage; 48 49 private JPanel editPanel; 50 private JLabel labelUsername; 51 private JTextField textUsername; 52 private JLabel labelPassword; 53 private JPasswordField textPassword; 54 private JLabel labelWorkstation; 55 private JTextField textWorkstation; 56 57 private JPanel detailWrapper; 58 private JPanel detailPanel; 59 private JLabel labelOrganisationID; 60 private JTextField textOrganisationID; 61 private JLabel labelSecurityProtocol; 62 private JTextField textSecurityProtocol; 63 private JLabel labelServerURL; 64 private JTextField textServerURL; 65 private JLabel labelInitialContextFactory; 66 private JTextField textInitialContextFactory; 67 68 private JPanel checkBoxPanel; 69 private JCheckBox checkBoxSaveSettings; 70 71 private JPanel buttonPanel; 72 private JButton buttonLogin; 73 private JButton buttonQuit; 74 private JToggleButton buttonShowDetails; 75 76 private JPanel southPanel; 77 78 private LoginConfigModule persistentLoginModule; 79 80 81 82 85 public SplashLoginPanel(IpanemaLoginContext loginContext, LoginConfigModule loginConfigModule) { 86 super(); 87 this.loginContext = loginContext; 88 this.loginConfigModule = loginConfigModule; 89 90 this.setFont(this.getFont().deriveFont(Font.ROMAN_BASELINE)); 91 92 try { 93 persistentLoginModule = ((LoginConfigModule)Config.sharedInstance().createConfigModule(LoginConfigModule.class)); 94 } catch (ConfigException e) { 95 throw new RuntimeException (e); 96 } 97 98 this.setLayout(new BorderLayout ()); 99 100 fieldWrapperPanel = new JPanel (); 101 fieldWrapperPanel.setLayout(new BorderLayout ()); 102 add(fieldWrapperPanel, BorderLayout.NORTH); 103 104 editPanel = new JPanel (); 105 editPanel.setLayout(new SpringLayout ()); 106 107 108 messagePanel = new JPanel (); 109 messagePanel.setLayout(new FlowLayout (FlowLayout.LEADING, 10, 10)); 110 labelMessage = new JLabel ("Here could be a useful message"); 111 messagePanel.add(labelMessage); 112 fieldWrapperPanel.add(messagePanel, BorderLayout.NORTH); 113 114 fieldWrapperPanel.add(editPanel, BorderLayout.SOUTH); 115 116 labelUsername = new JLabel ("Username:", JLabel.LEADING); 117 editPanel.add(labelUsername); 118 textUsername = new JTextField (15); 119 labelUsername.setLabelFor(textUsername); 120 textUsername.setText(persistentLoginModule.getUserID()); 121 editPanel.add(textUsername); 122 123 labelPassword = new JLabel ("Password:", JLabel.LEADING); 124 editPanel.add(labelPassword); 125 textPassword = new JPasswordField (15); 126 labelPassword.setLabelFor(textPassword); 127 textPassword.setText(""); 128 textPassword.setEchoChar('*'); 129 editPanel.add(textPassword); 130 131 labelWorkstation = new JLabel ("Workstation:", JLabel.LEADING); 132 editPanel.add(labelWorkstation); 133 textWorkstation = new JTextField (15); 134 labelWorkstation.setLabelFor(textWorkstation); 135 textWorkstation.setText(persistentLoginModule.getWorkstationID()); 136 editPanel.add(textWorkstation); 137 138 SpringUtilities.makeCompactGrid( 139 editPanel, 140 3, 2, 141 10, 20, 142 10, 10 143 ); 144 detailWrapper = new JPanel (); 145 detailWrapper.setLayout(new BorderLayout ()); 146 147 detailPanel = new JPanel (); 148 detailPanel.setLayout(new SpringLayout ()); 149 detailWrapper.add(detailPanel, BorderLayout.NORTH); 150 151 labelOrganisationID = new JLabel ("Organisation:", JLabel.LEADING); 152 detailPanel.add(labelOrganisationID); 153 textOrganisationID = new JTextField (15); 154 labelOrganisationID.setLabelFor(textOrganisationID); 155 textOrganisationID.setText(persistentLoginModule.getOrganisationID()); 156 detailPanel.add(textOrganisationID); 157 158 labelSecurityProtocol = new JLabel ("SecurityProtocol:", JLabel.LEADING); 159 detailPanel.add(labelSecurityProtocol); 160 textSecurityProtocol = new JTextField (15); 161 labelSecurityProtocol.setLabelFor(textSecurityProtocol); 162 textSecurityProtocol.setText(persistentLoginModule.getSecurityProtocol()); 163 detailPanel.add(textSecurityProtocol); 164 165 labelServerURL = new JLabel ("ServerURL:", JLabel.LEADING); 166 detailPanel.add(labelServerURL); 167 textServerURL = new JTextField (15); 168 labelServerURL.setLabelFor(textServerURL); 169 textServerURL.setText(persistentLoginModule.getServerURL()); 170 detailPanel.add(textServerURL); 171 172 labelInitialContextFactory = new JLabel ("InitialContextFactory:", JLabel.LEADING); 173 detailPanel.add(labelInitialContextFactory); 174 textInitialContextFactory = new JTextField (15); 175 labelInitialContextFactory.setLabelFor(textInitialContextFactory); 176 textInitialContextFactory.setText(persistentLoginModule.getInitialContextFactory()); 177 detailPanel.add(textInitialContextFactory); 178 SpringUtilities.makeCompactGrid( 179 detailPanel, 180 4, 2, 181 10, 20, 182 10, 10 183 ); 184 checkBoxPanel = new JPanel (); 185 checkBoxPanel.setLayout(new FlowLayout (FlowLayout.LEADING)); 186 checkBoxSaveSettings = new JCheckBox ("Save settings to config", false); 187 checkBoxPanel.add(checkBoxSaveSettings); 188 detailWrapper.add(checkBoxPanel, BorderLayout.SOUTH); 189 190 191 setPreferredSize(SplashScreen.getSplashImageDimension()); 192 196 southPanel = new JPanel (); 197 add(southPanel, BorderLayout.SOUTH); 198 southPanel.setLayout(new BorderLayout ()); 199 200 buttonPanel = new JPanel (); 201 buttonPanel.setAlignmentX(JPanel.RIGHT_ALIGNMENT); 202 buttonPanel.setLayout(new FlowLayout (FlowLayout.TRAILING)); 203 southPanel.add(buttonPanel, BorderLayout.SOUTH); 204 buttonLogin = new JButton ("Login"); 205 buttonPanel.add(buttonLogin); 206 buttonLogin.addActionListener(new ActionListener (){ 207 public void actionPerformed(ActionEvent e) { 208 synchronized (SplashScreen.getMutex()) { 209 SplashScreen.getMutex().notifyAll(); 210 } 211 } 212 }); 213 214 buttonQuit = new JButton ("Quit"); 215 buttonPanel.add(buttonQuit); 216 buttonQuit.addActionListener(new ActionListener (){ 217 public void actionPerformed(ActionEvent e) { 218 System.exit(0); 219 } 220 }); 221 buttonShowDetails = new JToggleButton ("Details"); 222 buttonPanel.add(buttonShowDetails); 223 buttonShowDetails.addActionListener(new ActionListener () { 224 public void actionPerformed(ActionEvent e) { 225 if (buttonShowDetails.isSelected()) { 226 fieldWrapperPanel.remove(editPanel); 227 fieldWrapperPanel.add(detailWrapper, BorderLayout.SOUTH); 228 } 229 else { 230 fieldWrapperPanel.remove(detailWrapper); 231 fieldWrapperPanel.add(editPanel, BorderLayout.SOUTH); 232 } 233 setComponentFont(SplashLoginPanel.this, SplashLoginPanel.this.getFont()); 234 SplashScreen.setSplashPanel(SplashLoginPanel.this); 235 } 236 }); 237 238 setComponentFont(this, this.getFont()); 239 } 240 241 245 public void assignLoginValues() { 246 loginConfigModule.setUserID(textUsername.getText()); 247 loginConfigModule.setOrganisationID(textOrganisationID.getText()); 248 loginConfigModule.setSecurityProtocol(textSecurityProtocol.getText()); 249 loginConfigModule.setServerURL(textServerURL.getText()); 250 loginConfigModule.setInitialContextFactory(textInitialContextFactory.getText()); 251 loginConfigModule.setWorkstationID(textWorkstation.getText()); 252 253 if (checkBoxSaveSettings.isSelected()) { 254 try { 255 BeanUtils.copyProperties(persistentLoginModule,loginConfigModule); 256 persistentLoginModule.setChanged(); 257 } catch (Exception e) { 258 LOGGER.error("Saving config failed!", e); 259 } 260 } 261 262 loginContext.setCredentials( 263 loginConfigModule.getUserID(), 264 loginConfigModule.getOrganisationID(), 265 new String (textPassword.getPassword()) 266 ); 267 } 268 269 public void setErrMessage(String message) { 270 labelMessage.setText(message); 271 SplashScreen.setSplashPanel(SplashLoginPanel.this); 272 } 273 274 private void setComponentFont(JComponent component, Font font) { 275 component.setFont(font); 276 Component [] components = component.getComponents(); 277 for (int i = 0; i < components.length; i++) { 278 if (components[i] instanceof JComponent ) 279 setComponentFont((JComponent )components[i], font); 280 else { 281 components[i].setFont(font); 282 } 283 } 284 } 285 286 } 287 | Popular Tags |