SWT/JFace AbstractTableViewer 分页

本文主要介绍JFace AbstractTableViewer 控件的分页,采用数据库分页,分页数据封装在一个Page<T>对象中,子类只需要实现抽象方法doSplitPageQuery,获取分页数据即可,伪代码如下:
/**
 * 含分页的表格控件封装
 * 
 * @author <a href="mailto:[email protected]">brofe</a>
 * @since 2010-12-19
 * @version v 1.0
 */
@SuppressWarnings({"rawtypes", "unchecked"})
public abstract class SplitPageTableViewer<T> {
	
	protected AbstractTableViewer tableViewer;
	protected SplitPageUI<T> splitPageUI;
	protected Composite parent;
	protected Composite tableComposite;
	protected Group tableGroup;
	protected String title;
	protected Page<T> page = new Page(Constants.DEFAULT_PAGE_SIZE);
	
	protected double defaultFirstHeight = 0.88; 
	protected double defaultSecondHeigth = 0.12;
	
	public SplitPageTableViewer(Composite parent, String title) {
		this.parent = parent;
		this.title = title;
		
		this.draw();
	}
	
	public SplitPageTableViewer(Composite parent, String title, double defaultFirstHeight, double defaultSecondHeigth) {
		this.parent = parent;
		this.title = title;
		this.defaultFirstHeight = defaultFirstHeight;
		this.defaultSecondHeigth = defaultSecondHeigth;
		
		this.draw();
	}
	
	private void draw() {
		tableGroup = new Group(parent, SWT.FILL); 
		tableGroup.setLayout(new FillLayout());
		tableGroup.setText(title);
		Composite composite = new Composite(tableGroup, SWT.FILL);
		composite.setLayout(new FillLayout());
	
		SashForm sashForm = new SashForm(composite, SWT.FILL | SWT.VERTICAL);
		sashForm.setLayout(new GridLayout(1, false));
		sashForm.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		
		tableComposite = new Composite(sashForm, SWT.NONE | SWT.FILL);
		tableComposite.setLayout(new FillLayout());
		tableComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		
		splitPageUI = new SplitPageUI<T>() {
			@Override
			public void doPageQuery() {
				doSplitPageQuery();
			}
		};
		splitPageUI.createSplitPageUI(sashForm, page);
		
		sashForm.setWeights(new int[] {
				(int) (Constants.RECT.width * defaultFirstHeight),
				(int) (Constants.RECT.width * defaultSecondHeigth)
		});
		sashForm.SASH_WIDTH = 0;
	}
	
	public void addTableViewer(AbstractTableViewer tableViewer) {
		this.tableViewer = tableViewer;
		this.layout();
	}
	
	protected void layout() {
		tableComposite.layout();
	}
	
	public abstract void doSplitPageQuery();

	public Composite getTableComposite() {
		return tableComposite;
	}

	public Page<T> getPage() {
		return page;
	}

	public void setPage(Page<T> page) {
		this.page = page;
		this.splitPageUI.setPage(page);
	}

	public SplitPageUI<T> getSplitPageUI() {
		return splitPageUI;
	}

	public Group getTableGroup() {
		return tableGroup;
	}
}

// 使用方法如下:
final SplitPageTableViewer sptv = new SplitPageTableViewer<Bol>(mainComposite, "出库单") {
	@Override
	public void doSplitPageQuery() {
		Page<Bol> page = DefaultCommonManager.getBusinessWarehouseManager().pageQueryBol(sptv.getPage(), null, bol);;
		mainTableViewer.setInput(page.getResult());
		sptv.getSplitPageUI().setPage(page);
	}
};

// 初始化数据
Page<Bol> page = .............
BolTableViewer mainTableViewer = new BolTableViewer(this, sptv.getTableComposite(), 
		WarehouseApp.tableDigester.getTable(WHConstants.TEMPLATE_BOL), page.getResult());
sptv.addTableViewer(mainTableViewer);
sptv.setPage(page);


效果见截图。


你可能感兴趣的:(tableview)