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

org.fife.rsta.ui.search.SearchComboBox Maven / Gradle / Ivy

Go to download

RSTAUI is an add-on library for RSyntaxTextArea that provides pre-build dialog boxes commonly needed in text editing applications.

There is a newer version: 3.3.1
Show newest version
/*
 * 09/20/2013
 *
 * SearchComboBox - The combo box used for "find" and "replace" dropdowns.
 *
 * This library is distributed under a modified BSD license.  See the included
 * RSyntaxTextArea.License.txt file for details.
 */
package org.fife.rsta.ui.search;

import java.util.Vector;

import javax.swing.InputMap;
import javax.swing.KeyStroke;
import javax.swing.text.JTextComponent;

import org.fife.rsta.ui.UIUtil;


/**
 * The combo box used for entering text to "find" and "replace" in both the
 * Find/Replace dialogs as well as tool bars.
 *
 * @author Robert Futrell
 * @version 1.0
 */
// NOTE: This class is public to facilitate applications creating other
// subclasses, such as a FindInFilesDialog.
public class SearchComboBox extends RegexAwareComboBox {

	private FindToolBar toolBar;


	/**
	 * Constructor.
	 *
	 * @param toolBar The tool bar that owns this combo box, or {@code null}
	 *        if it is not in a tool bar.
	 * @param replace Whether this combo box is for "replace" text (as opposed
	 *        to "find" text).
	 */
	public SearchComboBox(FindToolBar toolBar, boolean replace) {
		super(replace);
		this.toolBar = toolBar;
		UIUtil.fixComboOrientation(this);
		updateTextFieldKeyMap();
	}


	/**
	 * Overridden to always select the newly-added item.  If the item is
	 * already in the list of choices, it is moved to the top before being
	 * selected.
	 *
	 * @param item The item to add.
	 */
	@Override
	public void addItem(Object item) {

		// If they just searched for an item that's already in the list
		// other than the first, move it to the first position.
		int curIndex = getIndexOf(item);
		if (curIndex==-1) {
			super.addItem(item);
		}
		else if (curIndex>0) {
			removeItem(item);
			insertItemAt(item, 0);
		}

		// Always leave with the new item selected
		setSelectedIndex(0);
	}


	private int getIndexOf(Object item) {
		for (int i=0; iStrings contained in this combo box.
	 *
	 * @return A java.util.Vector of strings found in this
	 *         combo box.  If that combo box is empty, than a zero-length
	 *         Vector is returned.
	 */
	public Vector getSearchStrings() {

		// First, ensure that the item in the editor component is indeed in the
		// combo box.
		int selectedIndex = getSelectedIndex();
		if (selectedIndex==-1) {
			addItem(getSelectedString());
		}

		// If they just searched for an item that's already in the list other
		// than the first, move it to the first position.
		else if (selectedIndex>0) {
			Object item = getSelectedItem();
			removeItem(item);
			insertItemAt(item, 0);
			setSelectedIndex(0);
		}

		int itemCount = getItemCount();
		Vector vector = new Vector(itemCount);
		for (int i=0; i




© 2015 - 2025 Weber Informatics LLC | Privacy Policy