01: /*
02: * FindBugs - Find Bugs in Java programs
03: * Copyright (C) 2006, University of Maryland
04: *
05: * This library is free software; you can redistribute it and/or
06: * modify it under the terms of the GNU Lesser General Public
07: * License as published by the Free Software Foundation; either
08: * version 2.1 of the License, or (at your option) any later version.
09: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library; if not, write to the Free Software
17: * Foundation, Inc., 59 Temple Place, Suite 330, Boston MA 02111-1307, USA
18: */
19:
20: package edu.umd.cs.findbugs.gui2;
21:
22: import java.awt.Component;
23: import java.awt.event.MouseAdapter;
24: import java.awt.event.MouseEvent;
25:
26: import javax.swing.JCheckBox;
27: import javax.swing.JList;
28: import javax.swing.ListCellRenderer;
29: import javax.swing.ListSelectionModel;
30: import javax.swing.UIManager;
31: import javax.swing.border.Border;
32: import javax.swing.border.EmptyBorder;
33:
34: /**
35: * A list of JCheckBoxes! How convenient!
36: *
37: * Adapted from: http://www.devx.com/tips/Tip/5342
38: * @author Trevor Harmon
39: */
40: @SuppressWarnings("serial")
41: public class CheckBoxList extends JList {
42: private static Border noFocusBorder = new EmptyBorder(1, 1, 1, 1);
43:
44: public CheckBoxList() {
45: setCellRenderer(new CellRenderer());
46:
47: addMouseListener(new MouseAdapter() {
48: @Override
49: public void mousePressed(MouseEvent e) {
50: int index = locationToIndex(e.getPoint());
51:
52: if (index != -1) {
53: JCheckBox checkbox = (JCheckBox) getModel()
54: .getElementAt(index);
55: checkbox.setSelected(!checkbox.isSelected());
56: repaint();
57: }
58: }
59: });
60:
61: setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
62: }
63:
64: public CheckBoxList(Object[] list) {
65: this ();
66: setListData(list);
67: }
68:
69: @Override
70: public void setEnabled(boolean enabled) {
71: super .setEnabled(enabled);
72:
73: for (int i = 0; i < getModel().getSize(); i++)
74: ((JCheckBox) getModel().getElementAt(i))
75: .setEnabled(enabled);
76: }
77:
78: protected class CellRenderer implements ListCellRenderer {
79: public Component getListCellRendererComponent(JList list,
80: Object value, int index, boolean isSelected,
81: boolean cellHasFocus) {
82: JCheckBox checkbox = (JCheckBox) value;
83: checkbox
84: .setBackground(isSelected ? getSelectionBackground()
85: : getBackground());
86: checkbox
87: .setForeground(isSelected ? getSelectionForeground()
88: : getForeground());
89: checkbox.setEnabled(isEnabled());
90: checkbox.setFont(getFont());
91: checkbox.setFocusPainted(false);
92: checkbox.setBorderPainted(true);
93: checkbox.setBorder(isSelected ? UIManager
94: .getBorder("List.focusCellHighlightBorder")
95: : noFocusBorder);
96: return checkbox;
97: }
98: }
99: }
|