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

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: JXFindPanel.java,v 1.10 2005/10/12 11:26:56 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;
  22. import java.awt.Component;
  23. import java.awt.event.ActionEvent;
  24. import java.util.regex.Pattern;
  25. import javax.swing.AbstractAction;
  26. import javax.swing.Action;
  27. import javax.swing.Box;
  28. import javax.swing.BoxLayout;
  29. import javax.swing.JCheckBox;
  30. import javax.swing.JLabel;
  31. import javax.swing.JOptionPane;
  32. /**
  33.  * Simple FindPanel for usage in a JXDialog.
  34.  * 
  35.  * 
  36.  * @author ??
  37.  * @author Jeanette Winzenburg
  38.  */
  39. public class JXFindPanel extends AbstractPatternPanel {
  40.     
  41.     public static final String FIND_NEXT_ACTION_COMMAND = "findNext";
  42.     public static final String FIND_PREVIOUS_ACTION_COMMAND = "findPrevious";
  43.     
  44.     protected Searchable searchable;
  45.     protected JCheckBox wrapCheck;
  46.     protected JCheckBox backCheck;
  47.     private boolean initialized;
  48.     public JXFindPanel() {
  49.         this(null);
  50.     }
  51.     
  52.     public JXFindPanel(Searchable searchable) {
  53.         setSearchable(searchable);
  54.         initActions();
  55.     }
  56.     
  57.     /**
  58.      * Sets the Searchable targeted of this find widget.
  59.      * Triggers a search with null pattern to release the old
  60.      * searchable, if any.
  61.      * 
  62.      * @param searchable 
  63.      */
  64.     public void setSearchable(Searchable searchable) {
  65.         if ((this.searchable != null) && this.searchable.equals(searchable)) return;
  66.         Searchable old = this.searchable;
  67.         if (old != null) {
  68.             old.search((Pattern) null);
  69.         }
  70.         this.searchable = searchable;
  71.         getPatternModel().setFoundIndex(-1);
  72.         firePropertyChange("searchable", old, this.searchable);
  73.     }
  74.     
  75.     public void addNotify() {
  76.         init();
  77.         super.addNotify();
  78.     }
  79.     
  80.     protected void init() {
  81.         if (initialized) return;
  82.         initialized = true;
  83.         initComponents();
  84.         build();
  85.         bind();
  86.         setName(getUIString(SEARCH_TITLE));
  87.         
  88.     }
  89.     
  90.     //------------------ support synch the model <--> components
  91.     
  92.     protected void bind() {
  93.         super.bind();
  94.         getActionContainerFactory().configureButton(wrapCheck, 
  95.                 getAction(PatternModel.MATCH_WRAP_ACTION_COMMAND),
  96.                 null);
  97.         getActionContainerFactory().configureButton(backCheck, 
  98.                 getAction(PatternModel.MATCH_BACKWARDS_ACTION_COMMAND),
  99.                 null);
  100.     }
  101.     
  102.     /**
  103.      * called from listening to empty property of PatternModel.
  104.      * 
  105.      * this implementation calls super and additionally synchs the 
  106.      * enabled state of FIND_NEXT_ACTION_COMMAND, FIND_PREVIOUS_ACTION_COMMAND
  107.      * to !empty.
  108.      */
  109.     @Override
  110.     protected void refreshEmptyFromModel() {
  111.         super.refreshEmptyFromModel();
  112.         boolean enabled = !getPatternModel().isEmpty();
  113.         getAction(FIND_NEXT_ACTION_COMMAND).setEnabled(enabled);
  114.         getAction(FIND_PREVIOUS_ACTION_COMMAND).setEnabled(enabled);
  115.     }
  116.     //--------------------- action callbacks
  117.     /**
  118.      * Action callback for Find action.
  119.      * Find next/previous match using current setting of direction flag.
  120.      * 
  121.      */
  122.     public void match() {
  123.         doFind();
  124.     }
  125.     /**
  126.      * Action callback for FindNext action.
  127.      * Sets direction flag to forward and calls find.
  128.      */
  129.     public void findNext() {
  130.         getPatternModel().setBackwards(false);
  131.         doFind();
  132.     }
  133.     
  134.     /**
  135.      * Action callback for FindPrevious action.
  136.      * Sets direction flag to previous and calls find.
  137.      */
  138.     public void findPrevious() {
  139.         getPatternModel().setBackwards(true);
  140.         doFind();
  141.     }
  142.     
  143.     protected void doFind() {
  144.         if (searchable == null)
  145.             return;
  146.         int foundIndex = doSearch();
  147.         boolean notFound = (foundIndex == -1) && !getPatternModel().isEmpty();
  148.         if (notFound) {
  149.             if (getPatternModel().isWrapping()) {
  150.                 notFound = doSearch() == -1;
  151.             }
  152.         }
  153.         if (notFound) {
  154.             showNotFoundMessage();
  155.         } else {
  156.             showFoundMessage();
  157.         }
  158.     }
  159.     /**
  160.      * @return
  161.      */
  162.     protected int doSearch() {
  163.         int foundIndex = searchable.search(getPatternModel().getPattern(), 
  164.                 getPatternModel().getFoundIndex(), getPatternModel().isBackwards());
  165.         getPatternModel().setFoundIndex(foundIndex);
  166.         return getPatternModel().getFoundIndex();
  167.     }
  168.     protected void showFoundMessage() {
  169.         
  170.     }
  171.     /**
  172.      * 
  173.      */
  174.     protected void showNotFoundMessage() {
  175.         JOptionPane.showMessageDialog(this, "Value not found");
  176.     }
  177.     //-------------------------- initial
  178.     
  179.     /**
  180.      * 
  181.      */
  182.     protected void initExecutables() {
  183.         getActionMap().put(FIND_NEXT_ACTION_COMMAND, 
  184.                 createBoundAction(FIND_NEXT_ACTION_COMMAND, "findNext"));
  185.         getActionMap().put(FIND_PREVIOUS_ACTION_COMMAND, 
  186.                 createBoundAction(FIND_PREVIOUS_ACTION_COMMAND, "findPrevious"));
  187.         super.initExecutables();
  188.     }
  189.   
  190. //----------------------------- init ui
  191.     
  192.     /** create components.
  193.      * 
  194.      */
  195.     protected void initComponents() {
  196.         super.initComponents();
  197.         wrapCheck = new JCheckBox();
  198.         backCheck = new JCheckBox();
  199.     }
  200.     protected void build() {
  201.         Box lBox = new Box(BoxLayout.LINE_AXIS); 
  202.         lBox.add(searchLabel);
  203.         lBox.add(new JLabel(":"));
  204.         lBox.add(new JLabel("  "));
  205.         lBox.setAlignmentY(Component.TOP_ALIGNMENT);
  206.         Box rBox = new Box(BoxLayout.PAGE_AXIS); 
  207.         rBox.add(searchField);
  208.         rBox.add(matchCheck);
  209.         rBox.add(wrapCheck);
  210.         rBox.add(backCheck);
  211.         rBox.setAlignmentY(Component.TOP_ALIGNMENT);
  212.         setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
  213.         
  214.         add(lBox);
  215.         add(rBox);
  216.     }
  217. }