使用Annotation以1行代码完成一个TableViewer
2008-01-07 21:59
537 查看
本文转载自:http://solonote.javaeye.com/blog/148987作者:solonote
最近一直在找一个方便的SWT开发方法...但是还是陷入了写一个TableViewer就得200多行(包括 Table ContentProvider...Sorter..)等等..也用了些设计模式,不过还是要写很多,这件事情真让人沮丧。昨天想到用注解( Annotation)尝试着完成这个工作,今天早晨就开始做了,终于一天的时间把它做了出来,效果十分令人满意,本来200多行的代码现在变成了3 行..我从来没想过TableViewer可以那么容易的创建,确实反射机制给java增添了无限的扩展^^
好的下面展示一下用这个工具编写一个TableViewer的
清单1 DTO 在get方法上做的注解最终将被用作创建TableViewer
1. package solonote.common.swt.test;
2.
3. import java.util.Date;
4.
5. import solonote.common.swt.table.ColumnAnnotation;
6.
7. /** *//**
8. * 测试用的DTO
9. * @author solonote
10. * @version 0.1.0 2007-12-17 下午07:40:28
11. */
12. public class TestDTO...{
13.
14. private String string;
15.
16. private Date date;
17.
18. private int integer;
19.
20. @ColumnAnnotation(
21. header = "字符", index = 0, imageBundleId = "solonote.common.swt",
22. imangURL = "/icon/hourglass.png", width = 120)
23. public String getString() ...{
24. return string;
25. }
26.
27. public void setString(String string) ...{
28. this.string = string;
29. }
30.
31.
32. @ColumnAnnotation(
33. header = "日期", index = 1,
34. imangURL = "icon/error.png", width = 180)
35. public Date getDate() ...{
36. return date;
37. }
38.
39. public void setDate(Date date) ...{
40. this.date = date;
41. }
42.
43. @ColumnAnnotation(
44. header = "数字", index = 2,
45. imangURL = "icon/a.png", isSort = false,
46. width = 100)
47. public int getInteger() ...{
48. return integer;
49. }
50.
51. public void setInteger(int integer) ...{
52. this.integer = integer;
53. }
54. }
清单2 执行程序
1. package solonote.common.swt.test;
2.
3. import java.util.Date;
4.
5. import org.eclipse.jface.viewers.TableViewer;
6. import org.eclipse.swt.SWT;
7. import org.eclipse.swt.layout.FillLayout;
8. import org.eclipse.swt.widgets.Display;
9. import org.eclipse.swt.widgets.Shell;
10. import org.eclipse.swt.widgets.Table;
11.
12. import solonote.common.swt.table.TableRender;
13.
14. public class TestTable ...{
15.
16. public static void main(String[] args) throws Exception ...{
17. final Display display = Display.getDefault();
18. final Shell shell = new Shell();
19. shell.setLayout(new FillLayout());
20. shell.setSize(420, 375);
21. shell.setText("SWT Application");
22. shell.open();
23. //定义表格
24. Table table = new Table(shell, SWT.FULL_SELECTION | SWT.BORDER);
25. table.setLinesVisible(true);
26. table.setHeaderVisible(true);
27.
28. //一行代码创建TableViewer
29. TableViewer tableViewer =TableRender.renderTable(table, TestDTO.class);
30. //定义表格结束
31.
32. //定义数据
33. TestDTO dto1 = new TestDTO();
34. dto1.setString("bbc");
35. dto1.setDate(new Date());
36. dto1.setInteger(13);
37.
38. TestDTO dto2 = new TestDTO();
39. dto2.setString("abc");
40. dto2.setDate(new Date(dto1.getDate().getTime() + 800));
41. dto2.setInteger(11);
42. tableViewer.setInput(new TestDTO[]...{dto1,dto2});
43.
44. shell.layout();
45. while (!shell.isDisposed()) ...{
46. if (!display.readAndDispatch())
47. display.sleep();
48. }
49. }
50.
51. }
好的,运用的设计模式什么都直接看doc和源代码吧 注释很全的,
自己认为这个工具还是可以帮助你的,需要更强大的功能请自己扩展,
这个小工具在此GPL3下开源 http://www.gnu.org/licenses/gpl-3.0.txt
看懂源代码您还需要以下知识:
Swt Jface 关于Table和TableViewer的知识
Annotation的知识
关于java反射机制的知识
设计模式:工厂方法、策略模式、适配器模式
最近一直在找一个方便的SWT开发方法...但是还是陷入了写一个TableViewer就得200多行(包括 Table ContentProvider...Sorter..)等等..也用了些设计模式,不过还是要写很多,这件事情真让人沮丧。昨天想到用注解( Annotation)尝试着完成这个工作,今天早晨就开始做了,终于一天的时间把它做了出来,效果十分令人满意,本来200多行的代码现在变成了3 行..我从来没想过TableViewer可以那么容易的创建,确实反射机制给java增添了无限的扩展^^
好的下面展示一下用这个工具编写一个TableViewer的
清单1 DTO 在get方法上做的注解最终将被用作创建TableViewer
1. package solonote.common.swt.test;
2.
3. import java.util.Date;
4.
5. import solonote.common.swt.table.ColumnAnnotation;
6.
7. /** *//**
8. * 测试用的DTO
9. * @author solonote
10. * @version 0.1.0 2007-12-17 下午07:40:28
11. */
12. public class TestDTO...{
13.
14. private String string;
15.
16. private Date date;
17.
18. private int integer;
19.
20. @ColumnAnnotation(
21. header = "字符", index = 0, imageBundleId = "solonote.common.swt",
22. imangURL = "/icon/hourglass.png", width = 120)
23. public String getString() ...{
24. return string;
25. }
26.
27. public void setString(String string) ...{
28. this.string = string;
29. }
30.
31.
32. @ColumnAnnotation(
33. header = "日期", index = 1,
34. imangURL = "icon/error.png", width = 180)
35. public Date getDate() ...{
36. return date;
37. }
38.
39. public void setDate(Date date) ...{
40. this.date = date;
41. }
42.
43. @ColumnAnnotation(
44. header = "数字", index = 2,
45. imangURL = "icon/a.png", isSort = false,
46. width = 100)
47. public int getInteger() ...{
48. return integer;
49. }
50.
51. public void setInteger(int integer) ...{
52. this.integer = integer;
53. }
54. }
清单2 执行程序
1. package solonote.common.swt.test;
2.
3. import java.util.Date;
4.
5. import org.eclipse.jface.viewers.TableViewer;
6. import org.eclipse.swt.SWT;
7. import org.eclipse.swt.layout.FillLayout;
8. import org.eclipse.swt.widgets.Display;
9. import org.eclipse.swt.widgets.Shell;
10. import org.eclipse.swt.widgets.Table;
11.
12. import solonote.common.swt.table.TableRender;
13.
14. public class TestTable ...{
15.
16. public static void main(String[] args) throws Exception ...{
17. final Display display = Display.getDefault();
18. final Shell shell = new Shell();
19. shell.setLayout(new FillLayout());
20. shell.setSize(420, 375);
21. shell.setText("SWT Application");
22. shell.open();
23. //定义表格
24. Table table = new Table(shell, SWT.FULL_SELECTION | SWT.BORDER);
25. table.setLinesVisible(true);
26. table.setHeaderVisible(true);
27.
28. //一行代码创建TableViewer
29. TableViewer tableViewer =TableRender.renderTable(table, TestDTO.class);
30. //定义表格结束
31.
32. //定义数据
33. TestDTO dto1 = new TestDTO();
34. dto1.setString("bbc");
35. dto1.setDate(new Date());
36. dto1.setInteger(13);
37.
38. TestDTO dto2 = new TestDTO();
39. dto2.setString("abc");
40. dto2.setDate(new Date(dto1.getDate().getTime() + 800));
41. dto2.setInteger(11);
42. tableViewer.setInput(new TestDTO[]...{dto1,dto2});
43.
44. shell.layout();
45. while (!shell.isDisposed()) ...{
46. if (!display.readAndDispatch())
47. display.sleep();
48. }
49. }
50.
51. }
好的,运用的设计模式什么都直接看doc和源代码吧 注释很全的,
自己认为这个工具还是可以帮助你的,需要更强大的功能请自己扩展,
这个小工具在此GPL3下开源 http://www.gnu.org/licenses/gpl-3.0.txt
看懂源代码您还需要以下知识:
Swt Jface 关于Table和TableViewer的知识
Annotation的知识
关于java反射机制的知识
设计模式:工厂方法、策略模式、适配器模式
相关文章推荐
- 完成一个学生管理程序,使用学号作为键添加5个学生对象,并可以将全部信息保存在文件中,可以实现对学生信息的学号查找,输出全部学生信息的功能。
- 数组循环移动代码,只使用一个临时变量
- Vivado2015.4使用教程(一个完成工程的建立)
- iOS网络缓存扫盲篇--使用两行代码就能完成80%的缓存需求
- iOS开发UI基础—24使用纯代码自定义UItableviewcell实现一个简单的微博界面布局
- 一个jsp+cgi+html小工程,完成注册,后台使用CGI
- 一个基于jQuery ajax和.net httphandler 的超轻异步框架,千行代码完成。
- 2、完成一个简单的时间片轮转多道程序内核代码
- iOS开发UI篇—使用picker View控件完成一个简单的选餐应用
- 历年世界杯冠军队伍如下,使用map存储这些队伍,使用年份作为key,冠军队伍名称作为value。 完成下面的功能: 1) 输入一个年份,如果当年有世界杯比赛,则输出获奖队伍,如果没有则输出“当年没有举
- iOS使用核心的50行代码实现一个路由组件
- android 使用MVP模式完成一个小demo——登陆
- 使用ajax完成一个简单加法运算
- 使用IO技术,创建一个目录,然后复制一个文件到该目录!实现复制的功能。(在博客园上传的第一份代码)
- 使用VC内嵌Python实现的一个代码检测工具
- iOS网络缓存扫盲篇--使用两行代码就能完成80%的缓存需求
- QLineEdit 自动完成(使用setCompleter,内含一个ListView)
- 从零开始,使用Cocos2d HTML5完成一个游戏——第一步
- 80行代码使用Python+tkinter实现一个计算器
- 推荐一个代码自动完成的工具AutoCode