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     *  Implements a component that manages an HTML <input type=radio> form element.
025     *  Such a component must be wrapped (possibly indirectly)
026     *  inside a {@link RadioGroup} component.
027     *
028     *  [<a href="../../../../../ComponentReference/Radio.html">Component Reference</a>]
029     *
030     * 
031     *  <p>{@link Radio} and {@link RadioGroup} are generally not used (except
032     *  for very special cases).  Instead, a {@link PropertySelection} component is used.
033     *
034     *
035     *  @author Howard Lewis Ship
036     * 
037     **/
038    
039    public abstract class Radio extends AbstractComponent
040    {
041        /**
042         *  Renders the form element, or responds when the form containing the element
043         *  is submitted (by checking {@link Form#isRewinding()}.
044         *
045         *
046         **/
047    
048        protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
049        {
050    
051            RadioGroup group = RadioGroup.get(cycle);
052            if (group == null)
053                throw new ApplicationRuntimeException(
054                    Tapestry.getMessage("Radio.must-be-contained-by-group"),
055                    this,
056                    null,
057                    null);
058    
059            // The group determines rewinding from the form.
060    
061            boolean rewinding = group.isRewinding();
062    
063            int option = group.getNextOptionId();
064    
065            if (rewinding)
066            {
067                // If not disabled and this is the selected button within the radio group,
068                // then update set the selection from the group to the value for this
069                // radio button.  This will update the selected parameter of the RadioGroup.
070    
071                if (!isDisabled() && !group.isDisabled() && group.isSelected(option))
072                    group.updateSelection(getValue());
073                return;
074            }
075    
076            writer.beginEmpty("input");
077    
078            writer.attribute("type", "radio");
079    
080            writer.attribute("name", group.getName());
081    
082            // As the group if the value for this Radio matches the selection
083            // for the group as a whole; if so this is the default radio and is checked.
084    
085            if (group.isSelection(getValue()))
086                writer.attribute("checked", "checked");
087    
088            if (isDisabled() || group.isDisabled())
089                writer.attribute("disabled", "disabled");
090    
091            // The value for the Radio matches the option number (provided by the RadioGroup).
092            // When the form is submitted, the RadioGroup will know which option was,
093            // in fact, selected by the user.
094    
095            writer.attribute("value", option);
096    
097            renderInformalParameters(writer, cycle);
098    
099        }
100    
101        public abstract boolean isDisabled();
102    
103        public abstract Object getValue();
104    }