gvsig-desktop-customize / trunk / org.gvsig.customize.app / org.gvsig.customize.app.mainplugin / src / main / java / org / gvsig / customize / JListWithCheckbox.java @ 1
History | View | Annotate | Download (13.9 KB)
1 |
/*
|
---|---|
2 |
* To change this license header, choose License Headers in Project Properties.
|
3 |
* To change this template file, choose Tools | Templates
|
4 |
* and open the template in the editor.
|
5 |
*/
|
6 |
package org.gvsig.customize; |
7 |
|
8 |
import java.awt.BorderLayout; |
9 |
import java.awt.Color; |
10 |
import java.awt.Component; |
11 |
import java.awt.Dimension; |
12 |
import java.awt.Point; |
13 |
import java.awt.Rectangle; |
14 |
import java.awt.event.MouseAdapter; |
15 |
import java.awt.event.MouseEvent; |
16 |
import java.beans.Transient; |
17 |
import java.util.HashSet; |
18 |
import java.util.Iterator; |
19 |
import java.util.List; |
20 |
import java.util.Set; |
21 |
import java.util.Vector; |
22 |
import javax.accessibility.AccessibleContext; |
23 |
import javax.swing.DefaultListSelectionModel; |
24 |
import javax.swing.JCheckBox; |
25 |
import javax.swing.JList; |
26 |
import javax.swing.JPanel; |
27 |
import javax.swing.ListCellRenderer; |
28 |
import javax.swing.ListModel; |
29 |
import javax.swing.ListSelectionModel; |
30 |
import javax.swing.event.ListSelectionEvent; |
31 |
import javax.swing.event.ListSelectionListener; |
32 |
import javax.swing.plaf.ListUI; |
33 |
import javax.swing.text.Position; |
34 |
import org.slf4j.Logger; |
35 |
import org.slf4j.LoggerFactory; |
36 |
|
37 |
public class JListWithCheckbox extends JList { |
38 |
|
39 |
private static final Logger logger = LoggerFactory.getLogger(JListWithCheckbox.class); |
40 |
|
41 |
|
42 |
public class CheckListCellRenderer extends JPanel implements ListCellRenderer { |
43 |
|
44 |
private ListCellRenderer delegate; |
45 |
private ListSelectionModel selectionModel; |
46 |
private JCheckBox checkBox = new JCheckBox(); |
47 |
|
48 |
public CheckListCellRenderer(ListCellRenderer renderer, ListSelectionModel selectionModel) { |
49 |
this.delegate = renderer;
|
50 |
this.selectionModel = selectionModel;
|
51 |
this.setLayout(new BorderLayout()); |
52 |
this.setOpaque(false); |
53 |
this.checkBox.setOpaque(false); |
54 |
checkBoxWidth = this.checkBox.getPreferredSize().width;
|
55 |
} |
56 |
|
57 |
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { |
58 |
Component renderer = delegate.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
|
59 |
checkBox.setSelected(selectionModel.isSelectedIndex(index)); |
60 |
removeAll(); |
61 |
add(checkBox, BorderLayout.WEST);
|
62 |
add(renderer, BorderLayout.CENTER);
|
63 |
return this; |
64 |
} |
65 |
} |
66 |
|
67 |
private int checkBoxWidth = 0; |
68 |
private final JList wrappedList; |
69 |
private ListSelectionModel checkedsModel = new DefaultListSelectionModel(); |
70 |
private Set<ListSelectionListener> checkedsListeners; |
71 |
|
72 |
public JListWithCheckbox(final JList wrappedList) { |
73 |
this.wrappedList = wrappedList;
|
74 |
this.checkedsModel.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); |
75 |
this.checkedsListeners = new HashSet<ListSelectionListener>(); |
76 |
|
77 |
this.wrappedList.setCellRenderer(
|
78 |
new CheckListCellRenderer(
|
79 |
this.wrappedList.getCellRenderer(),
|
80 |
this.checkedsModel
|
81 |
) |
82 |
); |
83 |
this.wrappedList.addMouseListener(new MouseAdapter() { |
84 |
public void mouseClicked(MouseEvent me) { |
85 |
int index = wrappedList.locationToIndex(me.getPoint());
|
86 |
if (index < 0) { |
87 |
return;
|
88 |
} |
89 |
if (me.getX() > wrappedList.getCellBounds(index, index).x + checkBoxWidth) {
|
90 |
return;
|
91 |
} |
92 |
toggleCheck(index); |
93 |
} |
94 |
}); |
95 |
this.checkedsModel.addListSelectionListener(new ListSelectionListener() { |
96 |
@Override
|
97 |
public void valueChanged(ListSelectionEvent lse) { |
98 |
wrappedList.repaint(); |
99 |
//fireCheckedsListeners(lse);
|
100 |
} |
101 |
}); |
102 |
|
103 |
} |
104 |
|
105 |
public void toggleCheck(int index) { |
106 |
if (index < 0) { |
107 |
return;
|
108 |
} |
109 |
if (this.checkedsModel.isSelectedIndex(index)) { |
110 |
this.checkedsModel.removeSelectionInterval(index, index);
|
111 |
} else {
|
112 |
this.checkedsModel.addSelectionInterval(index, index);
|
113 |
} |
114 |
// FIXME: isAdjusting is set to false always
|
115 |
fireCheckedsListeners(new ListSelectionEvent(this, index, index, false)); |
116 |
} |
117 |
|
118 |
@Override
|
119 |
public ListUI getUI() { |
120 |
return this.wrappedList.getUI(); |
121 |
} |
122 |
|
123 |
@Override
|
124 |
public void setUI(ListUI ui) { |
125 |
this.wrappedList.setUI(ui);
|
126 |
} |
127 |
|
128 |
@Override
|
129 |
public void updateUI() { |
130 |
if( this.wrappedList!= null ) { |
131 |
this.wrappedList.updateUI();
|
132 |
} |
133 |
} |
134 |
|
135 |
@Override
|
136 |
public String getUIClassID() { |
137 |
return this.wrappedList.getUIClassID(); |
138 |
} |
139 |
|
140 |
@Override
|
141 |
public Object getPrototypeCellValue() { |
142 |
return this.wrappedList.getPrototypeCellValue(); |
143 |
} |
144 |
|
145 |
@Override
|
146 |
public void setPrototypeCellValue(Object prototypeCellValue) { |
147 |
this.wrappedList.setPrototypeCellValue(prototypeCellValue);
|
148 |
} |
149 |
|
150 |
@Override
|
151 |
public int getFixedCellWidth() { |
152 |
return this.wrappedList.getFixedCellWidth(); |
153 |
} |
154 |
|
155 |
@Override
|
156 |
public void setFixedCellWidth(int width) { |
157 |
this.wrappedList.setFixedCellWidth(width);
|
158 |
} |
159 |
|
160 |
@Override
|
161 |
public int getFixedCellHeight() { |
162 |
return this.wrappedList.getFixedCellHeight(); |
163 |
} |
164 |
|
165 |
@Override
|
166 |
public void setFixedCellHeight(int height) { |
167 |
this.wrappedList.setFixedCellHeight(height);
|
168 |
} |
169 |
|
170 |
@Transient
|
171 |
@Override
|
172 |
public ListCellRenderer getCellRenderer() { |
173 |
return this.wrappedList.getCellRenderer(); |
174 |
} |
175 |
|
176 |
@Override
|
177 |
public void setCellRenderer(ListCellRenderer cellRenderer) { |
178 |
this.wrappedList.setCellRenderer(cellRenderer);
|
179 |
} |
180 |
|
181 |
@Override
|
182 |
public Color getSelectionForeground() { |
183 |
return this.wrappedList.getSelectionForeground(); |
184 |
} |
185 |
|
186 |
@Override
|
187 |
public void setSelectionForeground(Color selectionForeground) { |
188 |
this.wrappedList.setSelectionForeground(selectionForeground);
|
189 |
} |
190 |
|
191 |
@Override
|
192 |
public Color getSelectionBackground() { |
193 |
return this.wrappedList.getSelectionBackground(); |
194 |
} |
195 |
|
196 |
@Override
|
197 |
public void setSelectionBackground(Color selectionBackground) { |
198 |
this.wrappedList.setSelectionBackground(selectionBackground);
|
199 |
} |
200 |
|
201 |
@Override
|
202 |
public int getVisibleRowCount() { |
203 |
return this.wrappedList.getVisibleRowCount(); |
204 |
} |
205 |
|
206 |
@Override
|
207 |
public void setVisibleRowCount(int visibleRowCount) { |
208 |
this.wrappedList.setVisibleRowCount(visibleRowCount);
|
209 |
} |
210 |
|
211 |
@Override
|
212 |
public int getLayoutOrientation() { |
213 |
return this.wrappedList.getLayoutOrientation(); |
214 |
} |
215 |
|
216 |
@Override
|
217 |
public void setLayoutOrientation(int layoutOrientation) { |
218 |
this.wrappedList.setLayoutOrientation(layoutOrientation);
|
219 |
} |
220 |
|
221 |
@Override
|
222 |
public int getFirstVisibleIndex() { |
223 |
return this.wrappedList.getFirstVisibleIndex(); |
224 |
} |
225 |
|
226 |
@Override
|
227 |
public int getLastVisibleIndex() { |
228 |
return this.wrappedList.getLastVisibleIndex(); |
229 |
} |
230 |
|
231 |
@Override
|
232 |
public void ensureIndexIsVisible(int index) { |
233 |
this.wrappedList.ensureIndexIsVisible(index);
|
234 |
} |
235 |
|
236 |
@Override
|
237 |
public void setDragEnabled(boolean b) { |
238 |
this.wrappedList.setDragEnabled(b);
|
239 |
} |
240 |
|
241 |
@Override
|
242 |
public boolean getDragEnabled() { |
243 |
return this.wrappedList.getDragEnabled(); |
244 |
} |
245 |
//
|
246 |
// public final void setDropMode(DropMode dropMode) {
|
247 |
// this.wrappedList.setDropMode(dropMode);
|
248 |
// }
|
249 |
//
|
250 |
// public final DropMode getDropMode() {
|
251 |
//
|
252 |
// }
|
253 |
//
|
254 |
// public final DropLocation getDropLocation() {
|
255 |
//
|
256 |
// }
|
257 |
|
258 |
@Override
|
259 |
public int getNextMatch(String prefix, int startIndex, Position.Bias bias) { |
260 |
return this.wrappedList.getNextMatch(prefix, startIndex, bias); |
261 |
} |
262 |
|
263 |
@Override
|
264 |
public String getToolTipText(MouseEvent event) { |
265 |
return this.wrappedList.getToolTipText(event); |
266 |
} |
267 |
|
268 |
@Override
|
269 |
public int locationToIndex(Point location) { |
270 |
return this.wrappedList.locationToIndex(location); |
271 |
} |
272 |
|
273 |
@Override
|
274 |
public Point indexToLocation(int index) { |
275 |
return this.wrappedList.indexToLocation(index); |
276 |
} |
277 |
|
278 |
@Override
|
279 |
public Rectangle getCellBounds(int index0, int index1) { |
280 |
return this.wrappedList.getCellBounds(index0, index1); |
281 |
} |
282 |
|
283 |
@Override
|
284 |
public ListModel getModel() { |
285 |
return this.wrappedList.getModel(); |
286 |
} |
287 |
|
288 |
@Override
|
289 |
public void setModel(ListModel model) { |
290 |
this.wrappedList.setModel(model);
|
291 |
} |
292 |
|
293 |
@Override
|
294 |
public void setListData(Object[] listData) { |
295 |
this.wrappedList.setListData(listData);
|
296 |
} |
297 |
|
298 |
@Override
|
299 |
public void setListData(Vector listData) { |
300 |
this.wrappedList.setListData(listData);
|
301 |
} |
302 |
|
303 |
@Override
|
304 |
public ListSelectionModel getSelectionModel() { |
305 |
return this.wrappedList.getSelectionModel(); |
306 |
} |
307 |
|
308 |
public ListSelectionModel getCheckedModel() { |
309 |
return this.checkedsModel; |
310 |
} |
311 |
|
312 |
@Override
|
313 |
public void addListSelectionListener(ListSelectionListener listener) { |
314 |
this.wrappedList.addListSelectionListener(listener);
|
315 |
} |
316 |
|
317 |
public void addChecksListener(ListSelectionListener listener) { |
318 |
this.checkedsListeners.add(listener);
|
319 |
} |
320 |
|
321 |
@Override
|
322 |
public void removeListSelectionListener(ListSelectionListener listener) { |
323 |
this.wrappedList.removeListSelectionListener(listener);
|
324 |
} |
325 |
|
326 |
public void removeChecksListener(ListSelectionListener listener) { |
327 |
this.checkedsListeners.remove(listener);
|
328 |
} |
329 |
|
330 |
@Override
|
331 |
public ListSelectionListener[] getListSelectionListeners() { |
332 |
return this.wrappedList.getListSelectionListeners(); |
333 |
} |
334 |
|
335 |
public ListSelectionListener[] getChecksListeners() { |
336 |
return this.checkedsListeners.toArray(new ListSelectionListener[1]); |
337 |
} |
338 |
|
339 |
protected void fireCheckedsListeners(ListSelectionEvent event) { |
340 |
Iterator<ListSelectionListener> it = this.checkedsListeners.iterator(); |
341 |
while(it.hasNext()) {
|
342 |
ListSelectionListener listener = it.next();
|
343 |
try {
|
344 |
listener.valueChanged(event); |
345 |
} catch(Throwable th) { |
346 |
logger.warn("Problems calling check listener ("+listener+").",th); |
347 |
} |
348 |
} |
349 |
} |
350 |
|
351 |
@Override
|
352 |
public void setSelectionModel(ListSelectionModel selectionModel) { |
353 |
this.wrappedList.setSelectionModel(selectionModel);
|
354 |
} |
355 |
|
356 |
@Override
|
357 |
public void setSelectionMode(int selectionMode) { |
358 |
this.wrappedList.setSelectionMode(selectionMode);
|
359 |
} |
360 |
|
361 |
@Override
|
362 |
public int getSelectionMode() { |
363 |
return this.wrappedList.getSelectionMode(); |
364 |
} |
365 |
|
366 |
@Override
|
367 |
public int getAnchorSelectionIndex() { |
368 |
return this.wrappedList.getAnchorSelectionIndex(); |
369 |
} |
370 |
|
371 |
@Override
|
372 |
public int getLeadSelectionIndex() { |
373 |
return this.wrappedList.getLeadSelectionIndex(); |
374 |
} |
375 |
|
376 |
@Override
|
377 |
public int getMinSelectionIndex() { |
378 |
return this.wrappedList.getMinSelectionIndex(); |
379 |
} |
380 |
|
381 |
@Override
|
382 |
public int getMaxSelectionIndex() { |
383 |
return this.wrappedList.getMaxSelectionIndex(); |
384 |
} |
385 |
|
386 |
@Override
|
387 |
public boolean isSelectedIndex(int index) { |
388 |
return this.wrappedList.isSelectedIndex(index); |
389 |
} |
390 |
|
391 |
@Override
|
392 |
public boolean isSelectionEmpty() { |
393 |
return this.wrappedList.isSelectionEmpty(); |
394 |
} |
395 |
|
396 |
@Override
|
397 |
public void clearSelection() { |
398 |
this.wrappedList.clearSelection();
|
399 |
} |
400 |
|
401 |
@Override
|
402 |
public void setSelectionInterval(int anchor, int lead) { |
403 |
this.wrappedList.setSelectionInterval(anchor, lead);
|
404 |
} |
405 |
|
406 |
@Override
|
407 |
public void addSelectionInterval(int anchor, int lead) { |
408 |
this.wrappedList.addSelectionInterval(anchor, lead);
|
409 |
} |
410 |
|
411 |
@Override
|
412 |
public void removeSelectionInterval(int index0, int index1) { |
413 |
this.wrappedList.removeSelectionInterval(index0, index1);
|
414 |
} |
415 |
|
416 |
@Override
|
417 |
public void setValueIsAdjusting(boolean b) { |
418 |
this.wrappedList.setValueIsAdjusting(b);
|
419 |
} |
420 |
|
421 |
@Override
|
422 |
public boolean getValueIsAdjusting() { |
423 |
return this.wrappedList.getValueIsAdjusting(); |
424 |
} |
425 |
|
426 |
@Transient
|
427 |
@Override
|
428 |
public int[] getSelectedIndices() { |
429 |
return this.wrappedList.getSelectedIndices(); |
430 |
} |
431 |
|
432 |
@Override
|
433 |
public void setSelectedIndex(int index) { |
434 |
this.wrappedList.setSelectedIndex(index);
|
435 |
} |
436 |
|
437 |
@Override
|
438 |
public void setSelectedIndices(int[] indices) { |
439 |
this.wrappedList.setSelectedIndices(indices);
|
440 |
} |
441 |
|
442 |
@Deprecated
|
443 |
@Override
|
444 |
public Object[] getSelectedValues() { |
445 |
return this.wrappedList.getSelectedValues(); |
446 |
} |
447 |
|
448 |
@Override
|
449 |
public List getSelectedValuesList() { |
450 |
return this.wrappedList.getSelectedValuesList(); |
451 |
} |
452 |
|
453 |
@Override
|
454 |
public int getSelectedIndex() { |
455 |
return this.wrappedList.getSelectedIndex(); |
456 |
} |
457 |
|
458 |
@Override
|
459 |
public Object getSelectedValue() { |
460 |
return this.wrappedList.getSelectedValue(); |
461 |
} |
462 |
|
463 |
@Override
|
464 |
public void setSelectedValue(Object anObject, boolean shouldScroll) { |
465 |
this.wrappedList.setSelectedValue(anObject, shouldScroll);
|
466 |
} |
467 |
|
468 |
@Override
|
469 |
public Dimension getPreferredScrollableViewportSize() { |
470 |
return this.wrappedList.getPreferredScrollableViewportSize(); |
471 |
} |
472 |
|
473 |
@Override
|
474 |
public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) { |
475 |
return this.wrappedList.getScrollableUnitIncrement(visibleRect, orientation, direction); |
476 |
} |
477 |
|
478 |
@Override
|
479 |
public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) { |
480 |
return this.wrappedList.getScrollableBlockIncrement(visibleRect, orientation, direction); |
481 |
} |
482 |
|
483 |
@Override
|
484 |
public boolean getScrollableTracksViewportWidth() { |
485 |
return this.wrappedList.getScrollableTracksViewportWidth(); |
486 |
} |
487 |
|
488 |
@Override
|
489 |
public boolean getScrollableTracksViewportHeight() { |
490 |
return this.wrappedList.getScrollableTracksViewportHeight(); |
491 |
} |
492 |
|
493 |
@Override
|
494 |
public AccessibleContext getAccessibleContext() { |
495 |
return this.wrappedList.getAccessibleContext(); |
496 |
} |
497 |
} |