package net.sourceforge.fenixedu.presentationTier.renderers; import java.util.ArrayList; import java.util.Collection; import java.util.List; import org.apache.commons.beanutils.PropertyUtils; import org.apache.commons.lang.StringUtils; import pt.iscte.ci.metadata.ISCTE; import pt.ist.fenixWebFramework.renderers.components.HtmlCheckBox; import pt.ist.fenixWebFramework.renderers.components.HtmlComponent; import pt.ist.fenixWebFramework.renderers.components.HtmlMultipleHiddenField; import pt.ist.fenixWebFramework.renderers.components.HtmlText; import pt.ist.fenixWebFramework.renderers.layouts.Layout; import pt.ist.fenixWebFramework.renderers.model.MetaObject; import pt.ist.fenixWebFramework.renderers.model.MetaObjectCollection; import pt.ist.fenixWebFramework.renderers.model.MetaObjectFactory; import pt.ist.fenixWebFramework.renderers.model.MetaSlot; import pt.ist.fenixWebFramework.renderers.schemas.Schema; import pt.ist.fenixWebFramework.renderers.utils.RenderUtils; /** * This renderer allows you choose several object from a list of choices. The * list of choices is presented in a table but each row has a checkbox that * allows you to select the object in that row. * *

* The list of options is given by a * {@link pt.ist.fenixWebFramework.renderers.DataProvider data provider}. * *

* Example: * * * * * * * * * * * * * * * * * * * * * * * * * *
NameAgeGender
Name A20Female
Name B22Male
Name C21Female
* * @author pcma */ public class TabularOptionInputRenderer extends pt.ist.fenixWebFramework.renderers.TabularOptionInputRenderer { private String optionHeaderKey; private String optionHeaderBundle; private String checkableIf; public String getOptionHeaderKey() { return optionHeaderKey; } public void setOptionHeaderKey(String optionHeaderKey) { this.optionHeaderKey = optionHeaderKey; } public String getOptionHeaderBundle() { return optionHeaderBundle; } public void setOptionHeaderBundle(String optionHeaderBundle) { this.optionHeaderBundle = optionHeaderBundle; } public String getCheckableIf() { return checkableIf; } public void setCheckableIf(String checkableIf) { this.checkableIf = checkableIf; } @Override protected Layout getLayout(Object object, Class type) { List metaObjects = (List) getMetaObjects(getPossibleObjects()); Collection objectsReceived = (Collection) object; HtmlMultipleHiddenField hiddenField = new HtmlMultipleHiddenField(); hiddenField.bind((MetaSlot) getContext().getMetaObject()); hiddenField.setConverter(getConverter()); return new CheckableTabularLayout(metaObjects, objectsReceived, hiddenField); } @ISCTE(comment = "unnecessary when super class method visibilty changed to protected") private List getMetaObjects(Collection collection) { List metaObjects = new ArrayList(); MetaObject contextMetaObject = getContext().getMetaObject(); if (contextMetaObject instanceof MetaObjectCollection) { // reuse meta objects MetaObjectCollection multipleMetaObject = (MetaObjectCollection) getContext().getMetaObject(); for (Object object : collection) { for (MetaObject metaObject : multipleMetaObject.getAllMetaObjects()) { if (object.equals(metaObject.getObject())) { metaObjects.add(metaObject); break; } } } } else { Schema schema = getContext().getSchema(); for (Object object : collection) { metaObjects.add(MetaObjectFactory.createObject(object, schema)); } } return metaObjects; } public class CheckableTabularLayout extends pt.ist.fenixWebFramework.renderers.TabularOptionInputRenderer.CheckableTabularLayout { public CheckableTabularLayout(List metaObjects, Collection collection, HtmlMultipleHiddenField hiddenField) { super(metaObjects, collection, hiddenField); } @Override protected HtmlComponent getHeaderComponent(int columnIndex) { return (StringUtils.isNotEmpty(getOptionHeaderKey()) && columnIndex == 0) ? new HtmlText( RenderUtils.getResourceString(getOptionHeaderBundle(), getOptionHeaderKey())) : super .getHeaderComponent(columnIndex); } @Override protected HtmlComponent getComponent(int rowIndex, int columnIndex) { if (columnIndex == 0) { MetaObject metaObject = metaObjects.get(rowIndex); if (getCheckableIf() != null) { try { boolean negation = getCheckableIf().startsWith("!"); Object object = PropertyUtils.getProperty(metaObject.getObject(), negation ? getCheckableIf() .substring(1) : getCheckableIf()); if (object != null && ((Boolean) object).equals(negation)) { return new HtmlText(); } } catch (Exception e) { e.printStackTrace(); } } HtmlCheckBox checkBox = new HtmlCheckBox(); this.checkboxes.add(checkBox); checkBox.setUserValue(metaObject.getKey().toString()); checkBox.setName(hiddenField.getName() + "/" + metaObject.getKey().toString()); if (objectsReceived != null && objectsReceived.contains(metaObject.getObject())) { checkBox.setChecked(true); } return checkBox; } else { MetaSlot slot = getSlotUsingName(metaObjects.get(rowIndex), columnIndex - 1); slot.setReadOnly(true); return renderSlot(slot); } } }; }