KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.List JavaDoc;
23 import java.util.Set JavaDoc;
24 import java.util.Vector JavaDoc;
25 import java.beans.PropertyChangeEvent JavaDoc;
26 import java.beans.PropertyChangeListener JavaDoc;
27 import java.beans.PropertyChangeSupport JavaDoc;
28 import java.util.TreeSet JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.Collection JavaDoc;
31 import java.util.Set JavaDoc;
32 import java.util.AbstractCollection JavaDoc;
33 import java.util.SortedSet JavaDoc;
34 import java.util.AbstractSet JavaDoc;
35 import java.util.Collection JavaDoc;
36 import java.util.Comparator JavaDoc;
37 import java.util.TreeSet JavaDoc;
38 import java.util.Iterator JavaDoc;
39 import java.util.LinkedList JavaDoc;
40
41 /**
42  * Set of filters
43  */

44 public final class FilterRepository implements List JavaDoc {
45     
46   /**
47    * Property corresponding to the collection of filters in the
48    * repository. The old/new object fired with the property are null.
49    */

50   public static final String JavaDoc PROP_FILTERS = "propFilters";
51   
52   /**
53    * Property corresponding to the active filter.
54    */

55   public static final String JavaDoc PROP_ACTIVE_FILTER = "propActiveFilter";
56
57   /** collection of listeners **/
58   private PropertyChangeSupport JavaDoc pcs = null;
59
60
61   /** the set of filters of this repository **/
62   private LinkedList JavaDoc filters = new LinkedList JavaDoc();
63   private int active = -1; // index of the active filter
64

65   /** listener for nested filters **/
66   private PropertyChangeListener JavaDoc filterChangeListener =
67     new PropertyChangeListener JavaDoc() {
68       public void propertyChange(PropertyChangeEvent JavaDoc evt) {
69           fireFiltersChanged();
70       }
71     };
72   
73   /**
74    * Constructor, default.
75    */

76   public FilterRepository() {
77   }
78
79   public void assign(final FilterRepository rhs) {
80     if (rhs != this) {
81       filters.clear();
82       Iterator JavaDoc it = rhs.filters.iterator();
83       while (it.hasNext()) {
84     filters.add(((Filter)it.next()).clone());
85       }
86
87       active = rhs.active;
88       fireFiltersChanged();
89     }
90   }
91
92   public Object JavaDoc clone() {
93     FilterRepository ret = new FilterRepository();
94     ret.assign(this);
95     return ret;
96   }
97
98   
99   // Implementation of java.util.Set
100

101   /**
102    * Adds a new filter to the collection, if it was not present
103    * already.
104    * @param f the Filter to be added
105    * @return true iff it was not member before and was added
106    */

107   public boolean add(Object JavaDoc f) {
108     if (filters.add(f)) {
109         ((Filter)f).addPropertyChangeListener(filterChangeListener);
110         fireFiltersChanged();
111         return true;
112     } else {
113         return false;
114     }
115   }
116
117   public boolean addAll(Collection JavaDoc c) {
118     boolean ret = filters.addAll(c);
119     if (ret) {
120         hookCollection(c);
121         fireFiltersChanged();
122     }
123     return ret;
124   }
125   
126   public void add(int param, Object JavaDoc obj) {
127       filters.add(param, obj);
128       ((Filter)obj).addPropertyChangeListener(filterChangeListener);
129       fireFiltersChanged();
130   }
131   
132   public boolean addAll(int param, java.util.Collection JavaDoc collection) {
133       if (filters.addAll(param, collection)) {
134           hookCollection(collection);
135           fireFiltersChanged();
136           return true;
137       } else return false;
138   }
139   
140   
141   public void clear() {
142     if (!filters.isEmpty()) {
143       setActive(null);
144       unhookCollection(filters);
145       filters.clear();
146       fireFiltersChanged();
147     }
148   }
149
150   /**
151    * Returns true if this repository contains the specified filter.
152    * @param f filter whose presence is to be tested
153    * @return true if this repository contains the filter
154    */

155   public boolean contains(Object JavaDoc f) {
156     return filters.contains(f);
157   }
158
159
160   /**
161    * Remove the filter specified by parameter from the collection.
162    *
163    * @param filter the Filter to remove
164    * @return true iff the filter was found and removed
165    */

166   public boolean remove(Object JavaDoc filter) {
167     if (filter == getActive()) setActive(null);
168     if (filters.remove(filter)) {
169         ((Filter)filter).removePropertyChangeListener(filterChangeListener);
170         return true;
171     } else return false;
172   }
173
174   public boolean isEmpty() {
175     return filters.isEmpty();
176   }
177
178   public Iterator JavaDoc iterator() {
179     return filters.iterator();
180   }
181
182   /**
183    * Describe <code>removeAll</code> method here.
184    *
185    * @param collection a <code>Collection</code> value
186    * @return a <code>boolean</code> value
187    */

188   public boolean removeAll(Collection JavaDoc collection) {
189     if (collection.contains(getActive())) setActive(null);
190     unhookCollection(collection);
191     boolean ret = filters.removeAll(collection);
192     if (ret) fireFiltersChanged();
193     return ret;
194   }
195   
196
197   public Object JavaDoc[] toArray() {
198     return filters.toArray();
199   }
200
201   public Object JavaDoc[] toArray(Object JavaDoc[] objectArray) {
202     return filters.toArray(objectArray);
203   }
204
205   /**
206    * Describe <code>containsAll</code> method here.
207    *
208    * @param collection a <code>Collection</code> value
209    * @return a <code>boolean</code> value
210    */

211   public boolean containsAll(Collection JavaDoc collection) {
212     return filters.containsAll(collection);
213   }
214
215   /**
216    * Describe <code>retainAll</code> method here.
217    *
218    * @param collection a <code>Collection</code> value
219    * @return a <code>boolean</code> value
220    */

221   public boolean retainAll(Collection JavaDoc collection) {
222     if (getActive() != null && (!collection.contains(getActive()))) setActive(null);
223
224     unhookCollection(filters);
225     boolean ret = filters.retainAll(collection);
226     hookCollection(filters);
227     
228     if (ret) fireFiltersChanged();
229     return ret;
230   }
231
232   public int size() { return filters.size();}
233
234
235   public boolean equals(Object JavaDoc o) {
236       if (o instanceof FilterRepository && o != null) {
237           FilterRepository fr = (FilterRepository)o;
238           return filters.equals(fr.filters) && (active == fr.active);
239       } else
240           return false;
241   }
242
243   public int hashCode() {
244       return 31 * filters.hashCode() + (active == -1 ? 0 : getActive().hashCode());
245   }
246
247   // PROPERTY CHANGE implementation
248
private PropertyChangeSupport JavaDoc getPCS() {
249     if (pcs == null) pcs = new PropertyChangeSupport JavaDoc(this);
250     return pcs;
251   }
252
253   /**
254    * Adds a PropertyChangeListener to the listener list.
255    * @param l The listener to add.
256    */

257   public void addPropertyChangeListener(PropertyChangeListener JavaDoc l) {
258     getPCS().addPropertyChangeListener(l);
259   }
260     
261   /**
262    * Removes a PropertyChangeListener from the listener list.
263    * @param l The listener to remove.
264    */

265   public void removePropertyChangeListener(PropertyChangeListener JavaDoc l) {
266     pcs.removePropertyChangeListener(l);
267   }
268     
269   private final void fireActiveChanged(Filter old, Filter nnew) {
270       if (pcs!=null) getPCS().firePropertyChange(PROP_ACTIVE_FILTER, old, nnew);
271   }
272   
273   /**
274    * Fires a change event
275    */

276   private final void fireFiltersChanged() {
277     if (pcs != null) getPCS().firePropertyChange(PROP_FILTERS, null, null);
278   }
279   
280   /**
281    * Returns a filter with the given name or null if not found.
282    * @param name name of the filter to look up
283    * @return Filter with name or null
284    */

285   public Filter getFilterByName(String JavaDoc name) {
286     Iterator JavaDoc it = filters.iterator();
287     while (it.hasNext()) {
288         Filter f = (Filter)it.next();
289         if (f.getName().equals(name)) return f;
290     }
291     return null;
292   }
293   
294   public Filter getActive() {
295     return (active == -1) ? null : ((Filter)filters.get(active));
296   }
297   
298   public void setActive(Filter newactive) {
299     if (newactive == null) {
300         if (this.active != -1) {
301             Filter oldactive = getActive();
302             this.active = -1;
303             fireActiveChanged(oldactive, null);
304         }
305     } else {
306         int i = filters.indexOf(newactive);
307         if (i != -1) {
308             Filter oldactive = getActive();
309             this.active = i;
310             fireActiveChanged(oldactive, newactive);
311         } else {
312             throw new IllegalArgumentException JavaDoc("newactive not in collection");
313         }
314     }
315   }
316   
317   
318   public Object JavaDoc get(int param) {
319       return filters.get(param);
320   }
321   
322   public int indexOf(Object JavaDoc obj) {
323       return filters.indexOf(obj);
324   }
325   
326   public int lastIndexOf(Object JavaDoc obj) {
327       return filters.lastIndexOf(obj);
328   }
329   
330   public java.util.ListIterator JavaDoc listIterator() {
331     return filters.listIterator();
332   }
333   
334   public java.util.ListIterator JavaDoc listIterator(int param) {
335       return filters.listIterator(param);
336   }
337   
338   public Object JavaDoc remove(int param) {
339       Object JavaDoc o = filters.remove(param);
340       if (o != null) {
341           ((Filter)o).removePropertyChangeListener(filterChangeListener);
342           fireFiltersChanged();
343       }
344       return o;
345   }
346   
347   public Object JavaDoc set(int param, Object JavaDoc obj) {
348       Object JavaDoc o = filters.set(param, obj);
349       ((Filter)o).removePropertyChangeListener(filterChangeListener);
350       ((Filter)obj).addPropertyChangeListener(filterChangeListener);
351
352       fireFiltersChanged();
353       return o;
354   }
355   
356   public java.util.List JavaDoc subList(int param, int param1) {
357     return filters.subList(param, param1);
358   }
359
360   private void unhookCollection(Collection JavaDoc c) {
361       Iterator JavaDoc it = c.iterator();
362       while (it.hasNext()) ((Filter)it.next()).removePropertyChangeListener(filterChangeListener);
363   }
364
365   private void hookCollection(Collection JavaDoc c) {
366       Iterator JavaDoc it = c.iterator();
367       while (it.hasNext()) ((Filter)it.next()).addPropertyChangeListener(filterChangeListener);
368   }
369
370 }
371
372
Popular Tags