use of ca.odell.glazedlists.matchers.MatcherEditor 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);
}
}
Aggregations