使用Annotation以1行代码完成一个TableViewer

最近一直在找一个方便的SWT开发方法...但是还是陷入了写一个TableViewer就得200多行(包括 Table ContentProvider...Sorter..)等等..也用了些设计模式,不过还是要写很多,这件事情真让人沮丧。昨天想到用注解( Annotation)尝试着完成这个工作,今天早晨就开始做了,终于一天的时间把它做了出来,效果十分令人满意,本来200多行的代码现在变成了3行..我从来没想过TableViewer可以那么容易的创建,确实反射机制给java增添了无限的扩展^^


好的下面展示一下用这个工具编写一个TableViewer的

清单1 DTO 在get方法上做的注解最终将被用作创建TableViewer
[code]
package solonote.common.swt.test;

import java.util.Date;

import solonote.common.swt.table.ColumnAnnotation;

/**
* 测试用的DTO
* @author solonote
* @version 0.1.0 2007-12-17 下午07:40:28
*/
public class TestDTO{

private String string;

private Date date;

private int integer;

@ColumnAnnotation(
header = "字符", index = 0, imageBundleId = "solonote.common.swt",
imangURL = "/icon/hourglass.png", width = 120)
public String getString() {
return string;
}

public void setString(String string) {
this.string = string;
}


@ColumnAnnotation(
header = "日期", index = 1,
imangURL = "icon/error.png", width = 180)
public Date getDate() {
return date;
}

public void setDate(Date date) {
this.date = date;
}

@ColumnAnnotation(
header = "数字", index = 2,
imangURL = "icon/a.png", isSort = false,
width = 100)
public int getInteger() {
return integer;
}

public void setInteger(int integer) {
this.integer = integer;
}
}
[/code]

清单2 执行程序
[code]
package solonote.common.swt.test;

import java.util.Date;

import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;

import solonote.common.swt.table.TableRender;

public class TestTable {

public static void main(String[] args) throws Exception {
final Display display = Display.getDefault();
final Shell shell = new Shell();
shell.setLayout(new FillLayout());
shell.setSize(420, 375);
shell.setText("SWT Application");
shell.open();
//定义表格
Table table = new Table(shell, SWT.FULL_SELECTION | SWT.BORDER);
table.setLinesVisible(true);
table.setHeaderVisible(true);

//一行代码创建TableViewer
TableViewer tableViewer =TableRender.renderTable(table, TestDTO.class);
//定义表格结束

//定义数据
TestDTO dto1 = new TestDTO();
dto1.setString("bbc");
dto1.setDate(new Date());
dto1.setInteger(13);

TestDTO dto2 = new TestDTO();
dto2.setString("abc");
dto2.setDate(new Date(dto1.getDate().getTime() + 800));
dto2.setInteger(11);
tableViewer.setInput(new TestDTO[]{dto1,dto2});

shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}

}
[/code]

好的,运用的设计模式什么都直接看doc和源代码吧 注释很全的,

自己认为这个工具还是可以帮助你的,需要更强大的功能请自己扩展,

这个小工具在此GPL3下开源 [url]http://www.gnu.org/licenses/gpl-3.0.txt[/url]

看懂源代码您还需要以下知识:

Swt Jface 关于Table和TableViewer的知识

Annotation的知识

关于java反射机制的知识

设计模式:工厂方法、策略模式、适配器模式

转载请附带此bolg文章的链接,感谢

20071218 10:44 增加了对每一列的位置控制,将注解由原来的字段上移到了get方法上,增加了一个类使得创建只需要1行代码了
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值