KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ca > commons > cbutil > CBListSelector


1 package com.ca.commons.cbutil;
2
3 import javax.swing.*;
4 import java.awt.*;
5 import java.awt.event.*;
6 import java.util.ArrayList JavaDoc;
7 import java.util.logging.Level JavaDoc;
8 import java.util.logging.Logger JavaDoc;
9
10
11 /**
12  * This class acts as a item selector. It sets up a dialog that lets you select items
13  * from one list and add them to another list. It also allows you to remove selected
14  * items from the list.
15  *
16  * @author Trudi.
17  */

18
19 public class CBListSelector extends CBDialog
20 {
21     JList availableList, selectedList;
22     JScrollPane availableScrollPane, selectedScrollPane;
23     CBButton btnAdd, btnRemove;
24     JLabel availableLabel, selectedLabel;
25     CBPanel leftPanel, middlePanel, rightPanel;
26     ArrayList JavaDoc arrayList = new ArrayList JavaDoc();
27
28     private static Logger JavaDoc log = Logger.getLogger(CBListSelector.class.getName());
29
30     /**
31      * Static method that should be used rather than creating an object directly if
32      * you wish to get the user input after the user has finished making selections and
33      * has closed the window.
34      * <p/>
35      * This method creates a CBListSelector object and calls its 'show' method. When the
36      * user is finished with the dialog this method returns the user's selections.
37      *
38      * @param owner the parent frame (usally JXplorer).
39      * @param list the strings that are to appear in the list on the left of the dialog.
40      * @param title the words that will appear in the title bar of the dialog.
41      * @param helpID the help ID to attach to the Help button.
42      * @return a list of user selections.
43      */

44
45     public static ArrayList JavaDoc getSelectedValues(Frame owner, String JavaDoc[] list, String JavaDoc title, String JavaDoc helpID)
46     {
47         CBListSelector listSelector = new CBListSelector(owner, list, title, helpID);
48
49         listSelector.setVisible(true);
50
51         return listSelector.getSelectedValues();
52     }
53
54
55     /**
56      * Sets up a dialog with two lists. The list on the left displays all available values
57      * that the user can select from. When the user selects a value it is displayed in the
58      * list on the right either by double clicking on the value or clicking the '>>' button.
59      * The user can also remove a value from the selection list either by double clicking on
60      * it or clicking the '<<' button.
61      *
62      * @param owner the parent frame (usally JXplorer).
63      * @param list the strings that are to appear in the list on the left of the dialog.
64      * @param title the words that will appear in the title bar of the dialog.
65      * @param helpID the help ID to attach to the Help button.
66      */

67
68     public CBListSelector(Frame owner, String JavaDoc[] list, String JavaDoc title, String JavaDoc helpID)
69     {
70         super(owner, CBIntText.get(title), helpID);
71
72         leftPanel = new CBPanel();
73         middlePanel = new CBPanel();
74         rightPanel = new CBPanel();
75
76         //TE: left panel...
77

78         leftPanel.addln(availableLabel = new JLabel(CBIntText.get("Available Attributes:")));
79         leftPanel.makeHeavy();
80
81         availableList = new JList(list);
82         availableList.setSelectionMode(0); //TE: only one item can be selected at anyone time.
83
availableList.setSelectionModel(new CBSingleSelectionModel(availableList)); //TE: tries to ensure that the selected item is visible.
84

85         leftPanel.addln(availableScrollPane = new JScrollPane(availableList));
86
87         //TE: middle panel...
88

89         btnAdd = new CBButton(CBIntText.get(">>"), CBIntText.get("Add an attribute from the attribute list on the left to the selection list on the right."));
90         btnAdd.addActionListener(new ActionListener()
91         {
92             public void actionPerformed(ActionEvent e)
93             {
94                 add();
95             }
96         });
97
98         btnRemove = new CBButton(CBIntText.get("<<"), CBIntText.get("Remove an attribute from the selection list on the right."));
99         btnRemove.addActionListener(new ActionListener()
100         {
101             public void actionPerformed(ActionEvent e)
102             {
103                 remove();
104             }
105         });
106
107         middlePanel.makeHeavy();
108         middlePanel.addln(new JLabel(" ")); //TE: top padding.
109
middlePanel.makeLight();
110         middlePanel.addln(btnAdd);
111         middlePanel.addln(btnRemove);
112         middlePanel.makeHeavy();
113         middlePanel.addln(new JLabel(" ")); //TE: bottom padding.
114

115         //TE: right panel...
116

117         rightPanel.addln(selectedLabel = new JLabel(CBIntText.get("Selected Attributes:")));
118         rightPanel.makeHeavy();
119
120         selectedList = new JList();
121         selectedList.setSelectionMode(0); //TE: only one item can be selected at anyone time.
122
selectedList.setSelectionModel(new CBSingleSelectionModel(selectedList)); //TE: tries to ensure that the selected item is visible.
123

124         rightPanel.addln(selectedScrollPane = new JScrollPane(selectedList));
125
126         //TE: main panel...
127

128         display.addln(new JLabel(" ")); //TE: padding.
129
display.makeHeavy();
130         display.add(leftPanel);
131         display.makeLight();
132         display.add(middlePanel);
133         display.makeHeavy();
134         display.add(rightPanel);
135
136         setSize(400, 300);
137         CBUtility.center(this, owner);
138
139         registerMouseListeners();
140     }
141
142
143     /**
144      * Adds a double click mouse listener to both lists in this dialog.
145      * If the double click occurs in the list on the left, the 'add' method is
146      * called. If the double click occurs in the list on the right, the
147      * remove method is called.
148      */

149
150     protected void registerMouseListeners()
151     {
152         availableList.addMouseListener(new MouseAdapter()
153         {
154             public void mouseClicked(MouseEvent e)
155             {
156                 if (e.getClickCount() == 2)
157                     add();
158             }
159         });
160
161         selectedList.addMouseListener(new MouseAdapter()
162         {
163             public void mouseClicked(MouseEvent e)
164             {
165                 if (e.getClickCount() == 2)
166                     remove();
167             }
168         });
169     }
170
171
172     /**
173      * Gets the selected item from the list on the left and adds it to
174      * the list on the right. It uses a global array list to keep track
175      * of the selections in the right hand list.
176      */

177
178     public void add()
179     {
180         try
181         {
182             if (!arrayList.contains(availableList.getSelectedValue()))
183                 arrayList.add(availableList.getSelectedValue());
184
185             selectedList.setListData(arrayList.toArray());
186         }
187         catch (Exception JavaDoc e)
188         {
189             log.log(Level.FINER, "No selection to add.", e);
190         }
191     }
192
193
194     /**
195      * Removes the selected item from the list on the left. It uses a
196      * global array list to keep track of the selections in the right hand list.
197      */

198
199     public void remove()
200     {
201         try
202         {
203             arrayList.remove(selectedList.getSelectedIndex());
204             selectedList.setListData(arrayList.toArray());
205         }
206         catch (Exception JavaDoc e)
207         {
208             log.log(Level.FINER, "No selection to remove.", e);
209         }
210     }
211
212
213     /**
214      * Returns a list of the values that the user has selected.
215      *
216      * @return a list of the values that the user has selected.
217      */

218
219     public ArrayList JavaDoc getSelectedValues()
220     {
221         if (arrayList.isEmpty())
222             return null;
223         else
224             return arrayList;
225     }
226 }
Popular Tags