jkfview.java

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

JAVA
194
字号
package de.fhm.jkf.gui;

import java.awt.LayoutManager;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import javax.swing.JPanel;

import de.fhm.jkf.resource.cl.JKFClientLogger;
import de.fhm.jkf.utils.clsv.ReflectionUtils;

/**
 * <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>
 * 
 * @author		marten wulff
 * @version 	1.0
 * 
 * JKF version for a panel. Implements support for simple data exchange.
 * Simple means data exchange is driven by transfer objects. It's specified
 * in the data exchange contract for the JKF framework.
 */
public abstract class JKFView extends JPanel {
	/**
	 * Constructor for JKFView.
	 * 
	 * @param layout
	 * @param isDoubleBuffered
	 */
	public JKFView(LayoutManager layout, boolean isDoubleBuffered) {

		super(layout, isDoubleBuffered);

	}

	/**
	 * Constructor for JKFView.
	 * 
	 * @param layout
	 */
	public JKFView(LayoutManager layout) {
		super(layout);
	}

	/**
	 * Constructor for JKFView.
	 * 
	 * @param isDoubleBuffered
	 */
	public JKFView(boolean isDoubleBuffered) {
		super(isDoubleBuffered);
	}

	/**
	 * Constructor for JKFView.
	 */
	public JKFView() {
		super();
	}

	/**
	 * Method for dynamically updating the declared fields with the
	 * values given by this TransferObject.
	 * 
	 * @param to transfer object with values to set.
	 */
	public void setTransferObject(Object to)
		throws InvocationTargetException, IllegalAccessException {			

		// get all the getter methods of the transfer object				
		Method[] methods = ReflectionUtils.getGetters(to.getClass());

		// now iterate over all these methods and use the method name to 
		// find a corresponding field in the sub class
		String methodName = null;
		String fieldName = null;
		Field subClassField = null;
		Object fieldObject = null;
		Object value = null;

		for (int i = 0; i < methods.length; i++) {
			methodName = methods[i].getName();

			// build the corresponding filed name
			// remove the get from every method name
			fieldName = methodName.substring(3, methodName.length());


			// convert the first character to a lower character
			fieldName =
				fieldName.substring(0, 1).toLowerCase()
					+ fieldName.substring(1, fieldName.length());

			try {
				subClassField = this.getClass().getField(fieldName);
			} catch (NoSuchFieldException e) {
				if (JKFClientLogger.isDebugEnabled()) {
					JKFClientLogger.debug(
						JKFView.class.getName()
							+ " - "
							+ e.getMessage());
				}				
				
				continue;
			}

			// if we've found a corresponding field, invoke an update 
			// on this field if it's an DataExchangable
			fieldObject = subClassField.get(this);

			if (fieldObject instanceof DataExchangable) {
				value = methods[i].invoke(to, null);
				((DataExchangable) fieldObject).setData(value);
			}
		}
	}

	/**
	 * Sets the fields of the transfer object with the values
	 * from the gui components with the corresponding name.
	 * See data exchange contract for information about data exchange.
	 * 
	 * @param to return parameter filled with the values of
	 * the gui components.
	 */
	public void getTransferObject(Object to)
		throws InvocationTargetException, IllegalAccessException {

		// get all the getter methods of the transfer object				
		Method[] methods = ReflectionUtils.getSetters(to.getClass());

		// now iterate over all these methods and use the method name to 
		// find a corresponding field in the sub class
		String methodName = null;
		String fieldName = null;
		Field subClassField = null;
		Object fieldObject = null;
		Object[] signature = new Object[1];

		for (int i = 0; i < methods.length; i++) {
			methodName = methods[i].getName();

			// build the corresponding field name
			// remove the get from every method name
			fieldName = methodName.substring(3, methodName.length());

			// convert the first character to a lower character
			fieldName =
				fieldName.substring(0, 1).toLowerCase()
					+ fieldName.substring(1, fieldName.length());

			try {
				subClassField = this.getClass().getField(fieldName);
			} catch (NoSuchFieldException e) {
				if (JKFClientLogger.isDebugEnabled()) {
					JKFClientLogger.debug(
						JKFView.class.getName()
							+ " - "
							+ e.getMessage());
				}
				continue;
			}

			// if we've found a corresponding field, invoke an update 
			// on this field if it's an DataExchangable
			fieldObject = subClassField.get(this);

			if (fieldObject instanceof DataExchangable) {
				signature[0] = ((DataExchangable) fieldObject).getData();
				methods[i].invoke(to, signature);
			}
		}
	}		
}

⌨️ 快捷键说明

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