最近一直在找一個(gè)方便的SWT開發(fā)方法...但是還是陷入了寫一個(gè)TableViewer就得200多行(包括 Table ContentProvider...Sorter..)等等..也用了些設(shè)計(jì)模式,不過還是要寫很多,這件事情真讓人沮喪。昨天想到用注解( Annotation)嘗試著完成這個(gè)工作,今天早晨就開始做了,終于一天的時(shí)間把它做了出來,效果十分令人滿意,本來200多行的代碼現(xiàn)在變成了3行..我從來沒想過TableViewer可以那么容易的創(chuàng)建,確實(shí)反射機(jī)制給java增添了無限的擴(kuò)展^^
好的下面展示一下用這個(gè)工具編寫一個(gè)TableViewer的
清單1 DTO 在get方法上做的注解最終將被用作創(chuàng)建TableViewer
清單2 執(zhí)行程序
好的,運(yùn)用的設(shè)計(jì)模式什么都直接看doc和源代碼吧 注釋很全的,
自己認(rèn)為這個(gè)工具還是可以幫助你的,需要更強(qiáng)大的功能請(qǐng)自己擴(kuò)展,
這個(gè)小工具在此GPL3下開源 http://www.gnu.org/licenses/gpl-3.0.txt
看懂源代碼您還需要以下知識(shí):
Swt Jface 關(guān)于Table和TableViewer的知識(shí)
Annotation的知識(shí)
關(guān)于java反射機(jī)制的知識(shí)
設(shè)計(jì)模式:工廠方法、策略模式、適配器模式
轉(zhuǎn)載請(qǐng)附帶此bolg文章的鏈接,感謝
20071218 10:44 增加了對(duì)每一列的位置控制,將注解由原來的字段上移到了get方法上,增加了一個(gè)類使得創(chuàng)建只需要1行代碼了
好的下面展示一下用這個(gè)工具編寫一個(gè)TableViewer的
清單1 DTO 在get方法上做的注解最終將被用作創(chuàng)建TableViewer
- 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 = "數(shù)字", index = 2,
- imangURL = "icon/a.png", isSort = false,
- width = 100)
- public int getInteger() {
- return integer;
- }
- public void setInteger(int integer) {
- this.integer = integer;
- }
- }
清單2 執(zhí)行程序
- 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);
- //一行代碼創(chuàng)建TableViewer
- TableViewer tableViewer =TableRender.renderTable(table, TestDTO.class);
- //定義表格結(jié)束
- //定義數(shù)據(jù)
- 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();
- }
- }
- }
好的,運(yùn)用的設(shè)計(jì)模式什么都直接看doc和源代碼吧 注釋很全的,
自己認(rèn)為這個(gè)工具還是可以幫助你的,需要更強(qiáng)大的功能請(qǐng)自己擴(kuò)展,
這個(gè)小工具在此GPL3下開源 http://www.gnu.org/licenses/gpl-3.0.txt
看懂源代碼您還需要以下知識(shí):
Swt Jface 關(guān)于Table和TableViewer的知識(shí)
Annotation的知識(shí)
關(guān)于java反射機(jī)制的知識(shí)
設(shè)計(jì)模式:工廠方法、策略模式、適配器模式
轉(zhuǎn)載請(qǐng)附帶此bolg文章的鏈接,感謝
20071218 10:44 增加了對(duì)每一列的位置控制,將注解由原來的字段上移到了get方法上,增加了一個(gè)類使得創(chuàng)建只需要1行代碼了
安徽新華電腦學(xué)校專業(yè)職業(yè)規(guī)劃師為你提供更多幫助【在線咨詢】