Search in sources :

Example 1 with Filters

use of org.springsource.ide.eclipse.commons.livexp.util.Filters in project eclipse-integration-commons by spring-projects.

the class SwtConnect method connectTextBasedFilter.

/**
 * Connect a filterbox model to a treeviewer. This assumes that the filter is text-based. The filter is applied to the labels of the elements in the tree.
 * <p>
 * For the viewer filter to work correctly the ITreeContentProvider must provide a proper implementation of the 'getParent' method. If getParent only
 * returns null the viewer filter will not be able to check whether an element should be shown when a parent element is selected by the search filter.
 * <p>
 * Note: you can use {@link TreeElementWrappingContentProvider} in order to ensure that ITreeContentProvider keeps track of parent nodes properly.
 */
public static void connectTextBasedFilter(TreeViewer viewer, LiveExpression<Filter<String>> searchBoxModel, LabelProvider labels, ITreeContentProvider treeContent) {
    TreeAwareViewerFilter viewerFilter = new TreeAwareViewerFilter(viewer, Filters.acceptAll(), labels, treeContent);
    Disposable disposable = searchBoxModel.onChange(UIValueListener.from((e, filter) -> {
        viewerFilter.setFilter(searchBoxModel.getValue());
        viewer.refresh(true);
    }));
    // TODO: what if there are existing filters?
    viewer.setFilters(viewerFilter);
    viewer.getControl().addDisposeListener(de -> {
        disposable.dispose();
    });
    Stylers stylers = new Stylers(viewer.getTree().getFont());
    viewer.getControl().addDisposeListener(de -> {
        disposable.dispose();
        stylers.dispose();
    });
    ILabelProvider baseLabels = (ILabelProvider) viewer.getLabelProvider();
    // Can't add bolding support without this! Ensure label provider is set before calling this method
    Assert.isNotNull(baseLabels);
    viewer.setLabelProvider(boldMatchedElements(stylers, baseLabels, Filters.delegatingTo(searchBoxModel)));
}
Also used : Disposable(org.springsource.ide.eclipse.commons.livexp.ui.Disposable) Disposable(org.springsource.ide.eclipse.commons.livexp.ui.Disposable) Text(org.eclipse.swt.widgets.Text) StyledString(org.eclipse.jface.viewers.StyledString) Stylers(org.springsource.ide.eclipse.commons.livexp.ui.Stylers) Assert(org.eclipse.core.runtime.Assert) Filter(org.springsource.ide.eclipse.commons.livexp.util.Filter) UIValueListener(org.springsource.ide.eclipse.commons.livexp.core.UIValueListener) ILabelProvider(org.eclipse.jface.viewers.ILabelProvider) StyledCellLabelProvider(org.eclipse.jface.viewers.StyledCellLabelProvider) Styler(org.eclipse.jface.viewers.StyledString.Styler) LiveExpression(org.springsource.ide.eclipse.commons.livexp.core.LiveExpression) ViewerCell(org.eclipse.jface.viewers.ViewerCell) ModifyListener(org.eclipse.swt.events.ModifyListener) Duration(java.time.Duration) TreeViewer(org.eclipse.jface.viewers.TreeViewer) Filters(org.springsource.ide.eclipse.commons.livexp.util.Filters) ITreeContentProvider(org.eclipse.jface.viewers.ITreeContentProvider) ValueListener(org.springsource.ide.eclipse.commons.livexp.core.ValueListener) Label(org.eclipse.swt.widgets.Label) LabelProvider(org.eclipse.jface.viewers.LabelProvider) LiveVariable(org.springsource.ide.eclipse.commons.livexp.core.LiveVariable) IRegion(org.eclipse.jface.text.IRegion) Stylers(org.springsource.ide.eclipse.commons.livexp.ui.Stylers) ILabelProvider(org.eclipse.jface.viewers.ILabelProvider)

Aggregations

Duration (java.time.Duration)1 Assert (org.eclipse.core.runtime.Assert)1 IRegion (org.eclipse.jface.text.IRegion)1 ILabelProvider (org.eclipse.jface.viewers.ILabelProvider)1 ITreeContentProvider (org.eclipse.jface.viewers.ITreeContentProvider)1 LabelProvider (org.eclipse.jface.viewers.LabelProvider)1 StyledCellLabelProvider (org.eclipse.jface.viewers.StyledCellLabelProvider)1 StyledString (org.eclipse.jface.viewers.StyledString)1 Styler (org.eclipse.jface.viewers.StyledString.Styler)1 TreeViewer (org.eclipse.jface.viewers.TreeViewer)1 ViewerCell (org.eclipse.jface.viewers.ViewerCell)1 ModifyListener (org.eclipse.swt.events.ModifyListener)1 Label (org.eclipse.swt.widgets.Label)1 Text (org.eclipse.swt.widgets.Text)1 LiveExpression (org.springsource.ide.eclipse.commons.livexp.core.LiveExpression)1 LiveVariable (org.springsource.ide.eclipse.commons.livexp.core.LiveVariable)1 UIValueListener (org.springsource.ide.eclipse.commons.livexp.core.UIValueListener)1 ValueListener (org.springsource.ide.eclipse.commons.livexp.core.ValueListener)1 Disposable (org.springsource.ide.eclipse.commons.livexp.ui.Disposable)1 Stylers (org.springsource.ide.eclipse.commons.livexp.ui.Stylers)1