123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
package net.sf.tapestry.form; import net.sf.tapestry.AbstractComponent; import net.sf.tapestry.IBinding; import net.sf.tapestry.IMarkupWriter; import net.sf.tapestry.IRequestCycle; import net.sf.tapestry.RequestCycleException; import net.sf.tapestry.RequiredParameterException; import net.sf.tapestry.Tapestry; /** * A component that renders an HTML <option> form element. * Such a component must be wrapped (possibly indirectly) * inside a {@link Select} component. * * [<a href="../../../../../ComponentReference/Option.html">Component Reference</a>] * * @author Howard Lewis Ship * @version $Id: Option.java,v 1.9 2002/11/27 17:58:47 hship Exp $ * **/ public class Option extends AbstractComponent { private IBinding _selectedBinding; private String _label; public IBinding getSelectedBinding() { return _selectedBinding; } /** * Renders the <option> element, or responds when the form containing the element * is submitted (by checking {@link Form#isRewinding()}. * * <table border=1> * <tr> <th>attribute</th> <th>value</th> </tr> * <tr> <td>value</td> <td>from {@link Select#getNextOptionId()}</td> </tr> * <tr> <td>selected</td> <td>from selected property</td> </tr> * <tr> <td><i>other</i></td> <td>from extra bindings</td> </tr> * </tr> * </table> * * <p>If the <code>label</code> property is set, it is inserted after the * <option> tag. * **/ protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle) throws RequestCycleException { String value; Select select; boolean rewinding; select = Select.get(cycle); if (select == null) throw new RequestCycleException(Tapestry.getString("Option.must-be-contained-by-select"), this); // It isn't enough to know whether the cycle in general is rewinding, need to know // specifically if the form which contains this component is rewinding. rewinding = select.isRewinding(); value = select.getNextOptionId(); if (rewinding) { if (!select.isDisabled()) _selectedBinding.setBoolean(select.isSelected(value)); } else { writer.beginEmpty("option"); writer.attribute("value", value); if (_selectedBinding.getBoolean()) writer.attribute("selected"); generateAttributes(writer, cycle); if (_label != null) writer.print(_label); writer.println(); } } public void setSelectedBinding(IBinding value) { _selectedBinding = value; } public String getLabel() { return _label; } public void setLabel(String label) { this._label = label; } }