01: /*******************************************************************************
02: * Copyright (c) 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: *******************************************************************************/package org.eclipse.pde.internal.ui.editor.outline;
11:
12: import org.eclipse.jface.viewers.ILabelProvider;
13: import org.eclipse.jface.viewers.ITreeContentProvider;
14: import org.eclipse.jface.viewers.TreeViewer;
15: import org.eclipse.jface.viewers.Viewer;
16: import org.eclipse.jface.viewers.ViewerFilter;
17: import org.eclipse.pde.internal.ui.util.StringMatcher;
18:
19: /**
20: * QuickOutlineNamePatternFilter
21: *
22: */
23: public class QuickOutlineNamePatternFilter extends ViewerFilter {
24:
25: private StringMatcher fStringMatcher;
26:
27: /**
28: *
29: */
30: public QuickOutlineNamePatternFilter() {
31: fStringMatcher = null;
32: }
33:
34: /* (non-Javadoc)
35: * @see org.eclipse.jface.viewers.ViewerFilter#select(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
36: */
37: public boolean select(Viewer viewer, Object parentElement,
38: Object element) {
39: // Element passes the filter if the string matcher is undefined or the
40: // viewer is not a tree viewer
41: if ((fStringMatcher == null)
42: || ((viewer instanceof TreeViewer) == false)) {
43: return true;
44: }
45: TreeViewer treeViewer = (TreeViewer) viewer;
46: // Match the pattern against the label of the given element
47: String matchName = ((ILabelProvider) treeViewer
48: .getLabelProvider()).getText(element);
49: // Element passes the filter if it matches the pattern
50: if ((matchName != null) && fStringMatcher.match(matchName)) {
51: return true;
52: }
53: // Determine whether the element has children that pass the filter
54: return hasUnfilteredChild(treeViewer, element);
55: }
56:
57: /**
58: * @param viewer
59: * @param element
60: * @return
61: */
62: private boolean hasUnfilteredChild(TreeViewer viewer, Object element) {
63: // No point calling hasChildren() because the operation is the same cost
64: // as getting the children
65: // If the element has a child that passes the filter, then we want to
66: // keep the parent around - even if it does not pass the filter itself
67: Object[] children = ((ITreeContentProvider) viewer
68: .getContentProvider()).getChildren(element);
69: for (int i = 0; i < children.length; i++) {
70: if (select(viewer, element, children[i])) {
71: return true;
72: }
73: }
74: // Element does not pass the filter
75: return false;
76: }
77:
78: /**
79: * @param stringMatcher
80: */
81: public void setStringMatcher(StringMatcher stringMatcher) {
82: fStringMatcher = stringMatcher;
83: }
84:
85: }
|