Coverage Report - org.apache.tapestry.valid.FieldLabel
 
Classes in this File Line Coverage Branch Coverage Complexity
FieldLabel
100% 
100% 
3.2
 
 1  
 // Copyright 2004, 2005 The Apache Software Foundation
 2  
 //
 3  
 // Licensed under the Apache License, Version 2.0 (the "License");
 4  
 // you may not use this file except in compliance with the License.
 5  
 // You may obtain a copy of the License at
 6  
 //
 7  
 //     http://www.apache.org/licenses/LICENSE-2.0
 8  
 //
 9  
 // Unless required by applicable law or agreed to in writing, software
 10  
 // distributed under the License is distributed on an "AS IS" BASIS,
 11  
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  
 // See the License for the specific language governing permissions and
 13  
 // limitations under the License.
 14  
 
 15  
 package org.apache.tapestry.valid;
 16  
 
 17  
 import org.apache.tapestry.AbstractComponent;
 18  
 import org.apache.tapestry.BindingException;
 19  
 import org.apache.tapestry.IForm;
 20  
 import org.apache.tapestry.IMarkupWriter;
 21  
 import org.apache.tapestry.IRequestCycle;
 22  
 import org.apache.tapestry.Tapestry;
 23  
 import org.apache.tapestry.TapestryUtils;
 24  
 import org.apache.tapestry.form.IFormComponent;
 25  
 
 26  
 /**
 27  
  * Used to label an {@link IFormComponent}. Because such fields know their
 28  
  * displayName (user-presentable name), there's no reason to hard code the label
 29  
  * in a page's HTML template (this also helps with localization). [ <a
 30  
  * href="../../../../../ComponentReference/FieldLabel.html">Component Reference
 31  
  * </a>]
 32  
  * 
 33  
  * @author Howard Lewis Lewis Ship
 34  
  */
 35  
 
 36  8
 public abstract class FieldLabel extends AbstractComponent
 37  
 {
 38  
 
 39  
     // Parameter
 40  
     public abstract boolean isPrerender();
 41  
 
 42  
     /**
 43  
      * Gets the {@link IForm}&nbsp;and {@link IValidationDelegate delegate},
 44  
      * then renders the label obtained from the field. Does nothing when
 45  
      * rewinding.
 46  
      */
 47  
 
 48  
     protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
 49  
     {
 50  8
         IForm form = TapestryUtils.getForm(cycle, this);
 51  
         
 52  8
         IFormComponent field = getField();
 53  
         
 54  8
         if (field != null && isPrerender())
 55  4
             form.prerenderField(writer, field, getLocation());
 56  
         
 57  8
         if (cycle.isRewinding()) 
 58  1
             return;
 59  
         
 60  7
         String displayName = getDisplayName();
 61  
         
 62  7
         if (displayName == null)
 63  
         {
 64  5
             if (field == null)
 65  1
                 throw Tapestry.createRequiredParameterException(this, "field");
 66  
             
 67  4
             displayName = field.getDisplayName();
 68  
             
 69  4
             if (displayName == null)
 70  1
                 throw new BindingException(ValidMessages.noDisplayName(this,
 71  
                         field), this, null, getBinding("field"), null);
 72  
         }
 73  
         
 74  5
         IValidationDelegate delegate = form.getDelegate();
 75  
         
 76  5
         String id = (field == null) ? null : field.getClientId();
 77  
         
 78  5
         if (field != null)
 79  3
             delegate.writeLabelPrefix(field, writer, cycle);
 80  
         
 81  5
         writer.begin("label");
 82  
         
 83  5
         if (id != null) 
 84  1
             writer.attribute("for", id);
 85  
         
 86  5
         delegate.writeLabelAttributes(writer, cycle, field);
 87  5
         renderInformalParameters(writer, cycle);
 88  
         
 89  5
         delegate.beforeLabelText(writer, cycle, field);
 90  
         
 91  5
         writer.print(displayName, getRaw());
 92  
         
 93  5
         delegate.afterLabelText(writer, cycle, field);
 94  
         
 95  5
         writer.end();
 96  
         
 97  5
         if (field != null)
 98  3
             delegate.writeLabelSuffix(field, writer, cycle);
 99  5
     }
 100  
 
 101  
     /** displayName parameter. */
 102  
     public abstract String getDisplayName();
 103  
 
 104  
     /** field parameter. */
 105  
     public abstract IFormComponent getField();
 106  
 
 107  
     /** raw parameter. */
 108  
     public abstract boolean getRaw();
 109  
 }