⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rcomboboxmodel.java

📁 具有不同语法高亮的编辑器实例
💻 JAVA
字号:
/*
 * 01/15/2004
 *
 * RComboBoxModel.java - A combo box model that limits the number of items
 * the combo box wil remember.  It also won't add an item to the combo box
 * if it is already there.
 * Copyright (C) 2004 Robert Futrell
 * email@address.com
 * www.website.com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package org.fife.ui;

import java.io.Serializable;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;


/**
 * A combo box model that limits the number of items the combo box will
 * "remember."  You can use it like so:
 * <br>
 * <code>RComboBoxModel model = new RComboBoxModel();</code><br>
 * <code>model.setMaxNumElements(10);</code><br>
 * <code>JComboBox comboBox = new JComboBox(model);</code><br><br>
 * It also won't let you add an item to the combo box twice (i.e., no
 * duplicates), and it adds new items to the beginning of the list, not
 * the end (as <code>JComboBox</code>'s do by default).<br><br>
 * It defaults to 10 elements remembered.
 *
 * @author Robert Futrell
 * @version 0.8
 */
public class RComboBoxModel extends DefaultComboBoxModel
										implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7656706291506122984L;

	/**
	 * The number of items the combo box will remember.
	 */
	private int maxNumElements;


/*****************************************************************************/


	/**
	 * Creates a new combo box model with a maximum element count of
	 * <code>8</code>.
	 */
	public RComboBoxModel() {
		super();
		setMaxNumElements(8);
	}


/*****************************************************************************/


	/**
	 * Creates a new combo box model with a maximum element count of
	 * <code>8</code>.
	 *
	 * @param items The initial items to use to populate the combo box.
	 */
	public RComboBoxModel(Object[] items) {
		super(items);
		setMaxNumElements(8);
	}


/*****************************************************************************/


	/**
	 * Creates a new combo box model with a maximum element count of
	 * <code>8</code>.
	 *
	 * @param v The initial items to use to populate the combo box.
	 */
	public RComboBoxModel(Vector v) {
		super(v);
		setMaxNumElements(8);
	}


/*****************************************************************************/


	/**
	 * Adds the object (if it's not already in the list) to the front of
	 * the list.  If it's already in the list, move it to the top.
	 *
	 * @param anObject The object to add.
	 */
	public void addElement(Object anObject) {

		// If the object is already in the list, ensure that it is at
		// the top of the list.
		int index = getIndexOf(anObject);
		if (index>0) {
			removeElementAt(index);
			super.insertElementAt(anObject, 0);
		}

		// If the item isn't in the list yet, add it to the top.
		else if (index<0) {
			super.insertElementAt(anObject, 0);
			ensureValidItemCount();
		}

	}


/*****************************************************************************/


	/**
	 * Ensures the number if items remembered by this combo box is valid.
	 */
	private void ensureValidItemCount() {
		while (getSize()>maxNumElements)
			removeElementAt(getSize()-1);
	}


/*****************************************************************************/


	/**
	 * Returns the maximum number of items this combo box can hold.
	 *
	 * @return The maximum number of items this combo box can hold.
	 */
	public int getMaxNumElements() {
		return maxNumElements;
	}


/*****************************************************************************/


	/**
	 * Adds an item at a specified index.  The implementation of this method
	 * should notify all registered <code>ListDataListeners</code> that the
	 * item has been added.
	 *
	 * @param anObject The <code>Object</code> to be added.
	 * @param index Location to add the object.
	 */
	public void insertElementAt(Object anObject, int index) {

		// Don't add the object if it's already in the list.
		if (getIndexOf(anObject)>-1)
			return;

		super.insertElementAt(anObject, index);
		ensureValidItemCount();

	}


/*****************************************************************************/


	/**
	 * Sets the maximum number of items this combo box can hold.
	 *
	 * @param numElements The maximum number of items this combo box can hold.
	 *        If <code>numElements <= 0</code>, then the capacity
	 *        of this combo box is set to <code>4</code>.
	 */
	public void setMaxNumElements(int numElements) {
		maxNumElements = numElements<=0 ? 4 : numElements;
		ensureValidItemCount();
	}


/*****************************************************************************/

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -