1 package org.catacomb.druid.swing;
2
3 import org.catacomb.druid.event.LabelActor;
4 import org.catacomb.interlish.interact.DComponent;
5 import org.catacomb.interlish.structure.Named;
6 import org.catacomb.report.E;
7
8
9 import java.awt.Color;
10 import java.awt.Font;
11 import java.awt.event.MouseEvent;
12 import java.awt.event.MouseListener;
13 import java.util.ArrayList;
14 import java.util.HashMap;
15
16 import javax.swing.DefaultListSelectionModel;
17 import javax.swing.JCheckBox;
18 import javax.swing.JList;
19 import javax.swing.event.ListSelectionEvent;
20 import javax.swing.event.ListSelectionListener;
21
22
23 public class DCheckboxList extends JList implements DComponent, ListSelectionListener,
24 MouseListener {
25 static final long serialVersionUID = 1001;
26
27
28
29 static Font boldfont;
30
31 public LabelActor labelActor;
32
33
34 Object bufSel;
35
36 boolean dropEvents;
37
38 int hotspot = new JCheckBox().getPreferredSize().width;
39
40 ToggleItem[] toggleItems;
41 HashMap<String, ToggleItem> tiHM;
42
43 ToggleItem activeTI;
44
45 boolean multiple;
46
47
48 public DCheckboxList() {
49 super();
50 dropEvents = false;
51 addListSelectionListener(this);
52
53 setCellRenderer(new CheckListCellRenderer(getCellRenderer()));
54 addMouseListener(this);
55
56 tiHM = new HashMap<String, ToggleItem>();
57 }
58
59
60 public void setTooltip(String s) {
61 setToolTipText(s);
62 }
63
64
65 public void setBg(Color c) {
66 setBackground(c);
67 }
68
69 public void setMultiple() {
70 multiple = true;
71 super.setSelectionMode(DefaultListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
72 }
73
74
75
76
77
78 public void setLabelActor(LabelActor la) {
79 labelActor = la;
80 }
81
82
83 public void valueChanged(ListSelectionEvent lse) {
84 if (dropEvents) {
85
86 } else {
87 if (lse.getValueIsAdjusting()) {
88
89 } else {
90 Object obj = getSelectedValue();
91 activeTI = (ToggleItem)obj;
92
93
94
95 if (obj != null && obj != bufSel && labelActor != null) {
96 bufSel = obj;
97 labelActor.labelAction("selected", true);
98
99 }
100 }
101 }
102
103 repaint();
104
105
106
107 }
108
109
110 public void selectAt(int i) {
111 dropEvents = true;
112 setSelectedIndex(i);
113 dropEvents = false;
114 }
115
116
117
118 private void toggleSelection(int index) {
119
120 if (index >= 0) {
121 toggleItems[index].toggle();
122 activeTI = toggleItems[index];
123 repaint();
124
125 if (labelActor != null) {
126 labelActor.labelAction("toggle", toggleItems[index].isOn());
127 }
128
129
130 }
131
132
133 }
134
135
136 public void mouseClicked(MouseEvent me) {
137
138 int x = me.getX();
139
140 int index = locationToIndex(me.getPoint());
141
142 if (x <= 16 && index >= 0 && index < toggleItems.length) {
143 toggleSelection(index);
144 }
145 }
146
147 public void mousePressed(MouseEvent e) {
148 }
149 public void mouseReleased(MouseEvent e) {
150 }
151 public void mouseEntered(MouseEvent e) {
152 }
153 public void mouseExited(MouseEvent e) {
154 }
155
156
157
158 public void setItems(String[] sa) {
159 Object[] oa = new Object[sa.length];
160 for (int i = 0; i < sa.length; i++) {
161 oa[i] = sa[i];
162 }
163 setItems(oa);
164 }
165
166
167
168 private String getLabel(Object obj) {
169 String ret = null;
170 if (obj instanceof Named) {
171 ret = ((Named)obj).getName();
172 } else {
173 ret = obj.toString();
174 }
175 return ret;
176 }
177
178
179 public void setItems(Object[] oa) {
180 ToggleItem[] ati = new ToggleItem[oa.length];
181 for (int i = 0; i < oa.length; i++) {
182 String key = getLabel(oa[i]);
183 if (tiHM.containsKey(key)) {
184 ati[i] = tiHM.get(key);
185 } else {
186 ati[i] = new ToggleItem(oa[i]);
187 }
188 }
189 toggleItems = ati;
190
191 tiHM.clear();
192 for (ToggleItem ti : ati) {
193 tiHM.put(ti.toString(), ti);
194 }
195
196 setListData(ati);
197
198 }
199
200
201 public ArrayList<Object> getCheckedItems() {
202 ArrayList<Object> ret = new ArrayList<Object>();
203 for (int i = 0; i < toggleItems.length; i++) {
204 if (toggleItems[i].isOn()) {
205 ret.add(toggleItems[i].getRef());
206 }
207 }
208 return ret;
209 }
210
211 public ArrayList<Object> getAllItems() {
212 ArrayList<Object> ret = new ArrayList<Object>();
213 for (int i = 0; i < toggleItems.length; i++) {
214 ret.add(toggleItems[i].getRef());
215 }
216 return ret;
217 }
218
219
220 public int[] getCheckedIndexes() {
221 int[] wk = new int[toggleItems.length];
222 int non = 0;
223 for (int i = 0; i < toggleItems.length; i++) {
224 if (toggleItems[i].isOn()) {
225 wk[non++] = i;
226 }
227 }
228 int[] ret = new int[non];
229 for (int i = 0; i < non; i++) {
230 ret[i] = wk[i];
231 }
232 return ret;
233 }
234
235
236 public void setCheckedIndexes(int[] inds) {
237 for (int i = 0; i < toggleItems.length; i++) {
238 toggleItems[i].setOff();
239 }
240 for (int i = 0; i < inds.length; i++) {
241 toggleItems[inds[i]].setOn();
242 }
243 repaint();
244 }
245
246
247
248 public Object getLastSelected() {
249 Object ret = null;
250 if (activeTI != null) {
251 ret = activeTI.getRef();
252 }
253 return ret;
254 }
255
256 public boolean hasActiveSelected() {
257 boolean ret = false;
258 if (activeTI != null) {
259 ret = activeTI.isOn();
260 }
261 return ret;
262 }
263
264
265 public void selectAll() {
266 dropEvents = true;
267 for (ToggleItem ti : toggleItems) {
268 ti.setOn();
269 }
270 dropEvents = false;
271 repaint();
272 }
273
274
275
276 public void setSelected(int[] ia) {
277 dropEvents = true;
278 for (ToggleItem ti : toggleItems) {
279 ti.setOff();
280 }
281 if (ia != null) {
282 for (int i : ia) {
283 if (i >= 0 && i < toggleItems.length) {
284 toggleItems[i].setOn();
285 }
286 }
287 }
288
289 dropEvents = false;
290 repaint();
291 }
292
293
294 public void setSelected(String[] sa) {
295 dropEvents = true;
296 for (ToggleItem ti : toggleItems) {
297 ti.setOff();
298 }
299 if (sa != null) {
300 for (String s : sa) {
301 if (tiHM.containsKey(s)) {
302 tiHM.get(s).setOn();
303 } else {
304 E.warning("Checkbox List - trying to select an item that is not in the list: " + s);
305 }
306 }
307 }
308 dropEvents = false;
309 repaint();
310 }
311
312
313 public void setToggleAction() {
314 E.missing();
315
316 }
317
318 }
319
320
321
322