Basic data binding (Ext GWT)
/*
* Ext GWT - Ext for GWT
* Copyright(c) 2007-2009, Ext JS, LLC.
* licensing@extjs.com
*
* http://extjs.com/license
*/
package com.google.gwt.sample.hello.client;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.extjs.gxt.ui.client.binding.FieldBinding;
import com.extjs.gxt.ui.client.binding.FormBinding;
import com.extjs.gxt.ui.client.binding.SimpleComboBoxFieldBinding;
import com.extjs.gxt.ui.client.core.XTemplate;
import com.extjs.gxt.ui.client.data.BaseModel;
import com.extjs.gxt.ui.client.data.ChangeEvent;
import com.extjs.gxt.ui.client.data.ChangeListener;
import com.extjs.gxt.ui.client.util.Util;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.widget.form.SimpleComboBox;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.RootPanel;
public class Hello implements EntryPoint {
public void onModuleLoad() {
RootPanel.get().add(new BasicBindingExample());
}
}
class BasicBindingExample extends LayoutContainer {
@Override
protected void onRender(Element parent, int index) {
super.onRender(parent, index);
final Stock stock = getStocks().get(0);
HorizontalPanel hp = new HorizontalPanel();
hp.setSpacing(10);
StringBuffer sb = new StringBuffer();
sb.append("<div class=text style='line-height: 1.5em'>");
sb.append("<b>Name:</b> {name}<br>");
sb.append("<b>Symbol:</b> {symbol}<br>");
sb.append("<b>Last:</b> {last}<br>");
sb.append("<b>Change:</b> {[new Number(values.change).toFixed(2)]}<br>");
sb.append("<b>Updated:</b> {date:date(\"MM/dd/y\")}<br>");
sb.append("</div>");
final XTemplate template = XTemplate.create(sb.toString());
final HTML html = new HTML();
html.setWidth("160px");
template.overwrite(html.getElement(), Util.getJsObject(stock));
hp.add(html);
// update template when model changes
stock.addChangeListener(new ChangeListener() {
public void modelChanged(ChangeEvent event) {
template.overwrite(html.getElement(), Util.getJsObject(stock));
}
});
FormPanel panel = new FormPanel();
panel.setHeaderVisible(false);
panel.setWidth(350);
TextField<String> name = new TextField<String>();
name.setName("nameField");
name.setFieldLabel("Name");
panel.add(name);
TextField<String> symbol = new TextField<String>();
symbol.setName("symbol");
symbol.setFieldLabel("Symbol");
panel.add(symbol);
NumberField open = new NumberField();
open.setName("last");
open.setFieldLabel("Last");
panel.add(open);
NumberField change = new NumberField();
change.setName("change");
change.setFieldLabel("Change");
change.setFormat(NumberFormat.getDecimalFormat());
panel.add(change);
DateField last = new DateField();
last.setName("date");
last.setFieldLabel("Updated");
panel.add(last);
SimpleComboBox<String> scb = new SimpleComboBox<String>();
for(Stock s: getStocks()) {
scb.add(s.getName());
}
scb.setFieldLabel("Name");
scb.setForceSelection(true);
scb.setTypeAhead(true);
scb.setName("company");
scb.setTriggerAction(TriggerAction.ALL);
panel.add(scb);
hp.add(panel);
FormBinding binding = new FormBinding(panel);
// manually add bindings
binding.addFieldBinding(new FieldBinding(name, "name"));
binding.addFieldBinding(new FieldBinding(symbol, "symbol"));
binding.addFieldBinding(new SimpleComboBoxFieldBinding(scb, "name"));
// auto bind remaining fields, field name must match model property name
binding.autoBind();
binding.bind(stock);
add(hp);
}
public static List<Stock> getStocks() {
List<Stock> stocks = new ArrayList<Stock>();
stocks.add(new Stock("Apple Inc.", "AAPL", 125.64, 123.43));
stocks.add(new Stock("Cisco Systems, Inc.", "CSCO", 25.84, 26.3));
stocks.add(new Stock("Google Inc.", "GOOG", 516.2, 512.6));
stocks.add(new Stock("Intel Corporation", "INTC", 21.36, 21.53));
stocks.add(new Stock("Level 3 Communications, Inc.", "LVLT", 5.55, 5.54));
stocks.add(new Stock("Microsoft Corporation", "MSFT", 29.56, 29.72));
stocks.add(new Stock("Nokia Corporation (ADR)", "NOK", 27.83, 27.93));
stocks.add(new Stock("Oracle Corporation", "ORCL", 18.73, 18.98));
stocks.add(new Stock("Starbucks Corporation", "SBUX", 27.33, 27.36));
stocks.add(new Stock("Yahoo! Inc.", "YHOO", 26.97, 27.29));
stocks.add(new Stock("Applied Materials, Inc.", "AMAT", 18.4, 18.66));
stocks.add(new Stock("Comcast Corporation", "CMCSA", 25.9, 26.4));
stocks.add(new Stock("Sirius Satellite", "SIRI", 2.77, 2.74));
stocks.add(new Stock("Tellabs, Inc.", "TLAB", 10.64, 10.75));
stocks.add(new Stock("eBay Inc.", "EBAY", 30.43, 31.21));
stocks.add(new Stock("Broadcom Corporation", "BRCM", 30.88, 30.48));
stocks.add(new Stock("CMGI Inc.", "CMGI", 2.14, 2.13));
stocks.add(new Stock("Amgen, Inc.", "AMGN", 56.22, 57.02));
stocks.add(new Stock("Limelight Networks", "LLNW", 23, 22.11));
stocks.add(new Stock("Amazon.com, Inc.", "AMZN", 72.47, 72.23));
stocks.add(new Stock("E TRADE Financial Corporation", "ETFC", 24.32, 24.58));
stocks.add(new Stock("AVANIR Pharmaceuticals", "AVNR", 3.7, 3.52));
stocks.add(new Stock("Gemstar-TV Guide, Inc.", "GMST", 4.41, 4.55));
stocks.add(new Stock("Akamai Technologies, Inc.", "AKAM", 43.08, 45.32));
stocks.add(new Stock("Motorola, Inc.", "MOT", 17.74, 17.69));
stocks.add(new Stock("Advanced Micro Devices, Inc.", "AMD", 13.77, 13.98));
stocks.add(new Stock("General Electric Company", "GE", 36.8, 36.91));
stocks.add(new Stock("Texas Instruments Incorporated", "TXN", 35.02, 35.7));
stocks.add(new Stock("Qwest Communications", "Q", 9.9, 10.03));
stocks.add(new Stock("Tyco International Ltd.", "TYC", 33.48, 33.26));
stocks.add(new Stock("Pfizer Inc.", "PFE", 26.21, 26.19));
stocks.add(new Stock("Time Warner Inc.", "TWX", 20.3, 20.45));
stocks.add(new Stock("Sprint Nextel Corporation", "S", 21.85, 21.76));
stocks.add(new Stock("Bank of America Corporation", "BAC", 49.92, 49.73));
stocks.add(new Stock("Taiwan Semiconductor", "TSM", 10.4, 10.52));
stocks.add(new Stock("AT&T Inc.", "T", 39.7, 39.66));
stocks.add(new Stock("United States Steel Corporation", "X", 115.81, 114.62));
stocks.add(new Stock("Exxon Mobil Corporation", "XOM", 81.77, 81.86));
stocks.add(new Stock("Valero Energy Corporation", "VLO", 72.46, 72.6));
stocks.add(new Stock("Micron Technology, Inc.", "MU", 12.02, 12.27));
stocks.add(new Stock("Verizon Communications Inc.", "VZ", 42.5, 42.61));
stocks.add(new Stock("Avaya Inc.", "AV", 16.96, 16.96));
stocks.add(new Stock("The Home Depot, Inc.", "HD", 37.66, 37.79));
stocks.add(new Stock("First Data Corporation", "FDC", 32.7, 32.65));
return stocks;
}
}
class Stock extends BaseModel {
public Stock() {
}
public Stock(String name, String symbol, double open, double last) {
set("name", name);
set("symbol", symbol);
set("open", open);
set("last", last);
set("date", new Date());
set("change", last - open);
}
public Stock(String name, double open, double change, double pctChange, Date date, String industry) {
set("name", name);
set("open", open);
set("change", change);
set("percentChange", pctChange);
set("date", date);
set("industry", industry);
}
public String getIndustry() {
return get("industry");
}
public void setIndustry(String industry) {
set("industry", industry);
}
public Date getLastTrans() {
return (Date) get("date");
}
public String getName() {
return (String) get("name");
}
public String getSymbol() {
return (String) get("symbol");
}
public double getOpen() {
Double open = (Double) get("open");
return open.doubleValue();
}
public double getLast() {
Double open = (Double) get("last");
return open.doubleValue();
}
public double getChange() {
return getLast() - getOpen();
}
public double getPercentChange() {
return getChange() / getOpen();
}
public String toString() {
return getName();
}
}
Ext-GWT.zip( 4,297 k)Related examples in the same category