Tree in a grid (Smart GWT) : Tree Table « GWT « Java






Tree in a grid (Smart GWT)

Tree in a grid (Smart GWT)
   

/*
 * SmartGWT (GWT for SmartClient)
 * Copyright 2008 and beyond, Isomorphic Software, Inc.
 *
 * SmartGWT is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3
 * as published by the Free Software Foundation.  SmartGWT is also
 * available under typical commercial license terms - see
 * http://smartclient.com/license
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 */
package com.smartgwt.sample.showcase.client;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.user.client.ui.RootPanel;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.data.fields.DataSourceDateField;
import com.smartgwt.client.data.fields.DataSourceFloatField;
import com.smartgwt.client.data.fields.DataSourceIntegerField;
import com.smartgwt.client.data.fields.DataSourceSequenceField;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.RecordSummaryFunctionType;
import com.smartgwt.client.types.SummaryFunctionType;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.grid.CellFormatter;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.grid.ListGridSummaryField;
import com.smartgwt.client.widgets.grid.SummaryFunction;

public class Showcase implements EntryPoint {
  public void onModuleLoad() {

    RootPanel.get().add(getViewPanel());
  }

  public Canvas getViewPanel() {
    DataSource dataSource = OrderItemLocalDS.getInstance();

    ListGridField orderIdField = new ListGridField("orderID");
    orderIdField.setIncludeInRecordSummary(false);
    orderIdField.setSummaryFunction(SummaryFunctionType.COUNT);

    ListGridField itemDescriptionField = new ListGridField("itemDescription");

    ListGridField categoryField = new ListGridField("category");
    categoryField.setShowGridSummary(true);
    categoryField.setSummaryFunction(new SummaryFunction() {
      public Object getSummaryValue(Record[] records, ListGridField field) {
        Set<String> uniqueCategories = new HashSet<String>();

        for (int i = 0; i < records.length; i++) {
          Record record = records[i];
          uniqueCategories.add(((OrderItem) record).getCategory());
        }
        return uniqueCategories.size() + " Categories";
      }
    });

    ListGridField shipDateField = new ListGridField("shipDate");
    shipDateField.setShowGroupSummary(true);
    shipDateField.setShowGridSummary(false);
    shipDateField.setSummaryFunction(SummaryFunctionType.MAX);

    ListGridField quantityField = new ListGridField("quantity");
    quantityField.setShowGroupSummary(false);
    quantityField.setShowGridSummary(false);

    ListGridField unitPriceField = new ListGridField("unitPrice");
    unitPriceField.setAlign(Alignment.RIGHT);
    unitPriceField.setCellFormatter(new CellFormatter() {
      public String format(Object value, ListGridRecord record, int rowNum, int colNum) {
        if (value == null)
          return null;
        try {
          NumberFormat nf = NumberFormat.getFormat("#,##0.00");
          return "$" + nf.format(((Number) value).doubleValue());
        } catch (Exception e) {
          return value.toString();
        }
      }
    });
    unitPriceField.setShowGroupSummary(false);
    unitPriceField.setShowGridSummary(false);

    ListGridSummaryField totalField = new ListGridSummaryField("total", "Total");
    totalField.setAlign(Alignment.RIGHT);
    totalField.setRecordSummaryFunction(RecordSummaryFunctionType.MULTIPLIER);
    totalField.setSummaryFunction(SummaryFunctionType.SUM);
    totalField.setShowGridSummary(true);
    totalField.setShowGroupSummary(true);
    totalField.setCellFormatter(new CellFormatter() {
      public String format(Object value, ListGridRecord record, int rowNum, int colNum) {
        if (value == null)
          return null;
        try {
          NumberFormat nf = NumberFormat.getFormat("#,##0.00");
          return "$" + nf.format(((Number) value).doubleValue());
        } catch (Exception e) {
          return value.toString();
        }
      }
    });

    final ListGrid listGrid = new ListGrid();

    listGrid.setWidth(600);
    listGrid.setHeight(224);
    listGrid.setAutoFetchData(true);
    listGrid.setAlternateRecordStyles(true);
    listGrid.setShowAllRecords(true);
    listGrid.setDataSource(dataSource);
    listGrid.setCanEdit(true);
    listGrid.setGroupByField("category");
    listGrid.setShowGridSummary(true);
    listGrid.setShowGroupSummary(true);

    listGrid.setFields(orderIdField, itemDescriptionField, categoryField, shipDateField,
        quantityField, unitPriceField, totalField);

    return listGrid;

  }

}

