KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2005, 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 package org.eclipse.ui.internal.dialogs;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Collection JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Map JavaDoc;
19
20 import org.eclipse.jface.preference.IPreferenceNode;
21 import org.eclipse.jface.viewers.ITreeContentProvider;
22 import org.eclipse.jface.viewers.TreeViewer;
23 import org.eclipse.jface.viewers.Viewer;
24 import org.eclipse.ui.dialogs.PatternFilter;
25 import org.eclipse.ui.internal.preferences.WorkbenchPreferenceExtensionNode;
26
27 /**
28  * A class that handles filtering preference node items based on a supplied
29  * matching string.
30  *
31  * @since 3.2
32  *
33  */

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

41     private Map JavaDoc keywordCache = new HashMap JavaDoc();
42
43     /**
44      * Create a new instance of a PreferencePatternFilter
45      *
46      * @param isMatchItem
47      */

48     public PreferencePatternFilter() {
49         super();
50     }
51
52     /*
53      * Return true if the given Object matches with any possible keywords that
54      * have been provided. Currently this is only applicable for preference and
55      * property pages.
56      */

57     private String JavaDoc[] getKeywords(Object JavaDoc element) {
58         List JavaDoc keywordList = new ArrayList JavaDoc();
59         if (element instanceof WorkbenchPreferenceExtensionNode) {
60             WorkbenchPreferenceExtensionNode workbenchNode = (WorkbenchPreferenceExtensionNode) element;
61
62             Collection JavaDoc keywordCollection = (Collection JavaDoc) keywordCache
63                     .get(element);
64             if (keywordCollection == null) {
65                 keywordCollection = workbenchNode.getKeywordLabels();
66                 keywordCache.put(element, keywordCollection);
67             }
68             if (!keywordCollection.isEmpty()){
69                 Iterator JavaDoc keywords = keywordCollection.iterator();
70                 while (keywords.hasNext()) {
71                     keywordList.add(keywords.next());
72                 }
73             }
74         }
75         return (String JavaDoc[]) keywordList.toArray(new String JavaDoc[keywordList.size()]);
76     }
77
78     /*
79      * (non-Javadoc)
80      *
81      * @see org.eclipse.ui.internal.dialogs.PatternFilter#isElementSelectable(java.lang.Object)
82      */

83     public boolean isElementSelectable(Object JavaDoc element) {
84         return element instanceof WorkbenchPreferenceExtensionNode;
85     }
86
87     /* (non-Javadoc)
88      * @see org.eclipse.ui.dialogs.PatternFilter#isElementVisible(org.eclipse.jface.viewers.Viewer, java.lang.Object)
89      */

90     public boolean isElementVisible(Viewer viewer, Object JavaDoc element) {
91         // Preference nodes are not differentiated based on category since
92
// categories are selectable nodes.
93
if (isLeafMatch(viewer, element)) {
94             return true;
95         }
96
97         ITreeContentProvider contentProvider = (ITreeContentProvider) ((TreeViewer) viewer)
98                 .getContentProvider();
99         IPreferenceNode node = (IPreferenceNode) element;
100         Object JavaDoc[] children = contentProvider.getChildren(node);
101         // Will return true if any subnode of the element matches the search
102
if (filter(viewer, element, children).length > 0) {
103             return true;
104         }
105         return false;
106     }
107
108     /* (non-Javadoc)
109      * @see org.eclipse.ui.dialogs.PatternFilter#isLeafMatch(org.eclipse.jface.viewers.Viewer, java.lang.Object)
110      *
111      */

112     protected boolean isLeafMatch(Viewer viewer, Object JavaDoc element) {
113         IPreferenceNode node = (IPreferenceNode) element;
114         String JavaDoc text = node.getLabelText();
115
116         if (wordMatches(text)) {
117             return true;
118         }
119
120         // Also need to check the keywords
121
String JavaDoc[] keywords = getKeywords(node);
122         for (int i = 0; i < keywords.length; i++){
123             if (wordMatches(keywords[i])) {
124                 return true;
125             }
126         }
127         return false;
128     }
129
130 }
131
Popular Tags