KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > applications > administrator > gui > UserPanel


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2003 Vincent Fiack <vfiack@mail15.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package org.lucane.applications.administrator.gui;
20
21 import java.util.*;
22 import java.awt.*;
23 import java.awt.event.*;
24
25 import javax.swing.*;
26
27 import org.lucane.applications.administrator.AdministratorPlugin;
28 import org.lucane.client.widgets.ListBox;
29 import org.lucane.common.concepts.*;
30 import org.lucane.common.crypto.MD5;
31
32 public class UserPanel extends JPanel
33 implements MouseListener, ActionListener
34 {
35     private transient AdministratorPlugin plugin;
36     
37     private JTextField userLogin;
38     private JPasswordField userPasswd;
39     private JTextField userRealName;
40     private JTextField userMailAddress;
41     private JTextField userLanguage;
42     private JCheckBox userLocked;
43     private JComboBox userStartupPlugin;
44     private JTextArea userDescription;
45
46     private UserConcept oldConcept;
47     
48     private JButton btnUpdate;
49     private JButton btnRemove;
50     
51     private ConceptTable userGroups;
52
53     private ConceptPanel panel;
54     
55     public UserPanel(AdministratorPlugin plugin, ConceptPanel panel)
56     {
57         super(new BorderLayout());
58         this.plugin = plugin;
59         this.panel = panel;
60         
61         //-- attributes
62
userLogin = new JTextField();
63         userLogin.setEnabled(false);
64         userPasswd = new JPasswordField();
65         userRealName = new JTextField();
66         userMailAddress = new JTextField();
67         userLanguage = new JTextField();
68         userLocked = new JCheckBox();
69         userStartupPlugin = new JComboBox();
70         userDescription = new JTextArea();
71         userGroups = new ConceptTable(plugin, tr("groups"));
72         userGroups.addMouseListener(this);
73         oldConcept = null;
74         
75         //-- metadata
76
JPanel labels = new JPanel(new GridLayout(7,1));
77         labels.add(new JLabel(tr("user.login")));
78         labels.add(new JLabel(tr("user.passwd")));
79         labels.add(new JLabel(tr("user.realName")));
80         labels.add(new JLabel(tr("user.mailAddress")));
81         labels.add(new JLabel(tr("user.language")));
82         labels.add(new JLabel(tr("user.locked")));
83         labels.add(new JLabel(tr("user.startupPlugin")));
84
85
86         JPanel fields = new JPanel(new GridLayout(7, 1));
87         fields.add(userLogin);
88         fields.add(userPasswd);
89         fields.add(userRealName);
90         fields.add(userMailAddress);
91         fields.add(userLanguage);
92         fields.add(userLocked);
93         fields.add(userStartupPlugin);
94         
95         JPanel metadata = new JPanel(new BorderLayout());
96         metadata.add(labels, BorderLayout.WEST);
97         metadata.add(fields, BorderLayout.CENTER);
98         
99         //-- description
100
JPanel description = new JPanel(new BorderLayout());
101         description.add(new JLabel(tr("description")), BorderLayout.NORTH);
102         description.add(new JScrollPane(userDescription), BorderLayout.CENTER);
103                 
104         //-- groups
105
JPanel groups = new JPanel(new BorderLayout());
106         groups.add(new JLabel(tr("groups")), BorderLayout.NORTH);
107         groups.add(new JScrollPane(userGroups));
108         
109         //-- description and groups
110
JPanel descriptionAndGroups = new JPanel(new GridLayout(2, 1));
111         descriptionAndGroups.add(description);
112         descriptionAndGroups.add(groups);
113         
114         //buttons
115
JPanel buttonsContainer = new JPanel(new BorderLayout());
116         JPanel buttons = new JPanel(new GridLayout(1, 2));
117         btnRemove = new JButton(tr("btn.remove"));
118         btnRemove.addActionListener(this);
119         buttons.add(btnRemove);
120         btnUpdate = new JButton(tr("btn.save"));
121         btnUpdate.addActionListener(this);
122         buttons.add(btnUpdate);
123         buttonsContainer.add(buttons, BorderLayout.EAST);
124
125         //-- complete
126
this.add(metadata, BorderLayout.NORTH);
127         this.add(descriptionAndGroups, BorderLayout.CENTER);
128         this.add(buttonsContainer, BorderLayout.SOUTH);
129     }
130     
131     public void showConcept(UserConcept concept)
132     {
133         oldConcept = concept;
134
135         userLogin.setText(concept.getName());
136         userPasswd.setText(concept.getPassword());
137         userRealName.setText(concept.getRealName());
138         userMailAddress.setText(concept.getMailAddress());
139         userLanguage.setText(concept.getLanguage());
140         userLocked.setSelected(concept.isLocked());
141
142         ArrayList authorizedPlugins = plugin.getAuthorizedPlugins(concept);
143         
144         //if no plugin available, show all plugins
145
//useful for new users
146
if(authorizedPlugins.size() == 0)
147             authorizedPlugins = plugin.getAllPlugins(false);
148         
149         ArrayList plugins = new ArrayList();
150         Iterator i = authorizedPlugins.iterator();
151         
152         while(i.hasNext())
153         {
154             PluginConcept plugin = (PluginConcept)i.next();
155             plugins.add(plugin.getName());
156         }
157         
158         
159         userStartupPlugin.setModel(new DefaultComboBoxModel(plugins.toArray()));
160         userStartupPlugin.setSelectedItem(concept.getStartupPlugin());
161         
162         userDescription.setText(concept.getDescription());
163         setGroups(concept);
164     }
165     
166     public void setGroups(UserConcept service)
167     {
168         ArrayList groups = new ArrayList();
169         Iterator allGroups = plugin.getAllGroups(false).iterator();
170         
171         while(allGroups.hasNext())
172         {
173             GroupConcept group = (GroupConcept)allGroups.next();
174             if(group.hasUser(service))
175                 groups.add(group);
176         }
177         
178         userGroups.setConcepts(groups);
179     }
180     
181     private String JavaDoc tr(String JavaDoc s)
182     {
183         return plugin.tr(s);
184     }
185
186     //-- mouse listener
187

188     public void mouseEntered(MouseEvent e) {}
189     public void mouseExited(MouseEvent e) {}
190     public void mousePressed(MouseEvent e) {}
191     public void mouseReleased(MouseEvent e) {}
192     public void mouseClicked(MouseEvent e)
193     {
194         if(e.getClickCount() < 2)
195             return;
196         
197         int row = userGroups.getSelectedRow();
198         if(row < 0)
199             return;
200         
201         panel.showConcept(userGroups.getConceptAt(row));
202     }
203     
204     //-- action listener
205

206     public void actionPerformed(ActionEvent ae)
207     {
208         UserConcept concept = new UserConcept(
209                 userLogin.getText(),
210                 new String JavaDoc(userPasswd.getPassword()),
211                 userRealName.getText(),
212                 userMailAddress.getText(),
213                 userLanguage.getText(),
214                 userLocked.isSelected(),
215                 (String JavaDoc)userStartupPlugin.getSelectedItem());
216         
217         concept.setDescription(userDescription.getText());
218         
219         // password changed
220
if(! concept.getPassword().equals(oldConcept.getPassword()))
221             concept.setPassword(MD5.encode(concept.getPassword()));
222              
223         if(ae.getSource() == btnUpdate)
224         {
225             //if the user does not have any group
226
if(userGroups.getRowCount() == 0)
227             {
228                 addToGroup(concept);
229                 setGroups(concept);
230             }
231             
232             plugin.updateConcept(concept);
233             panel.refresh();
234         }
235         else if(ae.getSource() == btnRemove)
236         {
237             plugin.removeConcept(concept);
238             panel.refresh();
239             panel.showConcept(null);
240         }
241     }
242     
243     private void addToGroup(UserConcept user)
244     {
245         Vector list = new Vector(plugin.getAllGroups(false));
246         if(list.size() > 0)
247         {
248             ListBox box = new ListBox(null, tr("msg.addToGroup"), tr("msg.selectGroup"), list);
249             Object JavaDoc[] groups = box.selectItems();
250             for(int i=0;i<groups.length;i++)
251             {
252                 GroupConcept group = (GroupConcept)groups[i];
253                 group.addUser(user);
254                 plugin.updateConcept(group, false);
255             }
256         }
257     }
258 }
Popular Tags