In Vaadin 7, is there an easy way to calculate the numeric total for selected columns? I know how to do it for Vaadin 8, defined here. But since Vaadin 7 uses a container, I am trying to think of the best way to do it. Currently, this is the best way I can think of, based on the documentation here. Code is a rough draft, so I expect there are some syntax problems. Treat it more as pseudo code, if possible.
Map<Object,Double> totals = new HashMap();
for (Iterator<?> i = container.getItemIds().iterator(); i.hasNext();) {
Object itemId = i.next();
Item item = container.getItem(itemId);
for(Object totalCol : totalColumns)
{
Object columnVal = item.getItemProperty(totalCol);
Double total = totals.get(totalCol);
if(!(total instanceof Double))
total = 0.0;
if(columnVal instanceof Double)
{
total += (Double)columnVal;
}
else if(columnVal instanceof Long)
{
total += (Long)columnVal;
}
else if(columnVal instanceof Integer)
{
total += (Integer)columnVal;
}
else if(columnVal instanceof String)
{
try {
Long value = Long.parseLong((String) columnVal);
total += value;
} catch (NumberFormatException e) {
try {
Double value = Double.parseDouble((String) columnVal);
total += value;
} catch (NumberFormatException e1) {
}
}
}
totals.put(totalCol, total);
}
/* At this point, go through totals Map, and set value to correct footer column with correct
* text formatting. This part is easy, and clearly documented, so leaving it off this
* code example.
*/
}
By the way, the above idea works, my question is more if this is the best idea or not?