KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > adminGui > widget > AssignmentPanel


1 //You can redistribute this software and/or modify it under the terms of
2
//the Ozone Library License version 1 published by ozone-db.org.
3
//
4
//The original code and portions created by SMB are
5
//Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
6
//
7
package org.ozoneDB.adminGui.widget;
8
9 import java.awt.Rectangle JavaDoc;
10 import java.awt.event.ActionEvent JavaDoc;
11 import java.awt.event.ActionListener JavaDoc;
12 import javax.swing.JButton JavaDoc;
13 import javax.swing.JLabel JavaDoc;
14 import javax.swing.JList JavaDoc;
15 import javax.swing.JPanel JavaDoc;
16 import javax.swing.JScrollPane JavaDoc;
17 import java.util.Collections JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.Vector JavaDoc;
20
21
22 //#############################################################################
23
/**
24  * This class creates an assignment panel which is used to assign/remove
25  * values from one list into another.
26  *
27  * @author <p align=center>Ibsen Ramos-Bonilla
28  * <br>Copyright &copy 1997-@year@ by SMB GmbH. All Rights Reserved.</p>
29  *
30  * @version 1.0
31  */

32 //#############################################################################
33

34 public class AssignmentPanel extends JPanel JavaDoc {
35
36     /** The list of starting values. */
37     private Vector JavaDoc startingValues;
38     /** The list of currently assigned values. */
39     private Vector JavaDoc assignedValues;
40     /** List holding all the values. */
41     private JList JavaDoc allValuesList = new JList JavaDoc();
42     /** List holding the assigned values. */
43     private JList JavaDoc assignedValuesList = new JList JavaDoc();
44
45
46     /**
47      * Default constructor not used.
48      */

49     private AssignmentPanel() {
50     }
51
52     /**
53      * Overloaded constructor sets the panel tag and gets a list of values.
54      *
55      * @param tagLabel - the panel tag label.
56      * @param startingValues - the values to display for assigment.
57      * @param assignedValues - the values assigned
58      */

59     public AssignmentPanel(String JavaDoc tagLabel, Vector JavaDoc startingValues,
60                            Vector JavaDoc assignedValues) {
61         try {
62             this.assignedValues = assignedValues;
63             this.startingValues = startingValues;
64             this.init(tagLabel);
65             //this.setSize(390, 290);
66
} catch (Exception JavaDoc e) {
67             e.printStackTrace();
68         }
69     }
70
71     /**
72      * This method initializes the dialog.
73      *
74      * @param tagLabel - the panel tag label.
75      */

76     private void init(String JavaDoc tagLabel) throws Exception JavaDoc {
77         //set the panel attributes
78
this.setLayout(null);
79
80         //set label attributes
81
JLabel JavaDoc allLabel = new JLabel JavaDoc(tagLabel);
82         allLabel.setBounds(new Rectangle JavaDoc(15, 10, 41, 17));
83         JLabel JavaDoc assignedLabel = new JLabel JavaDoc("Assigned");
84         assignedLabel.setBounds(new Rectangle JavaDoc(226, 10, 96, 17));
85
86         //button to assign one value
87
JButton JavaDoc assignOneButton = new JButton JavaDoc(">");
88         assignOneButton.setBounds(new Rectangle JavaDoc(167, 31, 49, 27));
89         assignOneButton.addActionListener(new ActionListener JavaDoc() {
90             public void actionPerformed(ActionEvent JavaDoc e) {
91                 assignValues();
92             }
93         });
94
95         //button to assign multiple values
96
JButton JavaDoc assignAllButton = new JButton JavaDoc(">>");
97         assignAllButton.setBounds(new Rectangle JavaDoc(167, 63, 49, 27));
98         assignAllButton.addActionListener(new ActionListener JavaDoc() {
99             public void actionPerformed(ActionEvent JavaDoc e) {
100                 assignAllValues();
101             }
102         });
103
104         //button to remove an assigned value
105
JButton JavaDoc removeOneButton = new JButton JavaDoc("<");
106         removeOneButton.setBounds(new Rectangle JavaDoc(167, 111, 49, 27));
107         removeOneButton.addActionListener(new ActionListener JavaDoc() {
108             public void actionPerformed(ActionEvent JavaDoc e) {
109                 removeValues();
110             }
111         });
112
113         //button to remove all assigned values
114
JButton JavaDoc removeAllButton = new JButton JavaDoc("<<");
115         removeAllButton.setBounds(new Rectangle JavaDoc(167, 146, 49, 27));
116         removeAllButton.addActionListener(new ActionListener JavaDoc() {
117             public void actionPerformed(ActionEvent JavaDoc e) {
118                 removeAllValues();
119             }
120         });
121
122         //scroll panels
123
JScrollPane JavaDoc assignedValuesScrollPane = new JScrollPane JavaDoc();
124         assignedValuesScrollPane.setBounds(new Rectangle JavaDoc(226, 27, 143, 148));
125         assignedValuesScrollPane.getViewport().add(assignedValuesList, null);
126
127         JScrollPane JavaDoc allValuesScrollPane = new JScrollPane JavaDoc();
128         allValuesScrollPane.setBounds(new Rectangle JavaDoc(15, 27, 143, 148));
129         allValuesScrollPane.getViewport().add(allValuesList, null);
130
131         //add components to the dialog
132
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         //populate the values list
142
populateLists();
143     }
144
145     /**
146      * This method loads all the values into the "all" list.
147      */

148     public void populateLists() {
149         Vector JavaDoc values = startingValues;
150
151         //check if the assigned values is empty
152
if (assignedValues == null)
153             assignedValues = new Vector JavaDoc();
154
155         //get the list of assigned values
156
Collections.sort(assignedValues);
157         assignedValuesList.setListData(assignedValues);
158
159         //remove the values from the "all" list that are in the
160
//"assigned" list.
161
if (values != null) {
162             //remove the values that are in the "assigned" list
163
Iterator JavaDoc it = assignedValues.iterator();
164
165             while (it.hasNext()) {
166                 Object JavaDoc value = it.next();
167
168                 if (values.contains(value))
169                     values.removeElement(value);
170             }
171
172             //sort the list and put it in the "all" list
173
Collections.sort(values);
174             allValuesList.setListData(values);
175         }
176     }
177
178     /**
179      * This method removes the selected values from the "all" list and places
180      * them in the "assigned" list.
181      */

182     private void assignValues() {
183         //make sure something is selected in the all value list
184
if (!allValuesList.isSelectionEmpty()) {
185             //get the selected values
186
Object JavaDoc[] value = allValuesList.getSelectedValues();
187
188             //add it to the assigned list
189
Vector JavaDoc values = this.addToVector(assignedValuesList, value);
190             assignedValuesList.removeAll();
191             assignedValuesList.setListData(values);
192             assignedValues = values;
193
194             //remove the selected values from the all value list
195
Vector JavaDoc allUsers = this.removeFromVector(allValuesList, value);
196             allValuesList.removeAll();
197             allValuesList.setListData(allUsers);
198         }
199     }
200
201     /**
202      * This method removes all values in the "all" list and adds them to the
203      * "assigned" list.
204      */

205     private void assignAllValues() {
206         //get all assigned values
207
Vector JavaDoc values = assignedValues;
208
209         //add everything in "all" to the values vector
210
for (int i = 0; i < allValuesList.getModel().getSize(); i++) {
211             values.addElement(allValuesList.getModel().getElementAt(i));
212         }
213
214         //clear the "all" list
215
Vector JavaDoc nothing = new Vector JavaDoc();
216         allValuesList.removeAll();
217         allValuesList.setListData(nothing);
218
219         //sort the vector and set the "assigned" data to all values
220
Collections.sort(values);
221         assignedValuesList.removeAll();
222         assignedValuesList.setListData(values);
223         assignedValues = values;
224     }
225
226     /**
227      * This method removes values from the "assigned" list and puts them in the
228      * "all" list.
229      */

230     private void removeValues() {
231         //make sure something is selected in the assigned value list
232
if (!assignedValuesList.isSelectionEmpty()) {
233             //get the selected values
234
Object JavaDoc[] value = assignedValuesList.getSelectedValues();
235
236             //add it to the all list
237
Vector JavaDoc allValues = this.addToVector(allValuesList, value);
238             allValuesList.removeAll();
239             allValuesList.setListData(allValues);
240
241             //remove the selected values from the assigned value list
242
Vector JavaDoc values = this.removeFromVector(assignedValuesList, value);
243             assignedValuesList.removeAll();
244             assignedValuesList.setListData(values);
245             assignedValues = values;
246         }
247     }
248
249     /**
250      * This method removes all values in the "assigned" list and adds them to
251      * the "all" list.
252      */

253     private void removeAllValues() {
254         //get all assigned values
255
Vector JavaDoc values = assignedValues;
256
257         //clear the "assigned" list
258
Vector JavaDoc nothing = new Vector JavaDoc();
259         assignedValuesList.removeAll();
260         assignedValuesList.setListData(nothing);
261         assignedValues = nothing;
262
263         //get all the items in the "all users" list and add them to the items
264
//removed from the assigned users list.
265
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     /**
275      * This method returns a vector created from the elements in the given
276      * list, adds the given string to the vector, and sorts the results.
277      *
278      * @param list - list of values.
279      * @param value - an array of values.
280      * @return Vector - the created vector after merging the list and array.
281      */

282     private Vector JavaDoc addToVector(JList JavaDoc list, Object JavaDoc[] value) {
283         Vector JavaDoc values = new Vector JavaDoc();
284
285         //first go ahead and add the values
286
if (value != null) {
287             for (int i = 0; i < value.length; i++) {
288                 values.addElement(value[i]);
289             }
290         }
291
292         //next go ahead and add the list entries
293
if (list != null) {
294             for (int j = 0; j < list.getModel().getSize(); j++) {
295                 values.addElement(list.getModel().getElementAt(j));
296             }
297         }
298
299         //sort and return
300
Collections.sort(values);
301         return values;
302     }
303
304     /**
305      * This method returns a vector created from the elements in the given
306      * list, removes the given string from the vector, and sorts the results.
307      *
308      * @param list - list of values.
309      * @param value - an array of values.
310      * @return Vector - the created vector after merging the list and array.
311      */

312     private Vector JavaDoc removeFromVector(JList JavaDoc list, Object JavaDoc[] value) {
313         Vector JavaDoc values = new Vector JavaDoc();
314
315         //first go ahead and remove the values
316
if (value != null) {
317
318         }
319
320         //add all the elements in the list to the vector EXCEPT for "item"
321
for (int i = 0; i < list.getModel().getSize(); i++) {
322
323             int loopCount = 0;
324             boolean found = false;
325             String JavaDoc listItem = (String JavaDoc) (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             //add the element if not in both lists
336
if (!found) {
337                 values.addElement(listItem);
338             }
339         }
340
341         //sort and return
342
Collections.sort(values);
343         return values;
344     }
345
346     /**
347      * This method returns the currently selected values.
348      *
349      * @return Vector - list of assigned values.
350      */

351     public Vector JavaDoc getAssignedValues() {
352         return this.assignedValues;
353     }
354
355 } //--------------------------------- E O F -----------------------------------
356
Popular Tags