KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > gui2 > CheckBoxList


1 /*
2  * FindBugs - Find Bugs in Java programs
3  * Copyright (C) 2006, University of Maryland
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
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 javax.swing.*;
23 import javax.swing.border.*;
24 import java.awt.*;
25 import java.awt.event.*;
26
27 /**
28  * A list of JCheckBoxes! How convenient!
29  *
30  * Adapted from: http://www.devx.com/tips/Tip/5342
31  * @author Trevor Harmon
32  */

33 @SuppressWarnings JavaDoc("serial")
34 public class CheckBoxList extends JList
35 {
36     private static Border noFocusBorder = new EmptyBorder(1, 1, 1, 1);
37
38     public CheckBoxList()
39     {
40         setCellRenderer(new CellRenderer());
41
42         addMouseListener(new MouseAdapter()
43         {
44             public void mousePressed(MouseEvent e)
45             {
46                 int index = locationToIndex(e.getPoint());
47
48                 if (index != -1)
49                 {
50                     JCheckBox checkbox = (JCheckBox) getModel().getElementAt(index);
51                     checkbox.setSelected(!checkbox.isSelected());
52                     repaint();
53                 }
54             }
55         });
56
57         setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
58     }
59
60     public CheckBoxList(Object JavaDoc[] list)
61     {
62         this();
63         setListData(list);
64     }
65     
66     @Override JavaDoc
67     public void setEnabled(boolean enabled)
68     {
69         super.setEnabled(enabled);
70         
71         for (int i = 0; i < getModel().getSize(); i++)
72             ((JCheckBox) getModel().getElementAt(i)).setEnabled(enabled);
73     }
74     
75     protected class CellRenderer implements ListCellRenderer
76     {
77         public Component getListCellRendererComponent(JList list, Object JavaDoc value,
78                 int index, boolean isSelected, boolean cellHasFocus)
79         {
80             JCheckBox checkbox = (JCheckBox) value;
81             checkbox.setBackground(isSelected ? getSelectionBackground() : getBackground());
82             checkbox.setForeground(isSelected ? getSelectionForeground() : getForeground());
83             checkbox.setEnabled(isEnabled());
84             checkbox.setFont(getFont());
85             checkbox.setFocusPainted(false);
86             checkbox.setBorderPainted(true);
87             checkbox.setBorder(isSelected ? UIManager.getBorder("List.focusCellHighlightBorder") : noFocusBorder);
88             return checkbox;
89         }
90     }
91 }
Popular Tags