Revision 2420 org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.swing/org.gvsig.tools.swing.impl/src/main/java/org/gvsig/tools/swing/impl/DefaultJListWithCheckbox.java
DefaultJListWithCheckbox.java | ||
---|---|---|
1 | 1 |
|
2 | 2 |
package org.gvsig.tools.swing.impl; |
3 | 3 |
|
4 |
import java.awt.BorderLayout; |
|
5 | 4 |
import java.awt.Color; |
6 |
import java.awt.Component; |
|
7 | 5 |
import java.awt.Dimension; |
8 | 6 |
import java.awt.Point; |
9 | 7 |
import java.awt.Rectangle; |
... | ... | |
17 | 15 |
import java.util.Vector; |
18 | 16 |
import javax.accessibility.AccessibleContext; |
19 | 17 |
import javax.swing.DefaultListSelectionModel; |
20 |
import javax.swing.JCheckBox; |
|
21 | 18 |
import javax.swing.JList; |
22 |
import javax.swing.JPanel; |
|
23 | 19 |
import javax.swing.ListCellRenderer; |
24 | 20 |
import javax.swing.ListModel; |
25 | 21 |
import javax.swing.ListSelectionModel; |
... | ... | |
27 | 23 |
import javax.swing.event.ListSelectionListener; |
28 | 24 |
import javax.swing.plaf.ListUI; |
29 | 25 |
import javax.swing.text.Position; |
26 |
import org.gvsig.tools.swing.api.DefaultCheckListCellRenderer; |
|
30 | 27 |
import org.gvsig.tools.swing.api.JListWithCheckbox; |
31 | 28 |
import org.slf4j.Logger; |
32 | 29 |
import org.slf4j.LoggerFactory; |
... | ... | |
36 | 33 |
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultJListWithCheckbox.class); |
37 | 34 |
|
38 | 35 |
|
39 |
private class CheckListCellRenderer extends JPanel implements ListCellRenderer { |
|
40 | 36 |
|
41 |
private final ListCellRenderer delegate; |
|
42 |
private ListSelectionModel selectionModel; |
|
43 |
private final JCheckBox checkBox = new JCheckBox(); |
|
44 |
|
|
45 |
public CheckListCellRenderer(ListCellRenderer renderer, ListSelectionModel selectionModel) { |
|
46 |
this.delegate = renderer; |
|
47 |
this.selectionModel = selectionModel; |
|
48 |
this.setLayout(new BorderLayout()); |
|
49 |
this.setOpaque(false); |
|
50 |
this.checkBox.setOpaque(false); |
|
51 |
checkBoxWidth = (int) this.checkBox.getPreferredSize().getWidth(); |
|
52 |
} |
|
53 |
|
|
54 |
@Override |
|
55 |
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { |
|
56 |
Component renderer = delegate.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); |
|
57 |
checkBox.setSelected(selectionModel.isSelectedIndex(index)); |
|
58 |
removeAll(); |
|
59 |
add(checkBox, BorderLayout.WEST); |
|
60 |
add(renderer, BorderLayout.CENTER); |
|
61 |
return this; |
|
62 |
} |
|
63 |
|
|
64 |
public void setCheckedModel(ListSelectionModel selectionModel) { |
|
65 |
this.selectionModel = selectionModel; |
|
66 |
} |
|
67 |
} |
|
68 |
|
|
69 |
private int checkBoxWidth = 0; |
|
70 | 37 |
private final JList wrappedList; |
71 | 38 |
private ListSelectionModel checkedsModel = new DefaultListSelectionModel(); |
72 | 39 |
private Set<ListSelectionListener> checkedsListeners; |
73 | 40 |
private final ListSelectionListener checksModelListener; |
74 |
private final CheckListCellRenderer checkListCellRenderer; |
|
41 |
private final DefaultCheckListCellRenderer checkListCellRenderer;
|
|
75 | 42 |
|
76 | 43 |
public DefaultJListWithCheckbox(final JList wrappedList) { |
77 | 44 |
this.wrappedList = wrappedList; |
78 | 45 |
this.checkedsModel.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); |
79 | 46 |
this.checkedsListeners = new HashSet<>(); |
80 | 47 |
|
81 |
this.checkListCellRenderer = new CheckListCellRenderer( |
|
48 |
this.checkListCellRenderer = new DefaultCheckListCellRenderer(
|
|
82 | 49 |
this.wrappedList.getCellRenderer(), |
83 | 50 |
this.checkedsModel |
84 | 51 |
); |
... | ... | |
90 | 57 |
if (index < 0) { |
91 | 58 |
return; |
92 | 59 |
} |
60 |
ListCellRenderer renderer = wrappedList.getCellRenderer(); |
|
61 |
int checkBoxWidth = 16; |
|
62 |
if(renderer instanceof DefaultCheckListCellRenderer){ |
|
63 |
checkBoxWidth = ((DefaultCheckListCellRenderer)renderer).getCheckBoxWidth(); |
|
64 |
} |
|
93 | 65 |
if (me.getX() > wrappedList.getCellBounds(index, index).getX() + checkBoxWidth) { |
94 | 66 |
return; |
95 | 67 |
} |
Also available in: Unified diff