package pt.ist.renderers; import org.apache.commons.lang.StringUtils; import pt.ist.renderers.components.HtmlComponent; import pt.ist.renderers.components.HtmlMenu; import pt.ist.renderers.components.HtmlMenuOption; import pt.ist.renderers.components.converters.Converter; import pt.ist.renderers.layouts.Layout; import pt.ist.renderers.model.MetaSlotKey; import pt.ist.renderers.utils.RenderUtils; /** * This renderer provides an alternative way of doing the input of a boolean * value. This renderer presents an html menu with two options: one for the * true value, and other for the false value. * *

* The options text is retrieved from the resources using the keys TRUE * and FALSE. * *

* Example: * * @author cfgi */ public class BooleanMenuInputRenderer extends InputRenderer { private String nullOptionKey; private String bundle; public String getNullOptionKey() { return nullOptionKey; } public void setNullOptionKey(String nullOptionKey) { this.nullOptionKey = nullOptionKey; } public String getBundle() { return bundle; } public void setBundle(String bundle) { this.bundle = bundle; } @Override protected Layout getLayout(Object object, Class type) { return new Layout() { @Override public HtmlComponent createComponent(Object object, Class type) { HtmlMenu menu = new HtmlMenu(); if (getNullOptionKey() != null) { String defaultOptionTitle = RenderUtils.getResourceString(getBundle(), getNullOptionKey()); menu.createDefaultOption(defaultOptionTitle).setSelected(object == null); menu.setConverter(new Converter() { @Override public Object convert(Class type, Object value) { return StringUtils.isEmpty((String) value) ? null : Boolean.valueOf((String) value); } }); } HtmlMenuOption trueOption = menu.createOption(RenderUtils.getResourceString("TRUE")); HtmlMenuOption falseOption = menu.createOption(RenderUtils.getResourceString("FALSE")); trueOption.setValue("true"); falseOption.setValue("false"); if (object != null) { (((Boolean) object) ? trueOption : falseOption).setSelected(true); } menu.setTargetSlot((MetaSlotKey) getInputContext().getMetaObject().getKey()); return menu; } }; } }