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

Last change on this file since 36255 was 36255, checked in by kjdon, 23 months ago

added a numTicked method

  • Property svn:keywords set to Author Date Id Revision
File size: 8.2 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 clearTicked()
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 /** Retrieve all the entries from the list
100 */
101 public ArrayList getAll()
102 {
103 ArrayList result = new ArrayList();
104 DefaultListModel model = (DefaultListModel) getModel();
105 for (int i = 0; i < model.size(); i++) {
106 CheckListEntry entry = (CheckListEntry) model.get(i);
107 result.add(entry.getObject());
108 }
109 return result;
110 }
111
112
113 /** Retrieve the currently ticked entries from this list.
114 * @return An <strong>ArrayList</strong> containing only those entries from the initial list that are checked.
115 * @see org.greenstone.gatherer.checklist.Entry
116 */
117 public ArrayList getTicked()
118 {
119 ArrayList result = new ArrayList();
120 DefaultListModel model = (DefaultListModel) getModel();
121 for (int i = 0; i < model.size(); i++) {
122 CheckListEntry entry = (CheckListEntry) model.get(i);
123 if (entry.isSelected()) {
124 result.add(entry.getObject());
125 }
126 }
127 return result;
128 }
129
130 public int numTicked() {
131 int count = 0;
132 DefaultListModel model = (DefaultListModel) getModel();
133 for (int i = 0; i < model.size(); i++) {
134 CheckListEntry entry = (CheckListEntry) model.get(i);
135 if (entry.isSelected()) {
136 count ++;
137 }
138 }
139 return count;
140 }
141
142
143 /** This is different from isSelectionEmpty! */
144 public boolean isNothingTicked()
145 {
146 DefaultListModel model = (DefaultListModel) getModel();
147 for (int i = 0; i < model.size(); i++) {
148 if (((CheckListEntry) model.get(i)).isSelected()) {
149 return false;
150 }
151 }
152
153 return true;
154 }
155
156
157 public void setListData(ArrayList list_data)
158 {
159 // Create a new model.
160 setModel(new DefaultListModel());
161
162 // Add the items from the list to the model
163 for (int i = 0; i < list_data.size(); i++) {
164 Object list_object = list_data.get(i);
165 if (list_object instanceof CheckListEntry) {
166 addEntry((CheckListEntry) list_object);
167 }
168 else {
169 addEntry(new CheckListEntry(list_object));
170 }
171 }
172 }
173
174
175 public void setTickedObjects(Object[] objects)
176 {
177 if (objects == null) {
178 return;
179 }
180
181 DefaultListModel model = (DefaultListModel) getModel();
182 for (int i = 0; i < model.size(); i++) {
183 CheckListEntry entry = (CheckListEntry) model.get(i);
184 for (int j = 0; j < objects.length; j++) {
185 if (entry.getObject().equals(objects[j])) {
186 entry.setSelected(true);
187 }
188 }
189 }
190 updateUI();
191 }
192
193 public void setAllTicked() {
194 DefaultListModel model = (DefaultListModel) getModel();
195 for (int i = 0; i < model.size(); i++) {
196 CheckListEntry entry = (CheckListEntry) model.get(i);
197 entry.setSelected(true);
198 }
199 updateUI();
200 }
201
202 /** A custom list cell renderer for producing rows which contain clickable check boxes. */
203 protected class CheckListCellRenderer
204 implements ListCellRenderer
205 {
206 /** 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.
207 * @param list The </strong>JList</strong> we're painting.
208 * @param value The value returned by list.getModel().getElementAt(index), as an <strong>Object</strong>.
209 * @param index The cells index as an <i>int</i>.
210 * @param is_selected <i>true</i> if the specified cell was selected, <i>false</i> otherwise.
211 * @param cell_has_focus <i>true</i> if and only if the specified cell has the focus.
212 * @return A <strong>Component</strong> whose paint() method will render the specified value.
213 */
214 public Component getListCellRendererComponent(JList list, Object value, int index, boolean is_selected, boolean cell_has_focus) {
215 JCheckBox checkbox = (JCheckBox) value;
216 if (show_selected_row) {
217 checkbox.setBackground(is_selected ? list.getSelectionBackground() : list.getBackground());
218 checkbox.setForeground(is_selected ? list.getSelectionForeground() : list.getForeground());
219 checkbox.setBorderPainted(true);
220 }
221 else {
222 checkbox.setBackground(list.getBackground());
223 checkbox.setForeground(list.getForeground());
224 checkbox.setBorderPainted(false);
225 }
226 checkbox.setEnabled(list.isEnabled());
227 checkbox.setFont(list.getFont());
228 checkbox.setFocusPainted(false);
229 checkbox.setBorder((is_selected) ? UIManager.getBorder("List.focusCellHighlightBorder") : noFocusBorder);
230 return checkbox;
231 }
232 }
233
234
235 /** Listens for clicks apon the checks within the list, and updates as necessary. */
236 private class CheckListMouseListener
237 extends MouseAdapter
238 {
239 /** Called whenever the mouse is clicked over our list. We find the nearest checkbox and change its state.
240 * @param e A <strong>MouseEvent</strong> containing everything you ever wanted to know about the mouse event but were afraid to ask.
241 */
242 public void mousePressed(MouseEvent e)
243 {
244 if (!isEnabled()) return;
245 JList list = (JList) e.getSource();
246 int index = list.locationToIndex(e.getPoint());
247 CheckListEntry checkbox = (CheckListEntry) list.getModel().getElementAt(index);
248 if (!checkbox.isFixed()) {
249 checkbox.setSelected(!checkbox.isSelected());
250 }
251 checkbox.grabFocus();
252
253 // We need to cause a ListSelectionEvent -- this is ugly but I can't find a better way quickly
254 list.removeSelectionInterval(0, 0);
255 list.setSelectionInterval(index, index);
256 }
257 }
258}
Note: See TracBrowser for help on using the repository browser.