com.npisoft.officine.client.application.GridExample.java Source code

Java tutorial

Introduction

Here is the source code for com.npisoft.officine.client.application.GridExample.java

Source

/**
 * Sencha GXT 3.1.0 - Sencha for GWT
 * Copyright(c) 2007-2014, Sencha, Inc.
 * licensing@sencha.com
 *
 * http://www.sencha.com/products/gxt/license/
 */
package com.npisoft.officine.client.application;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.cell.client.DateCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.npisoft.officine.client.application.resources.ExampleImages;
import com.sencha.gxt.cell.core.client.ButtonCell.IconAlign;
import com.sencha.gxt.cell.core.client.form.ComboBoxCell.TriggerAction;
import com.sencha.gxt.core.client.util.DateWrapper;
import com.sencha.gxt.core.client.util.Margins;
import com.sencha.gxt.data.shared.ListStore;
import com.sencha.gxt.data.shared.StringLabelProvider;
import com.sencha.gxt.state.client.GridStateHandler;
import com.sencha.gxt.widget.core.client.ContentPanel;
import com.sencha.gxt.widget.core.client.Resizable;
import com.sencha.gxt.widget.core.client.Resizable.Dir;
import com.sencha.gxt.widget.core.client.Window;
import com.sencha.gxt.widget.core.client.button.ButtonGroup;
import com.sencha.gxt.widget.core.client.button.TextButton;
import com.sencha.gxt.widget.core.client.container.BoxLayoutContainer.BoxLayoutData;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer.VerticalLayoutData;
import com.sencha.gxt.widget.core.client.event.CollapseEvent;
import com.sencha.gxt.widget.core.client.event.CollapseEvent.CollapseHandler;
import com.sencha.gxt.widget.core.client.event.ExpandEvent;
import com.sencha.gxt.widget.core.client.event.ExpandEvent.ExpandHandler;
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.form.SimpleComboBox;
import com.sencha.gxt.widget.core.client.grid.CellSelectionModel;
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;
import com.sencha.gxt.widget.core.client.grid.GridSelectionModel;
import com.sencha.gxt.widget.core.client.menu.Menu;
import com.sencha.gxt.widget.core.client.menu.MenuItem;
import com.sencha.gxt.widget.core.client.selection.CellSelectionChangedEvent;
import com.sencha.gxt.widget.core.client.selection.CellSelectionChangedEvent.CellSelectionChangedHandler;
import com.sencha.gxt.widget.core.client.tips.QuickTip;
import com.sencha.gxt.widget.core.client.toolbar.PagingToolBar;
import com.sencha.gxt.widget.core.client.toolbar.ToolBar;

public class GridExample implements IsWidget {

    private static final StockProperties props = GWT.create(StockProperties.class);

    // class SamplePanel extends ContentPanel {
    // private final VerticalLayoutContainer con = new
    // VerticalLayoutContainer();
    // private final ToolBar toolBar = new ToolBar();
    //
    // public SamplePanel() {
    // setPixelSize(500, 250);
    // toolBar.setSpacing(2);
    // con.add(toolBar, new VerticalLayoutData(1, -1));
    // // HTML text = new HTML("TestData.DUMMY_TEXT_LONG");
    // // text.getElement().getStyle().setOverflowY(Overflow.AUTO);
    // // con.add(text, new VerticalLayoutData(1, 1));
    // add(con);
    // }
    //
    // public ToolBar getToolBar() {
    // return toolBar;
    // }
    //
    // }

    private ContentPanel root;

