jkftextfield.java

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

JAVA
234
字号
/**
 * $Log: JKFTextField.java,v $
 * Revision 1.5  2003/02/04 21:46:48  willaxt
 * setData() calls internally now data.toString() and doesn't cast to (String) to bypass ClassCastExceptions if data is not a String objekt
 *
 * Revision 1.4  2003/01/30 14:47:49  mwulff
 * no message
 *
 * Revision 1.3  2003/01/30 11:24:53  willaxt
 * ErrorHandler is not an instance variable any more (not needed)
 *
 * Revision 1.2  2003/01/30 10:17:24  willaxt
 * *** empty log message ***
 *
 * Revision 1.1  2003/01/16 13:10:54  mwulff
 * initial version
 *
 * Revision 1.11  2003/01/11 10:30:19  willaxt
 * fixed typo
 *
 * Revision 1.9  2003/01/03 15:33:24  mwulff
 * no message
 *
 * Revision 1.8  2003/01/02 18:47:39  mwulff
 * no message
 *
 * Revision 1.7  2003/01/02 11:59:28  mwulff
 * no message
 *
 * Revision 1.6  2002/12/23 13:04:11  mwulff
 * no message
 *
 * Revision 1.5  2002/12/20 18:50:53  mwulff
 * no message
 *
 * Revision 1.4  2002/12/15 16:53:18  mwulff
 * no message
 *
 * Revision 1.3  2002/12/13 20:06:57  mwulff
 * no message
 *
 * Revision 1.2  2002/12/08 17:09:37  mwulff
 * no message
 *
 * Revision 1.1  2002/12/08 11:23:38  mwulff
 * 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.JTextField;
import javax.swing.border.Border;
import javax.swing.text.Document;

/**
 * @author	marten wulff
 * 
 * * <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 Marten Wulff
 * <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>
 * @stereotype implementationClass
 */
public class JKFTextField extends JTextField implements DataExchangable, Validatable {

	private InputValidator inputValidator = null;

	private Border basicBorder;
	private Border errorBorder;
	
	
	/* (non-Javadoc)
	 * @see java.lang.Object#Object()
	 */
	public JKFTextField() {
		this(null, null, 0);
	}

	
	public JKFTextField(String text) {
		this(null, text, 0);
	}

	
	/* (non-Javadoc)
	 * @see javax.swing.JTextField#JTextField(int)
	 */
	public JKFTextField(int columns) {
		this(null, null, columns);
	}

	
	/* (non-Javadoc)
	 * @see javax.swing.JTextField#JTextField(String, int)
	 */
	public JKFTextField(String text, int columns) {
		this(null, text, columns);
	}

	/* (non-Javadoc)
	 * @see javax.swing.JTextField#JTextField(Document, String, int)
	 */
	public JKFTextField(Document doc, String text, int columns) {
		super(doc, text, columns);
		
		this.addFocusListener(new FocusLostListener());
		this.addKeyListener(new KeyEnterListener());
		basicBorder = this.getBorder();		
		errorBorder = BorderFactory.createEtchedBorder(Color.BLUE, this.getBackground());
	}

	
	/* Interface methods: Validatable */
	public void markError() {
		this.setBorder(errorBorder);
		this.requestFocus();
		this.selectAll();
	}
	
	public void unmarkError() {	
		this.setBorder(basicBorder);
		this.select(0, 0);
	}
	
	public Object getValidationInformation() {
		return this.getText();
	}
	
	public void setInputValidator(InputValidator validator) {
		this.inputValidator = validator;
	}

	public void removeInputValidator() {
		inputValidator = null;
	}
			
	
	/**
	 * Returns the errorBorder.
	 * @return Border
	 */
	public Border getErrorBorder() {
		return errorBorder;
	}

	/**
	 * Sets the errorBorder.
	 * @param errorBorder The errorBorder to set
	 */
	public void setErrorBorder(Border errorBorder) {
		this.errorBorder = errorBorder;
	}

	
	/**
	 * Returns data represented by this component.
	 * 
	 * @return Object data of this component.
	 */
	public Object getData() {
		return getText();
	}
	
	/**
	 * Sets data to this component.
	 * 
	 * @param data the data to set.
	 */
	public void setData(Object data) {
		setText(data.toString());
	}

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

⌨️ 快捷键说明

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