KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > decorator > PatternHighlighter


1 /*
2  * $Id: PatternHighlighter.java,v 1.1.1.1 2004/06/16 01:43:39 davidson1 Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7
8 package org.jdesktop.swing.decorator;
9
10 import java.util.regex.Pattern JavaDoc;
11 import java.util.regex.PatternSyntaxException JavaDoc;
12
13 import java.awt.Color JavaDoc;
14
15 /**
16  * PatternHighlighter
17  *
18  * @author Ramesh Gupta
19  */

20 public class PatternHighlighter extends ConditionalHighlighter
21     implements PatternMatcher {
22     /**
23      * Constructs a <code>PatternHighlighter</code> instance with no
24      * background or foreground color and no pattern
25      */

26     public PatternHighlighter() {
27         // default constructor
28
this(null, null, null, 0, 0); // match flags = 0; test column = 0
29
}
30
31     /**
32      * <p>Constructs a <code>PatternHighlighter</code> instance with the
33      * specified background and foreground colors that will be used to decorate
34      * the renderer component for all cell in a row if and only if the specified
35      * regExString defines a valid {@link java.util.regex.Pattern}, and the value
36      * of the cell in the specified testColumn of that row matches that pattern.</p>
37      *
38      * @param background background color for decorated cells, or null, if
39      * background should not be changed
40      * @param foreground foreground color for decorated cells, or null, if
41      * foreground should not be changed
42      * @param regExString the regular expression string to compile, or null to
43      * leave the pattern undefined
44      * @param testColumn column to which the pattern matching test is applied;
45      * must be a valid column index in model coordinates
46      * @throws java.util.regex.PatternSyntaxException if regExString is not null,
47      * but it does not define a valid {@link java.util.regex.Pattern}
48      * @see java.util.regex.Pattern
49      */

50     public PatternHighlighter(Color JavaDoc background, Color JavaDoc foreground,
51         String JavaDoc regExString, int matchFlags, int testColumn) throws PatternSyntaxException JavaDoc {
52         this(background, foreground, regExString, matchFlags, testColumn, -1);
53     }
54
55     /**
56      * <p>Constructs a <code>PatternHighlighter</code> instance with the
57      * specified background and foreground colors that will be used to decorate
58      * the renderer component for a cell in the specified decorateColumn of any
59      * row if and only if the specified regExString and matchFlags define a valid
60      * {@link java.util.regex.Pattern}, and the value of the cell in the specified
61      * testColumn of the same row matches that pattern.</p>
62      *
63      * @param background background color for decorated cells, or null, if
64      * background should not be changed
65      * @param foreground foreground color for decorated cells, or null, if
66      * foreground should not be changed
67      * @param regExString the regular expression string to compile, or null to
68      * leave the pattern undefined
69      * @param matchFlags a bit mask that may include
70      * {@link java.util.regex.Pattern#CASE_INSENSITIVE},
71      * {@link java.util.regex.Pattern#MULTILINE},
72      * {@link java.util.regex.Pattern#DOTALL},
73      * {@link java.util.regex.Pattern#UNICODE_CASE}, and
74      * {@link java.util.regex.Pattern#CANON_EQ}
75      * @param testColumn column to which the pattern matching test is applied;
76      * must be a valid column index in model coordinates
77      * @param decorateColumn column to which decorator attributes will be
78      * applied; may be a valid column index in model coordinates,
79      * or -1 to indicate all columns
80      * @throws java.util.regex.PatternSyntaxException if regExString is not null,
81      * but regExString and matchFlags do not define a valid {@link java.util.regex.Pattern}
82      * @see java.util.regex.Pattern
83      */

84     public PatternHighlighter(Color JavaDoc background, Color JavaDoc foreground, String JavaDoc regExString,
85         int matchFlags, int testColumn, int decorateColumn) throws PatternSyntaxException JavaDoc {
86         super(background, foreground, testColumn, decorateColumn);
87         setPattern(regExString, matchFlags);
88     }
89
90     /**
91      * Tests whether the string representation of the value of the cell
92      * identified by the specified adapter matches the pattern, if any, that is
93      * set for this <code>PatternHighlighter</code>, and returns true
94      * if the test succeeds; Otherwise, it returns false.
95      *
96      * @param adapter the current cell rendering adapter
97      * @return true if the test succeeds; false otherwise
98      */

99     protected boolean test(ComponentAdapter adapter) {
100         if (pattern == null) {
101             return false;
102         }
103
104         // getValueAt() requires column in view coordinates. So convert.
105
int testColumnV = adapter.modelToView(testColumn);
106         if (testColumnV < 0) {
107             return false; // column is not being displayed
108
}
109         Object JavaDoc value = adapter.getFilteredValueAt(adapter.row, testColumnV);
110
111         if (value == null) {
112             return false;
113         }
114         else {
115             boolean matches = pattern.matcher(value.toString()).matches();
116             return matches;
117         }
118     }
119
120     /**
121      * Returns the pattern used by this cell decorator for matching against a
122      * cell's value to determine if the conditions for cell decoration are met.
123      *
124      * @return the pattern used by this cell decorator for matching
125      * @see java.util.regex.Pattern
126      */

127     public Pattern JavaDoc getPattern() {
128         return pattern;
129     }
130
131     public void setPattern(String JavaDoc regularExpr, int matchFlags) {
132         if ((regularExpr == null) || (regularExpr.length() == 0)) {
133             regularExpr = ".*";
134         }
135         setPattern(Pattern.compile(regularExpr, matchFlags));
136     }
137
138     /**
139      * Sets the pattern used by this cell decorator to match against a cell's
140      * value to determine if the conditions for cell decoration are met.
141      *
142      * @param pattern the pattern used by this cell decorator for matching
143      * @see java.util.regex.Pattern
144      */

145     public void setPattern(Pattern JavaDoc pattern) {
146         this.pattern = pattern;
147     }
148
149     protected Pattern JavaDoc pattern = null;
150 }
Popular Tags