001 // Copyright 2004, 2005 The Apache Software Foundation 002 // 003 // Licensed under the Apache License, Version 2.0 (the "License"); 004 // you may not use this file except in compliance with the License. 005 // You may obtain a copy of the License at 006 // 007 // http://www.apache.org/licenses/LICENSE-2.0 008 // 009 // Unless required by applicable law or agreed to in writing, software 010 // distributed under the License is distributed on an "AS IS" BASIS, 011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 012 // See the License for the specific language governing permissions and 013 // limitations under the License. 014 015 package org.apache.tapestry.form; 016 017 import org.apache.hivemind.ApplicationRuntimeException; 018 import org.apache.tapestry.AbstractComponent; 019 import org.apache.tapestry.IMarkupWriter; 020 import org.apache.tapestry.IRequestCycle; 021 import org.apache.tapestry.Tapestry; 022 023 /** 024 * A component that renders an HTML <option> form element. Such a component must be wrapped 025 * (possibly indirectly) inside a {@link Select}component. [ <a 026 * href="../../../../../ComponentReference/Option.html">Component Reference </a>] 027 * 028 * @author Howard Lewis Ship 029 */ 030 031 public abstract class Option extends AbstractComponent 032 { 033 /** 034 * Renders the <option> element, or responds when the form containing the element is 035 * submitted (by checking {@link Form#isRewinding()}. 036 * <p> 037 * If the <code>label</code> property is set, it is inserted inside the <option> 038 * element. 039 */ 040 041 protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle) 042 { 043 Select select = Select.get(cycle); 044 if (select == null) 045 throw new ApplicationRuntimeException(Tapestry 046 .getMessage("Option.must-be-contained-by-select"), this, null, null); 047 048 // It isn't enough to know whether the cycle in general is rewinding, need to know 049 // specifically if the form which contains this component is rewinding. 050 051 boolean rewinding = select.isRewinding(); 052 053 String value = select.getNextOptionId(); 054 055 if (rewinding) 056 { 057 if (!select.isDisabled()) 058 setSelected(select.isSelected(value)); 059 060 renderBody(writer, cycle); 061 } 062 else 063 { 064 writer.begin("option"); 065 066 writer.attribute("value", value); 067 068 if (isSelected()) 069 writer.attribute("selected", "selected"); 070 071 renderInformalParameters(writer, cycle); 072 073 String label = getLabel(); 074 075 if (label != null) 076 writer.print(label); 077 078 renderBody(writer, cycle); 079 080 writer.end(); 081 } 082 083 } 084 085 public abstract String getLabel(); 086 087 public abstract boolean isSelected(); 088 089 public abstract void setSelected(boolean selected); 090 }