001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jface.viewers;
011:
012: import java.util.ArrayList;
013:
014: /**
015: * A viewer filter is used by a structured viewer to
016: * extract a subset of elements provided by its content provider.
017: * <p>
018: * Subclasses must implement the <code>select</code> method
019: * and may implement the <code>isFilterProperty</code> method.
020: * </p>
021: * @see IStructuredContentProvider
022: * @see StructuredViewer
023: */
024: public abstract class ViewerFilter {
025: /**
026: * Creates a new viewer filter.
027: */
028: protected ViewerFilter() {
029: }
030:
031: /**
032: * Filters the given elements for the given viewer.
033: * The input array is not modified.
034: * <p>
035: * The default implementation of this method calls
036: * <code>select</code> on each element in the array,
037: * and returns only those elements for which <code>select</code>
038: * returns <code>true</code>.
039: * </p>
040: * @param viewer the viewer
041: * @param parent the parent element
042: * @param elements the elements to filter
043: * @return the filtered elements
044: */
045: public Object[] filter(Viewer viewer, Object parent,
046: Object[] elements) {
047: int size = elements.length;
048: ArrayList out = new ArrayList(size);
049: for (int i = 0; i < size; ++i) {
050: Object element = elements[i];
051: if (select(viewer, parent, element)) {
052: out.add(element);
053: }
054: }
055: return out.toArray();
056: }
057:
058: /**
059: * Filters the given elements for the given viewer.
060: * The input array is not modified.
061: * <p>
062: * The default implementation of this method calls
063: * {@link #filter(Viewer, Object, Object[])} with the
064: * parent from the path. Subclasses may override
065: * </p>
066: * @param viewer the viewer
067: * @param parentPath the path of the parent element
068: * @param elements the elements to filter
069: * @return the filtered elements
070: * @since 3.2
071: */
072: public Object[] filter(Viewer viewer, TreePath parentPath,
073: Object[] elements) {
074: return filter(viewer, parentPath.getLastSegment(), elements);
075: }
076:
077: /**
078: * Returns whether this viewer filter would be affected
079: * by a change to the given property of the given element.
080: * <p>
081: * The default implementation of this method returns <code>false</code>.
082: * Subclasses should reimplement.
083: * </p>
084: *
085: * @param element the element
086: * @param property the property
087: * @return <code>true</code> if the filtering would be affected,
088: * and <code>false</code> if it would be unaffected
089: */
090: public boolean isFilterProperty(Object element, String property) {
091: return false;
092: }
093:
094: /**
095: * Returns whether the given element makes it through this filter.
096: *
097: * @param viewer the viewer
098: * @param parentElement the parent element
099: * @param element the element
100: * @return <code>true</code> if element is included in the
101: * filtered set, and <code>false</code> if excluded
102: */
103: public abstract boolean select(Viewer viewer, Object parentElement,
104: Object element);
105: }
|