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

Last change on this file since 5590 was 5564, checked in by mdewsnip, 21 years ago

Many more small improvements and tooltips added. Still more to come!

  • Property svn:keywords set to Author Date Id Revision
File size: 12.1 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 CollectionPane and the MetaEditPane 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,25);
75 /** Preprogrammed default filters. */
76 static final private String DEFAULTS[] = {"^.*\\.html?$", "^.*\\.xml$", "^.*\\.txt$", "(^.*\\.jpe?g$)|(^.*\\.png$)|(^.\\.gif$)|(^.\\.bmp$)|(^.\\.tif$)"};
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();
100 combobox.add(new Entry());
101 for(int i = 0; i < DEFAULTS.length; i++) {
102 try {
103 Entry entry = new Entry(Dictionary.newget("Filter." + i), Pattern.compile(DEFAULTS[i]));
104 combobox.add(entry);
105 }
106 catch (Exception error) {
107 error.printStackTrace();
108 }
109 }
110 combobox.setEditable(true);
111 label = new JLabel();
112 label.setPreferredSize(SIZE);
113 Dictionary.setText(label, "Filter.Filter_Tree");
114 // Add listeners.
115 combobox.addActionListener(new ComboBoxListener());
116 // Layout.
117 label.setBorder(BorderFactory.createEmptyBorder(0,0,0,5));
118
119 setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
120 setLayout(new BorderLayout());
121 add(label, BorderLayout.WEST);
122 add(combobox, BorderLayout.CENTER);
123 }
124 /** Retrieve the combobox associated with this filter.
125 * @return A <strong>GComboBox</strong>.
126 */
127 public GComboBox getComboBox() {
128 return combobox;
129 }
130
131 /** Change the background color of the filter.
132 * @param color The new background <strong>Color</strong>.
133 */
134 public void setBackground(Color color) {
135 super.setBackground(color);
136 }
137
138 /** Used to restore the filter state to enabled, the normal state during collection editing.
139 * @param state The new state for the filter. <i>true</i> for enabled, <i>false</i> otherwise.
140 */
141 public void setEnabled(boolean state) {
142 ignore = true;
143 combobox.setEditable(state);
144 combobox.setEnabled(state);
145 ignore = false;
146 }
147
148 /** Set the combobox model for this filter.
149 * @param model The new <strong>ComboBoxModel</strong> to use.
150 */
151 public void setComboBoxModel(ComboBoxModel model) {
152 combobox.setModel(model);
153 }
154
155 /** Ensure that a certain entry is selected from the combobox.
156 * @param selection The <strong>Entry</strong> that should be selected.
157 */
158 public void setComboBoxSelection(Entry selection) {
159 ignore = true;
160 combobox.setSelectedItem(selection);
161 ignore = false;
162 }
163
164 /** Set to signify that this filter is the first in a new run of filters.
165 * @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.
166 */
167 public void setFirst(boolean first) {
168 this.first = first;
169 }
170
171 /** Spawn produces a copy of this filter, which has new controls, but shares listeners with this filter, and vice versa. Thus we can have two synchronized, but differing sets of controls.
172 * @param tree_spawn The <strong>JTree</strong> this filter will affect.
173 * @return A new <strong>Filter</strong> which is in the same run as this filter.
174 */
175 public Filter spawn(DragTree tree_spawn) {
176 Filter filter = new Filter(tree_spawn, others);
177 filter.setFirst(false); // No spawned copies should generate error messages, but fail silently.
178 filter.setComboBoxModel(combobox.getModel());
179 filter.setComboBoxSelection((Entry)combobox.getSelectedItem());
180 return filter;
181 }
182
183 /** Encode an expression in pseudo-regular expression into regular expression.
184 * @param raw The pseudo-regular expression <strong>String</strong> which includes several characters which differ in meaning from regular expression queries.
185 * @return A proper regular expression as a <strong>String</strong>.
186 */
187 private String encode(String raw) {
188 StringBuffer working = new StringBuffer();
189 for(int i = 0; i < raw.length(); i++) {
190 char c = raw.charAt(i);
191 switch(c) {
192 case '.':
193 working.append("\\.");
194 break;
195 case '*':
196 working.append(".*");
197 break;
198 case '?':
199 working.append(".");
200 break;
201 default:
202 working.append(Character.toLowerCase(c));
203 }
204 }
205 return working.toString();
206 }
207 /** This method applies the given pattern to the tree registered as belonging to this filter.*/
208 private void setFilter(Pattern pattern) {
209 // Show busy cursor.
210 Gatherer.g_man.wait(true);
211 FileSystemModel model = (FileSystemModel) tree.getModel();
212 // Apply filter
213 if(pattern != null) {
214 model.setFilter(pattern.pattern());
215 }
216 else {
217 model.setFilter(null);
218 }
219 // Ask tree to completely refresh
220 tree.refresh(null);
221 // Restore cursor
222 Gatherer.g_man.wait(false);
223 }
224
225 /** 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. */
226 private class ComboBoxListener
227 implements ActionListener {
228 /** 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.
229 * @param event An <strong>ActionEvent</strong> containing more information about the change performed.
230 * @see org.greenstone.gatherer.gui.Filter.Entry
231 */
232 public void actionPerformed(ActionEvent event) {
233 try {
234 Object temp = combobox.getSelectedItem();
235 Entry entry = null;
236 if(temp instanceof String) {
237 String temp_str = (String) temp;
238 ///ystem.err.println("Filter = " + temp_str);
239 // Ignore any string which matches a predefined filter, starting with All Files.
240 if(temp_str.equals(Dictionary.newget("Filter.All_Files"))) {
241 }
242 // HTM & HTML
243 else if(temp_str.equals(Dictionary.newget("Filter.0"))) {
244 }
245 // XML
246 else if(temp_str.equals(Dictionary.newget("Filter.1"))) {
247 }
248 // Text files
249 else if(temp_str.equals(Dictionary.newget("Filter.2"))) {
250 }
251 // Images
252 else if(temp_str.equals(Dictionary.newget("Filter.3"))) {
253 }
254 else {
255 ///ystem.err.println("Temporary Custom Filter");
256 entry = new Entry((String)temp, Pattern.compile(encode((String)temp)));
257 combobox.removeItem(temp);
258 int position = combobox.getItemCount();
259 combobox.insertItemAt(entry, position);
260 combobox.setSelectedIndex(position);
261 ///ystem.err.println("Inserting permanent custom filter.");
262 }
263 }
264 else {
265 ///ystem.err.println("Custom Filter");
266 entry = (Entry) temp;
267 }
268 if(entry != null) {
269 setFilter(entry.getPattern());
270 }
271 // 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.
272 }
273 catch (PatternSyntaxException error) {
274 if(first) {
275 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.newget("Filter.Invalid_Pattern"), Dictionary.newget("General.Error"), JOptionPane.ERROR_MESSAGE);
276 }
277 }
278 }
279 }
280 /** An object that holds a filter entry. This is string used for the filter pattern and, if not custom built, its name. */
281 private class Entry
282 implements Comparable {
283 /** The compiled pattern created from a regular expression. */
284 private Pattern pattern = null;
285 /** The name of this filter entry. */
286 private String name = null;
287 /** Constructor. */
288 public Entry() {
289 }
290 /** Constructor.
291 * @param name The name of this entry as a <strong>String</strong>.
292 * @param pattern The compiled regular expression as a <strong>Pattern</strong>.
293 */
294 public Entry(String name, Pattern pattern) {
295 this.name = name;
296 this.pattern = pattern;
297 }
298 /** Compare two Entrys for ordering.
299 * @param object The other Entry to compare to, as an <strong>Object</strong>.
300 * @return An <i>int</i> indicating the respective ordering, as defined in java.lang.String#compareTo
301 */
302 public int compareTo(Object object) {
303 return toString().compareTo(object.toString());
304 }
305 /** Retrieve the pattern associated with this entry.
306 * @return The <strong>Pattern</strong>.
307 */
308 public Pattern getPattern() {
309 return pattern;
310 }
311 /** Translate this entry into a textual representation.
312 * @return A <strong>String</strong> containing the representation.
313 */
314 public String toString() {
315 String result = null;
316 if (name != null) {
317 result = name;
318 }
319 else if (pattern == null) {
320 result = Dictionary.newget("Filter.All_Files");
321 }
322 else {
323 result = pattern.pattern();
324 }
325 return result;
326 }
327 }
328}
Note: See TracBrowser for help on using the repository browser.