package pt.ist.renderers; import java.util.List; import pt.ist.renderers.components.HtmlCheckBox; import pt.ist.renderers.components.HtmlComponent; import pt.ist.renderers.components.HtmlHiddenField; import pt.ist.renderers.components.HtmlInlineContainer; import pt.ist.renderers.components.HtmlSimpleValueComponent; import pt.ist.renderers.components.controllers.HtmlController; import pt.ist.renderers.components.state.IViewState; import pt.ist.renderers.components.state.ViewDestination; import pt.ist.renderers.layouts.Layout; /** * 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.renderers.DataProvider data provider}. * *

* Example: * * * * * * * * * * * * * * * * * * * * * * * * * *
NameAgeGender
Name A20Female
Name B22Male
Name C21Female
* * @author pcma */ public class TabularOptionInputRendererWithPostBack extends TabularOptionInputRenderer { private String destination; public String getDestination() { return destination; } public void setDestination(String destination) { this.destination = destination; } @Override protected HtmlComponent renderComponent(Layout layout, Object object, Class type) { HtmlComponent component = super.renderComponent(layout, object, type); CheckableTabularLayout checkableLayout = (CheckableTabularLayout) layout; List checkboxes = checkableLayout.getCheckBoxes(); for (HtmlCheckBox checkbox : checkboxes) { checkbox.setOnClick("this.form.postback.value=1; this.form.submit();"); } HtmlInlineContainer htmlInlineContainer = new HtmlInlineContainer(); HtmlHiddenField hiddenField = new HtmlHiddenField(); hiddenField.setName("postback"); htmlInlineContainer.addChild(hiddenField); htmlInlineContainer.addChild(component); hiddenField.setController(new PostBackController(getDestination())); return htmlInlineContainer; } private static class PostBackController extends HtmlController { private final String destination; public PostBackController(String destination) { this.destination = destination; } @Override public void execute(IViewState viewState) { HtmlSimpleValueComponent component = (HtmlSimpleValueComponent) getControlledComponent(); if (component.getValue() != null && component.getValue().length() > 0) { String destinationName = this.destination == null ? "postback" : this.destination; ViewDestination destination = viewState.getDestination(destinationName); if (destination != null) { viewState.setCurrentDestination(destination); } else { viewState.setCurrentDestination("postBack"); } viewState.setSkipValidation(true); } component.setValue(null); } } }