Hi everyone, I have some troubles with the column generators, actually I think they are explained in a little understandable way for a Vaadin newbie. Here is the code I wrote:
// Generated Table columns
Table tableWithGeneratedCol = new Table();
tableWithGeneratedCol.addContainerProperty(
"date", Date.class, null, "Date", null, null);
tableWithGeneratedCol.addContainerProperty(
"quantity", Double.class, null, "Quantity (l)", null, null);
tableWithGeneratedCol.addContainerProperty(
"price", Double.class, null, "Price (e/l)", null, null);
tableWithGeneratedCol.addContainerProperty(
"total", Double.class, null, "Total (e)", null, null);
// Some rows as example
tableWithGeneratedCol.addGeneratedColumn("date", new DateColumnGenerator());
tableWithGeneratedCol.addGeneratedColumn("quantity", new ValueColumnGenerator("%.2f l"));
tableWithGeneratedCol.addGeneratedColumn("price", new PriceColumnGeneretor());
tableWithGeneratedCol.addGeneratedColumn("total",new TotalColumnGenerator("%.2f e", "quantity", "price"));
tableWithGeneratedCol.addItem(new Object[] { new GregorianCalendar().getTime(), // date column
new Double(10), // quantity column
new Double(10), // price column
// nothing here // total column
}, 1); // itemId
tableWithGeneratedCol.addItem(new Object[] { new GregorianCalendar().getTime(),
new Double(16.2), // quantity column
new Double(21.2), // price column
// nothing here // total column
}, 2); // itemId
tableWithGeneratedCol.addItem(new Object[] { new GregorianCalendar().getTime(),
new Double(10), // quantity column
new Double(22), // price column
// nothing here // total column
}, 3); // itemId
tableWithGeneratedCol.addItem(new Object[] { new GregorianCalendar().getTime(),
new Double(10), // quantity column
new Double(20), // price column
// nothing here // total column
}, 4); // itemId
tableWithGeneratedCol.addItem(new Object[] { new GregorianCalendar().getTime(),
new Double(15), // quantity column
new Double(19.12), // price column
// nothing here // total column
}, 5); // itemId
tableWithGeneratedCol.addItem(new Object[] { new GregorianCalendar().getTime(),
new Double(10), // quantity column
new Double(20.30), // price column
// nothing here // total column
}, 6); // itemId
tableWithGeneratedCol.addItem(new Object[] { new GregorianCalendar().getTime(),
new Double(50), // quantity column
new Double(32.89), // price column
// nothing here // total column
}, 7); // itemId
tableWithGeneratedCol.setVisibleColumns(new Object[] {"date", "quantity", "price", "total"});
tableWithGeneratedCol.setPageLength(tableWithGeneratedCol.size());
layout.addComponent(tableWithGeneratedCol);
This code is placed inside the UI’s init() method. Now the column generators:
DateColumnGenerator:
public class DateColumnGenerator implements ColumnGenerator {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public Component generateCell(Table source, Object itemId, Object columnId) {
System.out.println("TIMEZONE : " + TimeZone.getDefault().getID());
//Date date = new Date();
Property<?> prop = source.getItem(itemId).getItemProperty(columnId);
if (prop.getType().equals(Date.class)) {
Date date = (Date) prop.getValue();
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd, yyyy, HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("Europe/Moscow"));
return new Label(sdf.format(date));
}
return null;
}
}
ValueColumnGenerator:
/** Formats the value in a column containing Double objects. */
class ValueColumnGenerator implements Table.ColumnGenerator {
/**
*
*/
private static final long serialVersionUID = 1L;
String format; /* Format string for the Double values. */
/**
* Creates double value column formatter with the given
* format string.
*/
public ValueColumnGenerator(String format) {
this.format = format;
}
/**
* Generates the cell containing the Double value.
* The column is irrelevant in this use case.
*/
public Component generateCell(Table source, Object itemId,
Object columnId) {
// Get the object stored in the cell as a property
Property<?> prop = source.getItem(itemId).getItemProperty(columnId);
if (prop.getType().equals(Double.class)) {
Label label = new Label(String.format(format, new Object[] { (Double) prop.getValue() }));
// Set styles for the column: one indicating that it's
// a value and a more specific one with the column
// name in it. This assumes that the column name
// is proper for CSS.
label.addStyleName("column-type-value");
label.addStyleName("column-" + (String) columnId);
return label;
}
return null;
}
}
PriceColumnGenerator:
public class PriceColumnGeneretor implements ColumnGenerator {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public Component generateCell(Table source, Object itemId, Object columnId) {
Property<?> prop = source.getItem(itemId).getItemProperty(columnId);
if (prop.getClass().equals(Double.class)) {
Double price = (Double) prop.getValue();
String priceStr = String.format("%.2 €", price);
return new Label(priceStr);
}
return null;
}
}
TotalColumnGenerator:
public class TotalColumnGenerator implements Table.ColumnGenerator {
/**
*
*/
private static final long serialVersionUID = 1L;
protected String format;
protected String quantityId;
protected String priceId;
public TotalColumnGenerator(String format, String quantityId, String priceId) {
this.format = format;
this.quantityId = quantityId;
this.priceId = priceId;
}
@Override
public Component generateCell(Table source, Object itemId, Object columnId) {
Double quantity = (Double) source.getItem(itemId).getItemProperty(this.quantityId).getValue();
Integer price = (Integer) source.getItem(itemId).getItemProperty(this.priceId).getValue();
String res = String.format(this.format, new Double(quantity * price));
return new Label(res);
}
}
But my problem is that the resulting table is empty, i.e. it has no rows… Why does this happen? What should I do in order to implement properly the column generators inside a Table component? I am asking this all because in Book of Vaadin which I am reading nothing is said about this, only snippets of code are given, but not how column generators should work as a whole…
So how can I actually make this code work properly?
Thank you for the attention!