package pt.ist.fenixWebFramework.renderers;
import pt.ist.fenixWebFramework.renderers.components.HtmlComponent;
import pt.ist.fenixWebFramework.renderers.components.HtmlLabel;
import pt.ist.fenixWebFramework.renderers.components.HtmlListItem;
import pt.ist.fenixWebFramework.renderers.components.HtmlRadioButton;
import pt.ist.fenixWebFramework.renderers.components.HtmlRadioButtonList;
import pt.ist.fenixWebFramework.renderers.components.HtmlText;
import pt.ist.fenixWebFramework.renderers.contexts.PresentationContext;
import pt.ist.fenixWebFramework.renderers.layouts.Layout;
import pt.ist.fenixWebFramework.renderers.model.MetaObject;
import pt.ist.fenixWebFramework.renderers.model.MetaObjectFactory;
import pt.ist.fenixWebFramework.renderers.model.MetaSlotKey;
import pt.ist.fenixWebFramework.renderers.utils.RenderKit;
import pt.ist.fenixWebFramework.renderers.utils.RenderMode;
import pt.ist.fenixWebFramework.renderers.utils.RenderUtils;
/**
* The BooleanRadioInputRender
provides a way of doing the
* input of an Boolean value by using a list of radio buttons. All the values
* of that Boolean are presented as a radio button and the use can choose
* one of the values.
*
*
* Example: *
false
value.
*
* @property
*/
public void setFalseLabel(String falseLabel) {
this.falseLabel = falseLabel;
}
public String getTrueLabel() {
return this.trueLabel;
}
/**
* Sets the key to use 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 booleanObject = (Boolean) object;
HtmlRadioButtonList radioList = new HtmlRadioButtonList();
Boolean booleanTrue = Boolean.TRUE;
MetaObject booleanMetaObject = MetaObjectFactory.createObject(booleanTrue, null);
PresentationContext newContext = getContext().createSubContext(booleanMetaObject);
newContext.setRenderMode(RenderMode.getMode("output"));
HtmlComponent component = getBooleanComponent(booleanTrue, newContext);
HtmlLabel trueLabel = new HtmlLabel();
trueLabel.setBody(component);
HtmlRadioButton buttonTrue = radioList.addOption(trueLabel, booleanTrue.toString());
trueLabel.setFor(buttonTrue);
Boolean booleanFalse = Boolean.FALSE;
booleanMetaObject = MetaObjectFactory.createObject(booleanFalse, null);
newContext = getContext().createSubContext(booleanMetaObject);
newContext.setRenderMode(RenderMode.getMode("output"));
component = getBooleanComponent(booleanFalse, newContext);
HtmlLabel falseLabel = new HtmlLabel();
falseLabel.setBody(component);
HtmlRadioButton buttonFalse = radioList.addOption(falseLabel, booleanFalse.toString());
falseLabel.setFor(buttonFalse);
buttonTrue.setChecked(booleanObject == null ? false : booleanObject);
buttonFalse.setChecked(booleanObject == null ? false : !booleanObject);
radioList.setTargetSlot((MetaSlotKey) getInputContext().getMetaObject().getKey());
for (HtmlListItem item : radioList.getList().getItems()) {
item.setClasses(getEachClasses());
item.setStyle(getEachStyle());
}
return radioList;
}
private HtmlComponent getBooleanComponent(Boolean value, PresentationContext newContext) {
if (value == null) {
return RenderKit.getInstance().render(newContext, value);
}
else {
if (value) {
if (getTrueLabel() == null) {
return RenderKit.getInstance().render(newContext, value);
}
else {
return new HtmlText(RenderUtils.getResourceString(getBundle(), getTrueLabel()));
}
}
else {
if (getFalseLabel() == null) {
return RenderKit.getInstance().render(newContext, value);
}
else {
return new HtmlText(RenderUtils.getResourceString(getBundle(), getFalseLabel()));
}
}
}
}
};
}
}