001: /*
002: * $Id: PatternHighlighter.java,v 1.7 2005/10/26 14:30:00 kleopatra Exp $
003: *
004: * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
005: * Santa Clara, California 95054, U.S.A. All rights reserved.
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
020: */
021:
022: package org.jdesktop.swingx.decorator;
023:
024: import java.awt.Color;
025: import java.util.regex.Pattern;
026: import java.util.regex.PatternSyntaxException;
027:
028: /**
029: * PatternHighlighter
030: *
031: * @author Ramesh Gupta
032: */
033: public class PatternHighlighter extends ConditionalHighlighter
034: implements PatternMatcher {
035:
036: protected Pattern pattern = null;
037:
038: /**
039: * Constructs a <code>PatternHighlighter</code> instance with no
040: * background or foreground color and no pattern
041: */
042: public PatternHighlighter() {
043: // default constructor
044: this (null, null, null, 0, 0); // match flags = 0; test column = 0
045: }
046:
047: /**
048: * <p>
049: * Constructs a <code>PatternHighlighter</code> instance with the
050: * specified background and foreground colors that will be used to decorate
051: * the renderer component for all cell in a row if and only if the specified
052: * regExString defines a valid {@link java.util.regex.Pattern}, and the
053: * value of the cell in the specified testColumn of that row matches that
054: * pattern.
055: * </p>
056: *
057: * @param background
058: * background color for decorated cells, or null, if background
059: * should not be changed
060: * @param foreground
061: * foreground color for decorated cells, or null, if foreground
062: * should not be changed
063: * @param regExString
064: * the regular expression string to compile, or null to leave the
065: * pattern undefined
066: * @param testColumn
067: * column to which the pattern matching test is applied; must be
068: * a valid column index in model coordinates
069: * @throws java.util.regex.PatternSyntaxException
070: * if regExString is not null, but it does not define a valid
071: * {@link java.util.regex.Pattern}
072: * @see java.util.regex.Pattern
073: */
074: public PatternHighlighter(Color background, Color foreground,
075: String regExString, int matchFlags, int testColumn)
076: throws PatternSyntaxException {
077: this (background, foreground, regExString, matchFlags,
078: testColumn, -1);
079: }
080:
081: /**
082: * <p>
083: * Constructs a <code>PatternHighlighter</code> instance with the
084: * specified background and foreground colors that will be used to decorate
085: * the renderer component for a cell in the specified decorateColumn of any
086: * row if and only if the specified regExString and matchFlags define a
087: * valid {@link java.util.regex.Pattern}, and the value of the cell in the
088: * specified testColumn of the same row matches that pattern.
089: * </p>
090: *
091: * @param background
092: * background color for decorated cells, or null, if background
093: * should not be changed
094: * @param foreground
095: * foreground color for decorated cells, or null, if foreground
096: * should not be changed
097: * @param regExString
098: * the regular expression string to compile, or null to leave the
099: * pattern undefined
100: * @param matchFlags
101: * a bit mask that may include
102: * {@link java.util.regex.Pattern#CASE_INSENSITIVE},
103: * {@link java.util.regex.Pattern#MULTILINE},
104: * {@link java.util.regex.Pattern#DOTALL},
105: * {@link java.util.regex.Pattern#UNICODE_CASE}, and
106: * {@link java.util.regex.Pattern#CANON_EQ}
107: * @param testColumn
108: * column to which the pattern matching test is applied; must be
109: * a valid column index in model coordinates
110: * @param decorateColumn
111: * column to which decorator attributes will be applied; may be a
112: * valid column index in model coordinates, or -1 to indicate all
113: * columns
114: * @throws java.util.regex.PatternSyntaxException
115: * if regExString is not null, but regExString and matchFlags do
116: * not define a valid {@link java.util.regex.Pattern}
117: * @see java.util.regex.Pattern
118: */
119: public PatternHighlighter(Color background, Color foreground,
120: String regExString, int matchFlags, int testColumn,
121: int decorateColumn) throws PatternSyntaxException {
122: super (background, foreground, testColumn, decorateColumn);
123: setPattern(regExString, matchFlags);
124: }
125:
126: /**
127: * Tests whether the string representation of the value of the cell
128: * identified by the specified adapter matches the pattern, if any, that is
129: * set for this <code>PatternHighlighter</code>, and returns true if the
130: * test succeeds; Otherwise, it returns false.
131: *
132: * @param adapter
133: * the current cell rendering adapter
134: * @return true if the test succeeds; false otherwise
135: */
136: protected boolean test(ComponentAdapter adapter) {
137: if (pattern == null) {
138: return false;
139: }
140:
141: if (!adapter.isTestable(testColumn))
142: return false;
143: Object value = adapter.getFilteredValueAt(adapter.row,
144: testColumn);
145:
146: if (value == null) {
147: return false;
148: } else {
149: boolean matches = pattern.matcher(value.toString()).find();
150: return matches;
151: }
152: }
153:
154: /**
155: * Returns the pattern used by this cell decorator for matching against a
156: * cell's value to determine if the conditions for cell decoration are met.
157: *
158: * @return the pattern used by this cell decorator for matching
159: * @see java.util.regex.Pattern
160: */
161: public Pattern getPattern() {
162: return pattern;
163: }
164:
165: public void setPattern(String regularExpr, int matchFlags) {
166: if ((regularExpr == null) || (regularExpr.length() == 0)) {
167: regularExpr = ".*";
168: }
169: setPattern(Pattern.compile(regularExpr, matchFlags));
170: }
171:
172: /**
173: * Sets the pattern used by this cell decorator to match against a cell's
174: * value to determine if the conditions for cell decoration are met.
175: *
176: * @param pattern
177: * the pattern used by this cell decorator for matching
178: * @see java.util.regex.Pattern
179: */
180: public void setPattern(Pattern pattern) {
181: this.pattern = pattern;
182: fireStateChanged();
183: }
184:
185: }
|