KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2005 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.text.BreakIterator JavaDoc;
14 import java.util.Collection JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.Map JavaDoc;
18
19 import org.eclipse.jface.preference.IPreferenceNode;
20 import org.eclipse.jface.viewers.ITreeContentProvider;
21 import org.eclipse.jface.viewers.TreeViewer;
22 import org.eclipse.jface.viewers.Viewer;
23 import org.eclipse.ui.internal.preferences.WorkbenchPreferenceExtensionNode;
24
25 /**
26  * A class which handles filtering preferences nodes based on a supplied
27  * matching string.
28  *
29  * @since 3.1
30  *
31  */

32 public class PatternItemFilter extends PatternFilter {
33
34     /**
35      * this cache is needed because
36      * WorkbenchPreferenceExtensionNode.getKeywordLabels() is expensive. When it
37      * tracks keyword changes effectivly than this cache can be removed.
38      */

39     private Map JavaDoc keywordCache = new HashMap JavaDoc();
40
41     protected boolean matchItem;
42
43     /**
44      * Create a new instance of a PatternItemFilter
45      *
46      * @param isMatchItem
47      */

48     public PatternItemFilter(boolean isMatchItem) {
49         super();
50         matchItem = isMatchItem;
51     }
52
53     /*
54      * (non-Javadoc)
55      *
56      * @see org.eclipse.jface.viewers.ViewerFilter#select(org.eclipse.jface.viewers.Viewer,
57      * java.lang.Object, java.lang.Object)
58      */

59     public boolean select(Viewer viewer, Object JavaDoc parentElement, Object JavaDoc element) {
60
61         ITreeContentProvider contentProvider = (ITreeContentProvider) ((TreeViewer) viewer)
62                 .getContentProvider();
63
64         IPreferenceNode node = (IPreferenceNode) element;
65         Object JavaDoc[] children = contentProvider.getChildren(node);
66         String JavaDoc text = node.getLabelText();
67
68         if(wordMatches(text))
69             return true;
70         
71         if (matchItem) {
72
73             // Will return true if any subnode of the element matches the search
74
if (filter(viewer, element, children).length > 0)
75                 return true;
76         }
77
78         if (node instanceof WorkbenchPreferenceExtensionNode) {
79             WorkbenchPreferenceExtensionNode workbenchNode = (WorkbenchPreferenceExtensionNode) node;
80
81             Collection JavaDoc keywordCollection = (Collection JavaDoc) keywordCache.get(node);
82             if (keywordCollection == null) {
83                 keywordCollection = workbenchNode.getKeywordLabels();
84                 keywordCache.put(node, keywordCollection);
85             }
86             if (keywordCollection.isEmpty())
87                 return false;
88             Iterator JavaDoc keywords = keywordCollection.iterator();
89             while (keywords.hasNext()) {
90                 if (wordMatches((String JavaDoc) keywords.next()))
91                     return true;
92             }
93         }
94         return false;
95
96     }
97
98     /**
99      * Return whether or not if any of the words in text satisfy the
100      * match critera.
101      * @param text
102      * @return boolean <code>true</code> if one of the words in text
103      * satisifes the match criteria.
104      */

105     private boolean wordMatches(String JavaDoc text) {
106         
107         //If the whole text matches we are all set
108
if(match(text))
109             return true;
110         
111         // Break the text up into words, separating based on whitespace and
112
// common punctuation.
113
// Previously used String.split(..., "\\W"), where "\W" is a regular
114
// expression (see the Javadoc for class Pattern).
115
// Need to avoid both String.split and regular expressions, in order to
116
// compile against JCL Foundation (bug 80053).
117
// Also need to do this in an NL-sensitive way. The use of BreakIterator
118
// was suggested in bug 90579.
119
BreakIterator JavaDoc iter = BreakIterator.getWordInstance();
120         iter.setText(text);
121         int i = iter.first();
122         while (i != java.text.BreakIterator.DONE && i < text.length()) {
123             int j = iter.following(i);
124             if (j == java.text.BreakIterator.DONE)
125                 j = text.length();
126             if (Character.isLetterOrDigit(text.charAt(i))) {
127                 String JavaDoc word = text.substring(i, j);
128                 if (match(word))
129                     return true;
130             }
131             i = j;
132         }
133         return false;
134     }
135
136 }
137
Popular Tags