注解下的FormBean自动生成Panel视图(暂不支持数据绑定)
2011-11-02 10:37
429 查看
panel工厂
import java.lang.reflect.Method; import java.text.*; import java.util.*; import java.util.Map.Entry; import javax.swing.*; import org.apache.log4j.Logger; import com.jgoodies.forms.builder.DefaultFormBuilder; import com.jgoodies.forms.layout.FormLayout; import com.***.AlarmColor; import com.***.annotation.*; @SuppressWarnings("all") public class FormPanelFactory { private static final Logger log = Logger.getLogger(FormPanelFactory.class); public static final DateFormat dFormat = new SimpleDateFormat( "yyyy-MM-dd hh:mm:ss"); public static final NumberFormat iFormat = NumberFormat .getIntegerInstance(); public static final NumberFormat lFormat = NumberFormat.getNumberInstance(); public static JComponent buildPanel(Object obj) { FormLayout layout = new FormLayout("left:pref, 10px,200px:grow"); DefaultFormBuilder builder = new DefaultFormBuilder(layout); builder.setDefaultDialogBorder(); Class clazz = obj.getClass(); Method[] methods = clazz.getMethods(); MaxGroupProperty maxGroupProperty = (MaxGroupProperty) clazz .getAnnotation(MaxGroupProperty.class); // 组名验证 int groupCount = maxGroupProperty == null ? 1 : maxGroupProperty .maxGroup(); String[] groupTitles = new String[groupCount]; if (maxGroupProperty == null) { groupTitles = new String[] { "" }; } else { String title = maxGroupProperty.groupTitles(); String titles[] = title.split(";"); System.arraycopy(titles, 0, groupTitles, 0, titles.length); } // 属性分组 Map groupMap = new HashMap(groupCount); for (int i = 0; i < groupCount; i++) { List showProperties = new ArrayList(); groupMap.put(i, showProperties); } for (int i = 0, size = methods.length; i < size; i++) { Method method = methods[i]; GroupProperty gp = method.getAnnotation(GroupProperty.class); ShowProperty sp = method.getAnnotation(ShowProperty.class); if (sp != null && gp != null) { int groupLocation; if (groupCount == 1) groupLocation = 0; else groupLocation = gp.group(); List showProperties = (List) groupMap.get(groupLocation); GroupShowProperty indexShowProperty = new GroupShowProperty( obj, sp, gp, method); showProperties.add(indexShowProperty); } } List[] groupArr = new ArrayList[groupCount]; for (Iterator it = groupMap.entrySet().iterator(); it.hasNext();) { Entry entry = (Entry) it.next(); groupArr[(Integer) entry.getKey()] = (List) entry.getValue(); } // 生成属性 组件 int size = groupCount - 1; for (int i = 0; i < size; i++) { List showProperties = groupArr[i]; Collections.sort(showProperties); builder.appendSeparator(groupTitles[i]); for (Object object : showProperties) { GroupShowProperty property = (GroupShowProperty) object; builder.append(new JLabel(property.showProperty.title()), buildCompoent(property)); } builder.nextLine(); } builder.appendSeparator(groupTitles[size]); for (Object object : groupArr[size]) { GroupShowProperty property = (GroupShowProperty) object; builder.append(new JLabel(property.showProperty.title()), buildCompoent(property)); } return builder.getPanel(); } private static JComponent buildCompoent(GroupShowProperty property) { try { ShowProperty sp = property.showProperty; Object obj = property.obj; Method method = property.method; switch (sp.type()) { case TEXTFIELD: String sValue = (String) method.invoke(obj, null); JTextField tret = new JTextField(sValue); tret.setEditable(false); return tret; case DATEFIELD: Date dValue = (Date) method.invoke(obj, null); JTextField dret = new JTextField(dFormat.format(dValue)); dret.setEditable(false); return dret; case CHECKBOX: boolean bValue = (Boolean) method.invoke(obj, null); JCheckBox cret = new JCheckBox("", bValue); cret.setEnabled(false); return cret; case LONGFIELD: Long lValue = (Long) method.invoke(obj, null); JFormattedTextField jtf = new JFormattedTextField(); jtf.setText(lFormat.format(lValue)); jtf.setEditable(false); return jtf; case BYTE2CHECKBOX: Object object=method.invoke(obj, null); int bcValue=((Byte)object).intValue(); JCheckBox bcret = new JCheckBox("", bcValue!=0); bcret.setEnabled(false); return bcret; case BYTE2INTFIELD: Object object2=method.invoke(obj, null); int btValue = ((Byte)object2).intValue(); JFormattedTextField btjtf = new JFormattedTextField(); btjtf.setText(iFormat.format(btValue)); btjtf.setEditable(false); return btjtf; case INTEGERFIELD: Integer iValue = (Integer)method.invoke(obj, null); JFormattedTextField ijtf = new JFormattedTextField(); ijtf.setText(iFormat.format(iValue)); ijtf.setEditable(false); return ijtf; case AlRAMFIELD: String aValue = (String) method.invoke(obj, null); JTextField aret = new JTextField(aValue); aret.setBackground(AlarmColor.searchColor(aValue)); aret.setEditable(false); return aret; default: String value = (String) method.invoke(obj, null); JTextField ret = new JTextField(value); ret.setEditable(false); return ret; } } catch (Exception e) { log.error(e.getMessage()); JTextField text = new JTextField(); text.setEditable(false); return text; } } } class GroupShowProperty implements Comparable<GroupShowProperty> { public GroupShowProperty(Object obj, ShowProperty showProperty, GroupProperty groupProperty, Method method) { this.obj = obj; this.showProperty = showProperty; this.groupProperty = groupProperty; this.method = method; } Object obj; ShowProperty showProperty; GroupProperty groupProperty; Method method; @Override public int compareTo(GroupShowProperty o) { return this.groupProperty.groupIndex() > o.groupProperty.groupIndex() ? 1 : -1; } }
注解:
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface GroupProperty { public int group() default 0; public int groupIndex() default 0; }
@Retention(RetentionPolicy.RUNTIME) @Target({ ElementType.TYPE }) public @interface MaxGroupProperty { public int maxGroup() default 1; public String groupTitles() default ""; }
@Retention(RetentionPolicy.RUNTIME) @Target({ ElementType.METHOD }) public @interface ShowProperty { public String title() default ""; public ComponentType type() default ComponentType.TEXTFIELD; public enum ComponentType { LABEL, TEXTFIELD, PASSWORDFIELD, TEXTAREA, CHECKBOX, DATEFIELD, INTEGERFIELD, LONGFIELD, COMBOBOX, AlRAMFIELD, BYTE2INTFIELD, BYTE2CHECKBOX } }
FormBean:
import org.apache.log4j.Logger; @MaxGroupProperty(maxGroup = 2, groupTitles = "1111;2222") public class FormBean { private static final Logger log = Logger.getLogger(FormBean.class); private String userLabel; private String code; @ShowProperty(title = "名称", type = ComponentType.AlRAMFIELD) @GroupProperty(groupIndex = 1, group = 0) public String getUserLabel() { return userLabel; } public void setUserLabel(String userLabel) { this.userLabel = userLabel; } @ShowProperty(title = "编码............") @GroupProperty(groupIndex = 1, group = 1) public String getCode() { return code; } public void setCode(String code) { this.code = code; } private java.util.Date Date; @ShowProperty(title = "时间", type = ComponentType.DATEFIELD) @GroupProperty(groupIndex = 1) public java.util.Date getDate() { return Date; } public void setDate(java.util.Date date) { Date = date; } private int intValue; @ShowProperty(title = "整形值", type = ComponentType.INTEGERFIELD) @GroupProperty(groupIndex = 2) public int getIntValue() { return intValue; } public void setIntValue(int intValue) { this.intValue = intValue; } private long longValue; private boolean flag; @ShowProperty(title = "Long值", type = ComponentType.LONGFIELD) @GroupProperty(groupIndex = 4) public long getLongValue() { return longValue; } public void setLongValue(long longValue) { this.longValue = longValue; } @ShowProperty(title = "Boolean值", type = ComponentType.CHECKBOX) @GroupProperty(groupIndex = 3) public boolean isFlag() { return flag; } public void setFlag(boolean flag) { this.flag = flag; } private byte byteValue; @ShowProperty(title = "byte:", type = ComponentType.BYTE2INTFIELD) @GroupProperty(groupIndex = 3) public byte getByteValue() { return byteValue; } public void setByteValue(byte byteValue) { this.byteValue = byteValue; } }
相关文章推荐
- JPA学习笔记---JPA实体Bean的建立+配置文件+junit测试+自动生成(对应实体Bean的)数据库表+插入数据
- 在数据绑定 DataGridView 控件时控制是否自动生成列
- selectKey 这个标签主要用于Insert数据时不支持主键自动生成的问题
- hibernate注解自动生成数据表
- 通过数据库表自动生成javaBean
- 在jsp中点击按钮,在bean中把已经查出的数据,生成csv文件,然后在ie中自动打开
- 基于反射和注解的Bean对应数据库表的自动生成
- hibernate注解自动生成数据表
- Form_Form Builder开发基于视图页面和自动代码生成包(案例)
- php自定义方法:自动生成数据表格(支持分页)
- 添加注解后,框架没有自动根据实体类生成数据表,Hibernate
- 打造Android集合控件数据绑定(支持添加监听,支持AbsListView与RecycleView,支持异步加载等)(二)ORM注解以及解析
- form表单通过GET提交的数据自动生成的链接地址如何伪静态化处理
- hibernate注解自动生成数据表出不来?
- jQuery插件:Ajax将Json数据自动绑定到Form表单
- 小工具 - 解析Bean自动生成Mapper信息支持级联查询
- WPF DataGrid 绑定DataSet数据 自动生成行号
- hibernate注解自动生成数据表
- Myeclipse自动生成java的Bean和BeanName.hbm.xml文件(java项目必须支持Hibernate)
- Spring MVC form 绑定,post 数据为空