root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.gui / src / main / java / es / unex / sextante / gui / algorithm / CheckBoxList.java @ 338
History | View | Annotate | Download (2.04 KB)
1 | 59 | nbrodin | package es.unex.sextante.gui.algorithm; |
---|---|---|---|
2 | |||
3 | import java.awt.Component; |
||
4 | import java.awt.event.MouseAdapter; |
||
5 | import java.awt.event.MouseEvent; |
||
6 | |||
7 | import javax.swing.JCheckBox; |
||
8 | import javax.swing.JList; |
||
9 | import javax.swing.ListCellRenderer; |
||
10 | import javax.swing.ListSelectionModel; |
||
11 | import javax.swing.border.Border; |
||
12 | import javax.swing.border.EmptyBorder; |
||
13 | |||
14 | /**
|
||
15 | * A list with checkboxes
|
||
16 | *
|
||
17 | * @author volaya
|
||
18 | *
|
||
19 | */
|
||
20 | public class CheckBoxList |
||
21 | extends
|
||
22 | JList {
|
||
23 | protected static final Border noFocusBorder = new EmptyBorder(1, 1, 1, 1); |
||
24 | |||
25 | |||
26 | /**
|
||
27 | * Creates a new checkbox list
|
||
28 | */
|
||
29 | public CheckBoxList() {
|
||
30 | setCellRenderer(new CellRenderer());
|
||
31 | |||
32 | addMouseListener(new MouseAdapter() { |
||
33 | @Override
|
||
34 | public void mousePressed(final MouseEvent e) { |
||
35 | final int index = locationToIndex(e.getPoint()); |
||
36 | |||
37 | if (index != -1) { |
||
38 | final JCheckBox checkbox = (JCheckBox) getModel().getElementAt(index); |
||
39 | checkbox.setSelected(!checkbox.isSelected()); |
||
40 | repaint(); |
||
41 | } |
||
42 | } |
||
43 | }); |
||
44 | |||
45 | setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
||
46 | |||
47 | } |
||
48 | |||
49 | protected class CellRenderer |
||
50 | implements
|
||
51 | ListCellRenderer {
|
||
52 | public Component getListCellRendererComponent(final JList list, |
||
53 | final Object value, |
||
54 | final int index, |
||
55 | final boolean isSelected, |
||
56 | final boolean cellHasFocus) { |
||
57 | final JCheckBox checkbox = (JCheckBox) value; |
||
58 | checkbox.setBackground(getBackground()); |
||
59 | checkbox.setForeground(getForeground()); |
||
60 | checkbox.setEnabled(isEnabled()); |
||
61 | checkbox.setFont(getFont()); |
||
62 | checkbox.setFocusPainted(false);
|
||
63 | checkbox.setBorderPainted(true);
|
||
64 | checkbox.setBorder(noFocusBorder); |
||
65 | return checkbox;
|
||
66 | } |
||
67 | } |
||
68 | } |