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

Last change on this file since 5581 was 5581, checked in by mdewsnip, 21 years ago

Many formatting, structural and code improvements.

  • Property svn:keywords set to Author Date Id Revision
File size: 10.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
39/**************************************************************************************
40 * Title: Gatherer
41 * Description: The Gatherer: a tool for gathering and enriching a digital collection.
42 * Company: The University of Waikato
43 * Written: 14/08/02
44 * Revised:
45 **************************************************************************************/
46import java.awt.*;
47import java.awt.event.*;
48import java.util.*;
49import javax.swing.*;
50import javax.swing.border.*;
51import org.greenstone.gatherer.checklist.Entry;
52
53/** 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.
54 * @author John Thompson
55 * @version 2.3
56 */
57// ####################################################################################
58// Optimization Saving
59// ####################################################################################
60// Vector -> ArrayList + Processor
61// ####################################################################################
62public class CheckList
63 extends JList {
64 /** The border used when a list row is not in focus. */
65 protected static Border noFocusBorder = new EmptyBorder(1, 1, 1, 1);
66
67 private boolean show_selected_row = true;
68 /** Constructor. */
69 public CheckList(boolean show_selected_row) {
70 super();
71 this.show_selected_row = show_selected_row;
72 setCellRenderer(new CellRenderer());
73 setModel(new DefaultListModel());
74 setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
75 addMouseListener(new CheckListListener());
76 }
77 /** Constructor.
78 * @param list_data An ArrayList of entries for this checklist.
79 * @see org.greenstone.gatherer.checklist.CheckList.Entry
80 */
81 public CheckList(ArrayList list_data, boolean show_selected_row) {
82 super();
83 this.show_selected_row = show_selected_row;
84 // Create a new model.
85 DefaultListModel model = new DefaultListModel();
86 for(int i = 0; i < list_data.size(); i++) {
87 Entry entry = null;
88 Object temp = list_data.get(i);
89 if(temp instanceof Entry) {
90 entry = (Entry) temp;
91 }
92 else {
93 entry = new Entry(list_data.get(i));
94 }
95 temp = null;
96 String name = entry.toString();
97 int index = 0;
98 boolean found = false;
99 while(index < model.size() && !found) {
100 Object sibling = model.getElementAt(index);
101 if(name.compareTo(sibling.toString()) <= 0) {
102 model.add(index, entry);
103 found = true;
104 }
105 index++;
106 }
107 if(!found) {
108 model.addElement(entry);
109 }
110 }
111 setModel(model);
112 setCellRenderer(new CellRenderer());
113 addMouseListener(new CheckListListener());
114 setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
115 }
116
117 /** Constructor. */
118 public CheckList(ListModel data_model, boolean show_selected_row) {
119 super(data_model);
120 this.show_selected_row = show_selected_row;
121 setCellRenderer(new CellRenderer());
122 addMouseListener(new CheckListListener());
123 setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
124 }
125
126 public void addEntry(Entry entry) {
127 DefaultListModel model = (DefaultListModel) getModel();
128 String name = entry.toString();
129 int index = 0;
130 boolean found = false;
131 while(index < model.size() && !found) {
132 Object sibling = model.getElementAt(index);
133 if(name.compareTo(sibling.toString()) <= 0) {
134 model.add(index, entry);
135 found = true;
136 }
137 index++;
138 sibling = null;
139 }
140 if(!found) {
141 model.addElement(entry);
142 }
143 name = null;
144 model = null;
145 }
146
147 public Entry get(int index) {
148 DefaultListModel model = (DefaultListModel) getModel();
149 return (Entry) model.get(index);
150 }
151
152 /** Retrieve the currently ticked entries from this list.
153 * @return An <strong>ArrayList</strong> containing only those entries from the initial list that are checked.
154 * @see org.greenstone.gatherer.checklist.CheckList.Entry
155 */
156 public ArrayList getSelected() {
157 ArrayList result = new ArrayList();
158 DefaultListModel model = (DefaultListModel) getModel();
159 int size = model.size();
160 for(int i = 0; i < size; i++) {
161 Entry entry = (Entry) model.get(i);
162 if(entry.isSelected()) {
163 result.add(entry.getObject());
164 }
165 }
166 return result;
167 }
168
169 public boolean isSelected(int index) {
170 DefaultListModel model = (DefaultListModel) getModel();
171 Entry entry = (Entry) model.get(index);
172 return entry.isSelected();
173 }
174
175 public boolean isSelectionEmpty() {
176 DefaultListModel model = (DefaultListModel) getModel();
177 int size = model.size();
178 for(int i = 0; i < size; i++) {
179 Entry entry = (Entry) model.get(i);
180 if(entry.isSelected()) {
181 entry = null;
182 model = null;
183 return false;
184 }
185 entry = null;
186 }
187 model = null;
188 return true;
189 }
190
191 public int getEntryCount() {
192 return getModel().getSize();
193 }
194
195 public void setListData(Object[] list_data) {
196 // Create a new model.
197 DefaultListModel model = new DefaultListModel();
198 for(int i = 0; i < list_data.length; i++) {
199 Entry entry = null;
200 Object temp = list_data[i];
201 if(temp instanceof Entry) {
202 entry = (Entry) temp;
203 }
204 else {
205 entry = new Entry(temp);
206 }
207 temp = null;
208 String name = entry.toString();
209 int index = 0;
210 boolean found = false;
211 while(index < model.size() && !found) {
212 Object sibling = model.getElementAt(index);
213 if(name.compareTo(sibling.toString()) <= 0) {
214 model.add(index, entry);
215 found = true;
216 }
217 index++;
218 }
219 if(!found) {
220 model.addElement(entry);
221 }
222 }
223 setModel(model);
224 }
225
226 /** Checks the entries in the list whose name appear in the given array.
227 * @param names The name of entries to be checked as a <strong>String[]</strong>.
228 * @see org.greenstone.gatherer.checklist.CheckList.Entry
229 */
230 public void setSelected(String names[]) {
231 DefaultListModel model = (DefaultListModel) getModel();
232 int size = model.size();
233 for(int i = 0; i < size; i++) {
234 Entry entry = (Entry) model.get(i);
235 for(int j = 0; names != null && j < names.length; j++) {
236 if(entry.toString().equals(names[j])) {
237 entry.setSelected(true);
238 }
239 }
240 }
241 }
242
243 private void selectionChanged(int index) {
244 fireSelectionValueChanged(index, index, false);
245 }
246
247 /** A custom list cell renderer for producing rows which contain clickable check boxes. */
248 protected class CellRenderer
249 implements ListCellRenderer {
250 /** 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.
251 * @param list The </strong>JList</strong> we're painting.
252 * @param value The value returned by list.getModel().getElementAt(index), as an <strong>Object</strong>.
253 * @param index The cells index as an <i>int</i>.
254 * @param is_selected <i>true</i> if the specified cell was selected, <i>false</i> otherwise.
255 * @param cell_has_focus <i>true</i> if and only if the specified cell has the focus.
256 * @return A <strong>Component</strong> whose paint() method will render the specified value.
257 */
258 public Component getListCellRendererComponent(JList list, Object value, int index, boolean is_selected, boolean cell_has_focus) {
259 JCheckBox checkbox = (JCheckBox) value;
260 if(show_selected_row) {
261 checkbox.setBackground(is_selected ? list.getSelectionBackground() : list.getBackground());
262 checkbox.setForeground(is_selected ? list.getSelectionForeground() : list.getForeground());
263 checkbox.setBorderPainted(true);
264 }
265 else {
266 checkbox.setBackground(list.getBackground());
267 checkbox.setForeground(list.getForeground());
268 checkbox.setBorderPainted(false);
269 }
270 checkbox.setEnabled(list.isEnabled());
271 checkbox.setFont(list.getFont());
272 checkbox.setFocusPainted(false);
273 checkbox.setBorder((is_selected) ? UIManager.getBorder("List.focusCellHighlightBorder") : noFocusBorder);
274 return checkbox;
275 }
276 }
277 /** Listens for clicks apon the checks within the list, and updates as necessary. */
278 private class CheckListListener
279 extends MouseAdapter {
280
281 //private Entry previous_checkbox = null;
282 /** Called whenever the mouse is clicked over our list. We find the nearest checkbox and change its state.
283 * @param e A <strong>MouseEvent</strong> containing everything you ever wanted to know about the mouse event but were afraid to ask.
284 */
285 public void mousePressed(MouseEvent e) {
286 JList list = (JList) e.getSource();
287 int index = list.locationToIndex(e.getPoint());
288 Entry checkbox = (Entry)list.getModel().getElementAt(index);
289 if(!checkbox.isFixed()) {
290 checkbox.setSelected(!checkbox.isSelected());
291 }
292 checkbox.grabFocus();
293 // Fire a selection changed event
294 selectionChanged(index);
295 }
296 }
297}
Note: See TracBrowser for help on using the repository browser.