class OrderItem extends ListGridRecord {

  public OrderItem(int pk, int orderID, String itemDescription, String category, Date shipDate,
      int quantity, float unitPrice) {
    setPk(pk);
    setOrderID(orderID);
    setItemDescription(itemDescription);
    setCategory(category);
    setShipDate(shipDate);
    setQuantity(quantity);
    setUnitPrice(unitPrice);
  }

  public int getPk() {
    return getAttributeAsInt("pk");
  }

  public void setPk(int pk) {
    setAttribute("pk", pk);
  }

  public int getOrderID() {
    return getAttributeAsInt("orderID");
  }

  public void setOrderID(int orderID) {
    setAttribute("orderID", orderID);
  }

  public String getItemDescription() {
    return getAttribute("itemDescription");
  }

  public void setItemDescription(String itemDescription) {
    setAttribute("itemDescription", itemDescription);
  }

  public String getCategory() {
    return getAttribute("category");
  }

  public void setCategory(String category) {
    setAttribute("category", category);
  }

  public Date getShipDate() {
    return getAttributeAsDate("shipDate");
  }

  public void setShipDate(Date shipDate) {
    setAttribute("shipDate", shipDate);
  }

  public int getQuantity() {
    return getAttributeAsInt("quantity");
  }

  public void setQuantity(int quantity) {
    setAttribute("quantity", quantity);
  }

  public float getUnitPrice() {
    return getAttributeAsFloat("unitPrice");
  }

  public void setUnitPrice(float unitPrice) {
    setAttribute("unitPrice", unitPrice);
  }
}

class OrderItemLocalDS extends DataSource {

  private static OrderItemLocalDS instance = null;

  public static OrderItemLocalDS getInstance() {
    if (instance == null) {
      instance = new OrderItemLocalDS("orderItemLocalDS");
    }
    return instance;
  }

  public OrderItemLocalDS(String id) {

    setID(id);

    DataSourceSequenceField pkField = new DataSourceSequenceField("pk");
    pkField.setHidden(true);
    pkField.setPrimaryKey(true);

    DataSourceIntegerField orderIdField = new DataSourceIntegerField("orderID", "Order ID");
    orderIdField.setCanEdit(false);
    orderIdField.setPluralTitle("Orders");

    DataSourceTextField itemDescriptionField = new DataSourceTextField("itemDescription",
        "Description");
    DataSourceTextField categoryField = new DataSourceTextField("category", "Category");

    DataSourceDateField shipDateField = new DataSourceDateField("shipDate", "Ship Date");
    DataSourceIntegerField quantityField = new DataSourceIntegerField("quantity", "Quantity");
    DataSourceFloatField unitPriceField = new DataSourceFloatField("unitPrice", "Price");

    setFields(pkField, orderIdField, itemDescriptionField, categoryField, shipDateField,
        quantityField, unitPriceField);

    setTestData(OrderItemData.getRecords());
    setClientOnly(true);
  }
}
class OrderItemData {

  private static OrderItem[] records;

  public static OrderItem[] getRecords() {
      if (records == null) {
          records = getNewRecords();
      }
      return records;
  }

