Search in sources :

Example 1 with BasicEventList

use of ca.odell.glazedlists.BasicEventList in project nebula.widgets.nattable by eclipse.

the class DefaultGlazedListsFilterStrategy method applyFilter.

/**
 * Create GlazedLists matcher editors and apply them to facilitate
 * filtering.
 */
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public void applyFilter(Map<Integer, Object> filterIndexToObjectMap) {
    if (filterIndexToObjectMap.isEmpty()) {
        // wait until all listeners had the chance to handle the clear event
        try {
            this.filterLock.writeLock().lock();
            this.matcherEditor.getMatcherEditors().clear();
        } finally {
            this.filterLock.writeLock().unlock();
        }
        return;
    }
    try {
        EventList<MatcherEditor<T>> matcherEditors = new BasicEventList<MatcherEditor<T>>();
        for (Entry<Integer, Object> mapEntry : filterIndexToObjectMap.entrySet()) {
            Integer columnIndex = mapEntry.getKey();
            String filterText = getStringFromColumnObject(columnIndex, mapEntry.getValue());
            String textDelimiter = this.configRegistry.getConfigAttribute(FilterRowConfigAttributes.TEXT_DELIMITER, DisplayMode.NORMAL, FilterRowDataLayer.FILTER_ROW_COLUMN_LABEL_PREFIX + columnIndex);
            TextMatchingMode textMatchingMode = this.configRegistry.getConfigAttribute(FilterRowConfigAttributes.TEXT_MATCHING_MODE, DisplayMode.NORMAL, FilterRowDataLayer.FILTER_ROW_COLUMN_LABEL_PREFIX + columnIndex);
            IDisplayConverter displayConverter = this.configRegistry.getConfigAttribute(FilterRowConfigAttributes.FILTER_DISPLAY_CONVERTER, DisplayMode.NORMAL, FilterRowDataLayer.FILTER_ROW_COLUMN_LABEL_PREFIX + columnIndex);
            Comparator comparator = this.configRegistry.getConfigAttribute(FilterRowConfigAttributes.FILTER_COMPARATOR, DisplayMode.NORMAL, FilterRowDataLayer.FILTER_ROW_COLUMN_LABEL_PREFIX + columnIndex);
            final Function<T, Object> columnValueProvider = getColumnValueProvider(columnIndex);
            List<ParseResult> parseResults = FilterRowUtils.parse(filterText, textDelimiter, textMatchingMode);
            EventList<MatcherEditor<T>> stringMatcherEditors = new BasicEventList<MatcherEditor<T>>();
            for (ParseResult parseResult : parseResults) {
                try {
                    MatchType matchOperation = parseResult.getMatchOperation();
                    if (matchOperation == MatchType.NONE) {
                        stringMatcherEditors.add(getTextMatcherEditor(columnIndex, textMatchingMode, displayConverter, parseResult.getValueToMatch()));
                    } else {
                        Object threshold = displayConverter.displayToCanonicalValue(parseResult.getValueToMatch());
                        matcherEditors.add(getThresholdMatcherEditor(columnIndex, threshold, comparator, columnValueProvider, matchOperation));
                    }
                } catch (PatternSyntaxException e) {
                    // $NON-NLS-1$
                    LOG.warn("Error on applying a filter: " + e.getLocalizedMessage());
                }
            }
            if (stringMatcherEditors.size() > 0) {
                final CompositeMatcherEditor<T> stringCompositeMatcherEditor = new CompositeMatcherEditor<T>(stringMatcherEditors);
                stringCompositeMatcherEditor.setMode(CompositeMatcherEditor.OR);
                matcherEditors.add(stringCompositeMatcherEditor);
            }
        }
        // wait until all listeners had the chance to handle the clear event
        try {
            this.filterLock.writeLock().lock();
            // Remove the existing matchers that are removed from
            // 'filterIndexToObjectMap'
            final Iterator<MatcherEditor<T>> existingMatcherEditors = this.matcherEditor.getMatcherEditors().iterator();
            while (existingMatcherEditors.hasNext()) {
                final MatcherEditor<T> existingMatcherEditor = existingMatcherEditors.next();
                if (!containsMatcherEditor(matcherEditors, existingMatcherEditor)) {
                    existingMatcherEditors.remove();
                }
            }
            // 'filterIndexToObjectMap'
            for (final MatcherEditor<T> matcherEditor : matcherEditors) {
                if (!containsMatcherEditor(this.matcherEditor.getMatcherEditors(), matcherEditor)) {
                    this.matcherEditor.getMatcherEditors().add(matcherEditor);
                }
            }
        } finally {
            this.filterLock.writeLock().unlock();
        }
    } catch (Exception e) {
        // $NON-NLS-1$
        LOG.error("Error on applying a filter", e);
    }
}
Also used : ThresholdMatcherEditor(ca.odell.glazedlists.matchers.ThresholdMatcherEditor) CompositeMatcherEditor(ca.odell.glazedlists.matchers.CompositeMatcherEditor) MatcherEditor(ca.odell.glazedlists.matchers.MatcherEditor) TextMatcherEditor(ca.odell.glazedlists.matchers.TextMatcherEditor) ParseResult(org.eclipse.nebula.widgets.nattable.filterrow.ParseResult) BasicEventList(ca.odell.glazedlists.BasicEventList) TextMatchingMode(org.eclipse.nebula.widgets.nattable.filterrow.TextMatchingMode) CompositeMatcherEditor(ca.odell.glazedlists.matchers.CompositeMatcherEditor) IDisplayConverter(org.eclipse.nebula.widgets.nattable.data.convert.IDisplayConverter) PatternSyntaxException(java.util.regex.PatternSyntaxException) Comparator(java.util.Comparator) MatchType(org.eclipse.nebula.widgets.nattable.filterrow.ParseResult.MatchType) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Aggregations

BasicEventList (ca.odell.glazedlists.BasicEventList)1 CompositeMatcherEditor (ca.odell.glazedlists.matchers.CompositeMatcherEditor)1 MatcherEditor (ca.odell.glazedlists.matchers.MatcherEditor)1 TextMatcherEditor (ca.odell.glazedlists.matchers.TextMatcherEditor)1 ThresholdMatcherEditor (ca.odell.glazedlists.matchers.ThresholdMatcherEditor)1 Comparator (java.util.Comparator)1 PatternSyntaxException (java.util.regex.PatternSyntaxException)1 IDisplayConverter (org.eclipse.nebula.widgets.nattable.data.convert.IDisplayConverter)1 ParseResult (org.eclipse.nebula.widgets.nattable.filterrow.ParseResult)1 MatchType (org.eclipse.nebula.widgets.nattable.filterrow.ParseResult.MatchType)1 TextMatchingMode (org.eclipse.nebula.widgets.nattable.filterrow.TextMatchingMode)1