source: trunk/gli/src/org/greenstone/gatherer/gui/metaaudit/Autofilter.java@ 4293

Last change on this file since 4293 was 4293, checked in by jmt12, 21 years ago

Initial revision

  • Property svn:keywords set to Author Date Id Revision
File size: 9.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 */
37
38
39
40
41
42
43/* GPL_HEADER */
44package org.greenstone.gatherer.gui.metaaudit;
45/**************************************************************************************
46 * Title: Gatherer
47 * Description: The Gatherer: a tool for gathering and enriching a digital collection.
48 * Copyright: Copyright (c) 2001
49 * Company: The University of Waikato
50 * Written: /05/02
51 * Revised: 22/08/02 Revamped, Optimized and Commented.
52 **************************************************************************************/
53
54import java.util.ArrayList;
55/** An Autofilter object stores the filters set on a single column, and provides a method for determining if a certain value passes the filter.
56 * @author John Thompson
57 * @version 2.3
58 */
59public class Autofilter {
60 /** <i>true</i> if the filter should be applied, <i>false</i> to indicate the filter is turned off. */
61 public boolean active;
62 /** <i>true</i> if the matching for the first expression should be case sensitive, <i>false</i> otherwise. */
63 public boolean casesense_one;
64 /** <i>true</i> if the matching for the second expression should be case sensitive, <i>false</i> otherwise. */
65 public boolean casesense_two;
66 /** Used to determine the operation intended when applying two filters, and set using the values of the OPERATION_TYPE enumeration. */
67 public boolean operation;
68 /** Used to determine how the column this filter is applied to should be sorted, and set using the values of the SORT_TYPE enumeration. */
69 public boolean sort;
70 /** The method to be used for the first filter expression, set from the values of the METHOD_LIST enumeration. */
71 public int method_one;
72 /** The method to be used for the second filter expression, set from the values of the METHOD_LIST enumeration. */
73 public int method_two;
74 /** The value to be matched against for the first expression. */
75 public String value_one;
76 /** The value to be matched against for the second expression. */
77 public String value_two;
78 /** An element of the SORT_TYPE enumeration, indicates lowest to highest value column ordering. */
79 public static final boolean ASCENDING = true;
80 /** An element of the OPERATION_TYPE enumeration, indicates that both filter expressions must be met (conjunction). */
81 public static final boolean AND = true;
82 /** An element of the SORT_TYPE enumeration, indicates highest to lowest value column ordering. */
83 public static final boolean DESCENDING = false;
84 /** An element of the OPERATION_TYPE enumeration, indicates that either (or both) filter expressions must be met (disjunction). */
85 public static final boolean OR = false;
86 /** An enumeration of symbolic names of various matching methods. */
87 public static final String METHOD_LIST[] = {"eqeq", "!eq", "<", "<eq", ">", ">eq", "^", "!^", "$", "!$", "?", "!?"};
88 /** Default Constructor. */
89 public Autofilter() {
90 operation = OR;
91 sort = ASCENDING;
92 }
93 /** Determine if this filter is currently active.
94 * @return <i>true</i> if it is active, <i>false</i> otherwise.
95 */
96 public boolean active() {
97 return active;
98 }
99 /** Determine if this list of values (for a certain cell) passes the filter.
100 * @param values An <strong>ArrayList</strong> of values sourced from a single cell in the associated column.
101 * @return <i>true</i> if the values match and should be displayed, <i>false</i> otherwise.
102 */
103 public boolean filter(ArrayList values) {
104 boolean result = false;
105 if(value_one != null) {
106 result = filter(values, method_one, value_one, casesense_one);
107 if(result) {
108 if(operation == AND && value_two != null) {
109 result = filter(values, method_two, value_two, casesense_two);
110 }
111 }
112 else if(operation == OR && value_two != null) {
113 result = filter(values, method_two, value_two, casesense_two);
114 }
115 }
116 return result;
117 }
118 /** Set the current activity state of this filter.
119 * @param active The new state of this filter, <i>true</i> to activate, <i>false</i> otherwise.
120 */
121 public void setActive(boolean active) {
122 this.active = active;
123 }
124 /** Set one of the filter expressions using the given information.
125 * @param number The number of the filter you wish to set as an <i>int</i>. Either 1 or 2.
126 * @param method An <i>int</i> indicating the method to be used when matching.
127 * @param value The <strong>String</strong> to be matched against.
128 * @param casesense <i>true</i> if this expression should be case sensitive, <i>false</i> otherwise.
129 */
130 public void setFilter(int number, int method, String value, boolean casesense) {
131 if(!casesense && value != null) {
132 value = value.toLowerCase();
133 }
134 if(number == 1) {
135 casesense_one = casesense;
136 method_one = method;
137 value_one = value;
138 }
139 else {
140 casesense_two = casesense;
141 method_two = method;
142 value_two = value;
143 }
144 }
145 /** Set the operation to be used to join the two filters (if a second filter is set).
146 * @param operation <i>true</i> for conjunct filters, <i>false</i> for disjunct.
147 */
148 public void setOperation(boolean operation) {
149 this.operation = operation;
150 }
151 /** Set the sort order of this column.
152 * @param sort <i>true</i> for ascending sort, <i>false</i> for descending.
153 */
154 public void setSort(boolean sort) {
155 this.sort = sort;
156 }
157 /** Decide whether a row should be displayed or filtered. The result depends on the selector set with setFilterType.
158 * @param values An <strong>ArrayList</strong> of values to be checked against the filter.
159 * @param method The method of matching to be used, as an <i>int</i>.
160 * @param target The <strong>String</Strong> to match against.
161 * @param casesense <i>true</i> if the match is to be case sensitive, <i>false</i> otherwise.
162 * @return <i>true</i> to display the row, <i>false</i> to hide it.
163 */
164 public boolean filter(ArrayList values, int method, String target, boolean casesense) {
165 boolean result = false;
166 // There are several special cases when the filter always returns turn, such as when the target is the wildcard character.
167 if(target == null || target.length() == 0 || target.equals("*")) {
168 result = true;
169 }
170 else {
171 // For each value in the list...
172 for(int i = 0; i < values.size(); i++) {
173 boolean pass;
174 String source;
175 // Account for case sensitivity.
176 if(casesense) {
177 source = values.get(i).toString();
178 }
179 else {
180 source = values.get(i).toString().toLowerCase();
181 }
182 // Perform the match, based on the selected method.
183 switch(method) {
184 case 1: // !EQ
185 pass = !(source.equals(target));
186 break;
187 case 2: // <
188 pass = (source.compareTo(target) < 0);
189 break;
190 case 3: // <eq
191 pass = (source.compareTo(target) <= 0);
192 break;
193 case 4: // >
194 pass = (source.compareTo(target) > 0);
195 break;
196 case 5: // >eq
197 pass = (source.compareTo(target) >= 0);
198 break;
199 case 6: // ^
200 pass = source.startsWith(target);
201 break;
202 case 7: // !^
203 pass = !(source.startsWith(target));
204 break;
205 case 8: // $
206 pass = source.endsWith(target);
207 break;
208 case 9: // !$
209 pass = !(source.endsWith(target));
210 break;
211 case 10: // ?
212 pass = (source.indexOf(target) != -1);
213 break;
214 case 11: // !?
215 pass = (source.indexOf(target) == -1);
216 break;
217 default: // EQEQ
218 pass = source.equals(target);
219 break;
220 }
221 result = result || pass;
222 }
223 }
224 return result;
225 }
226 /** Produce a textual representation of this autofilter.
227 * @return A <strong>String</strong> displaying details of this autofilter.
228 */
229 public String toString() {
230 String result = "One: " + method_one + " - " + value_one + " - " + casesense_one;
231 if(value_two != null) {
232 result = result + "\n" + "Operation: " + (operation?"AND":"OR") + "\n";
233 result = result + "Two: " + method_two + " - " + value_two + " - " + casesense_two;
234 }
235 return result;
236 }
237}
238
239
240
241
242
243
244
245
Note: See TracBrowser for help on using the repository browser.