  public static OrderItem[] getNewRecords() {
      return new OrderItem[]{
              new OrderItem(0, 8805, "Anti Virus Suite", "Software", new Date(109, 10,20), 2, 50),
              new OrderItem(1, 4833, "USB Ergonomic Keyboard", "Hardware", new Date(109, 10,13), 1, 99.99f),
              new OrderItem(2, 9551, "Lumbar Support Cushion", "Hardware", new Date(109, 1, 2), 1, 75),
              new OrderItem(3, 954, "USB Keyboard", "Hardware", new Date(109, 10,20), 12, 34.99f),
              new OrderItem(4, 616, "Consulting Services", "Services", new Date(109, 10, 10), 40, 100),
              new OrderItem(5, 8357, "Office Application Suite", "Software", new Date(109, 2,5), 14, 30),
              new OrderItem(6, 4582, "Anti Virus Suite", "Software", new Date(109, 1,20), 22, 50),
              new OrderItem(7, 9373, "On-site Training", "Services", new Date(109, 10,20), 20, 100),
              new OrderItem(8, 9311, "Software Development Kit", "Software", new Date(109, 10,10), 8, 249.99f),
              new OrderItem(9, 7806, "15 inch flat panel monitor", "Hardware", new Date(109, 11,12), 4, 300),
              new OrderItem(10, 8620, "18 inch flat panel monitor", "Hardware", new Date(109, 10,20), 2, 50),
              new OrderItem(11, 629, "Office Productivity Suite", "Software", new Date(109, 11,11), 2, 64.99f),
              new OrderItem(12, 1264, "Office Productivity Suite", "Software", new Date(109, 12,20), 2, 64.99f),
              new OrderItem(13, 7100, "USB Ergonomic Keyboard", "Hardware", new Date(109, 10,20), 6, 99.99f),
              new OrderItem(14, 9647, "Consulting Services", "Services", new Date(109, 10,21), 10, 100),
              new OrderItem(15, 8468, "USB Optical Mouse", "Hardware", new Date(109, 10,10), 5, 29.99f),
              new OrderItem(16, 9496, "Off-site Training", "Services", new Date(109, 1,1), 35, 80),
              new OrderItem(17, 5124, "USB Stereo Headset", "Hardware", new Date(109, 10,20), 1, 69.99f),
              new OrderItem(18, 1799, "Office Application Suite", "Software", new Date(109, 11,2), 222, 30),
              new OrderItem(19, 7020, "Consulting Services", "Services", new Date(109, 10,20), 100, 80)
      };
  }
}

   
    
    
  








SmartGWT.zip( 9,880 k)

Related examples in the same category

1.A Tree Table
2.Tree, table and detail panel (Smart GWT)Tree, table and detail panel (Smart GWT)
3.Tree table with Frozen Columns Sample (Smart GWT)Tree table with Frozen Columns Sample (Smart GWT)
4.TreeGrid and TreeGridField (Smart GWT)TreeGrid and TreeGridField (Smart GWT)
5.Tree with Multiple Columns Sample (Smart GWT)Tree with Multiple Columns Sample (Smart GWT)
6.ColumnTree provides an alternate navigation paradigm for Tree data (Smart GWT)ColumnTree provides an alternate navigation paradigm for Tree data (Smart GWT)
7.Formatter interfaces allow you to add custom tree titles (Smart GWT)Formatter interfaces allow you to add custom tree titles (Smart GWT)
8.TreeTable editing (Ext GWT)TreeTable editing (Ext GWT)
9.Double click to edit a table tree (Ext GWT)Double click to edit a table tree (Ext GWT)
10.TreeGrid example (Ext GWT)TreeGrid example (Ext GWT)
11.TreeGrid with Row Editor (Ext GWT)TreeGrid with Row Editor (Ext GWT)
12.TreeGrid with Widget Example (Ext GWT)TreeGrid with Widget Example (Ext GWT)
13.Adding row number to TreeGrid (Ext GWT)Adding row number to TreeGrid (Ext GWT)