Package org.gwtoolbox.sample.widget.client.table

Source Code of org.gwtoolbox.sample.widget.client.table.LiveDataGridSample$PersonDataSource

package org.gwtoolbox.sample.widget.client.table;

import com.google.gwt.user.client.*;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;
import org.gwtoolbox.commons.collections.client.Page;
import org.gwtoolbox.commons.types.client.Gender;
import org.gwtoolbox.commons.types.client.Time;
import org.gwtoolbox.ioc.core.client.annotation.Component;
import org.gwtoolbox.ioc.core.client.annotation.Order;
import org.gwtoolbox.sample.widget.client.SamplePanel;
import org.gwtoolbox.widget.client.data.*;
import org.gwtoolbox.widget.client.grid.Column;
import org.gwtoolbox.widget.client.grid.Columns;
import org.gwtoolbox.widget.client.grid.LiveDataGrid;
import org.gwtoolbox.widget.client.panel.contentpanel.ContentPanel;

import java.util.*;

import static com.google.gwt.user.client.ui.HasHorizontalAlignment.ALIGN_CENTER;
import static org.gwtoolbox.widget.client.grid.Column.Feature.HIDABLE;
import static org.gwtoolbox.widget.client.grid.Column.Feature.SORTABLE;

/**
* @author Uri Boness
*/
@Component
@Order(1)
@TableSample
public class LiveDataGridSample extends Composite implements SamplePanel {

    private LiveDataGrid grid;

    public LiveDataGridSample() {

        Columns columns = new Columns(
                new Column("firstName", "First Name", DataTypes.TEXT, 100),
                new Column("lastName", "Last Name", DataTypes.TEXT, 100),
                new Column("age", "Age", DataTypes.INT, 100),
                new Column("gender", "Gender", DataTypes.GENDER, 100, SORTABLE, HIDABLE).setAlignment(ALIGN_CENTER),
                new Column("birthTime", "Birth Time", DataTypes.TIME, 100, SORTABLE, HIDABLE),
                new Column("dutch", "Dutch", DataTypes.BOOLEAN, 50, SORTABLE, HIDABLE).setAlignment(ALIGN_CENTER)
        );
        columns.setFeatures("lastName", SORTABLE, HIDABLE);
        columns.setFeatures("age", SORTABLE, HIDABLE);
        columns.setAlignment("age", ALIGN_CENTER);

        grid = new LiveDataGrid(columns);
        grid.setSize("100%", "250px");

        DataSource personDataSource = new PersonDataSource();
        grid.setDataSource(personDataSource);
        grid.setPageSize(10);

        SimplePanel sp = new SimplePanel();
        sp.setWidget(grid);
        initWidget(sp);
    }


    public String getName() {
        return "Live Data Grid";
    }

    public Widget getContentWidget() {
        return this;
    }

    public void reset() {
        grid.clearSort();
        grid.showFirstPage();
    }

    //================================================= Inner Classes ==================================================

    private class PersonDataSource extends DataSourceAdapter<Record> {

        List<Record> records = new ArrayList<Record>();
        Map<Long, Record> recordById = new HashMap<Long, Record>();

        public PersonDataSource() {
            loadRecords();
        }

        public void getById(Object id, AsyncCallback<Record> callback) {
            callback.onSuccess(recordById.get(id));
        }

        public void getAll(SortSpec sortSpec, AsyncCallback<List<Record>> callback) {
            List<Record> result = new ArrayList<Record>(records);
            if (sortSpec != null) {
                Collections.sort(result, new SortSpecComparator<Record>(sortSpec));
            }
            callback.onSuccess(result);
        }

        public void getPage(int pageIndex, int pageSize, SortSpec sortSpec, final AsyncCallback<Page<Record>> callback) {
            List<Record> list = new ArrayList<Record>(records);
            if (sortSpec != null) {
                Collections.sort(list, new SortSpecComparator<Record>(sortSpec));
            }
            int from = pageIndex * pageSize;
            int to = from + pageSize;
            if (to > list.size()) {
                to = list.size();
            }
            List<Record> items = new ArrayList<Record>(to - from);
            for (int i = from; i < to; i++) {
                items.add(list.get(i));
            }
            final Page<Record> page = new Page<Record>(pageIndex, pageSize, list.size(), items);
            new com.google.gwt.user.client.Timer() {
                public void run() {
                    callback.onSuccess(page);
                }
            }.schedule(5000);
        }

