原创作者: solonote   阅读:2971次   评论:2条   更新时间:2011-06-01    
最近一直在找一个方便的SWT开发方法...但是还是陷入了写一个TableViewer就得200多行(包括 Table ContentProvider...Sorter..)等等..也用了些设计模式,不过还是要写很多,这件事情真让人沮丧。昨天想到用注解( Annotation)尝试着完成这个工作,今天早晨就开始做了,终于一天的时间把它做了出来,效果十分令人满意,本来200多行的代码现在变成了3行..我从来没想过TableViewer可以那么容易的创建,确实反射机制给java增添了无限的扩展^^


好的下面展示一下用这个工具编写一个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行代码了
评论 共 2 条 请登录后发表评论
2 楼 mic_scofield 2010-04-27 20:26
不错!正在学习中……
1 楼 zhoucheng1985 2009-03-27 22:28
  这里是个好专栏咧,怎么只有你一个人在写呀。。

发表评论

您还没有登录,请您登录后再发表评论

文章信息

Global site tag (gtag.js) - Google Analytics