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 ArrayList 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 = null;
|
---|
89 | Object temp = list_data.get(i);
|
---|
90 | if(temp instanceof Entry) {
|
---|
91 | entry = (Entry) temp;
|
---|
92 | }
|
---|
93 | else {
|
---|
94 | entry = new Entry(list_data.get(i));
|
---|
95 | }
|
---|
96 | temp = null;
|
---|
97 | String name = entry.toString();
|
---|
98 | int index = 0;
|
---|
99 | boolean found = false;
|
---|
100 | while(index < model.size() && !found) {
|
---|
101 | Object sibling = model.getElementAt(index);
|
---|
102 | if(name.compareTo(sibling.toString()) <= 0) {
|
---|
103 | model.add(index, entry);
|
---|
104 | found = true;
|
---|
105 | }
|
---|
106 | index++;
|
---|
107 | }
|
---|
108 | if(!found) {
|
---|
109 | model.addElement(entry);
|
---|
110 | }
|
---|
111 | }
|
---|
112 | setModel(model);
|
---|
113 | setCellRenderer(new CellRenderer());
|
---|
114 | addMouseListener(new CheckListListener());
|
---|
115 | setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
---|
116 | }
|
---|
117 |
|
---|
118 | /** Constructor. */
|
---|
119 | public CheckList(ListModel data_model) {
|
---|
120 | super(data_model);
|
---|
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 int getEntryCount() {
|
---|
176 | return getModel().getSize();
|
---|
177 | }
|
---|
178 |
|
---|
179 | /** Checks the entries in the list whose name appear in the given array.
|
---|
180 | * @param names The name of entries to be checked as a <strong>String[]</strong>.
|
---|
181 | * @see org.greenstone.gatherer.checklist.CheckList.Entry
|
---|
182 | */
|
---|
183 | public void setSelected(String names[]) {
|
---|
184 | DefaultListModel model = (DefaultListModel) getModel();
|
---|
185 | int size = model.size();
|
---|
186 | for(int i = 0; i < size; i++) {
|
---|
187 | Entry entry = (Entry) model.get(i);
|
---|
188 | for(int j = 0; names != null && j < names.length; j++) {
|
---|
189 | if(entry.toString().equals(names[j])) {
|
---|
190 | entry.setSelected(true);
|
---|
191 | }
|
---|
192 | }
|
---|
193 | }
|
---|
194 | }
|
---|
195 | /** A custom list cell renderer for producing rows which contain clickable check boxes. */
|
---|
196 | protected class CellRenderer
|
---|
197 | implements ListCellRenderer {
|
---|
198 | /** 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.
|
---|
199 | * @param list The </strong>JList</strong> we're painting.
|
---|
200 | * @param value The value returned by list.getModel().getElementAt(index), as an <strong>Object</strong>.
|
---|
201 | * @param index The cells index as an <i>int</i>.
|
---|
202 | * @param is_selected <i>true</i> if the specified cell was selected, <i>false</i> otherwise.
|
---|
203 | * @param cell_has_focus <i>true</i> if and only if the specified cell has the focus.
|
---|
204 | * @return A <strong>Component</strong> whose paint() method will render the specified value.
|
---|
205 | */
|
---|
206 | public Component getListCellRendererComponent(JList list, Object value, int index, boolean is_selected, boolean cell_has_focus) {
|
---|
207 | JCheckBox checkbox = (JCheckBox) value;
|
---|
208 | checkbox.setBackground(is_selected ? list.getSelectionBackground() : list.getBackground());
|
---|
209 | checkbox.setForeground(is_selected ? list.getSelectionForeground() : list.getForeground());
|
---|
210 | checkbox.setEnabled(list.isEnabled());
|
---|
211 | checkbox.setFont(list.getFont());
|
---|
212 | checkbox.setFocusPainted(false);
|
---|
213 | checkbox.setBorderPainted(true);
|
---|
214 | checkbox.setBorder((is_selected) ? UIManager.getBorder("List.focusCellHighlightBorder") : noFocusBorder);
|
---|
215 | return checkbox;
|
---|
216 | }
|
---|
217 | }
|
---|
218 | /** Listens for clicks apon the checks within the list, and updates as necessary. */
|
---|
219 | private class CheckListListener
|
---|
220 | extends MouseAdapter {
|
---|
221 | //private Entry previous_checkbox = null;
|
---|
222 | /** Called whenever the mouse is clicked over our list. We find the nearest checkbox and change its state.
|
---|
223 | * @param e A <strong>MouseEvent</strong> containing everything you ever wanted to know about the mouse event but were afraid to ask.
|
---|
224 | */
|
---|
225 | public void mousePressed(MouseEvent e) {
|
---|
226 | JList list = (JList) e.getSource();
|
---|
227 | int index = list.locationToIndex(e.getPoint());
|
---|
228 | Entry checkbox = (Entry)list.getModel().getElementAt(index);
|
---|
229 | // If this is the same checkbox as was recently selected, change the tick.
|
---|
230 | if (list.isSelectedIndex(index)) {
|
---|
231 | if(!checkbox.isFixed()) {
|
---|
232 | checkbox.setSelected(!checkbox.isSelected());
|
---|
233 | }
|
---|
234 | checkbox.grabFocus();
|
---|
235 | }
|
---|
236 | /*
|
---|
237 | if(list.isSelectedIndex(index) && checkbox == previous_checkbox) {
|
---|
238 | if(!checkbox.isFixed()) {
|
---|
239 | checkbox.setSelected(!checkbox.isSelected());
|
---|
240 | }
|
---|
241 | checkbox.grabFocus();
|
---|
242 | }
|
---|
243 | // Otherwise the selection has just changed, so select the new checkbox
|
---|
244 | else if(list.isSelectedIndex(index)) {
|
---|
245 | previous_checkbox = checkbox;
|
---|
246 | }
|
---|
247 | else {
|
---|
248 | previous_checkbox = null;
|
---|
249 | }
|
---|
250 | */
|
---|
251 | }
|
---|
252 | }
|
---|
253 | }
|
---|
254 |
|
---|