KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > core > filter > Filter


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.tasklist.core.filter;
21
22 import java.beans.PropertyChangeListener JavaDoc;
23 import java.beans.PropertyChangeSupport JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.LinkedList JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Vector JavaDoc;
30 import org.openide.util.NbBundle;
31
32
33 /**
34  * This class implements a filter for a tasklist
35  *
36  * @author Tor Norbye
37  */

38 public abstract class Filter {
39     /** If true, all conditions in the filter must be true.
40      * If false, any condition in the filter can be true to
41      * make the node pass.
42      */

43     private boolean allTrue = false;
44     
45     /** List of conditions to evaluate the task with */
46     private List JavaDoc appliedConditions = null;
47     
48     /** Use visible name of the filter */
49     private String JavaDoc name = null;
50     
51     /** Flatten the hierarchy? When true, don't show parents */
52     private boolean flattened;
53     
54     private PropertyChangeSupport JavaDoc pcs = null;
55     protected PropertyChangeSupport JavaDoc getPCS() {
56         if (pcs == null) pcs = new PropertyChangeSupport JavaDoc(this);
57         return pcs;
58     }
59     protected boolean hasListeners() { return pcs != null;}
60
61     private static final String JavaDoc NO_FILTER = NbBundle.getMessage(Filter.class, "no-filter");
62
63     public static final String JavaDoc PROP_NAME = "PropName";
64     public static final String JavaDoc PROP_ALLTRUE = "PropAllTrue";
65     public static final String JavaDoc PROP_CONDITIONS = "PropConditions";
66     public static final String JavaDoc PROP_FLATTENED = "PropFlattened";
67     
68
69     public void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {
70         getPCS().addPropertyChangeListener(listener);
71     }
72     
73     public void removePropertyChangeListener(PropertyChangeListener JavaDoc listener) {
74         getPCS().removePropertyChangeListener(listener);
75     }
76     
77     public void addPropertyChangeListener(String JavaDoc property, PropertyChangeListener JavaDoc listener) {
78         getPCS().addPropertyChangeListener(property, listener);
79     }
80     
81     public void removePropertyChangeListener(String JavaDoc property, PropertyChangeListener JavaDoc listener) {
82         getPCS().removePropertyChangeListener(property, listener);
83     }
84     /**
85      * Creates an empty filter
86      *
87      * @param name name of the filter
88      */

89     public Filter(String JavaDoc name) {
90         this(name, true, new ArrayList JavaDoc(), false);
91     }
92     
93     /**
94      * Create a new filter.
95      *
96      * @param name (User visible) name of the filter
97      * @param allTrue When true, all conditions must be true, when false, any
98      * condition can be true, to make a task pass through the filter.
99      * @param conditions List of AppliedFilterCondition objects to use when filtering a task.
100      * @param showParents Iff true this will cause parents (which do not match
101      * the filter) to be included if they have at least one child
102      * node which -does- match.
103      */

104     public Filter(String JavaDoc name, boolean allTrue, List JavaDoc conditions, boolean flattened) {
105         this.name = name;
106         this.allTrue = allTrue;
107         this.appliedConditions = conditions;
108         this.flattened = flattened;
109     }
110     
111     /**
112      * Copy constructor.
113      */

114     protected Filter(final Filter rhs) {
115        this(rhs.name, rhs.allTrue, cloneConditions(rhs.appliedConditions), rhs.flattened);
116     }
117
118
119     /** for deconvertization **/
120     protected Filter() {this.name = null; this.appliedConditions = null; };
121
122
123     private static List JavaDoc cloneConditions(List JavaDoc conditions) {
124         LinkedList JavaDoc l = new LinkedList JavaDoc();
125         Iterator JavaDoc it = conditions.iterator();
126         while (it.hasNext()) {
127       l.add(((AppliedFilterCondition)it.next()).clone());
128         }
129         
130         return l;
131     }
132     
133     public abstract Object JavaDoc clone();
134     
135     /**
136      * Creates filter conditions (options) for the specified property
137      * applied to the given property.
138      *
139      * @param property the property to get options for
140      */

141     public abstract AppliedFilterCondition[] createConditions(SuggestionProperty property);
142     
143     /**
144      * Returns properties used for filtering by this filter.
145      * <p>
146      * Versioning consideration: you may not remove
147      * any value to retain backward compatability.
148      *
149      * @return properties for searching
150      */

151     public abstract SuggestionProperty[] getProperties();
152     
153     
154     /**
155      * Removes all conditions from this filter
156      */

157     public void clear() {
158         Vector JavaDoc oldc = null;
159         if (hasListeners()) oldc = new Vector JavaDoc(this.appliedConditions);
160         getConditions().clear();
161         if (hasListeners()) fireChange(PROP_CONDITIONS, oldc, Collections.EMPTY_LIST);;
162     }
163     
164     /**
165      * When true, flatten the hierarchy such that only matching
166      * tasks are shown
167      *
168      * @return true = flattened, false = not matching parent tasks will be
169      * shown too
170      */

171     public boolean isFlattened() {
172         return flattened;
173     }
174     
175     /**
176      * Changes the attribute "flattened"
177      *
178      * @param f true = the hierarchy will be flattened such that only matching
179      * tasks are shown
180      */

181     public void setFlattened(boolean f) {
182         if (f != this.flattened) {
183             this.flattened = f;
184             fireChange(PROP_FLATTENED, Boolean.valueOf(!f), Boolean.valueOf(f));
185         }
186     }
187     
188     /**
189      * Return true iff the filter lets the task through
190      *
191      * @param node object to be filtered
192      */

193     public boolean accept(Object JavaDoc node) {
194         if (!hasConstraints()) {
195             return true; // No need to create iterator object...
196
}
197         Iterator JavaDoc it = appliedConditions.iterator();
198         boolean b = true;
199         while (it.hasNext()) {
200         AppliedFilterCondition acond = (AppliedFilterCondition)it.next();
201             b = acond.isTrue(node);
202             if (b && !allTrue) {
203                 return true;
204             } else if (!b && allTrue) {
205                 return false;
206             }
207         }
208         return b;
209     }
210
211     /**
212      * Return true iff all conditions should be matched.
213      *
214      * @return true iff all conditions should be matched.
215      */

216     public boolean matchAll() {
217         return allTrue;
218     }
219
220     /**
221      * Return true iff the filter is not "empty" (meaning that
222      * there are constraints on the view)
223      */

224     public boolean hasConstraints() {
225         return (appliedConditions != null) && (appliedConditions.size() > 0);
226     }
227
228     /**
229      * Should all conditions match?
230      *
231      * @param b true = all conditions should be matched, false = any
232      */

233     public void setMatchAll(boolean b) {
234         if (this.allTrue != b) {
235             this.allTrue = b;
236             fireChange(PROP_ALLTRUE, Boolean.valueOf(!b), Boolean.valueOf(b));
237         }
238     }
239     
240     /**
241      * Return the list of conditions actually used for this filter.
242      *
243      * @return the list of AppliedFilterConditions for this filter.
244      */

245     public final List JavaDoc getConditions() {
246         return appliedConditions;
247     }
248     
249     /**
250      * Sets new applied conditions used with this filter.
251      *
252      * @param new List[AppliedFilterCondition]
253      */

254     public final void setConditions(List JavaDoc conditions) {
255         Vector JavaDoc oldc = null;
256         if (hasListeners()) oldc = new Vector JavaDoc(conditions);
257         this.appliedConditions = conditions;
258         if (hasListeners()) fireChange(PROP_CONDITIONS, oldc, this.appliedConditions);
259     }
260     
261     /**
262      * Fires an event
263      */

264     private void fireChange(String JavaDoc property, Object JavaDoc oldv, Object JavaDoc newv) {
265         if (pcs!=null)
266             getPCS().firePropertyChange(property, oldv, newv);
267     }
268     
269     /**
270      * Gets the name of this filter.
271      * @return String name
272      */

273     public String JavaDoc getName() {
274         return name;
275     }
276     
277     /**
278      * Sets new name for this filter.
279      * @param name newName
280      */

281      public void setName(String JavaDoc name) {
282         String JavaDoc oldname = this.name;
283         this.name = name;
284     fireChange(PROP_NAME, oldname, name);
285      }
286     
287     /**
288      * Print out the filter for debugging purposes.
289      * Do NOT depend on its format or contents, it may change arbitrarily.
290      * It is not localized.
291      *
292      * @return string representation of this object
293      */

294     public String JavaDoc toString() {
295         Iterator JavaDoc it = appliedConditions.iterator();
296         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
297         sb.append(getClass().getName() + "[name=" + name + ", "); // NOI18N
298
sb.append(allTrue ?
299             "ALL of the following conditions" : // NOI18N
300
"ANY of the following conditions"); // NOI18N
301
sb.append(", "); // NOI18N
302
while (it.hasNext()) {
303             sb.append(it.next());
304             sb.append(", "); // NOI18N
305
}
306         sb.append("]"); // NOI18N
307
return sb.toString();
308     }
309
310     /**
311      * Handy utility class to insert Filters into lists.
312      */

313     public static class ListModelElement {
314        
315         public Filter filter;
316         public ListModelElement(Filter f) {
317             filter = f;
318         }
319         
320         public String JavaDoc toString() { return (filter != null) ? filter.getName() : NO_FILTER;}
321         
322         public int hashCode() {
323       return (filter != null) ? (filter.hashCode()) : 0;
324         }
325         
326         public boolean equals(Object JavaDoc rhs) {
327       if (rhs instanceof ListModelElement) rhs = ((ListModelElement)rhs).filter;
328       return this.filter == rhs;
329         }
330     }
331
332
333     /**
334      * Lift of AppliedFilterCondition(property, - ) to list of
335      * FilterConditions.
336      *
337      * @param property SuggestionProperty to apply to every
338      * FilterCondition in the list
339      * @param conds list of FilterConditions
340      * @return list of AppliedFilterConditions , each applied to
341      * property and the corresponding element of conds.
342      */

343     protected static AppliedFilterCondition [] applyConditions(SuggestionProperty property, FilterCondition [] conds) {
344       if (conds == null) return null;
345       else {
346     AppliedFilterCondition [] applied = new AppliedFilterCondition[conds.length];
347     for (int i = 0; i < conds.length; i++) {
348       applied[i] = new AppliedFilterCondition(property, conds[i]);
349     }
350     return applied;
351       }
352     }
353         
354 }
355
Popular Tags