| 1 package org.ozoneDB.adminGui.widget; 8 9 import java.awt.Rectangle ; 10 import java.awt.event.ActionEvent ; 11 import java.awt.event.ActionListener ; 12 import javax.swing.JButton ; 13 import javax.swing.JLabel ; 14 import javax.swing.JList ; 15 import javax.swing.JPanel ; 16 import javax.swing.JScrollPane ; 17 import java.util.Collections ; 18 import java.util.Iterator ; 19 import java.util.Vector ; 20 21 22 32 34 public class AssignmentPanel extends JPanel { 35 36 37 private Vector startingValues; 38 39 private Vector assignedValues; 40 41 private JList allValuesList = new JList (); 42 43 private JList assignedValuesList = new JList (); 44 45 46 49 private AssignmentPanel() { 50 } 51 52 59 public AssignmentPanel(String tagLabel, Vector startingValues, 60 Vector assignedValues) { 61 try { 62 this.assignedValues = assignedValues; 63 this.startingValues = startingValues; 64 this.init(tagLabel); 65 } catch (Exception e) { 67 e.printStackTrace(); 68 } 69 } 70 71 76 private void init(String tagLabel) throws Exception { 77 this.setLayout(null); 79 80 JLabel allLabel = new JLabel (tagLabel); 82 allLabel.setBounds(new Rectangle (15, 10, 41, 17)); 83 JLabel assignedLabel = new JLabel ("Assigned"); 84 assignedLabel.setBounds(new Rectangle (226, 10, 96, 17)); 85 86 JButton assignOneButton = new JButton (">"); 88 assignOneButton.setBounds(new Rectangle (167, 31, 49, 27)); 89 assignOneButton.addActionListener(new ActionListener () { 90 public void actionPerformed(ActionEvent e) { 91 assignValues(); 92 } 93 }); 94 95 JButton assignAllButton = new JButton (">>"); 97 assignAllButton.setBounds(new Rectangle (167, 63, 49, 27)); 98 assignAllButton.addActionListener(new ActionListener () { 99 public void actionPerformed(ActionEvent e) { 100 assignAllValues(); 101 } 102 }); 103 104 JButton removeOneButton = new JButton ("<"); 106 removeOneButton.setBounds(new Rectangle (167, 111, 49, 27)); 107 removeOneButton.addActionListener(new ActionListener () { 108 public void actionPerformed(ActionEvent e) { 109 removeValues(); 110 } 111 }); 112 113 JButton removeAllButton = new JButton ("<<"); 115 removeAllButton.setBounds(new Rectangle (167, 146, 49, 27)); 116 removeAllButton.addActionListener(new ActionListener () { 117 public void actionPerformed(ActionEvent e) { 118 removeAllValues(); 119 } 120 }); 121 122 JScrollPane assignedValuesScrollPane = new JScrollPane (); 124 assignedValuesScrollPane.setBounds(new Rectangle (226, 27, 143, 148)); 125 assignedValuesScrollPane.getViewport().add(assignedValuesList, null); 126 127 JScrollPane allValuesScrollPane = new JScrollPane (); 128 allValuesScrollPane.setBounds(new Rectangle (15, 27, 143, 148)); 129 allValuesScrollPane.getViewport().add(allValuesList, null); 130 131 this.add(allLabel, null); 133 this.add(assignedLabel, null); 134 this.add(assignOneButton, null); 135 this.add(assignAllButton, null); 136 this.add(removeOneButton, null); 137 this.add(removeAllButton, null); 138 this.add(allValuesScrollPane, null); 139 this.add(assignedValuesScrollPane, null); 140 141 populateLists(); 143 } 144 145 148 public void populateLists() { 149 Vector values = startingValues; 150 151 if (assignedValues == null) 153 assignedValues = new Vector (); 154 155 Collections.sort(assignedValues); 157 assignedValuesList.setListData(assignedValues); 158 159 if (values != null) { 162 Iterator it = assignedValues.iterator(); 164 165 while (it.hasNext()) { 166 Object value = it.next(); 167 168 if (values.contains(value)) 169 values.removeElement(value); 170 } 171 172 Collections.sort(values); 174 allValuesList.setListData(values); 175 } 176 } 177 178 182 private void assignValues() { 183 if (!allValuesList.isSelectionEmpty()) { 185 Object [] value = allValuesList.getSelectedValues(); 187 188 Vector values = this.addToVector(assignedValuesList, value); 190 assignedValuesList.removeAll(); 191 assignedValuesList.setListData(values); 192 assignedValues = values; 193 194 Vector allUsers = this.removeFromVector(allValuesList, value); 196 allValuesList.removeAll(); 197 allValuesList.setListData(allUsers); 198 } 199 } 200 201 205 private void assignAllValues() { 206 Vector values = assignedValues; 208 209 for (int i = 0; i < allValuesList.getModel().getSize(); i++) { 211 values.addElement(allValuesList.getModel().getElementAt(i)); 212 } 213 214 Vector nothing = new Vector (); 216 allValuesList.removeAll(); 217 allValuesList.setListData(nothing); 218 219 Collections.sort(values); 221 assignedValuesList.removeAll(); 222 assignedValuesList.setListData(values); 223 assignedValues = values; 224 } 225 226 230 private void removeValues() { 231 if (!assignedValuesList.isSelectionEmpty()) { 233 Object [] value = assignedValuesList.getSelectedValues(); 235 236 Vector allValues = this.addToVector(allValuesList, value); 238 allValuesList.removeAll(); 239 allValuesList.setListData(allValues); 240 241 Vector values = this.removeFromVector(assignedValuesList, value); 243 assignedValuesList.removeAll(); 244 assignedValuesList.setListData(values); 245 assignedValues = values; 246 } 247 } 248 249 253 private void removeAllValues() { 254 Vector values = assignedValues; 256 257 Vector nothing = new Vector (); 259 assignedValuesList.removeAll(); 260 assignedValuesList.setListData(nothing); 261 assignedValues = nothing; 262 263 for (int i = 0; i < allValuesList.getModel().getSize(); i++) { 266 values.addElement(allValuesList.getModel().getElementAt(i)); 267 } 268 269 Collections.sort(values); 270 allValuesList.removeAll(); 271 allValuesList.setListData(values); 272 } 273 274 282 private Vector addToVector(JList list, Object [] value) { 283 Vector values = new Vector (); 284 285 if (value != null) { 287 for (int i = 0; i < value.length; i++) { 288 values.addElement(value[i]); 289 } 290 } 291 292 if (list != null) { 294 for (int j = 0; j < list.getModel().getSize(); j++) { 295 values.addElement(list.getModel().getElementAt(j)); 296 } 297 } 298 299 Collections.sort(values); 301 return values; 302 } 303 304 312 private Vector removeFromVector(JList list, Object [] value) { 313 Vector values = new Vector (); 314 315 if (value != null) { 317 318 } 319 320 for (int i = 0; i < list.getModel().getSize(); i++) { 322 323 int loopCount = 0; 324 boolean found = false; 325 String listItem = (String ) (list.getModel().getElementAt(i)); 326 327 while (!found && loopCount < value.length) { 328 329 if (value[loopCount].equals(listItem)) { 330 found = true; 331 } 332 loopCount++; 333 } 334 335 if (!found) { 337 values.addElement(listItem); 338 } 339 } 340 341 Collections.sort(values); 343 return values; 344 } 345 346 351 public Vector getAssignedValues() { 352 return this.assignedValues; 353 } 354 355 } | Popular Tags |