1 11 package org.eclipse.ui.dialogs; 12 13 import java.util.ArrayList ; 14 import java.util.HashMap ; 15 import java.util.List ; 16 import java.util.Map ; 17 18 import org.eclipse.jface.viewers.AbstractTreeViewer; 19 import org.eclipse.jface.viewers.ILabelProvider; 20 import org.eclipse.jface.viewers.ITreeContentProvider; 21 import org.eclipse.jface.viewers.StructuredViewer; 22 import org.eclipse.jface.viewers.Viewer; 23 import org.eclipse.jface.viewers.ViewerFilter; 24 import org.eclipse.ui.internal.misc.StringMatcher; 25 26 import com.ibm.icu.text.BreakIterator; 27 28 37 public class PatternFilter extends ViewerFilter { 38 41 private Map cache = new HashMap (); 42 43 46 private Map foundAnyCache = new HashMap (); 47 48 private boolean useCache = false; 49 50 54 private boolean includeLeadingWildcard = false; 55 56 59 private StringMatcher matcher; 60 61 private boolean useEarlyReturnIfMatcherIsNull = true; 62 63 private static Object [] EMPTY = new Object [0]; 64 65 68 public final Object [] filter(Viewer viewer, Object parent, Object [] elements) { 69 if (matcher == null && useEarlyReturnIfMatcherIsNull) { 73 return elements; 74 } 75 76 if (!useCache) { 77 return super.filter(viewer, parent, elements); 78 } 79 80 Object [] filtered = (Object []) cache.get(parent); 81 if (filtered == null) { 82 Boolean foundAny = (Boolean ) foundAnyCache.get(parent); 83 if (foundAny != null && !foundAny.booleanValue()) { 84 filtered = EMPTY; 85 } else { 86 filtered = super.filter(viewer, parent, elements); 87 } 88 cache.put(parent, filtered); 89 } 90 return filtered; 91 } 92 93 103 private boolean isAnyVisible(Viewer viewer, Object parent, Object [] elements) { 104 if (matcher == null) { 105 return true; 106 } 107 108 if (!useCache) { 109 return computeAnyVisible(viewer, elements); 110 } 111 112 Object [] filtered = (Object []) cache.get(parent); 113 if (filtered != null) { 114 return filtered.length > 0; 115 } 116 Boolean foundAny = (Boolean ) foundAnyCache.get(parent); 117 if (foundAny == null) { 118 foundAny = computeAnyVisible(viewer, elements) ? Boolean.TRUE : Boolean.FALSE; 119 foundAnyCache.put(parent, foundAny); 120 } 121 return foundAny.booleanValue(); 122 } 123 124 130 private boolean computeAnyVisible(Viewer viewer, Object [] elements) { 131 boolean elementFound = false; 132 for (int i = 0; i < elements.length && !elementFound; i++) { 133 Object element = elements[i]; 134 elementFound = isElementVisible(viewer, element); 135 } 136 return elementFound; 137 } 138 139 142 public final boolean select(Viewer viewer, Object parentElement, 143 Object element) { 144 return isElementVisible(viewer, element); 145 } 146 147 154 public final void setIncludeLeadingWildcard( 155 final boolean includeLeadingWildcard) { 156 this.includeLeadingWildcard = includeLeadingWildcard; 157 } 158 159 165 public void setPattern(String patternString) { 166 if ("org.eclipse.ui.keys.optimization.true".equals(patternString)) { useEarlyReturnIfMatcherIsNull = true; 170 return; 171 } else if ("org.eclipse.ui.keys.optimization.false".equals(patternString)) { useEarlyReturnIfMatcherIsNull = false; 173 return; 174 } 175 clearCaches(); 176 if (patternString == null || patternString.equals("")) { matcher = null; 178 } else { 179 String pattern = patternString + "*"; if (includeLeadingWildcard) { 181 pattern = "*" + pattern; } 183 matcher = new StringMatcher(pattern, true, false); 184 } 185 } 186 187 191 void clearCaches() { 192 cache.clear(); 193 foundAnyCache.clear(); 194 } 195 196 203 private boolean match(String string) { 204 if (matcher == null) { 205 return true; 206 } 207 return matcher.match(string); 208 } 209 210 219 public boolean isElementSelectable(Object element){ 220 return element != null; 221 } 222 223 237 public boolean isElementVisible(Viewer viewer, Object element){ 238 return isParentMatch(viewer, element) || isLeafMatch(viewer, element); 239 } 240 241 252 protected boolean isParentMatch(Viewer viewer, Object element){ 253 Object [] children = ((ITreeContentProvider) ((AbstractTreeViewer) viewer) 254 .getContentProvider()).getChildren(element); 255 256 if ((children != null) && (children.length > 0)) { 257 return isAnyVisible(viewer, element, children); 258 } 259 return false; 260 } 261 262 272 protected boolean isLeafMatch(Viewer viewer, Object element){ 273 String labelText = ((ILabelProvider) ((StructuredViewer) viewer) 274 .getLabelProvider()).getText(element); 275 276 if(labelText == null) { 277 return false; 278 } 279 return wordMatches(labelText); 280 } 281 282 289 private String [] getWords(String text){ 290 List words = new ArrayList (); 291 BreakIterator iter = BreakIterator.getWordInstance(); 300 iter.setText(text); 301 int i = iter.first(); 302 while (i != java.text.BreakIterator.DONE && i < text.length()) { 303 int j = iter.following(i); 304 if (j == java.text.BreakIterator.DONE) { 305 j = text.length(); 306 } 307 if (Character.isLetterOrDigit(text.charAt(i))) { 309 String word = text.substring(i, j); 310 words.add(word); 311 } 312 i = j; 313 } 314 return (String []) words.toArray(new String [words.size()]); 315 } 316 317 325 protected boolean wordMatches(String text) { 326 if (text == null) { 327 return false; 328 } 329 330 if(match(text)) { 332 return true; 333 } 334 335 String [] words = getWords(text); 337 for (int i = 0; i < words.length; i++) { 338 String word = words[i]; 339 if (match(word)) { 340 return true; 341 } 342 } 343 344 return false; 345 } 346 347 352 void setUseCache(boolean useCache) { 353 this.useCache = useCache; 354 } 355 } 356 | Popular Tags |