        private void loadRecords() {
            addRecord(new MapRecord()
                    .setLongValue("id", 1)
                    .setValue("firstName", "Uri")
                    .setValue("lastName", "Boness")
                    .setIntValue("age", 33)
                    .setValue("gender", Gender.MALE)
                    .setValue("birthTime", new Time(5))
                    .setValue("dutch", false));

            addRecord(new MapRecord()
                    .setLongValue("id", 2)
                    .setValue("firstName", "Daan")
                    .setValue("lastName", "Boness")
                    .setIntValue("age", 5)
                    .setValue("gender", Gender.MALE)
                    .setValue("birthTime", new Time(5))
                    .setValue("dutch", true));

            addRecord(new MapRecord()
                    .setLongValue("id", 3)
                    .setValue("firstName", "Lian")
                    .setValue("lastName", "Boness")
                    .setIntValue("age", 2)
                    .setValue("gender", Gender.FEMALE)
                    .setValue("birthTime", new Time(10))
                    .setValue("dutch", true));

            addRecord(new MapRecord()
                    .setLongValue("id", 4)
                    .setValue("firstName", "Lian")
                    .setValue("lastName", "Boness")
                    .setIntValue("age", 2)
                    .setValue("gender", Gender.FEMALE)
                    .setValue("birthTime", new Time(10))
                    .setValue("dutch", true));

            addRecord(new MapRecord()
                    .setLongValue("id", 5)
                    .setValue("firstName", "Lian")
                    .setValue("lastName", "Boness")
                    .setIntValue("age", 2)
                    .setValue("gender", Gender.FEMALE)
                    .setValue("birthTime", new Time(10))
                    .setValue("dutch", true));

            addRecord(new MapRecord()
                    .setLongValue("id", 6)
                    .setValue("firstName", "Lian")
                    .setValue("lastName", "Boness")
                    .setIntValue("age", 2)
                    .setValue("gender", Gender.FEMALE)
                    .setValue("birthTime", new Time(10))
                    .setValue("dutch", true));

            addRecord(new MapRecord()
                    .setLongValue("id", 7)
                    .setValue("firstName", "Lian")
                    .setValue("lastName", "Boness")
                    .setIntValue("age", 2)
                    .setValue("gender", Gender.FEMALE)
                    .setValue("birthTime", new Time(10))
                    .setValue("dutch", true));

            addRecord(new MapRecord()
                    .setLongValue("id", 8)
                    .setValue("firstName", "Lian")
                    .setValue("lastName", "Boness")
                    .setIntValue("age", 2)
                    .setValue("gender", Gender.FEMALE)
                    .setValue("birthTime", new Time(10))
                    .setValue("dutch", true));

            addRecord(new MapRecord()
                    .setLongValue("id", 9)
                    .setValue("firstName", "Lian")
                    .setValue("lastName", "Boness")
                    .setIntValue("age", 2)
                    .setValue("gender", Gender.FEMALE)
                    .setValue("birthTime", new Time(10))
                    .setValue("dutch", true));

            addRecord(new MapRecord()
                    .setLongValue("id", 10)
                    .setValue("firstName", "Lian")
                    .setValue("lastName", "Boness")
                    .setIntValue("age", 2)
                    .setValue("gender", Gender.FEMALE)
                    .setValue("birthTime", new Time(10))
                    .setValue("dutch", true));

            addRecord(new MapRecord()
                    .setLongValue("id", 11)
                    .setValue("firstName", "Lian")
                    .setValue("lastName", "Boness")
                    .setIntValue("age", 2)
                    .setValue("gender", Gender.FEMALE)
                    .setValue("birthTime", new Time(10))
                    .setValue("dutch", true));

            addRecord(new MapRecord()
                    .setLongValue("id", 12)
                    .setValue("firstName", "Lian")
                    .setValue("lastName", "Boness")
                    .setIntValue("age", 2)
                    .setValue("gender", Gender.FEMALE)
                    .setValue("birthTime", new Time(10))
                    .setValue("dutch", true));

            addRecord(new MapRecord()
                    .setLongValue("id", 13)
                    .setValue("firstName", "Lian")
                    .setValue("lastName", "Boness")
                    .setIntValue("age", 2)
                    .setValue("gender", Gender.FEMALE)
                    .setValue("birthTime", new Time(10))
                    .setValue("dutch", true));

            addRecord(new MapRecord()
                    .setLongValue("id", 14)
                    .setValue("firstName", "Lian")
                    .setValue("lastName", "Boness")
                    .setIntValue("age", 2)
                    .setValue("gender", Gender.FEMALE)
                    .setValue("birthTime", new Time(10))
                    .setValue("dutch", true));
        }

        private void addRecord(Record record) {
            records.add(record);
            recordById.put(record.getLongValue("id"), record);
        }
    }
}
TOP

Related Classes of org.gwtoolbox.sample.widget.client.table.LiveDataGridSample$PersonDataSource

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.