com.google.gwt.sample.stockwatcher.client.Cells.java Source code

Java tutorial

Introduction

Here is the source code for com.google.gwt.sample.stockwatcher.client.Cells.java

Source

package com.google.gwt.sample.stockwatcher.client;

import java.util.Arrays;
import java.util.List;

import com.google.gwt.cell.client.TextCell;
import com.google.gwt.cell.client.TextInputCell;
import com.google.gwt.cell.client.ValueUpdater;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.cellview.client.CellBrowser;
import com.google.gwt.user.cellview.client.CellList;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.CellTree;
import com.google.gwt.user.cellview.client.HasKeyboardSelectionPolicy.KeyboardSelectionPolicy;
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.ui.Anchor;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SingleSelectionModel;
import com.google.gwt.view.client.TreeViewModel;

public class Cells extends Content {

    private Anchor cellListLink = new Anchor("Cell List");
    private Anchor cellTableLink = new Anchor("Cell Table");
    private Anchor cellTreeLink = new Anchor("Cell Tree");
    private Anchor cellBrowserLink = new Anchor("Cell Browser");

    @Override
    public String getPageTitle() {
        return "Cells";
    }

    @Override
    public void loadContent() {
        loadLinks();
    }

    private void loadContent(Widget content) {
        RootPanel contentPanel = RootPanel.get("content");
        contentPanel.clear();
        contentPanel.add(content);
    }

    private void loadLinks() {
        RootPanel links = RootPanel.get("navigationLinks");
        links.clear();

        cellListLink.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                loadContent(createCellList());
            }
        });

        cellTableLink.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                loadContent(createCellTable());
            }
        });

        cellTreeLink.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                loadContent(createCellTree());
            }
        });

        cellBrowserLink.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                loadContent(createCellBrowser());
            }
        });

        HorizontalPanel linksPanel = new HorizontalPanel();
        linksPanel.setSpacing(5);

        linksPanel.add(cellListLink);
        linksPanel.add(cellTableLink);
        linksPanel.add(cellTreeLink);
        linksPanel.add(cellBrowserLink);

        links.add(linksPanel);
    }

    private Widget createCellList() {
        TextCell textCell = new TextCell();
        CellList<String> cellList = new CellList<>(textCell);

        final List<String> DAYS = Arrays.asList("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
                "Saturday");

        /*
         * Selection Model
         */
        cellList.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED);

        //Add a selection model to handle user selection
        final SingleSelectionModel<String> selectionModel = new SingleSelectionModel<String>();
        cellList.setSelectionModel(selectionModel);
        selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
            @Override
            public void onSelectionChange(SelectionChangeEvent event) {
                String selected = selectionModel.getSelectedObject();
                if (selected != null) {
                    Window.alert("You selected: " + selected);
                }
            }
        });

        /*
         * Value updater
         */
        //      cellList.setValueUpdater(new ValueUpdater<String>() {
        //         @Override
        //         public void update(String value) {
        //            Window.alert("New Value: " + value);
        //         }
        //      });

        // Set the total row count. This isn't strictly necessary, but it affects
        // paging calculations, so its good habit to keep the row count up to date.
        cellList.setRowCount(DAYS.size(), true);

        cellList.setRowData(0, DAYS);

        // Create a SimplePager.
        SimplePager pager = new SimplePager();

        // Set the cellList as the display.
        pager.setDisplay(cellList);

        // Add the pager and list to the page.
        VerticalPanel vPanel = new VerticalPanel();
        vPanel.add(pager);
        vPanel.add(cellList);

        return vPanel;
    }

    private Widget createCellTable() {
        class Contact {
            private final String name;
            private final String address;

            public Contact(String name, String address) {
                this.name = name;
                this.address = address;
            }
        }

        List<Contact> CONTACTS = Arrays.asList(new Contact("John", "123 Fourth Road"),
                new Contact("Mary", "222 Lancer Lane"));

        CellTable<Contact> table = new CellTable<>();

        TextColumn<Contact> nameColumn = new TextColumn<Contact>() {
            @Override
            public String getValue(Contact contact) {
                return contact.name;
            }
        };

        TextColumn<Contact> addressColumn = new TextColumn<Contact>() {
            @Override
            public String getValue(Contact contact) {
                return contact.address;
            }
        };

        table.addColumn(nameColumn, "Name");
        table.addColumn(addressColumn, "Address");

        // Set the total row count. This isn't strictly necessary, but it affects
        // paging calculations, so its good habit to keep the row count up to date.
        table.setRowCount(CONTACTS.size(), true);

        // Push the data into the widget.
        table.setRowData(0, CONTACTS);

        return table;
    }

    class CustomTreeModel implements TreeViewModel {
        @Override
        public <T> NodeInfo<?> getNodeInfo(T value) {
            ListDataProvider<String> dataProvider = new ListDataProvider<>();
            for (int i = 0; i < 2; i++) {
                dataProvider.getList().add(value + "." + String.valueOf(i));
            }
            return new DefaultNodeInfo<String>(dataProvider, new TextCell());
        }

        //This defines the depth of the tree
        @Override
        public boolean isLeaf(Object value) {
            return value.toString().length() > 10;
        }
    }

    private Widget createCellTree() {
        TreeViewModel model = new CustomTreeModel();
        CellTree tree = new CellTree(model, "Item 1");
        return tree;
    }

    private Widget createCellBrowser() {
        TreeViewModel model = new CustomTreeModel();
        CellBrowser browser = new CellBrowser.Builder<String>(model, "Item 1").build();
        browser.setPixelSize(800, 200);
        return browser;
    }

}