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

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: PatternFilter.java,v 1.7 2005/10/13 08:59:52 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.ArrayList;
  23. import java.util.regex.Pattern;
  24. /**
  25.  * Pluggable pattern filter.
  26.  *
  27.  * @author Ramesh Gupta
  28.  */
  29. public class PatternFilter extends Filter implements PatternMatcher {
  30.     private ArrayList<Integer> toPrevious;
  31.     protected Pattern pattern = null;
  32.     public PatternFilter() {
  33.         this(null, 0, 0);
  34.     }
  35.     public PatternFilter(String regularExpr, int matchFlags, int col) {
  36.         super(col);
  37.         setPattern(regularExpr, matchFlags);
  38.     }
  39.     protected void init() {
  40. toPrevious = new ArrayList<Integer>();
  41.     }
  42.     public void setPattern(String regularExpr, int matchFlags) {
  43.         if ((regularExpr == null) || (regularExpr.length() == 0)) {
  44.             regularExpr = ".*";
  45.         }
  46.         setPattern(Pattern.compile(regularExpr, matchFlags));
  47.     }
  48.     /**
  49.      * Sets the pattern used by this filter for matching.
  50.      *
  51.      * @param pattern the pattern used by this filter for matching
  52.      * @see java.util.regex.Pattern
  53.      */
  54.     public void setPattern(Pattern pattern) {
  55.         if (pattern == null) {
  56.             setPattern(null, 0);
  57.         } else {
  58.             this.pattern = pattern;
  59.             refresh();
  60.         }
  61.     }
  62.     /**
  63.      * Returns the pattern used by this filter for matching.
  64.      *
  65.      * @return the pattern used by this filter for matching
  66.      * @see java.util.regex.Pattern
  67.      */
  68.     public Pattern getPattern() {
  69.         return pattern;
  70.     }
  71.     /**
  72.      * Resets the internal row mappings from this filter to the previous filter.
  73.      */
  74.     protected void reset() {
  75.         toPrevious.clear();
  76.         int inputSize = getInputSize();
  77.         fromPrevious = new int[inputSize];  // fromPrevious is inherited protected
  78.         for (int i = 0; i < inputSize; i++) {
  79.             fromPrevious[i] = -1;
  80.         }
  81.     }
  82.     protected void filter() {
  83.         if (pattern != null) {
  84.             int inputSize = getInputSize();
  85.             int current = 0;
  86.             for (int i = 0; i < inputSize; i++) {
  87.                 if (test(i)) {
  88.                     toPrevious.add(new Integer(i));
  89.                     // generate inverse map entry while we are here
  90.                     fromPrevious[i] = current++;
  91.                 }
  92.             }
  93.         }
  94.     }
  95.     /**
  96.      * @param row
  97.      * @return
  98.      */
  99.     public boolean test(int row) {
  100.         // PENDING: wrong false?
  101.         // null pattern should be treated the same as null searchString
  102.         // which is open
  103.         // !testable should be clarified to mean "ignore" when filtering
  104.         if (pattern == null) {
  105.             return false;
  106.         }
  107.         // ask the adapter if the column should be includes
  108.         if (!adapter.isTestable(getColumnIndex())) {
  109.             return false; 
  110.         }
  111.         Object value = getInputValue(row, getColumnIndex());
  112.         if (value == null) {
  113.             return false;
  114.         }
  115.         else {
  116.             boolean matches = pattern.matcher(value.toString()).find();
  117.             return matches;
  118.         }
  119.     }
  120.     public int getSize() {
  121.         return toPrevious.size();
  122.     }
  123.     protected int mapTowardModel(int row) {
  124.         return toPrevious.get(row);
  125.     }
  126. }