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.valid;
016    
017    import org.apache.tapestry.AbstractComponent;
018    import org.apache.tapestry.BindingException;
019    import org.apache.tapestry.IForm;
020    import org.apache.tapestry.IMarkupWriter;
021    import org.apache.tapestry.IRequestCycle;
022    import org.apache.tapestry.Tapestry;
023    import org.apache.tapestry.TapestryUtils;
024    import org.apache.tapestry.form.IFormComponent;
025    
026    /**
027     * Used to label an {@link IFormComponent}. Because such fields know their displayName
028     * (user-presentable name), there's no reason to hard code the label in a page's HTML template (this
029     * also helps with localization). [ <a
030     * href="../../../../../ComponentReference/FieldLabel.html">Component Reference </a>]
031     * 
032     * @author Howard Lewis Lewis Ship
033     */
034    
035    public abstract class FieldLabel extends AbstractComponent
036    {
037        // Parameter
038        public abstract boolean isPrerender();
039    
040        /**
041         * Gets the {@link IForm} and {@link IValidationDelegate delegate}, then renders the
042         * label obtained from the field. Does nothing when rewinding.
043         */
044    
045        protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
046        {
047            IForm form = TapestryUtils.getForm(cycle, this);
048            
049            IFormComponent field = getField();
050            
051            if (field != null && isPrerender())
052                form.prerenderField(writer, field, getLocation());
053    
054            if (cycle.isRewinding())
055                return;
056    
057            String displayName = getDisplayName();
058    
059            if (displayName == null)
060            {
061                if (field == null)
062                    throw Tapestry.createRequiredParameterException(this, "field");
063    
064                displayName = field.getDisplayName();
065    
066                if (displayName == null)
067                    throw new BindingException(ValidMessages.noDisplayName(this, field), this, null,
068                            getBinding("field"), null);
069            }
070    
071            IValidationDelegate delegate = form.getDelegate();
072    
073            String id = field == null ? null : field.getClientId();
074    
075            delegate.writeLabelPrefix(field, writer, cycle);
076    
077            writer.begin("label");
078    
079            if (id != null)
080                writer.attribute("for", id);
081            
082            delegate.writeLabelAttributes(writer, cycle, field);
083            renderInformalParameters(writer, cycle);
084    
085            writer.print(displayName, getRaw());
086    
087            writer.end();
088    
089            delegate.writeLabelSuffix(field, writer, cycle);
090        }
091    
092        /** displayName parameter */
093        public abstract String getDisplayName();
094    
095        /** field parameter */
096        public abstract IFormComponent getField();
097    
098        /** raw parameter */
099        public abstract boolean getRaw();
100    }