Java tutorial
// -*- mode: java; c-basic-offset: 2; -*- // Copyright 2013-2014 Vincent Zhang, All rights reserved // Released under the MIT License: https://github.com/karuto/gwt-celltable/blob/master/mitlicense.txt // Dependencies: Google App Engine SDK 1.8.3, GWT SDK 2.5.1 package com.mycompany.celltableexmaple.client; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import com.google.gwt.cell.client.DateCell; import com.google.gwt.core.client.EntryPoint; import com.google.gwt.core.client.GWT; import com.google.gwt.user.cellview.client.CellTable; import com.google.gwt.user.cellview.client.Column; import com.google.gwt.user.cellview.client.SimplePager; import com.google.gwt.user.cellview.client.TextColumn; import com.google.gwt.user.client.Window; import com.google.gwt.user.client.rpc.AsyncCallback; import com.google.gwt.user.client.rpc.IsSerializable; import com.google.gwt.user.client.ui.RootPanel; import com.google.gwt.user.client.ui.VerticalPanel; import com.google.gwt.view.client.AsyncDataProvider; import com.google.gwt.view.client.HasData; public class CellTableExample implements EntryPoint { /** * Create a remote service proxy to talk to the server-side Greeting service. */ private final GreetingServiceAsync greetingService = GWT.create(GreetingService.class); /** * A simple data type that represents an app. * Data that passed through the async callback must be serializable. */ public static class GalleryApp implements IsSerializable { private String title; private String image; private String description; // Recommended to have an empty constructor for serialized class. @SuppressWarnings("Unused") private GalleryApp() { } // The constructor that we will actually be using in the main code. public GalleryApp(String title, String description, String image) { this.title = title; this.description = description; this.image = image; } } /** * The list of data to display. */ @SuppressWarnings("deprecation") private static final List<GalleryApp> GALLERYAPPS = Arrays.asList( new GalleryApp("John", "123 Abc Avenue", "https://www.google.com/images/srpr/logo6w.png"), new GalleryApp("Joe", "22 Lance Ln", "https://www.google.com/images/srpr/logo6w.png"), new GalleryApp("Tom", "33 Lance Ln", "https://www.google.com/images/srpr/logo6w.png"), new GalleryApp("Jack", "44 Lance Ln", "https://www.google.com/images/srpr/logo6w.png"), new GalleryApp("Tim", "55 Lance Ln", "https://www.google.com/images/srpr/logo6w.png"), new GalleryApp("Mike", "66 Lance Ln", "https://www.google.com/images/srpr/logo6w.png"), new GalleryApp("George", "77 Lance Ln", "https://www.google.com/images/srpr/logo6w.png")); public void onModuleLoad() { // Create a CellTable. final CellTable<GalleryApp> galleryTable = new CellTable<GalleryApp>(); // Display 3 rows in one page galleryTable.setPageSize(5); // Add a text column to show the title. TextColumn<GalleryApp> titleColumn = new TextColumn<GalleryApp>() { @Override public String getValue(GalleryApp object) { return object.title; } }; galleryTable.addColumn(titleColumn, "Title"); // Add a text column to show the title. TextColumn<GalleryApp> descColumn = new TextColumn<GalleryApp>() { @Override public String getValue(GalleryApp object) { return object.description; } }; galleryTable.addColumn(descColumn, "Description"); // Add a text column to show the address. TextColumn<GalleryApp> imageColumn = new TextColumn<GalleryApp>() { @Override public String getValue(GalleryApp object) { return object.image; } }; galleryTable.addColumn(imageColumn, "Image URL"); /* * Associate an async data provider to the table. */ AsyncDataProvider<GalleryApp> provider = new AsyncDataProvider<GalleryApp>() { // @Override // /* // * The default event listener which involves no remote data handling, // * replace the onRangeChanged below with this for a simpler demo. // */ // protected void onRangeChanged(HasData<GalleryApp> display) { // int start = display.getVisibleRange().getStart(); // int end = start + display.getVisibleRange().getLength(); // end = end >= GALLERYAPPS.size() ? GALLERYAPPS.size() : end; // List<GalleryApp> sub = GALLERYAPPS.subList(start, end); // updateRowData(start, sub); // } @Override /* * Event handler that will grab data from remote server in async fashion * @see com.google.gwt.view.client.AbstractDataProvider#onRangeChanged(com.google.gwt.view.client.HasData) */ protected void onRangeChanged(HasData<GalleryApp> display) { final int start = display.getVisibleRange().getStart(); int length = display.getVisibleRange().getLength(); AsyncCallback<List<GalleryApp>> callback = new AsyncCallback<List<GalleryApp>>() { @Override public void onFailure(Throwable caught) { Window.alert(caught.getMessage()); } @Override public void onSuccess(List<GalleryApp> result) { // The result here will be the returned value in the async callback updateRowData(start, result); } }; // The remote service that should be implemented greetingService.getApps(start, length, callback); } }; provider.addDataDisplay(galleryTable); provider.updateRowCount(GALLERYAPPS.size(), true); SimplePager pager = new SimplePager(); pager.setDisplay(galleryTable); VerticalPanel vp = new VerticalPanel(); vp.add(galleryTable); vp.add(pager); // Add it to the root panel. RootPanel.get().add(vp); } }