All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jdesktop.swingx.JXSearchPanel Maven / Gradle / Ivy

Go to download

Contains extensions to the Swing GUI toolkit, including new and enhanced components that provide functionality commonly required by rich client applications.

There is a newer version: 1.6.1
Show newest version
/*
 * $Id: JXSearchPanel.java,v 1.13 2007/05/16 09:28:03 kleopatra Exp $
 *
 * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
 * Santa Clara, California 95054, U.S.A. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

package org.jdesktop.swingx;

import org.jdesktop.swingx.decorator.PatternFilter;
import org.jdesktop.swingx.decorator.PatternHighlighter;
import org.jdesktop.swingx.decorator.PatternMatcher;

import javax.swing.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Pattern;

/**
 * Rudimentary search panel.
 * 
 * Updates PatternMatchers from user input.
 * 
 * Supports 
 * 
 * 
    *
  1. text input to match *
  2. match rules like contains/equals/... *
  3. toggle case sensitive match *
* * TODO: allow custom PatternModel and/or access * to configuration of bound PatternModel. * * TODO: fully support control of multiple PatternMatchers. * * @author Ramesh Gupta * @author Jeanette Winzenburg * */ public class JXSearchPanel extends AbstractPatternPanel { public static final String MATCH_RULE_ACTION_COMMAND = "selectMatchRule"; private JComboBox searchCriteria; private List patternMatchers; public JXSearchPanel() { initComponents(); build(); initActions(); bind(); getPatternModel().setIncremental(true); } //----------------- accessing public properties public void addPatternMatcher(PatternMatcher matcher) { getPatternMatchers().add(matcher); updateFieldName(matcher); } /** * sets the PatternFilter control. * * PENDING: change to do a addPatternMatcher to enable multiple control. * */ public void setPatternFilter(PatternFilter filter) { getPatternMatchers().add(filter); updateFieldName(filter); } /** * sets the PatternHighlighter control. * * PENDING: change to do a addPatternMatcher to enable multiple control. * */ public void setPatternHighlighter(PatternHighlighter highlighter) { getPatternMatchers().add(highlighter); updateFieldName(highlighter); } /** * set the label of the search combo. * * @param name */ public void setFieldName(String name) { searchLabel.setText(name); } /** * returns the label of the search combo. * */ public String getFieldName() { return searchLabel.getText(); } /** * returns the current compiled Pattern. * * @return the current compiled Pattern */ public Pattern getPattern() { return patternModel.getPattern(); } /** * @param matcher */ protected void updateFieldName(PatternMatcher matcher) { if (matcher instanceof PatternFilter) { PatternFilter filter = (PatternFilter) matcher; searchLabel.setText(filter.getColumnName()); } else { if (searchLabel.getText().length() == 0) { // ugly hack searchLabel.setText("Field"); /** TODO: Remove this hack!!! */ } } } // ---------------- action callbacks /** * */ public void match() { for (Iterator iter = getPatternMatchers().iterator(); iter.hasNext();) { iter.next().setPattern(getPattern()); } } /** * set's the PatternModel's MatchRule to the selected in combo. * * NOTE: this * is public as an implementation side-effect! * No need to ever call directly. */ public void updateMatchRule() { getPatternModel().setMatchRule( (String) searchCriteria.getSelectedItem()); } private List getPatternMatchers() { if (patternMatchers == null) { patternMatchers = new ArrayList(); } return patternMatchers; } //---------------- init actions and model @Override protected void initExecutables() { super.initExecutables(); getActionMap().put(MATCH_RULE_ACTION_COMMAND, createBoundAction(MATCH_RULE_ACTION_COMMAND, "updateMatchRule")); } //--------------------- binding support /** * bind the components to the patternModel/actions. */ @Override protected void bind() { super.bind(); List matchRules = getPatternModel().getMatchRules(); // PENDING: map rules to localized strings ComboBoxModel model = new DefaultComboBoxModel(matchRules.toArray()); model.setSelectedItem(getPatternModel().getMatchRule()); searchCriteria.setModel(model); searchCriteria.setAction(getAction(MATCH_RULE_ACTION_COMMAND)); } //------------------------ init ui /** * build container by adding all components. * PRE: all components created. */ private void build() { add(searchLabel); add(searchCriteria); add(searchField); add(matchCheck); } /** * create contained components. * * */ @Override protected void initComponents() { super.initComponents(); searchCriteria = new JComboBox(); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy