KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > joram > client > tools > admin > ACLPanel


1 /*
2  * Created on Oct 25, 2003
3  *
4  * To change the template for this generated file go to
5  * Window>Preferences>Java>Code Generation>Code and Comments
6  */

7 /*
8  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23  * USA.
24  *
25  * Initial developer(s): Alexander Fedorowicz
26  * Contributor(s):
27  */

28 package org.objectweb.joram.client.tools.admin;
29
30 import java.util.*;
31 import java.awt.*;
32 import java.awt.event.ActionEvent JavaDoc;
33 import java.awt.event.ActionListener JavaDoc;
34 import javax.swing.*;
35 import javax.swing.event.ListSelectionEvent JavaDoc;
36 import javax.swing.event.ListSelectionListener JavaDoc;
37
38 import org.objectweb.joram.client.jms.admin.*;
39
40
41 /**
42  * @author afedoro
43  *
44  * To change the template for this generated type comment go to
45  * Window>Preferences>Java>Code Generation>Code and Comments
46  */

47 public class ACLPanel extends JPanel {
48     private static final Dimension LIST_DIMENSION = new Dimension(120, 100);
49
50     private JList userList = new JList();
51     private DefaultListModel userListModel = new DefaultListModel();
52     private JList accessList = new JList();
53     private DefaultListModel accessListModel = new DefaultListModel();
54     private JButton addButton = new JButton("Add >>");
55     private JButton removeButton = new JButton("<< Remove");
56     private java.util.List JavaDoc authorized = null;
57     private java.util.List JavaDoc unauthorized = null;
58
59   /**
60    * Default constructor
61    */

62   public ACLPanel(String JavaDoc title) {
63     super();
64     setLayout(new BorderLayout());
65
66         JLabel titleLabel = new JLabel(" " + title, AdminToolConstants.lockIcon, SwingConstants.LEFT);
67         add(titleLabel, BorderLayout.NORTH);
68         userList.setModel(userListModel);
69         userList.addListSelectionListener(new UnauthorizedSelectionListener());
70         JScrollPane userScrollList = new JScrollPane(userList);
71         userScrollList.setPreferredSize(LIST_DIMENSION);
72         add(userScrollList, BorderLayout.WEST);
73         Box aclButtonBox = Box.createVerticalBox();
74         addButton.setEnabled(false);
75         addButton.setAlignmentX(Component.CENTER_ALIGNMENT);
76         addButton.addActionListener(new AddActionListener());
77         aclButtonBox.add(addButton);
78         aclButtonBox.add(Box.createVerticalStrut(50));
79         removeButton.setEnabled(false);
80         removeButton.setAlignmentX(Component.CENTER_ALIGNMENT);
81         removeButton.addActionListener(new RemoveActionListener());
82         aclButtonBox.add(removeButton);
83         add(aclButtonBox, BorderLayout.CENTER);
84         accessList.setModel(accessListModel);
85         accessList.addListSelectionListener(new AuthorizedSelectionListener());
86         JScrollPane accessScrollList = new JScrollPane(accessList);
87         accessScrollList.setPreferredSize(LIST_DIMENSION);
88         add(accessScrollList, BorderLayout.EAST);
89   }
90
91     public void setupLists(java.util.List JavaDoc users, java.util.List JavaDoc auth) {
92         authorized = auth;
93         unauthorized = new ArrayList();
94         userListModel.removeAllElements();
95         accessListModel.removeAllElements();
96         for (int i = 0; i < auth.size(); i++)
97             accessListModel.addElement(getUserName((User) auth.get(i)));
98         for (int i = 0; i < users.size(); i++) {
99             String JavaDoc name = getUserName((User) users.get(i));
100             if (!accessListModel.contains(name)) {
101                 userListModel.addElement(name);
102                 unauthorized.add(users.get(i));
103             }
104         }
105         addButton.setEnabled(false);
106     }
107
108     public java.util.List JavaDoc getNewlyAuthorizedUsers() {
109         java.util.List JavaDoc users = new ArrayList();
110
111         for (Enumeration e = accessListModel.elements(); e.hasMoreElements();) {
112             String JavaDoc id = (String JavaDoc) e.nextElement();
113             if (findUser(authorized, id) == null)
114                 users.add(findUser(unauthorized, id));
115         }
116
117         return users;
118     }
119
120     public java.util.List JavaDoc getNewlyUnauthorizedUsers() {
121         java.util.List JavaDoc users = new ArrayList();
122
123         for (Enumeration e = userListModel.elements(); e.hasMoreElements();) {
124             String JavaDoc id = (String JavaDoc) e.nextElement();
125             if (findUser(unauthorized, id) == null)
126                 users.add(findUser(authorized, id));
127         }
128
129         return users;
130     }
131
132     private User findUser(java.util.List JavaDoc list, String JavaDoc id) {
133         for (Iterator i = list.iterator(); i.hasNext();) {
134             User user = (User) i.next();
135             if (user.code().get("name").equals(id))
136                 return user;
137         }
138
139         return null;
140     }
141
142     private String JavaDoc getUserName(User user) {
143         Map code = user.code();
144         if (code == null)
145             return null;
146         return (String JavaDoc) code.get("name");
147     }
148
149     private class AddActionListener implements ActionListener JavaDoc {
150         public void actionPerformed(ActionEvent JavaDoc e) {
151             int[] sel = userList.getSelectedIndices();
152
153             if (sel.length == 0)
154                 return;
155
156             for (int i = sel.length - 1; i >= 0; i--)
157                 accessListModel.addElement(userListModel.remove(sel[i]));
158
159             userList.clearSelection();
160             addButton.setEnabled(false);
161         }
162     }
163
164     private class RemoveActionListener implements ActionListener JavaDoc {
165         public void actionPerformed(ActionEvent JavaDoc e) {
166             int[] sel = accessList.getSelectedIndices();
167
168             if (sel.length == 0)
169                 return;
170
171             for (int i = sel.length - 1; i >= 0; i--)
172                 userListModel.addElement(accessListModel.remove(sel[i]));
173
174             accessList.clearSelection();
175             removeButton.setEnabled(false);
176         }
177     }
178
179     private class UnauthorizedSelectionListener implements ListSelectionListener JavaDoc {
180         public void valueChanged(ListSelectionEvent JavaDoc e) {
181             if (!userList.isSelectionEmpty()) {
182                 addButton.setEnabled(true);
183                 removeButton.setEnabled(false);
184                 accessList.clearSelection();
185             }
186         }
187     }
188
189     private class AuthorizedSelectionListener implements ListSelectionListener JavaDoc {
190         public void valueChanged(ListSelectionEvent JavaDoc e) {
191             if (!accessList.isSelectionEmpty()) {
192                 addButton.setEnabled(false);
193                 removeButton.setEnabled(true);
194                 userList.clearSelection();
195             }
196         }
197     }
198 }
199
Popular Tags