1 24 25 package org.objectweb.cjdbc.console.wizard.tab; 26 27 import java.awt.GridBagConstraints ; 28 import java.awt.GridBagLayout ; 29 import java.awt.event.ActionEvent ; 30 import java.awt.event.ActionListener ; 31 import java.awt.event.FocusEvent ; 32 import java.awt.event.FocusListener ; 33 import java.awt.event.ItemEvent ; 34 import java.awt.event.ItemListener ; 35 import java.util.ArrayList ; 36 37 import javax.swing.BorderFactory ; 38 import javax.swing.JButton ; 39 import javax.swing.JComboBox ; 40 import javax.swing.JLabel ; 41 import javax.swing.JPanel ; 42 import javax.swing.JTextField ; 43 44 import org.objectweb.cjdbc.common.i18n.WizardTranslate; 45 import org.objectweb.cjdbc.console.wizard.WizardConstants; 46 import org.objectweb.cjdbc.console.wizard.WizardTab; 47 import org.objectweb.cjdbc.console.wizard.WizardTabs; 48 import org.objectweb.cjdbc.console.wizard.objects.User; 49 50 58 public class AuthenticationTab extends WizardTab 59 { 60 61 public UserPanel users; 62 63 public UserPanel admin; 64 65 70 public AuthenticationTab(WizardTabs tabs) 71 { 72 super(tabs, WizardConstants.TAB_AUTHENTICATION); 73 74 admin = new UserPanel("label.admin"); 75 this.add(admin, constraints); 76 77 constraints.gridy = ++constraints.gridy; 78 79 users = new UserPanel("label.users"); 80 this.add(users, constraints); 81 } 82 83 88 public ArrayList getUsers() 89 { 90 JComboBox usersBox = users.getUsersCombo(); 91 return WizardConstants.getItemsFromCombo(usersBox); 92 } 93 94 100 public class UserPanel extends JPanel 101 implements 102 ItemListener , 103 ActionListener , 104 FocusListener 105 { 106 private JComboBox usersCombo; 107 private JButton buttonAdd; 108 private JButton buttonRemove; 109 int currentIndex = 0; 110 private JTextField userName; 111 private JTextField password; 112 113 118 public UserPanel(String title) 119 { 120 121 this.setBorder(BorderFactory.createTitledBorder(WizardTranslate 122 .get(title))); 123 this.setLayout(new GridBagLayout ()); 124 GridBagConstraints localconstraints = new GridBagConstraints (); 125 localconstraints.fill = GridBagConstraints.HORIZONTAL; 126 localconstraints.gridy = 0; 127 localconstraints.weightx = 1.0; 128 localconstraints.weighty = 1.0; 129 130 localconstraints.gridwidth = 2; 131 usersCombo = new JComboBox (new Object []{}); 132 usersCombo.addItemListener(this); 133 this.add(usersCombo, localconstraints); 134 135 localconstraints.gridwidth = 1; 136 137 localconstraints.gridy = ++localconstraints.gridy; 138 localconstraints.gridx = 0; 139 buttonAdd = new JButton (WizardTranslate.get("label.adduser")); 140 buttonAdd.setActionCommand(WizardConstants.COMMAND_ADD_USER); 141 buttonAdd.addActionListener(this); 142 this.add(buttonAdd, localconstraints); 143 localconstraints.gridx = 1; 144 buttonRemove = new JButton (WizardTranslate.get("label.removeuser")); 145 buttonRemove.setActionCommand(WizardConstants.COMMAND_REMOVE_USER); 146 buttonRemove.addActionListener(this); 147 this.add(buttonRemove, localconstraints); 148 149 localconstraints.gridy = ++localconstraints.gridy; 150 localconstraints.gridx = 0; 151 this.add(new JLabel (WizardTranslate.get("label.username")), 152 localconstraints); 153 localconstraints.gridx = 1; 154 userName = new JTextField (); 155 userName.addFocusListener(this); 156 this.add(userName, localconstraints); 157 158 localconstraints.gridy = ++localconstraints.gridy; 159 localconstraints.gridx = 0; 160 this.add(new JLabel (WizardTranslate.get("label.password")), 161 localconstraints); 162 localconstraints.gridx = 1; 163 password = new JTextField (); 164 password.addFocusListener(this); 165 this.add(password, localconstraints); 166 } 167 168 171 public void focusGained(FocusEvent e) 172 { 173 174 } 175 176 179 public void focusLost(FocusEvent e) 180 { 181 if (e.getSource() == userName || e.getSource() == password) 182 { 183 User user = ((User) usersCombo.getSelectedItem()); 184 user.setUsername(userName.getText()); 185 user.setPassword(password.getText()); 186 usersCombo.repaint(); 187 } 188 } 189 190 193 public void actionPerformed(ActionEvent e) 194 { 195 String command = e.getActionCommand(); 196 if (command.equals(WizardConstants.COMMAND_ADD_USER)) 197 { 198 User user = new User(); 199 usersCombo.addItem(user); 200 usersCombo.setSelectedItem(user); 201 usersCombo.validate(); 202 tabs.usersChanged(); 203 } 204 else if (command.equals(WizardConstants.COMMAND_REMOVE_USER)) 205 { 206 User user = (User) usersCombo.getSelectedItem(); 207 usersCombo.removeItem(user); 208 usersCombo.validate(); 209 usersCombo.repaint(); 210 tabs.usersChanged(); 211 } 212 213 } 214 215 218 public void itemStateChanged(ItemEvent e) 219 { 220 User user = (User) usersCombo.getSelectedItem(); 221 if (user == null) 222 { 223 userName.setText(""); 224 password.setText(""); 225 } 226 else 227 { 228 userName.setText(user.getUsername()); 229 password.setText(user.getPassword()); 230 } 231 } 232 233 238 public JComboBox getUsersCombo() 239 { 240 return usersCombo; 241 } 242 } 243 } | Popular Tags |