SWT/JFace AbstractTableViewer 分页

阅读更多
本文主要介绍JFace AbstractTableViewer 控件的分页,采用数据库分页,分页数据封装在一个Page对象中,子类只需要实现抽象方法doSplitPageQuery,获取分页数据即可,伪代码如下:
/**
 * 含分页的表格控件封装
 * 
 * @author brofe
 * @since 2010-12-19
 * @version v 1.0
 */
@SuppressWarnings({"rawtypes", "unchecked"})
public abstract class SplitPageTableViewer {
	
	protected AbstractTableViewer tableViewer;
	protected SplitPageUI splitPageUI;
	protected Composite parent;
	protected Composite tableComposite;
	protected Group tableGroup;
	protected String title;
	protected Page 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() {
			@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 getPage() {
		return page;
	}

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

	public SplitPageUI getSplitPageUI() {
		return splitPageUI;
	}

	public Group getTableGroup() {
		return tableGroup;
	}
}

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

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


效果见截图。


  • SWT/JFace AbstractTableViewer 分页_第1张图片
  • 大小: 172.6 KB
  • 查看图片附件

你可能感兴趣的:(SWT/JFace AbstractTableViewer 分页)