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.tapestry.IMarkupWriter;
018    import org.apache.tapestry.IRequestCycle;
019    import org.apache.tapestry.valid.ValidatorException;
020    
021    /**
022     * Implements a component that manages an HTML &lt;input type=checkbox&gt; form element. [ <a
023     * href="../../../../../ComponentReference/Checkbox.html">Component Reference </a>]
024     * <p>
025     * As of 4.0, this component can be validated.
026     * 
027     * @author Howard Lewis Ship
028     * @author Paul Ferraro
029     */
030    public abstract class Checkbox extends AbstractFormComponent implements ValidatableField
031    {
032        /**
033         * @see org.apache.tapestry.form.validator.AbstractRequirableField#renderRequirableFormComponent(org.apache.tapestry.IMarkupWriter,
034         *      org.apache.tapestry.IRequestCycle)
035         */
036        protected void renderFormComponent(IMarkupWriter writer, IRequestCycle cycle)
037        {
038            renderDelegatePrefix(writer, cycle);
039            
040            writer.beginEmpty("input");
041            writer.attribute("type", "checkbox");
042    
043            writer.attribute("name", getName());
044    
045            if (isDisabled())
046                writer.attribute("disabled", "disabled");
047    
048            if (getValue())
049                writer.attribute("checked", "checked");
050    
051            renderIdAttribute(writer, cycle);
052    
053            getValidatableFieldSupport().renderContributions(this, writer, cycle);
054            
055            renderInformalParameters(writer, cycle);
056    
057            writer.closeTag();
058            
059            renderDelegateSuffix(writer, cycle);
060        }
061    
062        /**
063         * In traditional HTML, many checkboxes would have the same name but different values. Under
064         * Tapestry, it makes more sense to have different names and a fixed value. For a checkbox, we
065         * only care about whether the name appears as a request parameter.
066         */
067        protected void rewindFormComponent(IMarkupWriter writer, IRequestCycle cycle)
068        {
069            String value = cycle.getParameter(getName());
070            
071            try
072            {
073                // This is atypical validation - since this component does not explicitly bind to an object
074                getValidatableFieldSupport().validate(this, writer, cycle, value);
075    
076                setValue(value != null);
077            }
078            catch (ValidatorException e)
079            {
080                getForm().getDelegate().record(e);
081            }
082        }
083    
084        public abstract boolean getValue();
085    
086        public abstract void setValue(boolean selected);
087    
088        /**
089         * Injected.
090         */
091        public abstract ValidatableFieldSupport getValidatableFieldSupport();
092    
093        /**
094         * @see org.apache.tapestry.form.AbstractFormComponent#isRequired()
095         */
096        public boolean isRequired()
097        {
098            return getValidatableFieldSupport().isRequired(this);
099        }
100    }