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