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 net.mygwt.ui.client.Style;
011: import net.mygwt.ui.client.data.DataLoader;
012: import net.mygwt.ui.client.data.HTTPProxy;
013: import net.mygwt.ui.client.data.LoadEvent;
014: import net.mygwt.ui.client.data.Loader;
015: import net.mygwt.ui.client.data.ModelType;
016: import net.mygwt.ui.client.data.XMLReader;
017: import net.mygwt.ui.client.event.BaseEvent;
018: import net.mygwt.ui.client.event.Listener;
019: import net.mygwt.ui.client.event.SelectionListener;
020: import net.mygwt.ui.client.viewer.ModelCellLabelProvider;
021: import net.mygwt.ui.client.viewer.ModelContentProvider;
022: import net.mygwt.ui.client.viewer.TableViewer;
023: import net.mygwt.ui.client.widget.Button;
024: import net.mygwt.ui.client.widget.ButtonBar;
025: import net.mygwt.ui.client.widget.WidgetContainer;
026: import net.mygwt.ui.client.widget.layout.RowData;
027: import net.mygwt.ui.client.widget.layout.RowLayout;
028: import net.mygwt.ui.client.widget.table.Table;
029: import net.mygwt.ui.client.widget.table.TableColumn;
030: import net.mygwt.ui.client.widget.table.TableColumnModel;
031:
032: import com.google.gwt.http.client.RequestBuilder;
033:
034: public class XMLTableViewerPage extends Page {
035:
036: protected void createWidget(WidgetContainer container) {
037: TableColumn[] columns = new TableColumn[5];
038: columns[0] = new TableColumn("Sender", .2f);
039: columns[1] = new TableColumn("Email", .4f);
040: columns[2] = new TableColumn("Phone", .2f);
041: columns[3] = new TableColumn("State", .1f);
042: columns[4] = new TableColumn("Zip", "Zip Code", .1f);
043:
044: // create the column model
045: TableColumnModel cm = new TableColumnModel(columns);
046: Table table = new Table(Style.SINGLE | Style.HORIZONTAL, cm);
047:
048: final TableViewer viewer = new TableViewer(table);
049: viewer.setContentProvider(new ModelContentProvider());
050:
051: ModelCellLabelProvider lp = new ModelCellLabelProvider();
052: viewer.getViewerColumn(0).setLabelProvider(lp);
053: viewer.getViewerColumn(1).setLabelProvider(lp);
054: viewer.getViewerColumn(2).setLabelProvider(lp);
055: viewer.getViewerColumn(3).setLabelProvider(lp);
056: viewer.getViewerColumn(4).setLabelProvider(lp);
057:
058: // defines the xml structure
059: ModelType type = new ModelType();
060: type.root = "records";
061: type.recordName = "record";
062: type.addField("Sender", "Name");
063: type.addField("Email");
064: type.addField("Phone");
065: type.addField("State");
066: type.addField("Zip");
067:
068: // use a http proxy to get the data
069: RequestBuilder builder = new RequestBuilder(RequestBuilder.GET,
070: "data.xml");
071: HTTPProxy proxy = new HTTPProxy(builder);
072:
073: // need a loader, proxy, and reader
074: XMLReader reader = new XMLReader(type);
075: final DataLoader loader = new DataLoader(proxy, reader);
076: loader.addListener(Loader.Load, new Listener() {
077:
078: public void handleEvent(BaseEvent be) {
079: LoadEvent de = (LoadEvent) be;
080: viewer.setInput(de.result.getData());
081: }
082:
083: });
084:
085: ButtonBar bar = new ButtonBar();
086: bar.add(new Button("Load XML", new SelectionListener() {
087: public void widgetSelected(BaseEvent be) {
088: loader.load();
089: }
090: }));
091:
092: RowLayout layout = new RowLayout(Style.VERTICAL);
093: layout.setMargin(8);
094: layout.setSpacing(4);
095:
096: container.setLayout(layout);
097: container.add(bar);
098: container.add(table, new RowData(RowData.FILL_BOTH));
099:
100: }
101:
102: }
|