KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jimm > datavision > gui > parameter > ListStringInq


1 package jimm.datavision.gui.parameter;
2 import jimm.datavision.Parameter;
3 import java.util.Iterator JavaDoc;
4 import javax.swing.*;
5
6 /**
7  * A single-choice string list inquisitor knows how to display and control
8  * the widgets needed to ask a user for a single string parameter value from
9  * a list.
10  *
11  * @author Jim Menard, <a HREF="mailto:jimm@io.com">jimm@io.com</a>
12  */

13 class ListStringInq extends Inquisitor {
14
15 protected JList list;
16
17 ListStringInq(Parameter param, boolean allowMultipleSelection) {
18     super(param);
19
20     // Build GUI
21
Box box = Box.createVerticalBox();
22
23     DefaultListModel model = new DefaultListModel();
24     list = new JList(model);
25     list.setSelectionMode(allowMultipleSelection
26               ? ListSelectionModel.MULTIPLE_INTERVAL_SELECTION
27               : ListSelectionModel.SINGLE_SELECTION);
28     box.add(list);
29
30     panel.add(box);
31
32     // Copy default value into "real" value
33
for (Iterator JavaDoc iter = parameter.defaultValues(); iter.hasNext(); )
34     model.addElement(iter.next());
35
36     if (!allowMultipleSelection)
37     list.setSelectedIndex(0); // Select first item
38
}
39
40 void copyGUIIntoParam() {
41     int[] is = list.getSelectedIndices();
42     parameter.removeValues();
43     for (int i = 0; i < is.length; ++i)
44     parameter.setValue(i, parameter.getDefaultValue(is[i]));
45 }
46
47 void copyParamIntoGUI() {
48     list.clearSelection();
49     boolean first = true;
50     for (Iterator JavaDoc iter = parameter.values(); iter.hasNext(); ) {
51     list.setSelectedValue(iter.next(), first); // Scroll if first
52
first = false;
53     }
54 }
55
56 }
57
Popular Tags