jkfcheckbox.java

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

JAVA
245
字号
/*
 * $Log: JKFCheckBox.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:13:58  willaxt
 * added validation api
 *
 * Revision 1.1  2003/01/23 15:02:22  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 javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.JCheckBox;
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>JCheckBox</code>. Implements the
 * <code>DataExchangable</code> interface for automatic data exchange.
 * 
 * @author Theodor Willax
 * @version $Revision: 1.4 $
 */
public class JKFCheckBox
	extends JCheckBox
	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 an initially unselected check box button with no text, no icon.
	 */
	public JKFCheckBox() {
		super();
		registerListener();
	}

	/**
	 * Creates an initially unselected check box with an icon.
	 */
	public JKFCheckBox(Icon icon) {
		super(icon);
		registerListener();
	}

	/**
	 * Creates a check box with an icon and specifies whether or not it is
	 * initially selected.
	 */
	public JKFCheckBox(Icon icon, boolean selected) {
		super(icon, selected);
		registerListener();
	}

	/**
	 * Creates an initially unselected check box with text.
	 */
	public JKFCheckBox(String text) {
		super(text);
		registerListener();
	}

	/**
	 * Creates a check box where properties are taken from the Action supplied.
	 */
	public JKFCheckBox(Action a) {
		super(a);
		registerListener();
	}

	/**
	 * Creates a check box with text and specifies whether or not it is
	 * initially selected.
	 */
	public JKFCheckBox(String text, boolean selected) {
		super(text, selected);
		registerListener();
	}

	/**
	 * Creates an initially unselected check box with the specified text
	 * and icon.
	 */
	public JKFCheckBox(String text, Icon icon) {
		super(text, icon);
		registerListener();
	}

	/**
	 * Creates a check box with text and icon, and specifies whether
	 * or not it is initially selected.
	 */
	public JKFCheckBox(String text, Icon icon, boolean selected) {
		super(text, icon, selected);
		registerListener();
	}

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

	/**
	 * Set's the check box selection state to the given value.
	 * Data is casted to <code>Boolean</code>.
	 * 
	 * @see de.fhm.jkf.gui.DataExchangable#setData(Object)
	 */
	public void setData(Object data) {
		setSelected(((Boolean) data).booleanValue());
	}

	/**
	 * Returns the selection state of this checkbox. Data is
	 * wrapped in an <code>Boolean</code> object.
	 * 
	 * @see de.fhm.jkf.gui.DataExchangable#getData()
	 */
	public Object getData() {
		if (isSelected())
			return Boolean.TRUE;
		else
			return Boolean.FALSE;
	}

	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(JKFCheckBox.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(JKFCheckBox.this);
					eh.setValidationResult(i);
				}
			}

		}
	}
}

⌨️ 快捷键说明

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