KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > dialogs > PatternFilter


1 /*******************************************************************************
2  * Copyright (c) 2004 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.ui.internal.dialogs;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.Map JavaDoc;
15
16 import org.eclipse.jface.viewers.AbstractTreeViewer;
17 import org.eclipse.jface.viewers.ILabelProvider;
18 import org.eclipse.jface.viewers.ITreeContentProvider;
19 import org.eclipse.jface.viewers.StructuredViewer;
20 import org.eclipse.jface.viewers.Viewer;
21 import org.eclipse.jface.viewers.ViewerFilter;
22 import org.eclipse.ui.internal.misc.StringMatcher;
23
24 /**
25  * A filter used in conjunction with <code>FilteredTree</code>. This filter is
26  * inefficient - in order to see if a node should be filtered it must use the
27  * content provider of the tree to do pattern matching on its children. This
28  * causes the entire tree structure to be realized.
29  *
30  * @see org.eclipse.ui.internal.dialogs.FilteredTree
31  * @since 3.0
32  */

33 public class PatternFilter extends ViewerFilter {
34
35     private Map JavaDoc cache = new HashMap JavaDoc();
36
37     private StringMatcher matcher;
38
39     /* (non-Javadoc)
40      * @see org.eclipse.jface.viewers.ViewerFilter#filter(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object[])
41      */

42     public Object JavaDoc[] filter(Viewer viewer, Object JavaDoc parent, Object JavaDoc[] elements) {
43         if (matcher == null)
44             return elements;
45
46         Object JavaDoc[] filtered = (Object JavaDoc[]) cache.get(parent);
47         if (filtered == null) {
48             filtered = super.filter(viewer, parent, elements);
49             cache.put(parent, filtered);
50         }
51         return filtered;
52     }
53
54     /* (non-Javadoc)
55      * @see org.eclipse.jface.viewers.ViewerFilter#select(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
56      */

57     public boolean select(Viewer viewer, Object JavaDoc parentElement, Object JavaDoc element) {
58         Object JavaDoc[] children = ((ITreeContentProvider) ((AbstractTreeViewer) viewer)
59                 .getContentProvider()).getChildren(element);
60         if (children.length > 0)
61             return filter(viewer, element, children).length > 0;
62
63         String JavaDoc labelText = ((ILabelProvider) ((StructuredViewer) viewer)
64                 .getLabelProvider()).getText(element);
65         if(labelText == null)
66             return false;
67         return match(labelText);
68     }
69
70     /**
71      *
72      * @param patternString
73      */

74     public void setPattern(String JavaDoc patternString) {
75         cache.clear();
76         if (patternString == null || patternString.equals("")) //$NON-NLS-1$
77
matcher = null;
78         else
79             matcher = new StringMatcher(patternString + "*", true, false); //$NON-NLS-1$
80
}
81
82     /**
83      * Answers whether the given String matches the pattern.
84      *
85      * @param string the String to test
86      * @return whether the string matches the pattern
87      */

88     protected boolean match(String JavaDoc string) {
89         return matcher.match(string);
90     }
91 }
92
Popular Tags