⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uidatabinder.java

📁 《j2ee开发全程实录》随书源码
💻 JAVA
字号:
package com.cownew.PIS.ui.commonUI.databind;

import java.awt.Component;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JSpinner;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.enums.EnumUtils;

import com.cownew.PIS.framework.common.IValueObject;
import com.cownew.PIS.ui.ctrl.prompt.PopupDatePicker;
import com.cownew.PIS.ui.ctrl.prompt.OVPicker.PopupValueObjectPicker;
import com.cownew.ctk.common.ExceptionUtils;
import com.cownew.ctk.common.StringEnum;
import com.cownew.ctk.ui.swing.JNumberTextField;
import com.cownew.ctk.ui.swing.SwingUtils;

public class UIDataBinder
{
	private IValueObject valueObject;

	private List bindList;

	private List enumBindList;

	public UIDataBinder()
	{
		super();
		bindList = new ArrayList();
		enumBindList = new ArrayList();
	}

	public IValueObject getValueObject()
	{
		return valueObject;
	}

	public void setValueObject(IValueObject valueObject)
	{
		this.valueObject = valueObject;
	}

	public void storeToVO()
	{
		try
		{
			for (int i = 0, n = bindList.size(); i < n; i++)
			{
				BindInfo bindInfo = (BindInfo) bindList.get(i);
				Object value = PropertyUtils.getProperty(bindInfo
						.getComponent(), bindInfo.getPropertyName());

				PropertyUtils.setProperty(valueObject, bindInfo.getFieldName(),
						value);

			}

			// 加载枚举属性
			for (int i = 0, n = enumBindList.size(); i < n; i++)
			{
				EnumBindInfo enumBindInfo = (EnumBindInfo) enumBindList.get(i);
				StringEnum enumValue = (StringEnum) enumBindInfo.getComboBox()
						.getSelectedItem();

				// 这里规定绑定枚举的属性必须是String类型
				String propValue = null;
				if (enumValue != null)
				{
					propValue = enumValue.getName();
				}
				PropertyUtils.setProperty(valueObject, enumBindInfo
						.getFieldName(), propValue);
			}
		} catch (IllegalAccessException e)
		{
			throw ExceptionUtils.toRuntimeException(e);
		} catch (InvocationTargetException e)
		{
			throw ExceptionUtils.toRuntimeException(e);
		} catch (NoSuchMethodException e)
		{
			throw ExceptionUtils.toRuntimeException(e);
		}
	}

	public void loadToUI()
	{
		try
		{
			for (int i = 0, n = bindList.size(); i < n; i++)
			{
				BindInfo bindInfo = (BindInfo) bindList.get(i);
				Object value = PropertyUtils.getProperty(valueObject, bindInfo
						.getFieldName());
				PropertyUtils.setProperty(bindInfo.getComponent(), bindInfo
						.getPropertyName(), value);
			}

			// 加载枚举属性
			for (int i = 0, n = enumBindList.size(); i < n; i++)
			{
				EnumBindInfo enumBindInfo = (EnumBindInfo) enumBindList.get(i);
				String value = (String) PropertyUtils.getProperty(valueObject,
						enumBindInfo.getFieldName());
				StringEnum enumValue = (StringEnum) EnumUtils.getEnum(
						enumBindInfo.getEnumClass(), value);
				SwingUtils.setSelectedItem(enumBindInfo.getComboBox(),
						enumValue);
			}
		} catch (IllegalAccessException e)
		{
			throw ExceptionUtils.toRuntimeException(e);
		} catch (InvocationTargetException e)
		{
			throw ExceptionUtils.toRuntimeException(e);
		} catch (NoSuchMethodException e)
		{
			throw ExceptionUtils.toRuntimeException(e);
		}

	}

	public void registerBind(Component component, String propertyName,
			String fieldName)
	{
		BindInfo info = new BindInfo(component, propertyName, fieldName);
		bindList.add(info);
	}

	/**
	 * 与控件的默认属性绑定
	 * 
	 * @param component
	 * @param fieldName
	 */
	public void registerBind(Component component, String fieldName)
	{
		BindInfo info = new BindInfo(component, getDefaultProperty(component),
				fieldName);
		bindList.add(info);
	}

	/**
	 * 绑定comboBox,绑定枚举的属性必须是String类型
	 * 
	 * @param combo
	 * @param enumClass
	 * @param fieldName
	 */
	public void registerEnumBind(JComboBox combo, Class enumClass,
			String fieldName)
	{
		EnumBindInfo enumBindInfo = new EnumBindInfo(combo, enumClass,
				fieldName);
		enumBindList.add(enumBindInfo);
		SwingUtils.fillWithEnum(combo, enumClass);
	}

	/**
	 * 得到控件的默认属性
	 * 
	 * @param component
	 * @return
	 */
	private static String getDefaultProperty(Component component)
	{
		// 因为JNumberTextField是JTextField的子类,所以JNumberTextField要放在前面
		if (component instanceof JNumberTextField)
		{
			return "bigDecimal";
		} else if (component instanceof JTextField)
		{
			return "text";
		} else if (component instanceof JLabel)
		{
			return "text";
		} else if (component instanceof JSpinner)
		{
			return "value";
		} else if (component instanceof PopupValueObjectPicker)
		{
			return "valueObject";
		} else if (component instanceof PopupDatePicker)
		{
			return "SQLDate";
		} else if (component instanceof JTextArea)
		{
			return "text";
		} else if (component instanceof JCheckBox)
		{
			return "selected";
		} else
		{
			throw new IllegalArgumentException("控件" + component.getClass()
					+ "没有默认属性,必须明确指定!");
		}
	}

}

/**
 * 枚举绑定属性类
 */
class EnumBindInfo
{
	private Class enumClass;

	private JComboBox comboBox;

	private String fieldName;

	/**
	 * 
	 * @param comboBox
	 *            控件
	 * @param enumClass
	 *            枚举类名
	 * @param fieldName
	 *            要绑定的属性名
	 */
	public EnumBindInfo(JComboBox comboBox, Class enumClass, String fieldName)
	{
		super();
		this.enumClass = enumClass;
		this.comboBox = comboBox;
		this.fieldName = fieldName;
	}

	public JComboBox getComboBox()
	{
		return comboBox;
	}

	public Class getEnumClass()
	{
		return enumClass;
	}

	public String getFieldName()
	{
		return fieldName;
	}

}

class BindInfo
{
	private Component component;

	private String propertyName;

	private String fieldName;

	/**
	 * 
	 * @param component
	 *            控件
	 * @param propertyName
	 *            控件的属性名
	 * @param fieldName
	 *            值对象的属性
	 */
	public BindInfo(Component component, String propertyName, String fieldName)
	{
		super();
		this.component = component;
		this.propertyName = propertyName;
		this.fieldName = fieldName;
	}

	public Component getComponent()
	{
		return component;
	}

	/**
	 * 值对象的属性
	 * 
	 * @return
	 */
	public String getFieldName()
	{
		return fieldName;
	}

	/**
	 * 控件的属性名
	 * 
	 * @return
	 */
	public String getPropertyName()
	{
		return propertyName;
	}

}

⌨️ 快捷键说明

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