source: trunk/gli/src/org/greenstone/gatherer/util/CheckList.java@ 9187

Last change on this file since 9187 was 9185, checked in by mdewsnip, 19 years ago

Fix to the bug with Java 1.5.0 where the checkbox would not repaint if it was clicked while the item was selected.

  • Property svn:keywords set to Author Date Id Revision
File size: 7.0 KB
Line 
1/**
2 *#########################################################################
3 *
4 * A component of the Gatherer application, part of the Greenstone digital
5 * library suite from the New Zealand Digital Library Project at the
6 * University of Waikato, New Zealand.
7 *
8 * <BR><BR>
9 *
10 * Author: John Thompson, Greenstone Digital Library, University of Waikato
11 *
12 * <BR><BR>
13 *
14 * Copyright (C) 1999 New Zealand Digital Library Project
15 *
16 * <BR><BR>
17 *
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
22 *
23 * <BR><BR>
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * <BR><BR>
31 *
32 * You should have received a copy of the GNU General Public License
33 * along with this program; if not, write to the Free Software
34 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 *########################################################################
36 */
37package org.greenstone.gatherer.util;
38
39import java.awt.*;
40import java.awt.event.*;
41import java.util.*;
42import javax.swing.*;
43import javax.swing.border.*;
44
45
46/** This class provides a visual component that has the form of a list, as provided by JList but uses JCheckBox for data selection. Thus several elements can be 'ticked' in the list, and this selection returned using the method getSelected().<BR>Parts of this code modified from Trevor Harmon's posting on www.dejanews.com.
47 * @author John Thompson
48 * @version 2.3
49 */
50public class CheckList
51 extends JList
52{
53 /** The border used when a list row is not in focus. */
54 static private Border noFocusBorder = new EmptyBorder(1, 1, 1, 1);
55
56 private boolean show_selected_row = true;
57
58
59 /** Constructor. */
60 public CheckList(boolean show_selected_row)
61 {
62 super();
63 this.show_selected_row = show_selected_row;
64
65 setCellRenderer(new CheckListCellRenderer());
66 setModel(new DefaultListModel());
67 setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
68 addMouseListener(new CheckListMouseListener());
69 }
70
71
72 public void addEntry(CheckListEntry entry)
73 {
74 DefaultListModel model = (DefaultListModel) getModel();
75
76 // Add the entry in alpabetical order
77 String name = entry.toString();
78 for (int i = 0; i < model.size(); i++) {
79 Object sibling = model.getElementAt(i);
80 if (name.compareTo(sibling.toString()) <= 0) {
81 model.add(i, entry);
82 return;
83 }
84 }
85
86 model.addElement(entry);
87 }
88
89
90 public void clearSelection()
91 {
92 DefaultListModel model = (DefaultListModel) getModel();
93 for (int i = 0; i < model.size(); i++) {
94 ((CheckListEntry) model.get(i)).setSelected(false);
95 }
96 updateUI();
97 }
98
99
100 /** Retrieve the currently ticked entries from this list.
101 * @return An <strong>ArrayList</strong> containing only those entries from the initial list that are checked.
102 * @see org.greenstone.gatherer.checklist.Entry
103 */
104 public ArrayList getSelected()
105 {
106 ArrayList result = new ArrayList();
107 DefaultListModel model = (DefaultListModel) getModel();
108 for (int i = 0; i < model.size(); i++) {
109 CheckListEntry entry = (CheckListEntry) model.get(i);
110 if (entry.isSelected()) {
111 result.add(entry.getObject());
112 }
113 }
114 return result;
115 }
116
117
118 public void setListData(ArrayList list_data)
119 {
120 // Create a new model.
121 setModel(new DefaultListModel());
122
123 // Add the items from the list to the model
124 for (int i = 0; i < list_data.size(); i++) {
125 Object list_object = list_data.get(i);
126 if (list_object instanceof CheckListEntry) {
127 addEntry((CheckListEntry) list_object);
128 }
129 else {
130 addEntry(new CheckListEntry(list_object));
131 }
132 }
133 }
134
135
136 public void setSelectedObjects(Object objects[])
137 {
138 if (objects == null) {
139 return;
140 }
141
142 DefaultListModel model = (DefaultListModel) getModel();
143 for (int i = 0; i < model.size(); i++) {
144 CheckListEntry entry = (CheckListEntry) model.get(i);
145 for (int j = 0; j < objects.length; j++) {
146 if (entry.getObject().equals(objects[j])) {
147 entry.setSelected(true);
148 }
149 }
150 }
151 updateUI();
152 }
153
154
155 /** A custom list cell renderer for producing rows which contain clickable check boxes. */
156 protected class CheckListCellRenderer
157 implements ListCellRenderer
158 {
159 /** Return a component that has been configured to display the specified value. That component's paint method is then called to "render" the cell. If it is necessary to compute the dimensions of a list because the list cells do not have a fixed size, this method is called to generate a component on which getPreferredSize can be invoked.
160 * @param list The </strong>JList</strong> we're painting.
161 * @param value The value returned by list.getModel().getElementAt(index), as an <strong>Object</strong>.
162 * @param index The cells index as an <i>int</i>.
163 * @param is_selected <i>true</i> if the specified cell was selected, <i>false</i> otherwise.
164 * @param cell_has_focus <i>true</i> if and only if the specified cell has the focus.
165 * @return A <strong>Component</strong> whose paint() method will render the specified value.
166 */
167 public Component getListCellRendererComponent(JList list, Object value, int index, boolean is_selected, boolean cell_has_focus) {
168 JCheckBox checkbox = (JCheckBox) value;
169 if (show_selected_row) {
170 checkbox.setBackground(is_selected ? list.getSelectionBackground() : list.getBackground());
171 checkbox.setForeground(is_selected ? list.getSelectionForeground() : list.getForeground());
172 checkbox.setBorderPainted(true);
173 }
174 else {
175 checkbox.setBackground(list.getBackground());
176 checkbox.setForeground(list.getForeground());
177 checkbox.setBorderPainted(false);
178 }
179 checkbox.setEnabled(list.isEnabled());
180 checkbox.setFont(list.getFont());
181 checkbox.setFocusPainted(false);
182 checkbox.setBorder((is_selected) ? UIManager.getBorder("List.focusCellHighlightBorder") : noFocusBorder);
183 return checkbox;
184 }
185 }
186
187
188 /** Listens for clicks apon the checks within the list, and updates as necessary. */
189 private class CheckListMouseListener
190 extends MouseAdapter
191 {
192 /** Called whenever the mouse is clicked over our list. We find the nearest checkbox and change its state.
193 * @param e A <strong>MouseEvent</strong> containing everything you ever wanted to know about the mouse event but were afraid to ask.
194 */
195 public void mousePressed(MouseEvent e)
196 {
197 JList list = (JList) e.getSource();
198 int index = list.locationToIndex(e.getPoint());
199 CheckListEntry checkbox = (CheckListEntry) list.getModel().getElementAt(index);
200 if (!checkbox.isFixed()) {
201 checkbox.setSelected(!checkbox.isSelected());
202 }
203 checkbox.grabFocus();
204
205 // This is necessary to get the checkbox to repaint in Java 1.5.0
206 list.getSelectionModel().clearSelection();
207 }
208 }
209}
Note: See TracBrowser for help on using the repository browser.