KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: PatternFilter.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.ArrayList JavaDoc;
11 import java.util.regex.Pattern JavaDoc;
12
13 /**
14  * Pluggable pattern filter.
15  *
16  * @author Ramesh Gupta
17  */

18 public class PatternFilter extends Filter implements PatternMatcher {
19     private ArrayList JavaDoc toPrevious, fromPrevious;
20     protected Pattern JavaDoc pattern = null;
21
22     public PatternFilter() {
23         this(null, 0, 0);
24     }
25
26     public PatternFilter(String JavaDoc regularExpr, int matchFlags, int col) {
27         super(col);
28         setPattern(regularExpr, matchFlags);
29     }
30
31     protected void init() {
32         toPrevious = new ArrayList JavaDoc();
33         fromPrevious = new ArrayList JavaDoc();
34     }
35
36     public void setPattern(String JavaDoc regularExpr, int matchFlags) {
37         if ((regularExpr == null) || (regularExpr.length() == 0)) {
38             regularExpr = ".*";
39         }
40         setPattern(Pattern.compile(regularExpr, matchFlags));
41     }
42
43     /**
44      * Sets the pattern used by this filter for matching.
45      *
46      * @param pattern the pattern used by this filter for matching
47      * @see java.util.regex.Pattern
48      */

49     public void setPattern(Pattern JavaDoc pattern) {
50         this.pattern = pattern;
51         refresh();
52     }
53
54     /**
55      * Returns the pattern used by this filter for matching.
56      *
57      * @return the pattern used by this filter for matching
58      * @see java.util.regex.Pattern
59      */

60     public Pattern JavaDoc getPattern() {
61         return pattern;
62     }
63
64     /**
65      * Resets the internal row mappings from this filter to the previous filter.
66      */

67     protected void reset() {
68         toPrevious.clear();
69         Integer JavaDoc none = new Integer JavaDoc(-1);
70         int inputSize = getInputSize();
71         fromPrevious = new ArrayList JavaDoc(inputSize);
72         for (int i = 0; i < inputSize; i++) {
73             fromPrevious.add(i, none);
74         }
75     }
76
77     /**
78      * Generates the row mappings from the previous filter to this filter.
79      */

80     protected void generateMappingFromPrevious() {
81         int outputSize = toPrevious.size();
82         for (int i = 0; i < outputSize; i++) {
83             Integer JavaDoc index = (Integer JavaDoc) toPrevious.get(i);
84             fromPrevious.set(index.intValue(), new Integer JavaDoc(i));
85         }
86     }
87
88     protected void filter() {
89         if (pattern != null) {
90             int inputSize = getInputSize();
91             for (int i = 0; i < inputSize; i++) {
92                 if (test(i)) {
93                     toPrevious.add(new Integer JavaDoc(i));
94                 }
95             }
96         }
97     }
98
99     public boolean test(int row) {
100         if (pattern == null) {
101             return false;
102         }
103
104         // If column index in view coordinates is negative, the column is hidden.
105
if (adapter.modelToView(getColumnIndex()) < 0) {
106             return false; // column is not being displayed; obviously no match!
107
}
108
109         Object JavaDoc value = getInputValue(row, getColumnIndex());
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     public int getSize() {
121         return toPrevious.size();
122     }
123
124     /**
125      * Returns the row in this filter that maps to the specified row in the
126      * previous filter. If there is no previous filter in the pipeline, this returns
127      * the row in this filter that maps to the specified row in the data model.
128      * This method is called from
129      * {@link org.jdesktop.swing.decorator.Filter#convertRowIndexToView(int) convertRowIndexToView}
130      *
131      * @param row a row index in the previous filter's "view" of the data model
132      * @return the row in this filter that maps to the specified row in
133      * the previous filter
134      */

135     protected int translateFromPreviousFilter(int row) {
136         return ((Integer JavaDoc) fromPrevious.get(row)).intValue();
137     }
138
139     /**
140      * Returns the row in the previous filter that maps to the specified row in
141      * this filter. If there is no previous filter in the pipeline, this returns
142      * the row in the data model that maps to the specified row in this filter.
143      * This method is called from
144      * {@link org.jdesktop.swing.decorator.Filter#convertRowIndexToModel(int) convertRowIndexToModel}
145      *
146      * @param row a row index in this filter's "view" of the data model
147      * @return the row in the previous filter that maps to the specified row in
148      * this filter
149      */

150     protected int translateToPreviousFilter(int row) {
151         return ((Integer JavaDoc) toPrevious.get(row)).intValue();
152     }
153 }
Popular Tags