Author: kmorin Date: 2013-02-26 18:07:11 +0100 (Tue, 26 Feb 2013) New Revision: 2577 Url: http://nuiton.org/projects/jaxx/repository/revisions/2577 Log: add text filter highlightas a parameter Modified: trunk/jaxx-runtime/src/main/java/jaxx/runtime/swing/model/JaxxFilterableListModel.java trunk/jaxx-runtime/src/main/java/jaxx/runtime/swing/renderer/FilteredDecoratorListCellRenderer.java Modified: trunk/jaxx-runtime/src/main/java/jaxx/runtime/swing/model/JaxxFilterableListModel.java =================================================================== --- trunk/jaxx-runtime/src/main/java/jaxx/runtime/swing/model/JaxxFilterableListModel.java 2013-02-26 15:07:50 UTC (rev 2576) +++ trunk/jaxx-runtime/src/main/java/jaxx/runtime/swing/model/JaxxFilterableListModel.java 2013-02-26 17:07:11 UTC (rev 2577) @@ -186,7 +186,8 @@ protected void refilter() { filteredItems.clear(); - if (StringUtils.isBlank(filterText) && filters.isEmpty()) { + if (StringUtils.isEmpty(StringUtils.remove(filterText, '*')) + && filters.isEmpty()) { filteredItems.addAll(delegate); } else { Modified: trunk/jaxx-runtime/src/main/java/jaxx/runtime/swing/renderer/FilteredDecoratorListCellRenderer.java =================================================================== --- trunk/jaxx-runtime/src/main/java/jaxx/runtime/swing/renderer/FilteredDecoratorListCellRenderer.java 2013-02-26 15:07:50 UTC (rev 2576) +++ trunk/jaxx-runtime/src/main/java/jaxx/runtime/swing/renderer/FilteredDecoratorListCellRenderer.java 2013-02-26 17:07:11 UTC (rev 2577) @@ -29,6 +29,7 @@ import java.util.regex.Pattern; import javax.swing.JList; import javax.swing.ListCellRenderer; +import org.apache.commons.lang3.StringUtils; import org.nuiton.util.decorator.Decorator; /** @@ -44,6 +45,10 @@ /** text to highlight */ protected String filterText; + protected Pattern pattern; + + protected boolean highlightFilterText = false; + public FilteredDecoratorListCellRenderer(Decorator<?> decorator) { super(decorator); } @@ -52,6 +57,13 @@ Decorator<?> decorator) { super(delegate, decorator); } + + public FilteredDecoratorListCellRenderer(ListCellRenderer delegate, + Decorator<?> decorator, + boolean highlightFilterText) { + super(delegate, decorator); + this.highlightFilterText = highlightFilterText; + } public String getFilterText() { return filterText; @@ -59,8 +71,18 @@ public void setFilterText(String filterText) { this.filterText = filterText; + computePattern(); } + public boolean isHighlightFilterText() { + return highlightFilterText; + } + + public void setHighlightFilterText(boolean highlightFilterText) { + this.highlightFilterText = highlightFilterText; + computePattern(); + } + @Override public Component getListCellRendererComponent(JList list, Object value, @@ -73,25 +95,24 @@ } String stringValue = String.valueOf(value); - if (filterText != null) { - // add the groups in the pattern - String patternText = "(" + filterText.replace("*", ").*(") + ").*"; - Pattern pattern = Pattern.compile(patternText, Pattern.CASE_INSENSITIVE); + if (pattern != null) { Matcher matcher = pattern.matcher(stringValue); - if (matcher.find()) { // for each group caught, add the text between the previous group // and the current group and surround the group with the highlighter - String s = ""; + StringBuilder sb = new StringBuilder(); int i = 0; for (int g = 1 ; g <= matcher.groupCount() ; g++) { String match = matcher.group(g); int indexOfMatch = stringValue.indexOf(match, i); - s += stringValue.substring(i, indexOfMatch) + "<em style='background:#FFFF00'>" + match + "</em>"; + sb.append(stringValue.substring(i, indexOfMatch)) + .append("<span style='background:#FFFF00'>") + .append(match) + .append("</span>"); i = indexOfMatch + match.length(); } - s += stringValue.substring(i); - stringValue = "<html>" + s + "</html>"; + sb.append(stringValue.substring(i)); + stringValue = "<html>" + sb.toString() + "</html>"; } } return delegate.getListCellRendererComponent(list, @@ -101,4 +122,17 @@ cellHasFocus ); } + + protected void computePattern() { + if (highlightFilterText + && !StringUtils.isEmpty(StringUtils.remove(filterText, '*'))) { + // add the groups in the pattern + String patternText = "(" + filterText.replace("*", ").*(") + ").*"; + patternText = StringUtils.remove(patternText, "()"); + pattern = Pattern.compile(patternText, Pattern.CASE_INSENSITIVE); + + } else { + pattern = null; + } + } }