1 |
| package edu.rice.cs.util.swing; |
2 |
| |
3 |
| import java.awt.*; |
4 |
| import javax.swing.*; |
5 |
| import javax.swing.event.*; |
6 |
| import java.util.*; |
7 |
| |
8 |
| |
9 |
| public class CheckBoxJList extends JList implements ListSelectionListener { |
10 |
| static Color listForeground; |
11 |
| static Color listBackground; |
12 |
| static Color listSelectionForeground; |
13 |
| static Color listSelectionBackground; |
14 |
| |
15 |
| static { |
16 |
0
| UIDefaults uid = UIManager.getLookAndFeel().getDefaults();
|
17 |
0
| listForeground = uid.getColor("List.foreground");
|
18 |
0
| listBackground = uid.getColor("List.background");
|
19 |
0
| listSelectionForeground = uid.getColor("List.selectionForeground");
|
20 |
0
| listSelectionBackground = uid.getColor("List.selectionBackground");
|
21 |
| } |
22 |
| |
23 |
| HashSet<Integer> selectionCache = new HashSet<Integer>(); |
24 |
| |
25 |
0
| protected void init(Vector<?> listData, Vector<?> selData) {
|
26 |
0
| setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
|
27 |
0
| CheckBoxListCellRenderer r = new CheckBoxListCellRenderer();
|
28 |
0
| if (selData != null) {
|
29 |
0
| int i = 0;
|
30 |
0
| for(Object t: listData) {
|
31 |
0
| boolean sel = false;
|
32 |
0
| if (selData.contains(t)) {
|
33 |
0
| selectionCache.add(i);
|
34 |
0
| getSelectionModel().addSelectionInterval(i, i);
|
35 |
0
| sel = true;
|
36 |
| } |
37 |
0
| r.getListCellRendererComponent(this,
|
38 |
| listData.get(i), |
39 |
| i, |
40 |
| sel, |
41 |
| (i == 0)); |
42 |
0
| ++i;
|
43 |
| } |
44 |
| } |
45 |
0
| setCellRenderer(r);
|
46 |
0
| addListSelectionListener(this);
|
47 |
| } |
48 |
| |
49 |
0
| public CheckBoxJList(Vector<?> listData) {
|
50 |
0
| super(listData);
|
51 |
0
| init(listData, null);
|
52 |
| } |
53 |
| |
54 |
0
| public CheckBoxJList(Vector<?> listData, Vector<?> selData) {
|
55 |
0
| super(listData);
|
56 |
0
| init(listData, selData);
|
57 |
| } |
58 |
| |
59 |
0
| @SuppressWarnings({"unchecked"})
|
60 |
| public CheckBoxJList(ListModel lm) { |
61 |
0
| super(lm);
|
62 |
0
| Vector<Object> listData = new Vector<Object>();
|
63 |
0
| for(int i = 0; i < lm.getSize(); ++i) listData.add(lm.getElementAt(i));
|
64 |
0
| init(listData, new Vector<Object>());
|
65 |
| } |
66 |
| |
67 |
| |
68 |
0
| public void valueChanged(ListSelectionEvent lse) {
|
69 |
0
| if (!lse.getValueIsAdjusting()) {
|
70 |
0
| removeListSelectionListener(this);
|
71 |
| |
72 |
| |
73 |
0
| HashSet<Integer> newSelections = new HashSet<Integer>();
|
74 |
0
| int size = getModel().getSize();
|
75 |
0
| for (int i = 0; i < size; i++) {
|
76 |
0
| if (getSelectionModel().isSelectedIndex(i)) {
|
77 |
0
| newSelections.add(i);
|
78 |
| } |
79 |
| } |
80 |
| |
81 |
| |
82 |
0
| Iterator<Integer> it = selectionCache.iterator();
|
83 |
0
| while (it.hasNext()) {
|
84 |
0
| int index = it.next();
|
85 |
0
| getSelectionModel().addSelectionInterval(index, index);
|
86 |
| } |
87 |
| |
88 |
| |
89 |
0
| it = newSelections.iterator();
|
90 |
0
| while (it.hasNext()) {
|
91 |
0
| Integer nextInt = it.next();
|
92 |
0
| if (selectionCache.contains(nextInt)) {
|
93 |
0
| getSelectionModel().removeSelectionInterval(nextInt, nextInt);
|
94 |
| } |
95 |
| else { |
96 |
0
| getSelectionModel().addSelectionInterval(nextInt, nextInt);
|
97 |
| } |
98 |
| } |
99 |
| |
100 |
| |
101 |
0
| selectionCache.clear();
|
102 |
0
| for (int i = 0; i < size; i++) {
|
103 |
0
| if (getSelectionModel().isSelectedIndex(i)) {
|
104 |
0
| selectionCache.add(i);
|
105 |
| } |
106 |
| } |
107 |
0
| addListSelectionListener(this);
|
108 |
| } |
109 |
| } |
110 |
| |
111 |
| private static class CheckBoxListCellRenderer extends JComponent implements ListCellRenderer { |
112 |
| DefaultListCellRenderer defaultComp; |
113 |
| JCheckBox checkbox; |
114 |
0
| public CheckBoxListCellRenderer() {
|
115 |
0
| setLayout(new BorderLayout());
|
116 |
0
| defaultComp = new DefaultListCellRenderer();
|
117 |
0
| checkbox = new JCheckBox();
|
118 |
0
| add(checkbox, BorderLayout.WEST);
|
119 |
0
| add(defaultComp, BorderLayout.CENTER);
|
120 |
| } |
121 |
| |
122 |
0
| public Component getListCellRendererComponent(JList list,
|
123 |
| Object value, |
124 |
| int index, |
125 |
| boolean isSelected, |
126 |
| boolean cellHasFocus){ |
127 |
0
| defaultComp.getListCellRendererComponent (list, value, index,
|
128 |
| isSelected, cellHasFocus); |
129 |
0
| checkbox.setSelected(isSelected);
|
130 |
0
| Component[] comps = getComponents();
|
131 |
0
| for (int i = 0; i < comps.length; ++i) {
|
132 |
0
| comps[i].setForeground(listForeground);
|
133 |
0
| comps[i].setBackground(listBackground);
|
134 |
| } |
135 |
0
| return this;
|
136 |
| } |
137 |
| } |
138 |
| } |