001: /*
002: * GWT-Ext Widget Library
003: * Copyright(c) 2007-2008, GWT-Ext.
004: * licensing@gwt-ext.com
005: *
006: * http://www.gwt-ext.com/license
007: */
008: package com.gwtext.sample.showcase2.client.grid;
009:
010: import com.gwtext.client.core.EventObject;
011: import com.gwtext.client.data.*;
012: import com.gwtext.client.widgets.Button;
013: import com.gwtext.client.widgets.Panel;
014: import com.gwtext.client.widgets.Toolbar;
015: import com.gwtext.client.widgets.ToolbarButton;
016: import com.gwtext.client.widgets.event.ButtonListenerAdapter;
017: import com.gwtext.client.widgets.grid.ColumnConfig;
018: import com.gwtext.client.widgets.grid.ColumnModel;
019: import com.gwtext.client.widgets.grid.GridPanel;
020: import com.gwtext.client.widgets.grid.GridView;
021: import com.gwtext.sample.showcase2.client.ShowcasePanel;
022:
023: public class LocalJsonGridSample extends ShowcasePanel {
024:
025: public String getSourceUrl() {
026: return "source/grid/LocalJsonGridSample.java.html";
027: }
028:
029: public Panel getViewPanel() {
030: if (panel == null) {
031: panel = new Panel();
032:
033: final String jsonString = "{\n"
034: + " \"totalCount\" : 15,\n"
035: + " \"data\":\n"
036: + " [\n"
037: + " {\"abbr\":\"au\", \"name\" : \"Australia\", \"capital\" : \"Canberra\", \"population\" : 18090000, \"area\" : 7713360},\n"
038: + " {\"abbr\":\"br\", \"name\" :\"Brazil\", \"capital\" : \"Brasilia\",\"population\" : 170000000, \"area\" : 8547404},\n"
039: + " {\"abbr\":\"ca\", \"name\" :\"Canada\", \"capital\" : \"Ottawa\",\"population\" : 31000000, \"area\" : 9976140},\n"
040: + " {\"abbr\":\"cn\", \"name\" :\"China\", \"capital\" : \"Beijing\", \"population\" : 1222017000, \"area\" : 9596960},\n"
041: + " {\"abbr\":\"de\", \"name\" :\"Germany\", \"capital\" : \"Berlin\", \"population\" : 80942000, \"area\" : 356910},\n"
042: + " {\"abbr\":\"fr\", \"name\" :\"France\", \"capital\" : \"Paris\", \"population\" : 57571000, \"area\" : 551500},\n"
043: + " {\"abbr\":\"in\", \"name\" :\"India\", \"capital\" : \"New Delhi\", \"population\" : 913747000, \"area\" : 3287590},\n"
044: + " {\"abbr\":\"sc\", \"name\" :\"Seychelles\", \"capital\" : \"Victoria\", \"population\" : 73000, \"area\" : 280},\n"
045: + " {\"abbr\":\"us\", \"name\" :\"United States\", \"capital\" : \"Washington, DC\", \"population\" : 260513000, \"area\" : 9372610},\n"
046: + " {\"abbr\":\"jp\", \"name\" :\"Japan\", \"capital\" : \"Tokyo\", \"population\" : 125422000, \"area\" : 377800},\n"
047: + " {\"abbr\":\"ie\", \"name\" :\"Italy\", \"capital\" : \"Rome\", \"population\" : 57867000, \"area\" : 301270},\n"
048: + " {\"abbr\":\"gh\", \"name\" :\"Ghana\", \"capital\" : \"Accra\", \"population\" : 16944000, \"area\" : 238540},\n"
049: + " {\"abbr\":\"ie\", \"name\" :\"Iceland\", \"capital\" : \"Reykjavik\", \"population\" : 270000, \"area\" : 103000},\n"
050: + " {\"abbr\":\"fi\", \"name\" :\"Finland\", \"capital\" : \"Helsinki\", \"population\" : 5033000, \"area\" : 338130},\n"
051: + " {\"abbr\":\"ch\", \"name\" :\"Switzerland\", \"capital\" : \"Berne\", \"population\" : 6910000, \"area\" : 41290}\n"
052: + " ]\n" + "}";
053:
054: final RecordDef recordDef = new RecordDef(new FieldDef[] {
055: new StringFieldDef("abbr", "abbr"),
056: new StringFieldDef("name", "name"),
057: new IntegerFieldDef("area", "area"),
058: new IntegerFieldDef("population", "population"), });
059: JsonReader reader = new JsonReader(recordDef);
060: reader.setRoot("data");
061: reader.setTotalProperty("totalCount");
062:
063: final Store store = new Store(reader);
064:
065: //setup column model
066: ColumnModel columnModel = new ColumnModel(
067: new ColumnConfig[] {
068: new ColumnConfig("Abbreviation", "abbr",
069: 100, true),
070: new ColumnConfig("Country", "name", 75,
071: true),
072: new ColumnConfig("Area", "area", 75, true),
073: new ColumnConfig("Population",
074: "population", 75, true) });
075:
076: GridPanel grid = new GridPanel();
077: grid.setTitle("Local Json Grid");
078: grid.setStore(store);
079: grid.setColumnModel(columnModel);
080: grid.setFrame(true);
081: grid.setWidth(375);
082: grid.setHeight(350);
083: grid.stripeRows(true);
084: grid.setIconCls("grid-icon");
085:
086: GridView view = new GridView();
087: view
088: .setEmptyText("Press the Load button to load the Local Json data.");
089: grid.setView(view);
090:
091: Toolbar toolbar = new Toolbar();
092:
093: ToolbarButton button = new ToolbarButton("Load Json Data",
094: new ButtonListenerAdapter() {
095: public void onClick(Button button, EventObject e) {
096: //load the store using local Json data
097: store.loadJsonData(jsonString, true);
098: }
099: });
100: button.setIconCls("add-icon");
101:
102: toolbar.addFill();
103: toolbar.addButton(button);
104: grid.setBottomToolbar(toolbar);
105:
106: panel.add(grid);
107: }
108: return panel;
109: }
110:
111: public String getIntro() {
112: return "This example shows how to populate a grid from Local Json data.";
113: }
114: }
|