KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > outline > QuickOutlineNamePatternFilter


1 /*******************************************************************************
2  * Copyright (c) 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
12 package org.eclipse.pde.internal.ui.editor.outline;
13
14 import org.eclipse.jface.viewers.ILabelProvider;
15 import org.eclipse.jface.viewers.ITreeContentProvider;
16 import org.eclipse.jface.viewers.TreeViewer;
17 import org.eclipse.jface.viewers.Viewer;
18 import org.eclipse.jface.viewers.ViewerFilter;
19 import org.eclipse.pde.internal.ui.util.StringMatcher;
20
21 /**
22  * QuickOutlineNamePatternFilter
23  *
24  */

25 public class QuickOutlineNamePatternFilter extends ViewerFilter {
26
27     private StringMatcher fStringMatcher;
28     
29     /**
30      *
31      */

32     public QuickOutlineNamePatternFilter() {
33         fStringMatcher = null;
34     }
35
36     /* (non-Javadoc)
37      * @see org.eclipse.jface.viewers.ViewerFilter#select(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
38      */

39     public boolean select(Viewer viewer, Object JavaDoc parentElement, Object JavaDoc element) {
40         // Element passes the filter if the string matcher is undefined or the
41
// viewer is not a tree viewer
42
if ((fStringMatcher == null) ||
43                 ((viewer instanceof TreeViewer) == false)) {
44             return true;
45         }
46         TreeViewer treeViewer = (TreeViewer)viewer;
47         // Match the pattern against the label of the given element
48
String JavaDoc matchName =
49             ((ILabelProvider) treeViewer.getLabelProvider()).getText(element);
50         // Element passes the filter if it matches the pattern
51
if ((matchName != null) &&
52                 fStringMatcher.match(matchName)) {
53             return true;
54         }
55         // Determine whether the element has children that pass the filter
56
return hasUnfilteredChild(treeViewer, element);
57     }
58
59     /**
60      * @param viewer
61      * @param element
62      * @return
63      */

64     private boolean hasUnfilteredChild(TreeViewer viewer, Object JavaDoc element) {
65         // No point calling hasChildren() because the operation is the same cost
66
// as getting the children
67
// If the element has a child that passes the filter, then we want to
68
// keep the parent around - even if it does not pass the filter itself
69
Object JavaDoc[] children =
70             ((ITreeContentProvider) viewer.getContentProvider()).getChildren(element);
71         for (int i = 0; i < children.length; i++) {
72             if (select(viewer, element, children[i])) {
73                 return true;
74             }
75         }
76         // Element does not pass the filter
77
return false;
78     }
79     
80     /**
81      * @param stringMatcher
82      */

83     public void setStringMatcher(StringMatcher stringMatcher) {
84         fStringMatcher = stringMatcher;
85     }
86     
87 }
88
Popular Tags