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

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: JXSearchPanel.java,v 1.10 2005/10/10 18:01:43 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;
  22. import java.util.ArrayList;
  23. import java.util.Iterator;
  24. import java.util.List;
  25. import java.util.regex.Pattern;
  26. import javax.swing.ComboBoxModel;
  27. import javax.swing.DefaultComboBoxModel;
  28. import javax.swing.JComboBox;
  29. import org.jdesktop.swingx.decorator.PatternFilter;
  30. import org.jdesktop.swingx.decorator.PatternHighlighter;
  31. import org.jdesktop.swingx.decorator.PatternMatcher;
  32. /**
  33.  * Rudimentary search panel.
  34.  * 
  35.  * Updates PatternMatchers from user input.
  36.  * 
  37.  * Supports 
  38.  * 
  39.  * <ol>
  40.  * <li> text input to match
  41.  * <li> match rules like contains/equals/... 
  42.  * <li> toggle case sensitive match
  43.  * </ol>
  44.  * 
  45.  * TODO: allow custom PatternModel and/or access 
  46.  * to configuration of bound PatternModel. 
  47.  * 
  48.  * TODO: fully support control of multiple PatternMatchers.
  49.  * 
  50.  * @author Ramesh Gupta
  51.  * @author Jeanette Winzenburg
  52.  * 
  53.  */
  54. public class JXSearchPanel extends AbstractPatternPanel {
  55.     public static final String MATCH_RULE_ACTION_COMMAND = "selectMatchRule";
  56.     private JComboBox searchCriteria;
  57.     private List<PatternMatcher> patternMatchers;
  58.     
  59.     public JXSearchPanel() {
  60.         initComponents();
  61.         build();
  62.         initActions();
  63.         bind();
  64.         getPatternModel().setIncremental(true);
  65.     }
  66. //----------------- accessing public properties
  67.     /**
  68.      * sets the PatternFilter control.
  69.      * 
  70.      * PENDING: change to do a addPatternMatcher to enable multiple control.
  71.      * 
  72.      */
  73.     public void setPatternFilter(PatternFilter filter) {
  74.         getPatternMatchers().add(filter);
  75.         updateFieldName(filter);
  76.     }
  77.     /**
  78.      * sets the PatternHighlighter control.
  79.      * 
  80.      * PENDING: change to do a addPatternMatcher to enable multiple control.
  81.      * 
  82.      */
  83.     public void setPatternHighlighter(PatternHighlighter highlighter) {
  84.         getPatternMatchers().add(highlighter);
  85.         updateFieldName(highlighter);
  86.     }
  87.     /**
  88.      * set the label of the search combo.
  89.      * 
  90.      * @param name
  91.      */
  92.     public void setFieldName(String name) {
  93.         searchLabel.setText(name);
  94.     }
  95.     /**
  96.      * returns the label of the search combo.
  97.      * 
  98.      */
  99.     public String getFieldName() {
  100.         return searchLabel.getText();
  101.     }
  102.     /**
  103.      * returns the current compiled Pattern.
  104.      * 
  105.      * @return
  106.      */
  107.     public Pattern getPattern() {
  108.         return patternModel.getPattern();
  109.     }
  110.     /**
  111.      * @param filter
  112.      */
  113.     protected void updateFieldName(PatternMatcher matcher) {
  114.         
  115.         if (matcher instanceof PatternFilter) {
  116.             PatternFilter filter = (PatternFilter) matcher;
  117.             if (filter == null) {
  118.                 searchLabel.setText("Field");
  119.             } else {
  120.                 searchLabel.setText(filter.getColumnName());
  121.             }
  122.         } else {
  123.             if (searchLabel.getText().length() == 0) { // ugly hack
  124.                 searchLabel.setText("Field");
  125.                 /** @todo Remove this hack!!! */
  126.             }
  127.         }
  128.     }
  129.     // ---------------- action callbacks
  130.     /**
  131.      * 
  132.      */
  133.     public void match() {
  134.         for (Iterator<PatternMatcher> iter = getPatternMatchers().iterator(); iter.hasNext();) {
  135.             iter.next().setPattern(getPattern());
  136.             
  137.         }
  138.     }
  139.     /**
  140.      * set's the PatternModel's MatchRule to the selected in combo. 
  141.      * 
  142.      * NOTE: this
  143.      * is public as an implementation side-effect! 
  144.      * No need to ever call directly.
  145.      */
  146.     public void updateMatchRule() {
  147.         getPatternModel().setMatchRule(
  148.                 (String) searchCriteria.getSelectedItem());
  149.     }
  150.     private List<PatternMatcher> getPatternMatchers() {
  151.         if (patternMatchers == null) {
  152.             patternMatchers = new ArrayList<PatternMatcher>();
  153.         }
  154.         return patternMatchers;
  155.     }
  156.     //---------------- init actions and model
  157.     
  158.     protected void initExecutables() {
  159.         super.initExecutables();
  160.         getActionMap().put(MATCH_RULE_ACTION_COMMAND,
  161.                 createBoundAction(MATCH_RULE_ACTION_COMMAND, "updateMatchRule"));
  162.     }
  163.     //--------------------- binding support
  164.     
  165.     /**
  166.      * bind the components to the patternModel/actions.
  167.      */
  168.     protected void bind() {
  169.         super.bind();
  170.         List matchRules = getPatternModel().getMatchRules();
  171.         // PENDING: map rules to localized strings
  172.         ComboBoxModel model = new DefaultComboBoxModel(matchRules.toArray());
  173.         model.setSelectedItem(getPatternModel().getMatchRule());
  174.         searchCriteria.setModel(model);
  175.         searchCriteria.setAction(getAction(MATCH_RULE_ACTION_COMMAND));
  176.         
  177.     }
  178.     
  179.     //------------------------ init ui
  180.     
  181.     /**
  182.      * build container by adding all components.
  183.      * PRE: all components created.
  184.      */
  185.     private void build() {
  186.         add(searchLabel);
  187.         add(searchCriteria);
  188.         add(searchField);
  189.         add(matchCheck);
  190.     }
  191.     /**
  192.      * create contained components.
  193.      * 
  194.      *
  195.      */
  196.     protected void initComponents() {
  197.         super.initComponents();
  198.         searchCriteria = new JComboBox();
  199.     }
  200. }