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