国产一区二区精品久久_蜜桃狠狠狠狠狠狠狠狠狠_午夜视频精品_激情都市一区二区

當前位置:首頁 > 網站舊欄目 > 學習園地 > 設計軟件教程 > 使用Annotation以1行代碼完成一個TableViewer

使用Annotation以1行代碼完成一個TableViewer
2010-01-14 23:13:47  作者:  來源:
最近一直在找一個方便的SWT開發方法...但是還是陷入了寫一個TableViewer就得200多行(包括 Table ContentProvider...Sorter..)等等..也用了些設計模式,不過還是要寫很多,這件事情真讓人沮喪。昨天想到用注解( Annotation)嘗試著完成這個工作,今天早晨就開始做了,終于一天的時間把它做了出來,效果十分令人滿意,本來200多行的代碼現在變成了3行..我從來沒想過TableViewer可以那么容易的創建,確實反射機制給java增添了無限的擴展^^


好的下面展示一下用這個工具編寫一個TableViewer的

清單1 DTO 在get方法上做的注解最終將被用作創建TableViewer
Java代碼 復制代碼
  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 執行程序
Java代碼 復制代碼
  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(420375);   
  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反射機制的知識

設計模式:工廠方法、策略模式、適配器模式

轉載請附帶此bolg文章的鏈接,感謝

20071218 10:44 增加了對每一列的位置控制,將注解由原來的字段上移到了get方法上,增加了一個類使得創建只需要1行代碼了

安徽新華電腦學校專業職業規劃師為你提供更多幫助【在線咨詢