您的位置:首页 > 其它

注解下的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;
}

}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: