1 7 28 package org.objectweb.joram.client.tools.admin; 29 30 import java.util.*; 31 import java.awt.*; 32 import java.awt.event.ActionEvent ; 33 import java.awt.event.ActionListener ; 34 import javax.swing.*; 35 import javax.swing.event.ListSelectionEvent ; 36 import javax.swing.event.ListSelectionListener ; 37 38 import org.objectweb.joram.client.jms.admin.*; 39 40 41 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 authorized = null; 57 private java.util.List unauthorized = null; 58 59 62 public ACLPanel(String 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 users, java.util.List 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 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 getNewlyAuthorizedUsers() { 109 java.util.List users = new ArrayList(); 110 111 for (Enumeration e = accessListModel.elements(); e.hasMoreElements();) { 112 String id = (String ) 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 getNewlyUnauthorizedUsers() { 121 java.util.List users = new ArrayList(); 122 123 for (Enumeration e = userListModel.elements(); e.hasMoreElements();) { 124 String id = (String ) 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 list, String 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 getUserName(User user) { 143 Map code = user.code(); 144 if (code == null) 145 return null; 146 return (String ) code.get("name"); 147 } 148 149 private class AddActionListener implements ActionListener { 150 public void actionPerformed(ActionEvent 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 { 165 public void actionPerformed(ActionEvent 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 { 180 public void valueChanged(ListSelectionEvent 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 { 190 public void valueChanged(ListSelectionEvent e) { 191 if (!accessList.isSelectionEmpty()) { 192 addButton.setEnabled(false); 193 removeButton.setEnabled(true); 194 userList.clearSelection(); 195 } 196 } 197 } 198 } 199 | Popular Tags |