【Java开源代码栏目提醒】:网学会员在Java开源代码频道为大家收集整理了UIDataBinder.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的子类,所以JNumberTex