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.explorer.client;
009:
010: import net.mygwt.samples.resources.client.Folder;
011: import net.mygwt.samples.resources.client.Stock;
012: import net.mygwt.samples.resources.client.TestData;
013: import net.mygwt.ui.client.Style;
014: import net.mygwt.ui.client.viewer.CellLabelProvider;
015: import net.mygwt.ui.client.viewer.ModelContentProvider;
016: import net.mygwt.ui.client.viewer.TableViewer;
017: import net.mygwt.ui.client.viewer.TableViewerColumn;
018: import net.mygwt.ui.client.viewer.Viewer;
019: import net.mygwt.ui.client.viewer.ViewerCell;
020: import net.mygwt.ui.client.viewer.ViewerSorter;
021: import net.mygwt.ui.client.widget.WidgetContainer;
022: import net.mygwt.ui.client.widget.layout.FillLayout;
023: import net.mygwt.ui.client.widget.table.Table;
024: import net.mygwt.ui.client.widget.table.TableColumn;
025: import net.mygwt.ui.client.widget.table.TableColumnModel;
026:
027: import com.google.gwt.i18n.client.DateTimeFormat;
028: import com.google.gwt.i18n.client.NumberFormat;
029:
030: public class TableViewerPage extends Page {
031:
032: protected void createWidget(WidgetContainer container) {
033: final DateTimeFormat dateFormat = DateTimeFormat
034: .getFormat("MM/d/y");
035: final NumberFormat currency = NumberFormat.getCurrencyFormat();
036:
037: TableColumn[] columns = new TableColumn[5];
038:
039: columns[0] = new TableColumn("Company", 180);
040: columns[0].setMinWidth(75);
041: columns[0].setMaxWidth(300);
042:
043: columns[1] = new TableColumn("Symbol", 75);
044:
045: columns[2] = new TableColumn("Last", 75);
046: columns[2].setMaxWidth(100);
047: columns[2].setAlignment(Style.RIGHT);
048:
049: columns[3] = new TableColumn("Change", 75);
050: columns[3].setAlignment(Style.RIGHT);
051: columns[4] = new TableColumn("Last Updated", 100);
052: columns[4].setAlignment(Style.RIGHT);
053:
054: TableColumnModel cm = new TableColumnModel(columns);
055:
056: Table table = new Table(Style.SINGLE | Style.HORIZONTAL, cm);
057:
058: TableViewer viewer = new TableViewer(table);
059: viewer.setContentProvider(new ModelContentProvider());
060:
061: TableViewerColumn col = viewer.getViewerColumn(0);
062: col.setLabelProvider(new CellLabelProvider() {
063: public void update(ViewerCell cell) {
064: cell.setText(((Stock) cell.getElement()).getName());
065: }
066: });
067:
068: col = viewer.getViewerColumn(1);
069: col.setLabelProvider(new CellLabelProvider() {
070: public void update(ViewerCell cell) {
071: cell.setText(((Stock) cell.getElement()).getSymbol());
072: }
073: });
074:
075: col = viewer.getViewerColumn(2);
076: col.setLabelProvider(new CellLabelProvider() {
077: public void update(ViewerCell cell) {
078: double val = ((Stock) cell.getElement()).getLast();
079: cell.setText(currency.format(val));
080: }
081: });
082: col.setViewerSorter(new ViewerSorter() {
083: public int compare(Viewer viewer, Object e1, Object e2) {
084: Stock s1 = (Stock) e1;
085: Stock s2 = (Stock) e2;
086: return s1.getLast() < s2.getLast() ? 0 : 1;
087: }
088: });
089:
090: col = viewer.getViewerColumn(3);
091: col.setLabelProvider(new CellLabelProvider() {
092: public void update(ViewerCell cell) {
093: Stock s = (Stock) cell.getElement();
094: cell
095: .setText(currency.format(s.getLast()
096: - s.getOpen()));
097: }
098: });
099: col.setViewerSorter(new ViewerSorter() {
100: public int compare(Viewer viewer, Object e1, Object e2) {
101: Stock s1 = (Stock) e1;
102: Stock s2 = (Stock) e2;
103: return (s1.getLast() - s1.getOpen()) < (s2.getLast() - s2
104: .getOpen()) ? 0 : 1;
105: }
106: });
107:
108: col = viewer.getViewerColumn(4);
109: col.setLabelProvider(new CellLabelProvider() {
110: public void update(ViewerCell cell) {
111: Stock s = (Stock) cell.getElement();
112: cell.setText(dateFormat.format(s.getLastTrans()));
113: }
114: });
115: col.setViewerSorter(new ViewerSorter() {
116: public int compare(Viewer viewer, Object e1, Object e2) {
117: Stock s1 = (Stock) e1;
118: Stock s2 = (Stock) e2;
119: return s1.getLastTrans().compareTo(s2.getLastTrans());
120: }
121: });
122:
123: Folder stocks = TestData.getStocks();
124: viewer.setInput(stocks);
125:
126: container.setLayout(new FillLayout(8));
127: container.add(table);
128: }
129:
130: }
|