001: /*
002: * MyGWT Widget Library
003: * Copyright(c) 2007, MyGWT.
004: * licensing@mygwt.net
005: *
006: * http://mygwt.net/license
007: */
008: package net.mygwt.samples.data.client;
009:
010: import java.util.List;
011:
012: import net.mygwt.ui.client.Registry;
013: import net.mygwt.ui.client.Style;
014: import net.mygwt.ui.client.data.DataCallback;
015: import net.mygwt.ui.client.data.LoadConfig;
016: import net.mygwt.ui.client.data.LoadResult;
017: import net.mygwt.ui.client.data.Model;
018: import net.mygwt.ui.client.viewer.IAsyncContentCallback;
019: import net.mygwt.ui.client.viewer.IElementComparer;
020: import net.mygwt.ui.client.viewer.ModelCellLabelProvider;
021: import net.mygwt.ui.client.viewer.RemoteContentProvider;
022: import net.mygwt.ui.client.viewer.TableViewer;
023: import net.mygwt.ui.client.widget.PagingToolBar;
024: import net.mygwt.ui.client.widget.WidgetContainer;
025: import net.mygwt.ui.client.widget.layout.RowData;
026: import net.mygwt.ui.client.widget.layout.RowLayout;
027: import net.mygwt.ui.client.widget.table.Table;
028: import net.mygwt.ui.client.widget.table.TableColumn;
029: import net.mygwt.ui.client.widget.table.TableColumnModel;
030:
031: import com.google.gwt.i18n.client.DateTimeFormat;
032: import com.google.gwt.user.client.Command;
033: import com.google.gwt.user.client.DeferredCommand;
034: import com.google.gwt.user.client.rpc.AsyncCallback;
035:
036: public class PagingTableViewerPage extends Page {
037:
038: protected void createWidget(WidgetContainer container) {
039: final SampleServiceAsync service = (SampleServiceAsync) Registry
040: .get("service");
041:
042: TableColumn[] columns = new TableColumn[4];
043: columns[0] = new TableColumn("forum", "Fourm", .2f);
044: columns[1] = new TableColumn("username", "Username", .2f);
045: columns[2] = new TableColumn("subject", "Subject", .4f);
046: columns[3] = new TableColumn("date", "Date", .2f);
047:
048: // create the column model
049: TableColumnModel cm = new TableColumnModel(columns);
050: Table table = new Table(Style.SINGLE | Style.HORIZONTAL, cm);
051: table.setBorders(false);
052:
053: final RemoteContentProvider cp = new RemoteContentProvider() {
054:
055: public void getElements(Object input,
056: IAsyncContentCallback callback) {
057: List list = (List) input;
058: callback.setElements(list.toArray());
059: }
060:
061: public void getData(LoadConfig config,
062: final DataCallback callback) {
063: service.getPosts(config, new AsyncCallback() {
064: public void onSuccess(Object result) {
065: callback.setResult((LoadResult) result);
066: }
067:
068: public void onFailure(Throwable caught) {
069: LoadResult result = new LoadResult();
070: result.success = false;
071: callback.setResult(result);
072: }
073: });
074: }
075:
076: };
077: cp.setRemoteSort(true);
078:
079: final TableViewer viewer = new TableViewer(table);
080: viewer.setComparer(new IElementComparer() {
081: public boolean equals(Object a, Object b) {
082: Post p1 = (Post) a;
083: Post p2 = (Post) b;
084: return p1.getId().equals(p2.getId());
085: }
086: });
087: viewer.setContentProvider(cp);
088:
089: final DateTimeFormat dateFormat = DateTimeFormat
090: .getFormat("MM/d/y");
091:
092: ModelCellLabelProvider lp = new ModelCellLabelProvider() {
093: public String getModelProperty(Model model, String columnId) {
094: if (columnId.equals("date")) {
095: Post p = (Post) model;
096: return dateFormat.format(p.getDate());
097: } else {
098: return super .getModelProperty(model, columnId);
099: }
100: };
101: };
102: viewer.getViewerColumn(0).setLabelProvider(lp);
103: viewer.getViewerColumn(1).setLabelProvider(lp);
104: viewer.getViewerColumn(2).setLabelProvider(lp);
105: viewer.getViewerColumn(3).setLabelProvider(lp);
106:
107: PagingToolBar toolBar = new PagingToolBar(50);
108: toolBar.bind(cp);
109:
110: container.setLayout(new RowLayout(Style.VERTICAL));
111: container.add(table, new RowData(RowData.FILL_BOTH));
112: container.add(toolBar, new RowData(RowData.FILL_HORIZONTAL));
113:
114: // table needs to be attached for mask to display properly
115: DeferredCommand.addCommand(new Command() {
116: public void execute() {
117: cp.load(0, 50);
118: }
119: });
120:
121: }
122:
123: }
|