jkfcombobox.java

来自「主要是对串口驱动的的一些控制源码!!! 在下载javacomm20-win32」· Java 代码 · 共 207 行

JAVA
207
字号
/*
 * $Log: JKFComboBox.java,v $
 * Revision 1.4  2003/02/01 17:37:23  willaxt
 * removed unnecessary instance variable for error handler
 *
 * Revision 1.3  2003/01/30 14:40:59  mwulff
 * no message
 *
 * Revision 1.2  2003/01/30 10:14:44  willaxt
 * added validation api
 *
 * Revision 1.1  2003/01/23 14:42:07  willaxt
 * initial version
 *
 */
package de.fhm.jkf.gui;

import java.awt.Color;
import java.awt.Component;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.ComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.border.Border;

/**
 * <br><br><center><table border="1" width="80%"><hr>
 * <strong><a href="http://jkf.sourceforge.net">The JKF Project</a></strong>
 * <p>
 * Copyright (C) 2002 by Theodor Willax
 * <p>
 * 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.
 * <p>
 * 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.
 * <p>
 * You should have received a copy of the <a href="http://www.gnu.org/copyleft/lesser.html">
 * GNU Lesser General Public License</a> along with this library; if not, write to
 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA  02111-1307  USA
 * <hr></table></center>
 * 
 * JKF implementation of a <code>JList</code>. Implements the
 * <code>DataExchangable</code> interface for automatic data exchange.

 * @author Theodor Willax
 * @version $Revision: 1.4 $
 */
public class JKFComboBox
	extends JComboBox
	implements DataExchangable, Validatable {
	/**
	 * Responsible for validating the made input.
	 * 
	 * @see de.fhm.jkf.gui.AbstractInputValidator
	 */
	private InputValidator inputValidator = null;

	/**
	 * Default border for this component.
	 */
	private Border basicBorder;

	/**
	 * Border if an input error is noticed in this component.
	 */
	private Border errorBorder;

	/**
	 * Creates a <code>JKFComboBox</code> that takes it's items from an existing
	 * <code>ComboBoxModel</code>.
	 */
	public JKFComboBox(ComboBoxModel aModel) {
		super(aModel);
		registerListener();
	}

	/**
	 * Creates a <code>JKFComboBox</code> that contains the elements in the
	 * specified array.
	 */
	public JKFComboBox(Object[] items) {
		super(items);
		registerListener();
	}

	/**
	 * Creates a <code>JKFComboBox</code> that contains
	 * the elements in the specified <code>Vector</code>.
	 */
	public JKFComboBox(Vector items) {
		super(items);
		registerListener();
	}

	/**
	 * Creates a <code>JKFComboBox</code> with a default data model.
	 */
	public JKFComboBox() {
		super();
		registerListener();
	}

	private void registerListener() {
		addFocusListener(new FocusLostListener());
		addKeyListener(new KeyEnterListener());
		basicBorder = this.getBorder();
		errorBorder =
			BorderFactory.createEtchedBorder(Color.BLUE, this.getBackground());
	}

	/**
	 * Set's the selected item to the given.
	 * @see de.fhm.jkf.gui.DataExchangable#setData(Object)
	 */
	public void setData(Object data) {
		setSelectedItem(data);
	}

	/**
	 * Returns the selected item.
	 * @see de.fhm.jkf.gui.DataExchangable#getData()
	 */
	public Object getData() {
		return getSelectedItem();
	}

	public Object getValidationInformation() {
		return getData();
	}

	public void markError() {
		setBorder(errorBorder);
		requestFocus();
	}

	public void unmarkError() {
		setBorder(basicBorder);
	}

	/**
	 * Set the user defined input validator for this component.
	 * 
	 * @see de.fhm.jkf.gui.AbstractInputValidator
	 */
	public void setInputValidator(InputValidator validator) {
		this.inputValidator = validator;
	}

	/**
	 * Disables the input validation.
	 */
	public void removeInputValidator() {
		inputValidator = null;
	}

	
	public Component getValidatableComponent() {
		return this;
	}


	/*
	 * ************************* Inner classes ******************
	 */
	private class FocusLostListener extends FocusAdapter {

		public void focusLost(FocusEvent e) {
			if (inputValidator != null) {
				ErrorHandler eh = ErrorList.instance();
				InputValidatorItem i =
					inputValidator.validateInput(JKFComboBox.this);
				eh.setValidationResult(i);
			}
		}
	}

	private class KeyEnterListener extends KeyAdapter {

		public KeyEnterListener() {
			super();
		}
		public void keyPressed(KeyEvent e) {
			if (e.getKeyCode() == KeyEvent.VK_ENTER) {
				if (inputValidator != null) {
					ErrorHandler eh = ErrorList.instance();
					InputValidatorItem i =
						inputValidator.validateInput(JKFComboBox.this);
					eh.setValidationResult(i);
				}
			}

		}
	}

}

⌨️ 快捷键说明

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