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