source: trunk/gli/src/org/greenstone/gatherer/gui/Filter.java@ 13531

Last change on this file since 13531 was 13531, checked in by mdewsnip, 17 years ago

Removed some unused functions.

  • Property svn:keywords set to Author Date Id Revision
File size: 11.4 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.gui;
38
39import java.awt.*;
40import java.awt.event.*;
41import java.util.*;
42import java.util.regex.*;
43import javax.swing.*;
44import javax.swing.tree.*;
45import org.greenstone.gatherer.Dictionary;
46import org.greenstone.gatherer.Gatherer;
47import org.greenstone.gatherer.file.FileSystemModel;
48import org.greenstone.gatherer.gui.tree.DragTree;
49
50/** <p>This object allows the user to set a filter on one of the workspace trees, specifying a preset type, or a regular expression that a files must match to be in the tree. Note that all directories are included. This class includes the controls for editing the filter. The trick is that several instances of the Filter class can share the same internal data (termed a 'run' of filters), so that the filter set on the GatherPane and the EnrichPane are virtually the same.</p>
51 * <p>The regular expression typed uses '*' as a wildcard character (equivalent to '.*'), and does not use '.' to match any single character (use '?' instead).</p>
52 * @author John Thompson, Greenstone Digital Library, University of Waikato
53 * @version 2.3
54 */
55public class Filter
56 extends JPanel {
57 /** The other filters in this run of filters, used to ensure they all show the same thing. */
58 private ArrayList others = null;
59 /** Is this the first filter of this run of filters created (later filters will share the same information). */
60 private boolean first = true;
61 /** Prevent any changes we make in the class from causing events which we then process causing events... */
62 private boolean ignore = false;
63 /** A reference to ourselves so inner classes can refer to us. */
64 private Filter this_filter = null;
65 /** The check box to enable/disable filter. */
66 private JCheckBox checkbox = null;
67 /** The editable combobox where you either choose a predefined filter, or type a new pseudo-regular expression. */
68 private GComboBox combobox = null;
69 /** The label shown on the filter controls. */
70 private JLabel label = null;
71 /** A reference to the tree this filter is being applied to. */
72 private DragTree tree = null;
73 /** The default size for the label. */
74 static final private Dimension SIZE = new Dimension(100,30);
75 /** Preprogrammed default filters. */
76 static final private String DEFAULTS[] = {"^.*\\.html?$", "^.*\\.xml$", "^.*\\.txt$", "(^.*\\.jpe?g$)|(^.*\\.png$)|(^.*\\.gif$)|(^.*\\.bmp$)|(^.*\\.tiff?$)"};
77
78 /** Constructor.
79 * @param tree A reference to the <strong>JTree</strong> being affected.
80 */
81 public Filter(DragTree tree) {
82 this(tree, null);
83 }
84
85 /** Constructor.
86 * @param tree A reference to the <strong>JTree</strong> being affected.
87 * @param others An <strong>ArrayList</strong> of the other Filters already in this run.
88 */
89 public Filter(DragTree tree, ArrayList others) {
90 super();
91 if (others == null) {
92 others = new ArrayList();
93 }
94 this.others = others;
95 this.others.add(this);
96 this.this_filter = this;
97 this.tree = tree;
98 // Create components.
99 combobox = new GComboBox(true);
100 try {
101 combobox.add(new Entry());
102 }
103 catch (Exception error) {
104 error.printStackTrace();
105 }
106 for(int i = 0; i < DEFAULTS.length; i++) {
107 try {
108 Entry entry = new Entry(Dictionary.get("Filter." + i), Pattern.compile(DEFAULTS[i]));
109 combobox.add(entry);
110 }
111 catch (Exception error) {
112 error.printStackTrace();
113 }
114 }
115 label = new JLabel(Dictionary.get("Filter.Filter_Tree"));
116 combobox.setToolTipText(Dictionary.get("Collection.Filter_Tooltip"));
117
118 // Add listeners.
119 combobox.addActionListener(new ComboBoxListener());
120 // Layout.
121 label.setBorder(BorderFactory.createEmptyBorder(0,0,0,5));
122
123 setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
124 setLayout(new BorderLayout());
125 add(label, BorderLayout.WEST);
126 add(combobox, BorderLayout.CENTER);
127 }
128
129 /** Used to restore the filter state to enabled, the normal state during collection editing.
130 * @param state The new state for the filter. <i>true</i> for enabled, <i>false</i> otherwise.
131 */
132 public void setEnabled(boolean state) {
133 ignore = true;
134 combobox.setEnabled(state);
135 ignore = false;
136 }
137
138 /** Set the combobox model for this filter.
139 * @param model The new <strong>ComboBoxModel</strong> to use.
140 */
141 public void setComboBoxModel(ComboBoxModel model) {
142 combobox.setModel(model);
143 }
144
145 /** Ensure that a certain entry is selected from the combobox.
146 * @param selection The <strong>Entry</strong> that should be selected.
147 */
148 public void setComboBoxSelection(Entry selection) {
149 ignore = true;
150 combobox.setSelectedItem(selection);
151 ignore = false;
152 }
153
154 /** Sets whether this combobox is editable or not
155 * @param value true to make the control editable, false otherwise
156 */
157 public void setEditable(boolean value) {
158 combobox.setEditable(value);
159 }
160
161 /** Set to signify that this filter is the first in a new run of filters.
162 * @param first <i>true</i> if this is the first filter in a run, <i>false</i> if it will just be added to the current run.
163 */
164 public void setFirst(boolean first) {
165 this.first = first;
166 }
167
168 /** Encode an expression in pseudo-regular expression into regular expression.
169 * @param raw The pseudo-regular expression <strong>String</strong> which includes several characters which differ in meaning from regular expression queries.
170 * @return A proper regular expression as a <strong>String</strong>.
171 */
172 private String encode(String raw) {
173 StringBuffer working = new StringBuffer();
174 for(int i = 0; i < raw.length(); i++) {
175 char c = raw.charAt(i);
176 switch(c) {
177 case '.':
178 working.append("\\.");
179 break;
180 case '*':
181 working.append(".*");
182 break;
183 case '?':
184 working.append(".");
185 break;
186 default:
187 working.append(Character.toLowerCase(c));
188 }
189 }
190 return working.toString();
191 }
192 /** This method applies the given pattern to the tree registered as belonging to this filter.*/
193 private void setFilter(Pattern pattern) {
194 // Show busy cursor.
195 Gatherer.g_man.wait(true);
196 FileSystemModel model = (FileSystemModel) tree.getModel();
197 // Apply filter
198 if(pattern != null) {
199 model.setFilter(pattern.pattern());
200 }
201 else {
202 model.setFilter(null);
203 }
204 // Ask tree to completely refresh
205 tree.refresh(null);
206 // Restore cursor
207 Gatherer.g_man.wait(false);
208 }
209
210 /** Listens for changes in the combobox as when one is detected attempts to compile a regular expression from whatever text was entered. If successful, or if the item chosen was a predefined filter, it then applies the filter to the target tree. */
211 private class ComboBoxListener
212 implements ActionListener {
213 /** Called when a new item is selected from the filter combobox, we treat the new entry as a pseudo-regular expression, compile it and then apply it to the tree.
214 * @param event An <strong>ActionEvent</strong> containing more information about the change performed.
215 * @see org.greenstone.gatherer.gui.Filter.Entry
216 */
217 public void actionPerformed(ActionEvent event) {
218 try {
219 Object temp = combobox.getSelectedItem();
220 Entry entry = null;
221 if(temp instanceof String) {
222 String temp_str = (String) temp;
223 ///ystem.err.println("Filter = " + temp_str);
224
225 // Ignore any string which matches a predefined filter
226 if(temp_str.equals(Dictionary.get("Filter.All_Files"))) {
227 }
228 // HTM & HTML
229 else if(temp_str.equals(Dictionary.get("Filter.0"))) {
230 }
231 // XML
232 else if(temp_str.equals(Dictionary.get("Filter.1"))) {
233 }
234 // Text files
235 else if(temp_str.equals(Dictionary.get("Filter.2"))) {
236 }
237 // Images
238 else if(temp_str.equals(Dictionary.get("Filter.3"))) {
239 }
240 else {
241 // Make sure the filter isn't already in the list
242 boolean already_exists = false;
243 for (int i = 0; i < combobox.getItemCount(); i++) {
244 if (temp_str.equals(combobox.getItemAt(i).toString())) {
245 already_exists = true;
246 entry = (Entry) combobox.getItemAt(i);
247 break;
248 }
249 }
250
251 if (already_exists == false) {
252 entry = new Entry(temp_str, Pattern.compile(encode(temp_str)));
253 int position = combobox.getItemCount();
254 combobox.insertItemAt(entry, position);
255 combobox.setSelectedIndex(position);
256 }
257 }
258 }
259 else {
260 ///ystem.err.println("Custom Filter");
261 entry = (Entry) temp;
262 }
263 if(entry != null) {
264 setFilter(entry.getPattern());
265 }
266 // Else we ignore this event as being one of the painfully erroneous events we receive because we've been silly enough to have an editable combobox.
267 }
268 catch (PatternSyntaxException error) {
269 if(first) {
270 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("Filter.Invalid_Pattern"), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
271 }
272 }
273 }
274 }
275 /** An object that holds a filter entry. This is string used for the filter pattern and, if not custom built, its name. */
276 private class Entry
277 implements Comparable {
278 /** The compiled pattern created from a regular expression. */
279 private Pattern pattern = null;
280 /** The name of this filter entry. */
281 private String name = null;
282 /** Constructor. */
283 public Entry() {
284 }
285 /** Constructor.
286 * @param name The name of this entry as a <strong>String</strong>.
287 * @param pattern The compiled regular expression as a <strong>Pattern</strong>.
288 */
289 public Entry(String name, Pattern pattern) {
290 this.name = name;
291 this.pattern = pattern;
292 }
293 /** Compare two Entrys for ordering.
294 * @param object The other Entry to compare to, as an <strong>Object</strong>.
295 * @return An <i>int</i> indicating the respective ordering, as defined in java.lang.String#compareTo
296 */
297 public int compareTo(Object object) {
298 return toString().compareTo(object.toString());
299 }
300 /** Retrieve the pattern associated with this entry.
301 * @return The <strong>Pattern</strong>.
302 */
303 public Pattern getPattern() {
304 return pattern;
305 }
306 /** Translate this entry into a textual representation.
307 * @return A <strong>String</strong> containing the representation.
308 */
309 public String toString() {
310 String result = null;
311 if (name != null) {
312 result = name;
313 }
314 else if (pattern == null) {
315 result = Dictionary.get("Filter.All_Files");
316 }
317 else {
318 result = pattern.pattern();
319 }
320 return result;
321 }
322 }
323}
Note: See TracBrowser for help on using the repository browser.