论坛首页 Java企业应用论坛

使用Annotation以1行代码完成一个TableViewer

浏览 6609 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-12-17  
最近一直在找一个方便的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行代码了
  • 描述: 运行结果
  • 大小: 24.2 KB
   发表时间:2008-02-21  
呵呵 不错 。
不过感觉要实现比较复杂的table可能还是比较困难。
0 请登录后投票
   发表时间:2008-06-27  
我用这个啦。很好用的。。。
很好,很强大!
0 请登录后投票
   发表时间:2008-06-27  
不方便Hacking...
0 请登录后投票
   发表时间:2008-09-05  
我有添加啦一个属性控制单元格里面数据的位置。。。。。有要的可以联系我。。。
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics