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

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: SearchHighlighter.java,v 1.7 2005/10/10 18:02:36 rbair 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.awt.Color;
  23. import java.util.regex.Pattern;
  24. /**
  25.  * Convenience Highlighter to test and highlight cells in searching.
  26.  *  
  27.  * @author Jeanette Winzenburg
  28.  */
  29. public class SearchHighlighter extends PatternHighlighter {
  30.     /** the row to highlight in view coordinates. -1 means all */
  31.     int highlightRow;
  32.     private boolean enableHighlight;
  33.     private static final String ALL = ".*";
  34.     
  35.     /**
  36.      * Instantiates a default SearchHighlighter. 
  37.      * The default colors are Yellow background and null foreground.
  38.      * The default matching state is 
  39.      *  Pattern == null, flags = 0, tests all columns and all rows.
  40.      * 
  41.      */
  42.     public SearchHighlighter() {
  43.         this(Color.YELLOW.brighter(), null);
  44.     }
  45.     
  46.     /**
  47.      * Instantiates a default SearchHighlighter with background/foreground colors.
  48.      * The default matching state is 
  49.      *  Pattern == null, flags = 0, tests all columns and all rows.
  50.      * 
  51.      * @param background color of hightlight background
  52.      * @param foreground color of highlight foreground
  53.      */
  54.     public SearchHighlighter(Color background, Color foreground) {
  55.         super(background, foreground, null, 0, -1);
  56.         setHighlightRow(-1);
  57.     }
  58.     /**
  59.      * Toggle to enable/disable - if disabled never hightlights.
  60.      * 
  61.      * @param enableHighlight
  62.      */
  63.     public void setEnabled(boolean enableHighlight) {
  64.         this.enableHighlight = enableHighlight;
  65.         fireStateChanged();
  66.     }
  67.     
  68.     protected boolean needsHighlight(ComponentAdapter adapter) {
  69.         if (!isEnabled()) return false;
  70.         if (highlightRow >= 0 && (adapter.row != highlightRow)) {
  71.             return false;
  72.         }
  73.         return super.needsHighlight(adapter);
  74.     }
  75.     private boolean isEnabled() {
  76.         Pattern pattern = getPattern();
  77.         if (pattern == null || ALL.equals(pattern.pattern())) {
  78.             return false;
  79.         }
  80.         return enableHighlight;
  81.     }
  82.     protected boolean test(ComponentAdapter adapter) {
  83.         if (pattern == null) {
  84.             return false;
  85.         }
  86.         int columnToTest = testColumn;
  87.         // use one highlighter for all columns
  88.         if (columnToTest < 0) {
  89.             columnToTest = adapter.viewToModel(adapter.column);
  90.         }
  91.         Object  value = adapter.getFilteredValueAt(adapter.row, columnToTest);
  92.         if (value == null) {
  93.             return false;
  94.         }
  95.         else {
  96.             boolean matches = pattern.matcher(value.toString()).find();
  97.             return matches;
  98.         }
  99.     }
  100.     /** set the row to match in test. 
  101.      * - 1 means all.
  102.      * @param row
  103.      */
  104.     public void setHighlightRow(int row) {
  105.         highlightRow = row;
  106.         fireStateChanged();
  107.     }
  108.     /** 
  109.      * convenience method to test and highlight all rows/columns and 
  110.      * enable.
  111.      *
  112.      */
  113.     public void setHighlightAll() {
  114.         setHighlightCell(-1, -1);
  115.         
  116.     }
  117.     /**
  118.      * Set's highlightRow to row, test- and highlight column = column
  119.      * @param row
  120.      * @param modelColumn
  121.      */
  122.     public void setHighlightCell(int row, int modelColumn) {
  123.         this.testColumn = modelColumn;
  124.         this.highlightColumn = modelColumn;
  125.         this.highlightRow = row;
  126.         setEnabled(true);
  127.     }
  128. }