Class PatternFilter

  • Direct Known Subclasses:
    XViewerPatternFilter

    public class PatternFilter
    extends org.eclipse.jface.viewers.ViewerFilter
    A filter used in conjunction with FilteredTree. In order to determine if a node should be filtered it uses the content and label provider of the tree to do pattern matching on its children. This causes the entire tree structure to be realized. Note that the label provider must implement ILabelProvider.
    Since:
    3.2
    See Also:
    FilteredTree
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.Map cache  
      private static java.lang.Object[] EMPTY  
      private java.util.Map foundAnyCache  
      private boolean includeLeadingWildcard
      Whether to include a leading wildcard for all provided patterns.
      private org.eclipse.ui.internal.misc.StringMatcher matcher
      The string pattern matcher used for this pattern filter.
      private boolean useCache  
      private boolean useEarlyReturnIfMatcherIsNull  
    • Constructor Summary

      Constructors 
      Constructor Description
      PatternFilter()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clearCaches()
      Clears the caches used for optimizing this filter.
      private boolean computeAnyVisible​(org.eclipse.jface.viewers.Viewer viewer, java.lang.Object[] elements)
      Returns true if any of the elements makes it through the filter.
      java.lang.Object[] filter​(org.eclipse.jface.viewers.Viewer viewer, java.lang.Object parent, java.lang.Object[] elements)  
      private java.lang.String[] getWords​(java.lang.String text)
      Take the given filter text and break it down into words using a BreakIterator.
      private boolean isAnyVisible​(org.eclipse.jface.viewers.Viewer viewer, java.lang.Object parent, java.lang.Object[] elements)
      Returns true if any of the elements makes it through the filter.
      boolean isElementSelectable​(java.lang.Object element)
      Answers whether the given element is a valid selection in the filtered tree.
      boolean isElementVisible​(org.eclipse.jface.viewers.Viewer viewer, java.lang.Object element)
      Answers whether the given element in the given viewer matches the filter pattern.
      boolean isLeafMatch​(org.eclipse.jface.viewers.Viewer viewer, java.lang.Object element)
      Check if the current (leaf) element is a match with the filter text.
      protected boolean isParentMatch​(org.eclipse.jface.viewers.Viewer viewer, java.lang.Object element)
      Check if the parent (category) is a match to the filter text.
      private boolean match​(java.lang.String string)
      Answers whether the given String matches the pattern.
      boolean select​(org.eclipse.jface.viewers.Viewer viewer, java.lang.Object parentElement, java.lang.Object element)  
      void setIncludeLeadingWildcard​(boolean includeLeadingWildcard)
      Sets whether a leading wildcard should be attached to each pattern string.
      void setPattern​(java.lang.String patternString)
      The pattern string for which this filter should select elements in the viewer.
      void setUseCache​(boolean useCache)
      Can be called by the filtered tree to turn on caching.
      protected boolean wordMatches​(java.lang.String text)
      Return whether or not if any of the words in text satisfy the match critera.
      • Methods inherited from class org.eclipse.jface.viewers.ViewerFilter

        filter, isFilterProperty
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • cache

        private final java.util.Map cache
      • foundAnyCache

        private final java.util.Map foundAnyCache
      • useCache

        private boolean useCache
      • includeLeadingWildcard

        private boolean includeLeadingWildcard
        Whether to include a leading wildcard for all provided patterns. A trailing wildcard is always included.
      • matcher

        private org.eclipse.ui.internal.misc.StringMatcher matcher
        The string pattern matcher used for this pattern filter.
      • useEarlyReturnIfMatcherIsNull

        private boolean useEarlyReturnIfMatcherIsNull
      • EMPTY

        private static java.lang.Object[] EMPTY
    • Constructor Detail

      • PatternFilter

        public PatternFilter()
    • Method Detail

      • filter

        public final java.lang.Object[] filter​(org.eclipse.jface.viewers.Viewer viewer,
                                               java.lang.Object parent,
                                               java.lang.Object[] elements)
        Overrides:
        filter in class org.eclipse.jface.viewers.ViewerFilter
        See Also:
        ViewerFilter.filter(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object[])
      • isAnyVisible

        private boolean isAnyVisible​(org.eclipse.jface.viewers.Viewer viewer,
                                     java.lang.Object parent,
                                     java.lang.Object[] elements)
        Returns true if any of the elements makes it through the filter. This method uses caching if enabled; the computation is done in computeAnyVisible.
        Parameters:
        viewer -
        parent -
        elements - the elements (must not be an empty array)
        Returns:
        true if any of the elements makes it through the filter.
      • computeAnyVisible

        private boolean computeAnyVisible​(org.eclipse.jface.viewers.Viewer viewer,
                                          java.lang.Object[] elements)
        Returns true if any of the elements makes it through the filter.
        Parameters:
        viewer - the viewer
        elements - the elements to test
        Returns:
        true if any of the elements makes it through the filter
      • select

        public final boolean select​(org.eclipse.jface.viewers.Viewer viewer,
                                    java.lang.Object parentElement,
                                    java.lang.Object element)
        Specified by:
        select in class org.eclipse.jface.viewers.ViewerFilter
        See Also:
        ViewerFilter.select(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
      • setIncludeLeadingWildcard

        public final void setIncludeLeadingWildcard​(boolean includeLeadingWildcard)
        Sets whether a leading wildcard should be attached to each pattern string.
        Parameters:
        includeLeadingWildcard - Whether a leading wildcard should be added.
      • setPattern

        public void setPattern​(java.lang.String patternString)
        The pattern string for which this filter should select elements in the viewer.
        Parameters:
        patternString -
      • clearCaches

        public void clearCaches()
        Clears the caches used for optimizing this filter. Needs to be called whenever the tree content changes.
      • match

        private boolean match​(java.lang.String string)
        Answers whether the given String matches the pattern.
        Parameters:
        string - the String to test
        Returns:
        whether the string matches the pattern
      • isElementSelectable

        public boolean isElementSelectable​(java.lang.Object element)
        Answers whether the given element is a valid selection in the filtered tree. For example, if a tree has items that are categorized, the category itself may not be a valid selection since it is used merely to organize the elements.
        Parameters:
        element -
        Returns:
        true if this element is eligible for automatic selection
      • isElementVisible

        public boolean isElementVisible​(org.eclipse.jface.viewers.Viewer viewer,
                                        java.lang.Object element)
        Answers whether the given element in the given viewer matches the filter pattern. This is a default implementation that will show a leaf element in the tree based on whether the provided filter text matches the text of the given element's text, or that of it's children (if the element has any). Subclasses may override this method.
        Parameters:
        viewer - the tree viewer in which the element resides
        element - the element in the tree to check for a match
        Returns:
        true if the element matches the filter pattern
      • isParentMatch

        protected boolean isParentMatch​(org.eclipse.jface.viewers.Viewer viewer,
                                        java.lang.Object element)
        Check if the parent (category) is a match to the filter text. The default behavior returns true if the element has at least one child element that is a match with the filter text. Subclasses may override this method.
        Parameters:
        viewer - the viewer that contains the element
        element - the tree element to check
        Returns:
        true if the given element has children that matches the filter text
      • isLeafMatch

        public boolean isLeafMatch​(org.eclipse.jface.viewers.Viewer viewer,
                                   java.lang.Object element)
        Check if the current (leaf) element is a match with the filter text. The default behavior checks that the label of the element is a match. Subclasses should override this method.
        Parameters:
        viewer - the viewer that contains the element
        element - the tree element to check
        Returns:
        true if the given element's label matches the filter text
      • getWords

        private java.lang.String[] getWords​(java.lang.String text)
        Take the given filter text and break it down into words using a BreakIterator.
        Parameters:
        text -
        Returns:
        an array of words
      • wordMatches

        protected boolean wordMatches​(java.lang.String text)
        Return whether or not if any of the words in text satisfy the match critera.
        Parameters:
        text - the text to match
        Returns:
        boolean true if one of the words in text satisifes the match criteria.
      • setUseCache

        public void setUseCache​(boolean useCache)
        Can be called by the filtered tree to turn on caching.
        Parameters:
        useCache - The useCache to set.