Typically, column generators create components, not values - so you’d normally need to cast to the appropriate component and call #getValue(); look at Table#parseItemIdToCells (private) to look at what precisely goes on.
(But in summary - if the value generated is a Component, you’ll need to cast it, otherwise Table does String.valueOf(generator.generateCell(blah))
In short, you need to know what the generator returns to process it appropriately.
HTH,
Cheers,
Charles.
public String getStyle(Table source, Object itemId, Object propertyId) {
if (propertyId == "amount") {
//extract the value generated in column generator. HOW?
//source.getItem(itemId).getItemProperty(BALANCE).getValue(); //this gives a NPE
Object value = source.getColumnGenerator(itemId).generateCell(source, itemId,columnId);
return value >= 0 ? "green" : "red";
}
}
So maybe I’m doing at wrong. I use ColumnGenerators to generate columns with values that are calculated depending on other cells, but are not stored anywhere. Is this wrong using ColumnGenerators?
So I just create Strings within the Generators, and these values I want to work with on selection.
By the way: your suggestion does also throw NPE:
Object value = source.getColumnGenerator(itemId).generateCell(source, itemId, propertyId);
[quote=member sound]
So I just create Strings within the Generators, and these values I want to work with on selection.
[/quote] Well, it’s one approach. Personally, If I’m wrapping a bean, I’d stick a readOnly property (i.e. a getTotalAmount() method) on the bean, and use it like any other item property. Calculating an amount seems more related to the bean rather than the table, and so (for me) sits more naturally on the bean.
If that’s not possible, you could add a your own property to the BeanItem (See pseudo code at end).
However, the table does support generating simple values - and so your approach works too; it just depends on personal preference. Working on large scale business apps, I have natural inclination to try and keep presentation and business logic separate!
NPE: I have assumed that there was a cell generator for “amount”, and that it returned a non-null value - not very belts-and-braces. There was a typo, though : it should have been
Object value = source.getColumnGenerator(propertyId).generateCell(source, itemId, propertyId)
i.e. getColumnGenerator(
propertyId ) - I was typing into notepad, not an IDE; Sorry!
Cheers,
Charles.
(PseudoCode - again, in notepad so probably typos! - for adding a dynamically calculated property to a BeanItem)
InvoiceLine invoiceLine = ...
BeanItem <InvoiceLine> beanItem = new BeanItem<InvoiceLine>(invoiceLine);
beanItem.addItemProperty("totalAmount", new AbstractProperty() {
@Override
public Object getValue() {
int itemCount = new BigDecimal(bean.getItemCount());
BigDecimal itemAmount = bean.getItemCount();
BigDecimal taxAmount = bean.getTaxAmount();
return itemAmount.multiply(new BigDecimal(itemCount)).add(taxAmount);
}
@Override
public void setValue(Object newValue) throws ReadOnlyException {
throw new ReadOnlyException();
}
@Override
public Class getType() {
return BigDecimal.class;
}
});