1 11 package org.eclipse.ui.internal.dialogs; 12 13 import java.util.HashMap ; 14 import java.util.Map ; 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 33 public class PatternFilter extends ViewerFilter { 34 35 private Map cache = new HashMap (); 36 37 private StringMatcher matcher; 38 39 42 public Object [] filter(Viewer viewer, Object parent, Object [] elements) { 43 if (matcher == null) 44 return elements; 45 46 Object [] filtered = (Object []) 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 57 public boolean select(Viewer viewer, Object parentElement, Object element) { 58 Object [] children = ((ITreeContentProvider) ((AbstractTreeViewer) viewer) 59 .getContentProvider()).getChildren(element); 60 if (children.length > 0) 61 return filter(viewer, element, children).length > 0; 62 63 String labelText = ((ILabelProvider) ((StructuredViewer) viewer) 64 .getLabelProvider()).getText(element); 65 if(labelText == null) 66 return false; 67 return match(labelText); 68 } 69 70 74 public void setPattern(String patternString) { 75 cache.clear(); 76 if (patternString == null || patternString.equals("")) matcher = null; 78 else 79 matcher = new StringMatcher(patternString + "*", true, false); } 81 82 88 protected boolean match(String string) { 89 return matcher.match(string); 90 } 91 } 92 | Popular Tags |