    @Override
    public Widget asWidget() {
        final NumberFormat number = NumberFormat.getFormat("0.00");

        ColumnConfig<Stock, String> nameCol = new ColumnConfig<Stock, String>(props.name(), 50,
                SafeHtmlUtils.fromTrustedString("<b>Company</b>"));

        ColumnConfig<Stock, String> symbolCol = new ColumnConfig<Stock, String>(props.symbol(), 100, "Symbol");
        ColumnConfig<Stock, Double> lastCol = new ColumnConfig<Stock, Double>(props.last(), 75, "Last");

        ColumnConfig<Stock, Double> changeCol = new ColumnConfig<Stock, Double>(props.change(), 100, "Change");
        changeCol.setCell(new AbstractCell<Double>() {

            @Override
            public void render(Context context, Double value, SafeHtmlBuilder sb) {
                String style = "style='color: " + (value < 0 ? "red" : "green") + "'";
                String v = number.format(value);
                sb.appendHtmlConstant("<span " + style + " qtitle='Change' qtip='" + v + "'>" + v + "</span>");
            }
        });

        ColumnConfig<Stock, Date> lastTransCol = new ColumnConfig<Stock, Date>(props.lastTrans(), 100,
                "Last Updated");
        lastTransCol.setCell(new DateCell(DateTimeFormat.getFormat("MM/dd/yyyy")));

        List<ColumnConfig<Stock, ?>> l = new ArrayList<ColumnConfig<Stock, ?>>();
        l.add(nameCol);
        l.add(symbolCol);
        l.add(lastCol);
        l.add(changeCol);
        l.add(lastTransCol);

        ColumnModel<Stock> cm = new ColumnModel<Stock>(l);

        ListStore<Stock> store = new ListStore<Stock>(props.key());
        store.addAll(getStocks());

        root = new ContentPanel();

        // panel = new ();
        // panel.setCollapsible(true);
        // panel.setPixelSize(500, 400);
        // panel.addStyleName("margin-10");

        root.setHeadingText("Basic Grid");
        root.getHeader().setIcon(ExampleImages.INSTANCE.table());
        root.setPixelSize(600, 300);
        root.addStyleName("margin-10");

        //ToolButton info = new ToolButton(ToolButton.QUESTION);
        //ToolTipConfig config = new ToolTipConfig("Example Info",
        //   "This examples includes resizable panel, reorderable columns and grid state.");
        //config.setMaxWidth(225);
        //info.setToolTipConfig(config);
        // root.addTool(info);

        final Resizable resizable = new Resizable(root, Dir.E, Dir.SE, Dir.S);
        root.addExpandHandler(new ExpandHandler() {
            @Override
            public void onExpand(ExpandEvent event) {
                resizable.setEnabled(true);
            }
        });
        root.addCollapseHandler(new CollapseHandler() {
            @Override
            public void onCollapse(CollapseEvent event) {
                resizable.setEnabled(false);
            }
        });

        final Grid<Stock> grid = new Grid<Stock>(store, cm);
        grid.getView().setAutoExpandColumn(nameCol);
        grid.getView().setStripeRows(true);
        grid.getView().setColumnLines(true);
        grid.setBorders(false);

        grid.setColumnReordering(true);
        grid.setStateful(true);
        grid.setStateId("gridExample");

        GridStateHandler<Stock> state = new GridStateHandler<Stock>(grid);
        state.loadState();

        ToolBar toolBar = new ToolBar();
        toolBar.setEnableOverflow(false);
        // toolBar.add(new LabelToolItem("Selection Mode: "));

        BoxLayoutData flex = new BoxLayoutData(new Margins(0, 5, 0, 5));
        flex.setFlex(1);
        toolBar.add(new Label(), flex);

        TextButton btn0 = new TextButton();
        btn0.setIcon(ExampleImages.INSTANCE.add());
        btn0.setWidth(50);
        btn0.setToolTip("Ajouter");
        btn0.setIconAlign(IconAlign.TOP);
        toolBar.add(btn0);

        HelloWindowExample helloWindowExample = new HelloWindowExample();
        final Window window = (Window) helloWindowExample.asWidget();
        btn0.addSelectHandler(new SelectHandler() {
            @Override
            public void onSelect(SelectEvent event) {
                window.show();
            }
        });
        window.setData("open", btn0);

        TextButton btn2 = new TextButton();
        btn2.setIcon(ExampleImages.INSTANCE.delete0());
        btn2.setWidth(50);
        btn2.setToolTip("Supprimer");
        btn2.setIconAlign(IconAlign.TOP);
        toolBar.add(btn2);

        TextButton btn1 = new TextButton();
        btn1.setIcon(ExampleImages.INSTANCE.edit());
        btn1.setWidth(50);
        btn1.setToolTip("Editer");
        btn1.setIconAlign(IconAlign.TOP);
        toolBar.add(btn1);

        TextButton btn3 = new TextButton();
        btn3.setIcon(ExampleImages.INSTANCE.search());
        btn3.setWidth(50);
        btn3.setToolTip("Chercher");
        btn3.setIconAlign(IconAlign.TOP);
        toolBar.add(btn3);

        // toolBar.getToolTip().add

        SimpleComboBox<String> type = new SimpleComboBox<String>(new StringLabelProvider<String>());
        type.setTriggerAction(TriggerAction.ALL);
        type.setEditable(false);
        type.setWidth(100);
        type.add("Row");
        type.add("Cell");
        type.setValue("Row");
        // we want to change selection model on select, not value change which
        // fires on blur
        type.addSelectionHandler(new SelectionHandler<String>() {

            @Override
            public void onSelection(SelectionEvent<String> event) {
                boolean cell = event.getSelectedItem().equals("Cell");
                if (cell) {
                    CellSelectionModel<Stock> c = new CellSelectionModel<Stock>();
                    c.addCellSelectionChangedHandler(new CellSelectionChangedHandler<Stock>() {

                        @Override
                        public void onCellSelectionChanged(CellSelectionChangedEvent<Stock> event) {

                        }
                    });

                    grid.setSelectionModel(c);
                } else {
                    grid.setSelectionModel(new GridSelectionModel<Stock>());
                }
            }
        });
        type.addValueChangeHandler(new ValueChangeHandler<String>() {

            @Override
            public void onValueChange(ValueChangeEvent<String> event) {

            }
        });
        // toolBar.add(type);

        VerticalLayoutContainer con = new VerticalLayoutContainer();
        root.setWidget(con);
        // root.setHeaderVisible(false);

        con.add(toolBar, new VerticalLayoutData(1, -1));
        con.add(grid, new VerticalLayoutData(1, 1));
        // final PagingLoader<PagingLoadConfig, PagingLoadResult<Post>> loader =
        // new PagingLoader<PagingLoadConfig, PagingLoadResult<Post>>(
        // proxy);
        // loader.setRemoteSort(true);
        // loader.addLoadHandler(new
        // LoadResultListStoreBinding<PagingLoadConfig, Post,
        // PagingLoadResult<Post>>(store));

        final PagingToolBar pagingToolBar = new PagingToolBar(50);
        pagingToolBar.getElement().getStyle().setProperty("borderBottom", "none");
        // pagingToolBar.bind(loader);
        con.add(pagingToolBar, new VerticalLayoutData(1, -1));

        // needed to enable quicktips (qtitle for the heading and qtip for the
        // content) that are setup in the change GridCellRenderer
        new QuickTip(grid);

        return root;
    }

