source: trunk/gli/src/org/greenstone/gatherer/checklist/CheckList.java@ 8496

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

Started off fixing a bug where the loaded collection wasn't being ticked on in the cross-collection searching page. Ended up removing a ton of stuff from the CheckList class, some of which was duplicated code and buggy.

  • Property svn:keywords set to Author Date Id Revision
File size: 7.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.checklist;
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 CellRenderer());
66 setModel(new DefaultListModel());
67 setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
68 addMouseListener(new CheckListListener());
69 }
70
71
72 public void addEntry(Entry 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 ((Entry) 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 Entry entry = (Entry) model.get(i);
110 if (entry.isSelected()) {
111 result.add(entry.getObject());
112 }
113 }
114 return result;
115 }
116
117
118// public boolean isSelectionEmpty()
119// {
120// System.err.println("In CheckList.isSelectionEmpty()...");
121// DefaultListModel model = (DefaultListModel) getModel();
122// for (int i = 0; i < model.size(); i++) {
123// Entry entry = (Entry) model.get(i);
124// if (entry.isSelected()) {
125// return false;
126// }
127// }
128
129// return true;
130// }
131
132
133 public void setListData(ArrayList list_data)
134 {
135 // Create a new model.
136 setModel(new DefaultListModel());
137
138 // Add the items from the list to the model
139 for (int i = 0; i < list_data.size(); i++) {
140 Object list_object = list_data.get(i);
141 if (list_object instanceof Entry) {
142 addEntry((Entry) list_object);
143 }
144 else {
145 addEntry(new Entry(list_object));
146 }
147 }
148 }
149
150
151 public void setSelectedObjects(Object objects[])
152 {
153 if (objects == null) {
154 return;
155 }
156
157 DefaultListModel model = (DefaultListModel) getModel();
158 for (int i = 0; i < model.size(); i++) {
159 Entry entry = (Entry) model.get(i);
160 for (int j = 0; j < objects.length; j++) {
161 if (entry.getObject().equals(objects[j])) {
162 entry.setSelected(true);
163 }
164 }
165 }
166 updateUI();
167 }
168
169
170 private void selectionChanged(int index) {
171 fireSelectionValueChanged(index, index, false);
172 }
173
174
175 /** A custom list cell renderer for producing rows which contain clickable check boxes. */
176 protected class CellRenderer
177 implements ListCellRenderer {
178
179 /** 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.
180 * @param list The </strong>JList</strong> we're painting.
181 * @param value The value returned by list.getModel().getElementAt(index), as an <strong>Object</strong>.
182 * @param index The cells index as an <i>int</i>.
183 * @param is_selected <i>true</i> if the specified cell was selected, <i>false</i> otherwise.
184 * @param cell_has_focus <i>true</i> if and only if the specified cell has the focus.
185 * @return A <strong>Component</strong> whose paint() method will render the specified value.
186 */
187 public Component getListCellRendererComponent(JList list, Object value, int index, boolean is_selected, boolean cell_has_focus) {
188 JCheckBox checkbox = (JCheckBox) value;
189 if(show_selected_row) {
190 checkbox.setBackground(is_selected ? list.getSelectionBackground() : list.getBackground());
191 checkbox.setForeground(is_selected ? list.getSelectionForeground() : list.getForeground());
192 checkbox.setBorderPainted(true);
193 }
194 else {
195 checkbox.setBackground(list.getBackground());
196 checkbox.setForeground(list.getForeground());
197 checkbox.setBorderPainted(false);
198 }
199 checkbox.setEnabled(list.isEnabled());
200 checkbox.setFont(list.getFont());
201 checkbox.setFocusPainted(false);
202 checkbox.setBorder((is_selected) ? UIManager.getBorder("List.focusCellHighlightBorder") : noFocusBorder);
203 return checkbox;
204 }
205 }
206
207
208 /** Listens for clicks apon the checks within the list, and updates as necessary. */
209 private class CheckListListener
210 extends MouseAdapter {
211
212 /** Called whenever the mouse is clicked over our list. We find the nearest checkbox and change its state.
213 * @param e A <strong>MouseEvent</strong> containing everything you ever wanted to know about the mouse event but were afraid to ask.
214 */
215 public void mousePressed(MouseEvent e) {
216 JList list = (JList) e.getSource();
217 int index = list.locationToIndex(e.getPoint());
218 Entry checkbox = (Entry)list.getModel().getElementAt(index);
219 if(!checkbox.isFixed()) {
220 checkbox.setSelected(!checkbox.isSelected());
221 }
222 checkbox.grabFocus();
223 // Fire a selection changed event
224 selectionChanged(index);
225 }
226 }
227}
Note: See TracBrowser for help on using the repository browser.