jkfslider.java

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

JAVA
233
字号
/*
 * $Log: JKFSlider.java,v $
 * Revision 1.4  2003/03/08 12:51:24  mwulff
 * removed reference for ErrorHandler
 *
 * Revision 1.3  2003/01/30 14:42:41  mwulff
 * no message
 *
 * Revision 1.2  2003/01/30 10:14:54  willaxt
 * added validation api
 *
 * Revision 1.1  2003/01/19 13:05:17  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.BorderFactory;
import javax.swing.BoundedRangeModel;
import javax.swing.JSlider;
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>JSlider</code>.
 * 
 * @author Theodor Willax
 * @version $Revision: 1.4 $
 */
public class JKFSlider
	extends JSlider
	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 horizontal slider with the range 0 to 100 and
	 * an initial value of 50.
	 */
	public JKFSlider() {
		super();
		registerListener();
	}

	/**
	 * Creates a <code>JKFSlider</code> using the specified orientation
	 * with the range 0 to 100 and an initial value of 50.
	 * 
	 * @param orientation
	 */
	public JKFSlider(int orientation) {
		super(orientation);
		registerListener();
	}

	/**
	 * Creates a <code>JKFSlider</code> using the specified min
	 * and max.
	 * 
	 * @param min
	 * @param max
	 */
	public JKFSlider(int min, int max) {
		super(min, max);
		registerListener();
	}

	/**
	 * Creates a horizontal slider using the specified min, max and value.
	 */
	public JKFSlider(int min, int max, int value) {
		super(min, max, value);
		registerListener();
	}

	/**
	 * Constructor for JKFSlider.
	 * 
	 * @param orientation
	 * @param min
	 * @param max
	 * @param value
	 * @throws IllegalArgumentException if orientation is not one of VERTICAL, HORIZONTAL
	 */
	public JKFSlider(int orientation, int min, int max, int value) {
		super(orientation, min, max, value);
		registerListener();
	}

	/**
	 * Creates a horizontal slider using the specified BoundedRangeModel.
	 */
	public JKFSlider(BoundedRangeModel brm) {
		super(brm);
	}

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

	/**
	 * Set's the value for this slider. The data is casted to an
	 * <code>Integer</code>.
	 * 
	 * @param data The new value for this slider.
	 * @see de.fhm.jkf.gui.DataExchangable#setData(Object)
	 */
	public void setData(Object data) {
		super.setValue(((Integer) data).intValue());
	}

	/**
	 * Returns the sliders value. It's wrapped in an <code>Integer</code>.
	 * 
	 * @return Object The value of this slider
	 * @see de.fhm.jkf.gui.DataExchangable#getData()
	 */
	public Object getData() {
		return new Integer(super.getValue());
	}

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

		}
	}

}

⌨️ 快捷键说明

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