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

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: JXFindBar.java,v 1.9 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.Color;
  23. import java.awt.FlowLayout;
  24. import javax.swing.JButton;
  25. import javax.swing.JLabel;
  26. import javax.swing.KeyStroke;
  27. import javax.swing.SwingConstants;
  28. /**
  29.  * A simple low-intrusion default widget for incremental search.
  30.  * 
  31.  * Actions registered (in addition to super):
  32.  * <ul>
  33.  * <li> {@link JXDialog#CLOSE_ACTION_COMMAND} - an action bound to this
  34.  * component's cancel method. The method itself is an empty implementation:
  35.  * Subclassing clients can override the method, all clients can register a
  36.  * custom action.
  37.  * </ul>
  38.  * 
  39.  * Key bindings:
  40.  * <ul>
  41.  * <li> ESCAPE - calls action registered for
  42.  * {@link JXDialog#CLOSE_ACTION_COMMAND}
  43.  * </ul>
  44.  * 
  45.  * This implementation uses textfield coloring as not-found visualization.
  46.  * 
  47.  * <p>
  48.  * PENDING: the coloring needs to be read from the UIManager instead of
  49.  * hardcoding.
  50.  * 
  51.  * <p>
  52.  * PENDING: the state transition of found/non-found coloring needs clean-up -
  53.  * there are spurious problems when re-using the same instance (as SearchFactory
  54.  * does).
  55.  * 
  56.  * @author Jeanette Winzenburg
  57.  * 
  58.  */
  59. public class JXFindBar extends JXFindPanel {
  60.     protected Color previousBackgroundColor;
  61.     protected Color previousForegroundColor;
  62.     // PENDING: need to read from UIManager
  63.     protected Color notFoundBackgroundColor = Color.decode("#FF6666");
  64.     protected Color notFoundForegroundColor = Color.white;
  65.     protected JButton findNext;
  66.     protected JButton findPrevious;
  67.     public JXFindBar() {
  68.         this(null);
  69.     }
  70.     public JXFindBar(Searchable searchable) {
  71.         super(searchable);
  72.         getPatternModel().setIncremental(true);
  73.     }
  74.     @Override
  75.     public void setSearchable(Searchable searchable) {
  76.         super.setSearchable(searchable);
  77.         match();
  78.     }
  79.     /**
  80.      * here: set textfield colors to not-found colors.
  81.      */
  82.     @Override
  83.     protected void showNotFoundMessage() {
  84.         searchField.setForeground(notFoundForegroundColor);
  85.         searchField.setBackground(notFoundBackgroundColor);
  86.     }
  87.     /**
  88.      * here: set textfield colors to normal.
  89.      */
  90.     @Override
  91.     protected void showFoundMessage() {
  92.         searchField.setBackground(previousBackgroundColor);
  93.         searchField.setForeground(previousForegroundColor);
  94.     }
  95.     @Override
  96.     public void addNotify() {
  97.         super.addNotify();
  98.         if (previousBackgroundColor == null) {
  99.             previousBackgroundColor = searchField.getBackground();
  100.             previousForegroundColor = searchField.getForeground();
  101.         } else {
  102.             searchField.setBackground(previousBackgroundColor);
  103.             searchField.setForeground(previousForegroundColor);
  104.         }
  105.     }
  106.     // --------------------------- action call back
  107.     /**
  108.      * Action callback method for bound action JXDialog.CLOSE_ACTION_COMMAND.
  109.      * 
  110.      * Here: does nothing. Subclasses can override to define custom "closing"
  111.      * behaviour. Alternatively, any client can register a custom action with
  112.      * the actionMap.
  113.      * 
  114.      * 
  115.      */
  116.     public void cancel() {
  117.     }
  118.     // -------------------- init
  119.     @Override
  120.     protected void initExecutables() {
  121.         getActionMap().put(JXDialog.CLOSE_ACTION_COMMAND,
  122.                 createBoundAction(JXDialog.CLOSE_ACTION_COMMAND, "cancel"));
  123.         super.initExecutables();
  124.     }
  125.     @Override
  126.     protected void bind() {
  127.         super.bind();
  128.         searchField
  129.                 .addActionListener(getAction(JXDialog.EXECUTE_ACTION_COMMAND));
  130.         findNext.setAction(getAction(FIND_NEXT_ACTION_COMMAND));
  131.         findPrevious.setAction(getAction(FIND_PREVIOUS_ACTION_COMMAND));
  132.         KeyStroke stroke = KeyStroke.getKeyStroke("ESCAPE");
  133.         getInputMap(WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(stroke,
  134.                 JXDialog.CLOSE_ACTION_COMMAND);
  135.     }
  136.     @Override
  137.     protected void build() {
  138.         setLayout(new FlowLayout(SwingConstants.LEADING));
  139.         add(searchLabel);
  140.         add(new JLabel(":"));
  141.         add(new JLabel("  "));
  142.         add(searchField);
  143.         add(findNext);
  144.         add(findPrevious);
  145.     }
  146.     @Override
  147.     protected void initComponents() {
  148.         super.initComponents();
  149.         findNext = new JButton();
  150.         findPrevious = new JButton();
  151.     }
  152. }