KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > console > wizard > tab > AuthenticationTab


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Nicolas Modrzyk.
22  * Contributor(s): ______________________.
23  */

24
25 package org.objectweb.cjdbc.console.wizard.tab;
26
27 import java.awt.GridBagConstraints JavaDoc;
28 import java.awt.GridBagLayout JavaDoc;
29 import java.awt.event.ActionEvent JavaDoc;
30 import java.awt.event.ActionListener JavaDoc;
31 import java.awt.event.FocusEvent JavaDoc;
32 import java.awt.event.FocusListener JavaDoc;
33 import java.awt.event.ItemEvent JavaDoc;
34 import java.awt.event.ItemListener JavaDoc;
35 import java.util.ArrayList JavaDoc;
36
37 import javax.swing.BorderFactory JavaDoc;
38 import javax.swing.JButton JavaDoc;
39 import javax.swing.JComboBox JavaDoc;
40 import javax.swing.JLabel JavaDoc;
41 import javax.swing.JPanel JavaDoc;
42 import javax.swing.JTextField JavaDoc;
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 /**
51  * This tab has al the fields for the authentication section of the virtual
52  * database.
53  *
54  * @see <code>WizardTab</code>
55  * @author <a HREF="mailto:Nicolas.Modrzyk@inria.fr">Nicolas Modrzyk </a>
56  * @version 1.0
57  */

58 public class AuthenticationTab extends WizardTab
59 {
60   /** User panel */
61   public UserPanel users;
62   /** Admin panel */
63   public UserPanel admin;
64
65   /**
66    * Creates a new <code>AuthenticationTab</code> object
67    *
68    * @param tabs the wizard tab
69    */

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   /**
84    * Return the list of users
85    *
86    * @return users list
87    */

88   public ArrayList JavaDoc getUsers()
89   {
90     JComboBox JavaDoc usersBox = users.getUsersCombo();
91     return WizardConstants.getItemsFromCombo(usersBox);
92   }
93
94   /**
95    * This class defines a UserPanel
96    *
97    * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
98    * @version 1.0
99    */

100   public class UserPanel extends JPanel JavaDoc
101       implements
102         ItemListener JavaDoc,
103         ActionListener JavaDoc,
104         FocusListener JavaDoc
105   {
106     private JComboBox JavaDoc usersCombo;
107     private JButton JavaDoc buttonAdd;
108     private JButton JavaDoc buttonRemove;
109     int currentIndex = 0;
110     private JTextField JavaDoc userName;
111     private JTextField JavaDoc password;
112
113     /**
114      * Creates a new <code>UserPanel</code> object
115      *
116      * @param title the panel title
117      */

118     public UserPanel(String JavaDoc title)
119     {
120
121       this.setBorder(BorderFactory.createTitledBorder(WizardTranslate
122           .get(title)));
123       this.setLayout(new GridBagLayout JavaDoc());
124       GridBagConstraints JavaDoc localconstraints = new GridBagConstraints JavaDoc();
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 JavaDoc(new Object JavaDoc[]{});
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 JavaDoc(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 JavaDoc(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 JavaDoc(WizardTranslate.get("label.username")),
152           localconstraints);
153       localconstraints.gridx = 1;
154       userName = new JTextField JavaDoc();
155       userName.addFocusListener(this);
156       this.add(userName, localconstraints);
157
158       localconstraints.gridy = ++localconstraints.gridy;
159       localconstraints.gridx = 0;
160       this.add(new JLabel JavaDoc(WizardTranslate.get("label.password")),
161           localconstraints);
162       localconstraints.gridx = 1;
163       password = new JTextField JavaDoc();
164       password.addFocusListener(this);
165       this.add(password, localconstraints);
166     }
167
168     /**
169      * @see java.awt.event.FocusListener#focusGained(java.awt.event.FocusEvent)
170      */

171     public void focusGained(FocusEvent JavaDoc e)
172     {
173
174     }
175
176     /**
177      * @see java.awt.event.FocusListener#focusLost(java.awt.event.FocusEvent)
178      */

179     public void focusLost(FocusEvent JavaDoc 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     /**
191      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
192      */

193     public void actionPerformed(ActionEvent JavaDoc e)
194     {
195       String JavaDoc 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     /**
216      * @see java.awt.event.ItemListener#itemStateChanged(java.awt.event.ItemEvent)
217      */

218     public void itemStateChanged(ItemEvent JavaDoc 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     /**
234      * Returns the usersCombo value.
235      *
236      * @return Returns the usersCombo.
237      */

238     public JComboBox JavaDoc getUsersCombo()
239     {
240       return usersCombo;
241     }
242   }
243 }
Popular Tags