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

Last change on this file since 6842 was 6842, checked in by mdewsnip, 20 years ago

Variable names changes needed because of the name change of the Gather, Enrich and Design pane java files.

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