PatternHighlighter.java
上传用户:zhengdagz
上传日期:2014-03-06
资源大小:1956k
文件大小:7k
源码类别:

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: PatternHighlighter.java,v 1.6 2005/10/13 08:59:54 kleopatra Exp $
  3.  *
  4.  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
  5.  * Santa Clara, California 95054, U.S.A. All rights reserved.
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  * 
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  * 
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  20.  */
  21. package org.jdesktop.swingx.decorator;
  22. import java.util.regex.Pattern;
  23. import java.util.regex.PatternSyntaxException;
  24. import java.awt.Color;
  25. /**
  26.  * PatternHighlighter
  27.  * 
  28.  * @author Ramesh Gupta
  29.  */
  30. public class PatternHighlighter extends ConditionalHighlighter implements
  31.         PatternMatcher {
  32.     protected Pattern pattern = null;
  33.     /**
  34.      * Constructs a <code>PatternHighlighter</code> instance with no
  35.      * background or foreground color and no pattern
  36.      */
  37.     public PatternHighlighter() {
  38.         // default constructor
  39.         this(null, null, null, 0, 0); // match flags = 0; test column = 0
  40.     }
  41.     /**
  42.      * <p>
  43.      * Constructs a <code>PatternHighlighter</code> instance with the
  44.      * specified background and foreground colors that will be used to decorate
  45.      * the renderer component for all cell in a row if and only if the specified
  46.      * regExString defines a valid {@link java.util.regex.Pattern}, and the
  47.      * value of the cell in the specified testColumn of that row matches that
  48.      * pattern.
  49.      * </p>
  50.      * 
  51.      * @param background
  52.      *            background color for decorated cells, or null, if background
  53.      *            should not be changed
  54.      * @param foreground
  55.      *            foreground color for decorated cells, or null, if foreground
  56.      *            should not be changed
  57.      * @param regExString
  58.      *            the regular expression string to compile, or null to leave the
  59.      *            pattern undefined
  60.      * @param testColumn
  61.      *            column to which the pattern matching test is applied; must be
  62.      *            a valid column index in model coordinates
  63.      * @throws java.util.regex.PatternSyntaxException
  64.      *             if regExString is not null, but it does not define a valid
  65.      *             {@link java.util.regex.Pattern}
  66.      * @see java.util.regex.Pattern
  67.      */
  68.     public PatternHighlighter(Color background, Color foreground,
  69.             String regExString, int matchFlags, int testColumn)
  70.             throws PatternSyntaxException {
  71.         this(background, foreground, regExString, matchFlags, testColumn, -1);
  72.     }
  73.     /**
  74.      * <p>
  75.      * Constructs a <code>PatternHighlighter</code> instance with the
  76.      * specified background and foreground colors that will be used to decorate
  77.      * the renderer component for a cell in the specified decorateColumn of any
  78.      * row if and only if the specified regExString and matchFlags define a
  79.      * valid {@link java.util.regex.Pattern}, and the value of the cell in the
  80.      * specified testColumn of the same row matches that pattern.
  81.      * </p>
  82.      * 
  83.      * @param background
  84.      *            background color for decorated cells, or null, if background
  85.      *            should not be changed
  86.      * @param foreground
  87.      *            foreground color for decorated cells, or null, if foreground
  88.      *            should not be changed
  89.      * @param regExString
  90.      *            the regular expression string to compile, or null to leave the
  91.      *            pattern undefined
  92.      * @param matchFlags
  93.      *            a bit mask that may include
  94.      *            {@link java.util.regex.Pattern#CASE_INSENSITIVE},
  95.      *            {@link java.util.regex.Pattern#MULTILINE},
  96.      *            {@link java.util.regex.Pattern#DOTALL},
  97.      *            {@link java.util.regex.Pattern#UNICODE_CASE}, and
  98.      *            {@link java.util.regex.Pattern#CANON_EQ}
  99.      * @param testColumn
  100.      *            column to which the pattern matching test is applied; must be
  101.      *            a valid column index in model coordinates
  102.      * @param decorateColumn
  103.      *            column to which decorator attributes will be applied; may be a
  104.      *            valid column index in model coordinates, or -1 to indicate all
  105.      *            columns
  106.      * @throws java.util.regex.PatternSyntaxException
  107.      *             if regExString is not null, but regExString and matchFlags do
  108.      *             not define a valid {@link java.util.regex.Pattern}
  109.      * @see java.util.regex.Pattern
  110.      */
  111.     public PatternHighlighter(Color background, Color foreground,
  112.             String regExString, int matchFlags, int testColumn,
  113.             int decorateColumn) throws PatternSyntaxException {
  114.         super(background, foreground, testColumn, decorateColumn);
  115.         setPattern(regExString, matchFlags);
  116.     }
  117.     /**
  118.      * Tests whether the string representation of the value of the cell
  119.      * identified by the specified adapter matches the pattern, if any, that is
  120.      * set for this <code>PatternHighlighter</code>, and returns true if the
  121.      * test succeeds; Otherwise, it returns false.
  122.      * 
  123.      * @param adapter
  124.      *            the current cell rendering adapter
  125.      * @return true if the test succeeds; false otherwise
  126.      */
  127.     protected boolean test(ComponentAdapter adapter) {
  128.         if (pattern == null) {
  129.             return false;
  130.         }
  131.         if (!adapter.isTestable(testColumn))
  132.             return false;
  133.         Object value = adapter.getFilteredValueAt(adapter.row, testColumn);
  134.         if (value == null) {
  135.             return false;
  136.         } else {
  137.             boolean matches = pattern.matcher(value.toString()).find();
  138.             return matches;
  139.         }
  140.     }
  141.     /**
  142.      * Returns the pattern used by this cell decorator for matching against a
  143.      * cell's value to determine if the conditions for cell decoration are met.
  144.      * 
  145.      * @return the pattern used by this cell decorator for matching
  146.      * @see java.util.regex.Pattern
  147.      */
  148.     public Pattern getPattern() {
  149.         return pattern;
  150.     }
  151.     public void setPattern(String regularExpr, int matchFlags) {
  152.         if ((regularExpr == null) || (regularExpr.length() == 0)) {
  153.             regularExpr = ".*";
  154.         }
  155.         setPattern(Pattern.compile(regularExpr, matchFlags));
  156.     }
  157.     /**
  158.      * Sets the pattern used by this cell decorator to match against a cell's
  159.      * value to determine if the conditions for cell decoration are met.
  160.      * 
  161.      * @param pattern
  162.      *            the pattern used by this cell decorator for matching
  163.      * @see java.util.regex.Pattern
  164.      */
  165.     public void setPattern(Pattern pattern) {
  166.         this.pattern = pattern;
  167.         fireStateChanged();
  168.     }
  169. }