package org.zkforge.converters;
/**
* @author Robert Pichelhofer
*
* Databindingconverter
* Direction: Output only
*
* Source: Numbers (cast to BigDecimal required)
* Destination: Label, Listcell
*
* Format the number and change the style to "red color" for negative values
*
* Examples:
* <listcell label="@{person.account, converter='org.zkforge.converters.ColorNumberCustom'}">
* <custom-attributes format="###,##0.00"/></listcell>
*
*
* TODO: add more examples
* TODO: check other components (i.e. treecell)
* TODO: find a way to keep the initial style, just add the color
* TODO: create final values for styles
* TODO: checkout the need of local variables (style, format)
*/
import java.math.BigDecimal;
import java.text.DecimalFormat;
import org.zkoss.zk.ui.Component;
import org.zkoss.zkplus.databind.TypeConverter;
import org.zkoss.zul.Decimalbox;
import org.zkoss.zul.Label;
import org.zkoss.zul.Listcell;
public class ColorNumberCustom implements TypeConverter {
final BigDecimal zero;
public ColorNumberCustom() {
zero = new BigDecimal(0);
}
public Object coerceToBean(Object arg0, Component arg1) {
// this is an output converter, no action here
return null;
}
public Object coerceToUi(Object arg0, Component arg1) {
if (arg0 == null) {
return null;
}
String format = (String) arg1.getAttribute("format");
if (format == null) format = "###,##0.00"; // default
DecimalFormat df = new DecimalFormat( format );
BigDecimal number = (BigDecimal) arg0;
String style;
if (number.compareTo(zero) == -1) {
style = "color: #ff0000; text-align: right";
} else {
style = "text-align: right";
}
if (arg1 instanceof Listcell) {
Listcell cell = (Listcell) arg1;
cell.setStyle(style);
} else if (arg1 instanceof Label) {
Label label = (Label) arg1;
label.setStyle(style);
}
return df.format(number);
}
}
|