KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > viewers > ViewerFilter


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jface.viewers;
12
13 import java.util.ArrayList JavaDoc;
14
15 /**
16  * A viewer filter is used by a structured viewer to
17  * extract a subset of elements provided by its content provider.
18  * <p>
19  * Subclasses must implement the <code>select</code> method
20  * and may implement the <code>isFilterProperty</code> method.
21  * </p>
22  * @see IStructuredContentProvider
23  * @see StructuredViewer
24  */

25 public abstract class ViewerFilter {
26     /**
27      * Creates a new viewer filter.
28      */

29     protected ViewerFilter() {
30     }
31
32     /**
33      * Filters the given elements for the given viewer.
34      * The input array is not modified.
35      * <p>
36      * The default implementation of this method calls
37      * <code>select</code> on each element in the array,
38      * and returns only those elements for which <code>select</code>
39      * returns <code>true</code>.
40      * </p>
41      * @param viewer the viewer
42      * @param parent the parent element
43      * @param elements the elements to filter
44      * @return the filtered elements
45      */

46     public Object JavaDoc[] filter(Viewer viewer, Object JavaDoc parent, Object JavaDoc[] elements) {
47         int size = elements.length;
48         ArrayList JavaDoc out = new ArrayList JavaDoc(size);
49         for (int i = 0; i < size; ++i) {
50             Object JavaDoc element = elements[i];
51             if (select(viewer, parent, element)) {
52                 out.add(element);
53             }
54         }
55         return out.toArray();
56     }
57
58     /**
59      * Filters the given elements for the given viewer.
60      * The input array is not modified.
61      * <p>
62      * The default implementation of this method calls
63      * {@link #filter(Viewer, Object, Object[])} with the
64      * parent from the path. Subclasses may override
65      * </p>
66      * @param viewer the viewer
67      * @param parentPath the path of the parent element
68      * @param elements the elements to filter
69      * @return the filtered elements
70      * @since 3.2
71      */

72     public Object JavaDoc[] filter(Viewer viewer, TreePath parentPath, Object JavaDoc[] elements) {
73         return filter(viewer, parentPath.getLastSegment(), elements);
74     }
75     
76     /**
77      * Returns whether this viewer filter would be affected
78      * by a change to the given property of the given element.
79      * <p>
80      * The default implementation of this method returns <code>false</code>.
81      * Subclasses should reimplement.
82      * </p>
83      *
84      * @param element the element
85      * @param property the property
86      * @return <code>true</code> if the filtering would be affected,
87      * and <code>false</code> if it would be unaffected
88      */

89     public boolean isFilterProperty(Object JavaDoc element, String JavaDoc property) {
90         return false;
91     }
92
93     /**
94      * Returns whether the given element makes it through this filter.
95      *
96      * @param viewer the viewer
97      * @param parentElement the parent element
98      * @param element the element
99      * @return <code>true</code> if element is included in the
100      * filtered set, and <code>false</code> if excluded
101      */

102     public abstract boolean select(Viewer viewer, Object JavaDoc parentElement,
103             Object JavaDoc element);
104 }
105
Popular Tags