package pt.ist.renderers;
import pt.ist.renderers.components.HtmlComponent;
import pt.ist.renderers.components.HtmlLabel;
import pt.ist.renderers.components.HtmlListItem;
import pt.ist.renderers.components.HtmlRadioButton;
import pt.ist.renderers.components.HtmlRadioButtonList;
import pt.ist.renderers.components.HtmlText;
import pt.ist.renderers.contexts.PresentationContext;
import pt.ist.renderers.layouts.Layout;
import pt.ist.renderers.model.MetaObject;
import pt.ist.renderers.model.MetaObjectFactory;
import pt.ist.renderers.model.MetaSlotKey;
import pt.ist.renderers.utils.RenderKit;
import pt.ist.renderers.utils.RenderMode;
import pt.ist.renderers.utils.RenderUtils;
import pt.ist.renderers.utils.RendererPropertyUtils;
/**
* 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;
if (getReadOnlyIf() != null || getReadOnlyIfNot() != null) {
//let's get its parent to assert what to do regarding the read only
Object parentObject = null;
if (getContext().getParentContext() != null) {
parentObject = getContext().getParentContext().getMetaObject().getObject();
}
Boolean useReadOnlyIfResult = null;
Boolean useReadOnlyIfNotResult = null;
try {
if (getReadOnlyIf() != null) {
useReadOnlyIfResult = (Boolean) RendererPropertyUtils.getProperty(parentObject, getReadOnlyIf(),
false);
}
if (getReadOnlyIfNot() != null) {
useReadOnlyIfNotResult = (Boolean) RendererPropertyUtils.getProperty(parentObject,
getReadOnlyIfNot(), false);
}
}
catch (ClassCastException e) {
e.printStackTrace();
}
if ((useReadOnlyIfNotResult != null || useReadOnlyIfResult != null)) {
if ((useReadOnlyIfNotResult != null && !useReadOnlyIfNotResult)
|| (useReadOnlyIfResult != null && useReadOnlyIfResult)) {
if (booleanObject == null) {
return new HtmlText("");
}
return new HtmlText(String.valueOf(booleanObject));
}
}
}
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()));
}
}
}
}
};
}
}