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.util;
|
---|
38 |
|
---|
39 | import java.awt.*;
|
---|
40 | import java.awt.event.*;
|
---|
41 | import java.util.*;
|
---|
42 | import javax.swing.*;
|
---|
43 | import 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 | */
|
---|
50 | public 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 CheckListCellRenderer());
|
---|
66 | setModel(new DefaultListModel());
|
---|
67 | setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
---|
68 | addMouseListener(new CheckListMouseListener());
|
---|
69 | }
|
---|
70 |
|
---|
71 |
|
---|
72 | public void addEntry(CheckListEntry 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 clearTicked()
|
---|
91 | {
|
---|
92 | DefaultListModel model = (DefaultListModel) getModel();
|
---|
93 | for (int i = 0; i < model.size(); i++) {
|
---|
94 | ((CheckListEntry) model.get(i)).setSelected(false);
|
---|
95 | }
|
---|
96 | updateUI();
|
---|
97 | }
|
---|
98 |
|
---|
99 | /** Retrieve all the entries from the list
|
---|
100 | */
|
---|
101 | public ArrayList getAll()
|
---|
102 | {
|
---|
103 | ArrayList result = new ArrayList();
|
---|
104 | DefaultListModel model = (DefaultListModel) getModel();
|
---|
105 | for (int i = 0; i < model.size(); i++) {
|
---|
106 | CheckListEntry entry = (CheckListEntry) model.get(i);
|
---|
107 | result.add(entry.getObject());
|
---|
108 | }
|
---|
109 | return result;
|
---|
110 | }
|
---|
111 |
|
---|
112 |
|
---|
113 | /** Retrieve the currently ticked entries from this list.
|
---|
114 | * @return An <strong>ArrayList</strong> containing only those entries from the initial list that are checked.
|
---|
115 | * @see org.greenstone.gatherer.checklist.Entry
|
---|
116 | */
|
---|
117 | public ArrayList getTicked()
|
---|
118 | {
|
---|
119 | ArrayList result = new ArrayList();
|
---|
120 | DefaultListModel model = (DefaultListModel) getModel();
|
---|
121 | for (int i = 0; i < model.size(); i++) {
|
---|
122 | CheckListEntry entry = (CheckListEntry) model.get(i);
|
---|
123 | if (entry.isSelected()) {
|
---|
124 | result.add(entry.getObject());
|
---|
125 | }
|
---|
126 | }
|
---|
127 | return result;
|
---|
128 | }
|
---|
129 |
|
---|
130 |
|
---|
131 | /** This is different from isSelectionEmpty! */
|
---|
132 | public boolean isNothingTicked()
|
---|
133 | {
|
---|
134 | DefaultListModel model = (DefaultListModel) getModel();
|
---|
135 | for (int i = 0; i < model.size(); i++) {
|
---|
136 | if (((CheckListEntry) model.get(i)).isSelected()) {
|
---|
137 | return false;
|
---|
138 | }
|
---|
139 | }
|
---|
140 |
|
---|
141 | return true;
|
---|
142 | }
|
---|
143 |
|
---|
144 |
|
---|
145 | public void setListData(ArrayList list_data)
|
---|
146 | {
|
---|
147 | // Create a new model.
|
---|
148 | setModel(new DefaultListModel());
|
---|
149 |
|
---|
150 | // Add the items from the list to the model
|
---|
151 | for (int i = 0; i < list_data.size(); i++) {
|
---|
152 | Object list_object = list_data.get(i);
|
---|
153 | if (list_object instanceof CheckListEntry) {
|
---|
154 | addEntry((CheckListEntry) list_object);
|
---|
155 | }
|
---|
156 | else {
|
---|
157 | addEntry(new CheckListEntry(list_object));
|
---|
158 | }
|
---|
159 | }
|
---|
160 | }
|
---|
161 |
|
---|
162 |
|
---|
163 | public void setTickedObjects(Object[] objects)
|
---|
164 | {
|
---|
165 | if (objects == null) {
|
---|
166 | return;
|
---|
167 | }
|
---|
168 |
|
---|
169 | DefaultListModel model = (DefaultListModel) getModel();
|
---|
170 | for (int i = 0; i < model.size(); i++) {
|
---|
171 | CheckListEntry entry = (CheckListEntry) model.get(i);
|
---|
172 | for (int j = 0; j < objects.length; j++) {
|
---|
173 | if (entry.getObject().equals(objects[j])) {
|
---|
174 | entry.setSelected(true);
|
---|
175 | }
|
---|
176 | }
|
---|
177 | }
|
---|
178 | updateUI();
|
---|
179 | }
|
---|
180 |
|
---|
181 | public void setAllTicked() {
|
---|
182 | DefaultListModel model = (DefaultListModel) getModel();
|
---|
183 | for (int i = 0; i < model.size(); i++) {
|
---|
184 | CheckListEntry entry = (CheckListEntry) model.get(i);
|
---|
185 | entry.setSelected(true);
|
---|
186 | }
|
---|
187 | updateUI();
|
---|
188 | }
|
---|
189 |
|
---|
190 | /** A custom list cell renderer for producing rows which contain clickable check boxes. */
|
---|
191 | protected class CheckListCellRenderer
|
---|
192 | implements ListCellRenderer
|
---|
193 | {
|
---|
194 | /** 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.
|
---|
195 | * @param list The </strong>JList</strong> we're painting.
|
---|
196 | * @param value The value returned by list.getModel().getElementAt(index), as an <strong>Object</strong>.
|
---|
197 | * @param index The cells index as an <i>int</i>.
|
---|
198 | * @param is_selected <i>true</i> if the specified cell was selected, <i>false</i> otherwise.
|
---|
199 | * @param cell_has_focus <i>true</i> if and only if the specified cell has the focus.
|
---|
200 | * @return A <strong>Component</strong> whose paint() method will render the specified value.
|
---|
201 | */
|
---|
202 | public Component getListCellRendererComponent(JList list, Object value, int index, boolean is_selected, boolean cell_has_focus) {
|
---|
203 | JCheckBox checkbox = (JCheckBox) value;
|
---|
204 | if (show_selected_row) {
|
---|
205 | checkbox.setBackground(is_selected ? list.getSelectionBackground() : list.getBackground());
|
---|
206 | checkbox.setForeground(is_selected ? list.getSelectionForeground() : list.getForeground());
|
---|
207 | checkbox.setBorderPainted(true);
|
---|
208 | }
|
---|
209 | else {
|
---|
210 | checkbox.setBackground(list.getBackground());
|
---|
211 | checkbox.setForeground(list.getForeground());
|
---|
212 | checkbox.setBorderPainted(false);
|
---|
213 | }
|
---|
214 | checkbox.setEnabled(list.isEnabled());
|
---|
215 | checkbox.setFont(list.getFont());
|
---|
216 | checkbox.setFocusPainted(false);
|
---|
217 | checkbox.setBorder((is_selected) ? UIManager.getBorder("List.focusCellHighlightBorder") : noFocusBorder);
|
---|
218 | return checkbox;
|
---|
219 | }
|
---|
220 | }
|
---|
221 |
|
---|
222 |
|
---|
223 | /** Listens for clicks apon the checks within the list, and updates as necessary. */
|
---|
224 | private class CheckListMouseListener
|
---|
225 | extends MouseAdapter
|
---|
226 | {
|
---|
227 | /** Called whenever the mouse is clicked over our list. We find the nearest checkbox and change its state.
|
---|
228 | * @param e A <strong>MouseEvent</strong> containing everything you ever wanted to know about the mouse event but were afraid to ask.
|
---|
229 | */
|
---|
230 | public void mousePressed(MouseEvent e)
|
---|
231 | {
|
---|
232 | if (!isEnabled()) return;
|
---|
233 | JList list = (JList) e.getSource();
|
---|
234 | int index = list.locationToIndex(e.getPoint());
|
---|
235 | CheckListEntry checkbox = (CheckListEntry) list.getModel().getElementAt(index);
|
---|
236 | if (!checkbox.isFixed()) {
|
---|
237 | checkbox.setSelected(!checkbox.isSelected());
|
---|
238 | }
|
---|
239 | checkbox.grabFocus();
|
---|
240 |
|
---|
241 | // We need to cause a ListSelectionEvent -- this is ugly but I can't find a better way quickly
|
---|
242 | list.removeSelectionInterval(0, 0);
|
---|
243 | list.setSelectionInterval(index, index);
|
---|
244 | }
|
---|
245 | }
|
---|
246 | }
|
---|