/** * Sencha GXT 3.0.0b - Sencha for GWT * Copyright(c) 2007-2012, Sencha, Inc. * licensing@sencha.com * * http://www.sencha.com/products/gxt/license/ */ package com.sencha.gxt.examples.test.client; import java.util.ArrayList; import java.util.List; import com.google.gwt.core.client.EntryPoint; import com.google.gwt.core.client.GWT; import com.google.gwt.editor.client.Editor.Path; import com.google.gwt.http.client.RequestBuilder; import com.google.gwt.user.client.ui.RootPanel; import com.google.web.bindery.autobean.shared.AutoBean; import com.google.web.bindery.autobean.shared.AutoBeanFactory; import com.sencha.gxt.core.client.ValueProvider; import com.sencha.gxt.data.client.loader.HttpProxy; import com.sencha.gxt.data.shared.ListStore; import com.sencha.gxt.data.shared.ModelKeyProvider; import com.sencha.gxt.data.shared.PropertyAccess; import com.sencha.gxt.data.shared.loader.ListLoadResultBean; import com.sencha.gxt.data.shared.loader.JsonReader; import com.sencha.gxt.data.shared.loader.ListLoadConfig; import com.sencha.gxt.data.shared.loader.ListLoadResult; import com.sencha.gxt.data.shared.loader.ListLoader; import com.sencha.gxt.data.shared.loader.LoadResultListStoreBinding; import com.sencha.gxt.widget.core.client.ContentPanel; import com.sencha.gxt.widget.core.client.button.TextButton; import com.sencha.gxt.widget.core.client.container.BoxLayoutContainer.BoxLayoutPack; import com.sencha.gxt.widget.core.client.event.SelectEvent; import com.sencha.gxt.widget.core.client.event.SelectEvent.SelectHandler; import com.sencha.gxt.widget.core.client.grid.ColumnConfig; import com.sencha.gxt.widget.core.client.grid.ColumnModel; import com.sencha.gxt.widget.core.client.grid.Grid; public class JsonGridTest implements EntryPoint { public interface EmailAutoBeanFactory extends AutoBeanFactory { AutoBean<RecordResult> items(); AutoBean<ListLoadConfig> loadConfig(); } public interface Email { String getName(); String getEmail(); String getPhone(); String getState(); String getZip(); } /** * Defines the structure of the root JSON object being returned by the server. * This class is needed as we cannot return a list of objects. Instead, we * return a single object with a single property that contains the data * records. */ public interface RecordResult { List<Email> getRecords(); } class DataRecordJsonReader extends JsonReader<ListLoadResult<Email>, RecordResult> { public DataRecordJsonReader(AutoBeanFactory factory, Class<RecordResult> rootBeanType) { super(factory, rootBeanType); } @Override protected ListLoadResult<Email> createReturnData(Object loadConfig, RecordResult incomingData) { return new ListLoadResultBean<Email>(incomingData.getRecords()); } } interface EmailProperties extends PropertyAccess<Email> { @Path("name") ModelKeyProvider<Email> key(); ValueProvider<Email, String> name(); ValueProvider<Email, String> email(); } @Override public void onModuleLoad() { EmailAutoBeanFactory factory = GWT.create(EmailAutoBeanFactory.class); DataRecordJsonReader reader = new DataRecordJsonReader(factory, RecordResult.class); String path = "data/data.json"; RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, path); HttpProxy<ListLoadConfig> proxy = new HttpProxy<ListLoadConfig>(builder); final ListLoader<ListLoadConfig, ListLoadResult<Email>> loader = new ListLoader<ListLoadConfig, ListLoadResult<Email>>( proxy, reader); EmailProperties props = GWT.create(EmailProperties.class); ListStore<Email> store = new ListStore<Email>(props.key()); loader.addLoadHandler(new LoadResultListStoreBinding<ListLoadConfig, Email, ListLoadResult<Email>>(store)); ColumnConfig<Email, String> cc1 = new ColumnConfig<Email, String>(props.name(), 100, "Sender"); ColumnConfig<Email, String> cc2 = new ColumnConfig<Email, String>(props.email(), 100, "Email"); List<ColumnConfig<Email, ?>> l = new ArrayList<ColumnConfig<Email, ?>>(); l.add(cc1); l.add(cc2); ColumnModel<Email> cm = new ColumnModel<Email>(l); Grid<Email> view = new Grid<Email>(store, cm); view.getView().setForceFit(true); view.setLoader(loader); ContentPanel cp = new ContentPanel(); cp.setHeadingText("Json Grid Example"); cp.setWidget(view); cp.setPixelSize(500, 400); cp.addStyleName("margin-10"); cp.setButtonAlign(BoxLayoutPack.CENTER); cp.addButton(new TextButton("Load Json", new SelectHandler() { @Override public void onSelect(SelectEvent event) { loader.load(); } })); RootPanel.get().add(cp); } }