树的内容过滤--常用于搜索

 设置树的内容过滤刷新 
 public void refreshFilter(String filterText, boolean canAddFilter)
    {
        TreeViewer viewer = getViewer();
        viewer.getControl().setRedraw(false);
        m_filter.setPattern(filterText);
        if(canAddFilter)
            viewer.addFilter(m_filter);
        else
            viewer.refresh();
        viewer.expandAll();
        viewer.getControl().setRedraw(true);
    }

 
定义树的过滤规则:
class PaletteTreeFilter extends ViewerFilter
    {

        public void setPattern(String pattern)
        {
            if(pattern.indexOf('*') == -1)
                pattern = (new StringBuilder(String.valueOf(pattern))).append('*').toString();
            m_matcher = new StringMatcher(pattern, true, false);
        }

        public boolean select(Viewer viewer, Object parent, Object element)
        {
            IImageElement imageElement = (IImageElement)element;
            if(m_matcher.match(imageElement.getName()))
                return true;
            if(element instanceof IImageContainer)
            {
                IImageContainer container = (IImageContainer)element;
                IImageElement elements[] = container.elements();
                List containers = new ArrayList();
                IImageElement aiimageelement[];
                int j = (aiimageelement = elements).length;
                for(int i = 0; i < j; i++)
                {
                    IImageElement subElement = aiimageelement[i];
                    if(m_matcher.match(subElement.getName()))
                        return true;
                    if(subElement instanceof IImageContainer)
                        containers.add((IImageContainer)subElement);
                }

                for(Iterator iterator = containers.iterator(); iterator.hasNext();)
                {
                    IImageContainer subContainer = (IImageContainer)iterator.next();
                    if(select(viewer, element, subContainer))
                        return true;
                }

            }
            return false;
        }

        private StringMatcher m_matcher;

        PaletteTreeFilter(PaletteTreeFilter palettetreefilter)
        {
        }
    }

 

 

 

 

你可能感兴趣的:(J#)