Skip to content

Instantly share code, notes, and snippets.

@KrishnaKotari
Last active August 29, 2015 14:18
Show Gist options
  • Save KrishnaKotari/117a465a081f18eb5be2 to your computer and use it in GitHub Desktop.
Save KrishnaKotari/117a465a081f18eb5be2 to your computer and use it in GitHub Desktop.
All Code related to Part 2 of my series on Vaadin Grid
/**
* Sub Class
* @author Krishna
*
*/
public class YearlySales implements Serializable {
/**
*
*/
private static final long serialVersionUID = -893627659352850558L;
private Integer q1;
private Integer q2;
private Integer q3;
private Integer q4;
//Getter and Setters
}
/**
*
* @author Krishna
*
*/
public class VehicleInfo implements Serializable {
/**
*
*/
private static final long serialVersionUID = -5412465534652505031L;
/**
* Various Vehicle Manufactures in India
* * @author Krishna
*
*/
public enum Manufacturer {
Maruti, Honda, Hero, Hyundai, TataMotors, Bajaj
}
public enum VehicleCategory {
TwoWheeler, FourWheeler;
}
private Integer id;
private String modelName;
private Manufacturer manufacturer;
private YearlySales sales2013;
private YearlySales sales2014;
private YearlySales sales2012;
private VehicleCategory category;
//Getter and Setters
}
Grid grid = new Grid();
BeanItemContainer<Vehicleinfo> container = new BeanItemContainer<Vehicleinfo>(VehicleInfo.class);
//Generates Random Data
container.addAll(DatasourceFactory.getVehicleSalesDS());
//Add nested properties to the header
container.addNestedContainerBean("sales2012");
container.addNestedContainerBean("sales2013");
container.addNestedContainerBean("sales2014");
grid.setSelectionMode(SelectionMode.SINGLE);
grid.removeColumn("id");
grid.setImmediate(true);
//This call prepends the header row to the existing grid
HeaderRow mainHeaderRow = grid.prependHeaderRow();
//Get hold of the columnID, mind you in my case this is a nestedID
HeaderCell sales2012Q1 = mainHeaderRow.getCell("sales2012.q1");
HeaderCell sales2012Q2 = mainHeaderRow.getCell("sales2012.q2");
HeaderCell sales2012Q3 = mainHeaderRow.getCell("sales2012.q3");
HeaderCell sales2012Q4 = mainHeaderRow.getCell("sales2012.q4");
//Now join all of these cells to form a logical block
HeaderCell mainHeaderCell = mainHeaderRow.join(sales2012Q1, sales2012Q2,sales2012Q3, sales2012Q4);
//Set caption for this logical block
mainHeaderCell.setText("2012");
//Get the container datasource
BeanItemContainer&lt;Vehicleinfo&gt; container = (BeanItemContainer&lt; Vehicleinfo&gt;) grid.getContainerDataSource();
// String to Long Converter
Converter&lt;String,long&gt; convertor = new StringToLongConverter();
//Iterating over years
for (int i = 2012; i &lt; 2015; i++) {
//Join all the columns associated with an year for secondary footer
FooterCell yearlySalesCell = secondaryFooter.join("sales" + i + ".q1", "sales" + i + ".q2", "sales" + i + ".q3", "sales" + i + ".q4");
long yearlySalesValue = 0l;
// Iterating over all items and calculating values over each quarter
for (int quarter = 1; quarter &lt;= 4; quarter++) {
long quarterSales = 0l;
final String columnName = "sales" + i + ".q" + quarter;
FooterCell sumOfQuarterlySales = mainFooter.getCell(columnName);
// Iterate over each Item for a quarter and calculate total no of vehicle sales per quarter
for (Object itemId : container.getItemIds()) {
Item item = container.getItem(itemId);
Integer q1Value = (Integer) item.getItemProperty(columnName).getValue();
quarterSales += Long.valueOf(q1Value);
}
//Sum of quarterly sales of all vehicles is being set as text for primary footer
sumOfQuarterlySales.setText(convertor.convertToPresentation(quarterSales, String.class, grid.getLocale()));
yearlySalesValue += quarterSales;
}
//Sum of Yearly sales of all vehicles is being set as text for secondary footer
yearlySalesCell.setText(convertor.convertToPresentation(yearlySalesValue, String.class, grid.getLocale()));
}
// Use setHtml to customize your headers
yearlySalesCell.setHtml("<b>"+ convertor.convertToPresentation(yearlySalesValue,String.class, grid.getLocale()) + " <b>");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment