您的位置:首页 > 编程语言

使用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反射机制的知识

设计模式:工厂方法、策略模式、适配器模式
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