    private static Date randomDate() {
        DateWrapper w = new DateWrapper();
        int r = (int) (Math.random() * 10) * 10;
        w = w.addDays(-r);
        return w.asDate();
    }

    public List<Stock> getStocks() {
        List<Stock> stocks = new ArrayList<Stock>();

        stocks.add(new Stock("Apple Inc.", "AAPL", 125.64, 123.43, randomDate()));
        stocks.add(new Stock("Cisco Systems, Inc.", "CSCO", 25.84, 26.3, randomDate()));
        stocks.add(new Stock("Google Inc.", "GOOG", 516.2, 512.6, randomDate()));
        stocks.add(new Stock("Intel Corporation", "INTC", 21.36, 21.53, randomDate()));
        stocks.add(new Stock("Level 3 Communications, Inc.", "LVLT", 5.55, 5.54, randomDate()));
        stocks.add(new Stock("Microsoft Corporation", "MSFT", 29.56, 29.72, randomDate()));
        stocks.add(new Stock("Nokia Corporation (ADR)", "NOK", 27.83, 27.93, randomDate()));
        stocks.add(new Stock("Oracle Corporation", "ORCL", 18.73, 18.98, randomDate()));
        stocks.add(new Stock("Starbucks Corporation", "SBUX", 27.33, 27.36, randomDate()));
        stocks.add(new Stock("Yahoo! Inc.", "YHOO", 26.97, 27.29, randomDate()));
        stocks.add(new Stock("Applied Materials, Inc.", "AMAT", 18.4, 18.66, randomDate()));
        stocks.add(new Stock("Comcast Corporation", "CMCSA", 25.9, 26.4, randomDate()));
        stocks.add(new Stock("Sirius Satellite", "SIRI", 2.77, 2.74, randomDate()));
        stocks.add(new Stock("Tellabs, Inc.", "TLAB", 10.64, 10.75, randomDate()));
        stocks.add(new Stock("eBay Inc.", "EBAY", 30.43, 31.21, randomDate()));
        stocks.add(new Stock("Broadcom Corporation", "BRCM", 30.88, 30.48, randomDate()));
        stocks.add(new Stock("CMGI Inc.", "CMGI", 2.14, 2.13, randomDate()));
        stocks.add(new Stock("Amgen, Inc.", "AMGN", 56.22, 57.02, randomDate()));
        stocks.add(new Stock("Limelight Networks", "LLNW", 23, 22.11, randomDate()));
        stocks.add(new Stock("Amazon.com, Inc.", "AMZN", 72.47, 72.23, randomDate()));
        stocks.add(new Stock("E TRADE Financial Corporation", "ETFC", 24.32, 24.58, randomDate()));
        stocks.add(new Stock("AVANIR Pharmaceuticals", "AVNR", 3.7, 3.52, randomDate()));
        stocks.add(new Stock("Gemstar-TV Guide, Inc.", "GMST", 4.41, 4.55, randomDate()));
        stocks.add(new Stock("Akamai Technologies, Inc.", "AKAM", 43.08, 45.32, randomDate()));
        stocks.add(new Stock("Motorola, Inc.", "MOT", 17.74, 17.69, randomDate()));
        stocks.add(new Stock("Advanced Micro Devices, Inc.", "AMD", 13.77, 13.98, randomDate()));
        stocks.add(new Stock("General Electric Company", "GE", 36.8, 36.91, randomDate()));
        stocks.add(new Stock("Texas Instruments Incorporated", "TXN", 35.02, 35.7, randomDate()));
        stocks.add(new Stock("Qwest Communications", "Q", 9.9, 10.03, randomDate()));
        stocks.add(new Stock("Tyco International Ltd.", "TYC", 33.48, 33.26, randomDate()));
        stocks.add(new Stock("Pfizer Inc.", "PFE", 26.21, 26.19, randomDate()));
        stocks.add(new Stock("Time Warner Inc.", "TWX", 20.3, 20.45, randomDate()));
        stocks.add(new Stock("Sprint Nextel Corporation", "S", 21.85, 21.76, randomDate()));
        stocks.add(new Stock("Bank of America Corporation", "BAC", 49.92, 49.73, randomDate()));
        stocks.add(new Stock("Taiwan Semiconductor", "TSM", 10.4, 10.52, randomDate()));
        stocks.add(new Stock("AT&T Inc.", "T", 39.7, 39.66, randomDate()));
        stocks.add(new Stock("United States Steel Corporation", "X", 115.81, 114.62, randomDate()));
        stocks.add(new Stock("Exxon Mobil Corporation", "XOM", 81.77, 81.86, randomDate()));
        stocks.add(new Stock("Valero Energy Corporation", "VLO", 72.46, 72.6, randomDate()));
        stocks.add(new Stock("Micron Technology, Inc.", "MU", 12.02, 12.27, randomDate()));
        stocks.add(new Stock("Verizon Communications Inc.", "VZ", 42.5, 42.61, randomDate()));
        stocks.add(new Stock("Avaya Inc.", "AV", 16.96, 16.96, randomDate()));
        stocks.add(new Stock("The Home Depot, Inc.", "HD", 37.66, 37.79, randomDate()));
        stocks.add(new Stock("First Data Corporation", "FDC", 32.7, 32.65, randomDate()));
        return stocks;

    }

