package pt.ist.renderers; import pt.ist.renderers.components.HtmlComponent; import pt.ist.renderers.components.HtmlText; import pt.ist.renderers.layouts.Layout; import pt.ist.renderers.utils.RenderUtils; /** * The default output renderer for a boolean value. The value is used to search * for the corresponding message in the resources. The key TRUE and * FALSE are used to retrieve the messages for the true and * false values. * * @author cfgi */ public class BooleanRenderer extends OutputRenderer { private String trueLabel; private String falseLabel; private String nullLabel; private String bundle; public String getBundle() { return this.bundle; } /** * Chooses the label to be displayed when it is null * * @property */ public String getNullLabel() { return nullLabel; } public void setNullLabel(String nullLabel) { this.nullLabel = nullLabel; } /** * Chooses the bundle in wich the labels will be searched. * * @property */ public void setBundle(String bundle) { this.bundle = bundle; } public String getFalseLabel() { return this.falseLabel; } /** * The label to be used when presenting a false value. * * @property */ public void setFalseLabel(String falseLabel) { this.falseLabel = falseLabel; } public String getTrueLabel() { return this.trueLabel; } /** * The label to be used when presenting the true value. * * @property */ public void setTrueLabel(String trueLabel) { this.trueLabel = trueLabel; } @Override protected Layout getLayout(Object object, Class type) { return new Layout() { @Override public HtmlComponent createComponent(Object object, Class type) { Boolean booleanValue = (Boolean) object; if (booleanValue == null) { String nullLabel = getNullLabel(); return new HtmlText(nullLabel != null ? RenderUtils.getResourceString(getBundle(), nullLabel) : ""); } String booleanResourceKey = getBooleanLabel(booleanValue); return new HtmlText(RenderUtils.getResourceString(getBundle(), booleanResourceKey)); } private String getBooleanLabel(Boolean booleanValue) { String label = booleanValue ? getTrueLabel() : getFalseLabel(); if (label != null) { return label; } else { return booleanValue.toString().toUpperCase(); } } }; } }