1 | package hozo.sparql.util;
|
---|
2 |
|
---|
3 | import java.awt.Component;
|
---|
4 | import java.awt.Point;
|
---|
5 | import java.awt.event.MouseEvent;
|
---|
6 | import java.awt.event.MouseListener;
|
---|
7 | import java.awt.event.MouseMotionListener;
|
---|
8 |
|
---|
9 | import javax.swing.DefaultListModel;
|
---|
10 | import javax.swing.JCheckBox;
|
---|
11 | import javax.swing.JList;
|
---|
12 | import javax.swing.ListCellRenderer;
|
---|
13 | import javax.swing.ListModel;
|
---|
14 |
|
---|
15 | public class CheckableList extends JList {
|
---|
16 | /**
|
---|
17 | *
|
---|
18 | */
|
---|
19 | private static final long serialVersionUID = -8207275088256642406L;
|
---|
20 | private CheckBoxCellRenderer renderer;
|
---|
21 |
|
---|
22 | public CheckableList() {
|
---|
23 | this(new DefaultListModel());
|
---|
24 | }
|
---|
25 |
|
---|
26 | public CheckableList(ListModel model) {
|
---|
27 | super(model);
|
---|
28 | }
|
---|
29 |
|
---|
30 |
|
---|
31 | public void updateUI() {
|
---|
32 | setForeground(null);
|
---|
33 | setBackground(null);
|
---|
34 | setSelectionForeground(null);
|
---|
35 | setSelectionBackground(null);
|
---|
36 | if(renderer!=null) {
|
---|
37 | removeMouseListener(renderer);
|
---|
38 | removeMouseMotionListener(renderer);
|
---|
39 | }
|
---|
40 | super.updateUI();
|
---|
41 | renderer = new CheckBoxCellRenderer();
|
---|
42 | setCellRenderer(renderer);
|
---|
43 | addMouseListener(renderer);
|
---|
44 | addMouseMotionListener(renderer);
|
---|
45 | }
|
---|
46 | }
|
---|
47 |
|
---|
48 | class CheckBoxCellRenderer extends JCheckBox implements ListCellRenderer, MouseListener, MouseMotionListener {
|
---|
49 | /**
|
---|
50 | *
|
---|
51 | */
|
---|
52 | private static final long serialVersionUID = 5134902478737266926L;
|
---|
53 | private int rollOverRowIndex = -1;
|
---|
54 |
|
---|
55 | @Override
|
---|
56 | public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
|
---|
57 | this.setOpaque(true);
|
---|
58 | if(isSelected) {
|
---|
59 | this.setBackground(list.getSelectionBackground());
|
---|
60 | this.setForeground(list.getSelectionForeground());
|
---|
61 | }else{
|
---|
62 | this.setBackground(list.getBackground());
|
---|
63 | this.setForeground(list.getForeground());
|
---|
64 | }
|
---|
65 | if(value instanceof CheckableListItem) {
|
---|
66 | this.setSelected(((CheckableListItem)value).selected);
|
---|
67 | this.getModel().setRollover(index==rollOverRowIndex);
|
---|
68 | }
|
---|
69 | this.setText(value.toString());
|
---|
70 | return this;
|
---|
71 | }
|
---|
72 |
|
---|
73 | @Override
|
---|
74 | public void mouseExited(MouseEvent e) {
|
---|
75 | JList l = (JList)e.getSource();
|
---|
76 | if(rollOverRowIndex>=0) {
|
---|
77 | l.repaint(l.getCellBounds(rollOverRowIndex, rollOverRowIndex));
|
---|
78 | rollOverRowIndex = -1;
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 | @Override
|
---|
83 | public void mouseClicked(MouseEvent e) {
|
---|
84 | if(e.getButton()==MouseEvent.BUTTON1) {
|
---|
85 | JList l = (JList)e.getComponent();
|
---|
86 | DefaultListModel m = (DefaultListModel)l.getModel();
|
---|
87 | Point p = e.getPoint();
|
---|
88 | int index = l.locationToIndex(p);
|
---|
89 | if(index>=0) {
|
---|
90 | CheckableListItem n = (CheckableListItem)m.get(index);
|
---|
91 | m.set(index, new CheckableListItem(n.text, !n.selected));
|
---|
92 | l.repaint(l.getCellBounds(index, index));
|
---|
93 | }
|
---|
94 | }
|
---|
95 | }
|
---|
96 |
|
---|
97 | @Override
|
---|
98 | public void mouseMoved(MouseEvent e) {
|
---|
99 | JList l = (JList)e.getSource();
|
---|
100 | int index = l.locationToIndex(e.getPoint());
|
---|
101 | if(index != rollOverRowIndex) {
|
---|
102 | rollOverRowIndex = index;
|
---|
103 | l.repaint();
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 | @Override
|
---|
108 | public void mouseEntered(MouseEvent e) {}
|
---|
109 |
|
---|
110 | @Override
|
---|
111 | public void mousePressed(MouseEvent e) {}
|
---|
112 |
|
---|
113 | @Override
|
---|
114 | public void mouseReleased(MouseEvent e) {}
|
---|
115 |
|
---|
116 | @Override
|
---|
117 | public void mouseDragged(MouseEvent e) {}
|
---|
118 | }
|
---|
119 |
|
---|