1

I am using a Nebula GridTreeViewer for which I need to add filters like eclipse filters. When we go to Window->Preferences, we get a filter on top of the left side tree which says 'type filter text'.

I tried the TreeViewer with the FilteredTree -

final FilteredTree filteredTree = new FilteredTree(parent, SWT.BORDER
                    | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION,
                    new MyPatternFilter(), true);
 TreeViewer treeViewer = filteredTree.getViewer();

The above code works fine for a TreeViewer. Is there a similar way to attach such kind of a filter to a Nebula GridTreeViewer? If yes, please tell me. Would be very helpful. Thanks.

flavio.donze
  • 7,432
  • 9
  • 58
  • 91
metal7
  • 265
  • 1
  • 3
  • 9

1 Answers1

2

You will have to create your own quick search adapter. Observe the code in FilteredTree.

I can't post my own adapter, since it's company property. Here are some hints.

Things you will need in your MyQuickSearchAdapter:

  • public QuickSearchAdapter(Composite parent) constructor

  • A setViewer(StructuredViewer) API that does this.viewer = viewer and this.viewer.addFilter(viewerFilter) (see below for viewerFilter)

  • An abstract method getLabelProvider() to access a viewer's LabelProvider (in our case, GridTreeViewer)

  • Creation methods for a Text field that will represent the quick search area.

  • (Optional) Creation method for a label/button that clears the text when clicked (as in FilteredTree) - although this is overkill, in my opinion.

  • private MyViewerFilter extends ViewerFilter nested class, that does the actual filtering. This will have a String instance field that holds the search text; this field will have a setter that will be called each time you type something in the filter box. Will look something like viewerFilter.setSearchText(filterBox.getText());. This nested class will also override the select(Viewer, Object, Object), which will use the getLabelProvider().getText(element) to extract the text for a certain cell. Something like:

    // Automatically adds wildcard characters before and after search string:
    public static final String QSEARCH_REGEX_PATTERN = "(.*)%s(.*)"; //$NON-NLS-1$
    
    private class MyViewerFilter extends ViewerFilter 
    {
        // --------------------- <Instance Fields> -----------------------
    
        private String searchString;
    
        // --------------------- <Setters> -----------------------
    
        /**
         * @param 
         *      Text that goes inside the REGEX pattern
         */
        public void setSearchText(final String searchString) 
        {
            //this.searchString = "(.*)" + searchString.toLowerCase() + "(.*)";  //$NON-NLS-1$ //$NON-NLS-2$
            this.searchString = String.format(QSEARCH_REGEX_PATTERN, searchString.toLowerCase());
        }
    
        // --------------------- <Overridden search method> -----------------------
    
        @Override
        public boolean select(final Viewer viewer, final Object parentElement, final Object element) 
        {
            if (StringUtils.isEmpty(searchString))
                return true;
    
            final String text = getLabelProvider().getText(element);
    
            if (StringUtils.isEmpty(text))
                return true;
    
            return text.toLowerCase().matches(searchString);
        }
    }
    

That's the hard part. After that, you just make a class called MyFilteredGridTreeViewer, and add the quick search adapter along side the grid viewer. Also remember to call quickSearchAdapter.setViewer(gridViewer), and you're done!

Georgian
  • 8,795
  • 8
  • 46
  • 87