    public ToolBar createMulti() {
        ToolBar toolBar = new ToolBar();
        toolBar.setSpacing(2);

        ButtonGroup group = new ButtonGroup();
        group.setHeadingText("Clipboard");
        toolBar.add(group);

        FlexTable table = new FlexTable();
        group.add(table);

        TextButton btn = new TextButton("Cool", ExampleImages.INSTANCE.add16());
        table.setWidget(0, 0, btn);

        btn = new TextButton("Cut", ExampleImages.INSTANCE.add16());
        Menu menu = new Menu();
        menu.add(new MenuItem("Copy me"));
        btn.setMenu(menu);
        table.setWidget(0, 1, btn);

        btn = new TextButton("Copy", ExampleImages.INSTANCE.add16());
        table.setWidget(1, 0, btn);
        // //
        btn = new TextButton("Paste", ExampleImages.INSTANCE.add16());
        table.setWidget(1, 1, btn);

        group = new ButtonGroup();
        group.setHeadingText("Other Bogus Actions");
        toolBar.add(group);

        table = new FlexTable();
        group.add(table);

        btn = new TextButton("Cool", ExampleImages.INSTANCE.add16());
        table.setWidget(0, 0, btn);

        btn = new TextButton("Cut", ExampleImages.INSTANCE.add16());
        menu = new Menu();
        menu.add(new MenuItem("Copy me"));
        btn.setMenu(menu);
        table.setWidget(0, 1, btn);

        btn = new TextButton("Copy", ExampleImages.INSTANCE.add16());
        table.setWidget(1, 0, btn);
        // //
        btn = new TextButton("Paste", ExampleImages.INSTANCE.add16());
        table.setWidget(1, 1, btn);

        return toolBar;
    }

}