Last active
August 29, 2015 14:18
-
-
Save KrishnaKotari/117a465a081f18eb5be2 to your computer and use it in GitHub Desktop.
All Code related to Part 2 of my series on Vaadin Grid
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
grid.setSelectionMode(SelectionMode.SINGLE); | |
grid.removeColumn("id"); | |
grid.setImmediate(true); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Get the container datasource | |
BeanItemContainer<Vehicleinfo> container = (BeanItemContainer< Vehicleinfo>) grid.getContainerDataSource(); | |
// String to Long Converter | |
Converter<String,long> convertor = new StringToLongConverter(); | |
//Iterating over years | |
for (int i = 2012; i < 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 <= 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())); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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