01: /*******************************************************************************
02: * Copyright (c) 2000, 2006 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: * Darrell Meyer <darrell@mygwt.net> - derived implementation
11: *******************************************************************************/package net.mygwt.ui.client.viewer;
12:
13: import java.util.ArrayList;
14:
15: /**
16: * A viewer filter is used by a viewer to extract a subset of elements provided
17: * by its content provider.
18: *
19: * <p>
20: * This code is based on JFace API from the Eclipse Project.
21: * </p>
22: *
23: * @see IContentProvider
24: * @see Viewer
25: */
26: public abstract class ViewerFilter {
27:
28: /**
29: * Creates a new viewer filter.
30: */
31: protected ViewerFilter() {
32: }
33:
34: /**
35: * Filters the given elements for the given viewer. The input array is not
36: * modified.
37: * <p>
38: * The default implementation of this method calls <code>select</code> on
39: * each element in the array, and returns only those elements for which
40: * <code>select</code> returns <code>true</code>.
41: * </p>
42: *
43: * @param viewer the viewer
44: * @param parent the parent element
45: * @param elements the elements to filter
46: * @return the filtered elements
47: */
48: public Object[] filter(Viewer viewer, Object parent,
49: Object[] elements) {
50: int size = elements.length;
51: ArrayList out = new ArrayList(size);
52: for (int i = 0; i < size; ++i) {
53: Object element = elements[i];
54: if (select(viewer, parent, element)) {
55: out.add(element);
56: }
57: }
58: return out.toArray();
59: }
60:
61: /**
62: * Returns whether the given element makes it through this filter.
63: *
64: * @param viewer the viewer
65: * @param parentElement the parent element
66: * @param element the element
67: * @return <code>true</code> if element is included in the filtered set, and
68: * <code>false</code> if excluded
69: */
70: public abstract boolean select(Viewer viewer, Object parentElement,
71: Object element);
72:
73: }
|