1 11 package org.eclipse.ui.internal.dialogs; 12 13 import java.text.BreakIterator ; 14 import java.util.Collection ; 15 import java.util.HashMap ; 16 import java.util.Iterator ; 17 import java.util.Map ; 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 32 public class PatternItemFilter extends PatternFilter { 33 34 39 private Map keywordCache = new HashMap (); 40 41 protected boolean matchItem; 42 43 48 public PatternItemFilter(boolean isMatchItem) { 49 super(); 50 matchItem = isMatchItem; 51 } 52 53 59 public boolean select(Viewer viewer, Object parentElement, Object element) { 60 61 ITreeContentProvider contentProvider = (ITreeContentProvider) ((TreeViewer) viewer) 62 .getContentProvider(); 63 64 IPreferenceNode node = (IPreferenceNode) element; 65 Object [] children = contentProvider.getChildren(node); 66 String text = node.getLabelText(); 67 68 if(wordMatches(text)) 69 return true; 70 71 if (matchItem) { 72 73 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 keywordCollection = (Collection ) 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 keywords = keywordCollection.iterator(); 89 while (keywords.hasNext()) { 90 if (wordMatches((String ) keywords.next())) 91 return true; 92 } 93 } 94 return false; 95 96 } 97 98 105 private boolean wordMatches(String text) { 106 107 if(match(text)) 109 return true; 110 111 BreakIterator 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 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 |