Coverage Report - org.apache.tapestry5.internal.DefaultValidationDecorator
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultValidationDecorator
100%
23/23
100%
12/12
0
 
 1  
 // Copyright 2007, 2008, 2009 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.tapestry5.internal;
 16  
 
 17  
 import org.apache.tapestry5.*;
 18  
 import org.apache.tapestry5.dom.Element;
 19  
 import org.apache.tapestry5.services.Environment;
 20  
 import org.apache.tapestry5.services.FormSupport;
 21  
 
 22  
 /**
 23  
  * Default implementation that writes an attribute into fields or labels that are in error.
 24  
  */
 25  
 public final class DefaultValidationDecorator extends BaseValidationDecorator
 26  
 {
 27  
     private final Environment environment;
 28  
 
 29  
     private final Asset spacerAsset;
 30  
 
 31  
     private final MarkupWriter markupWriter;
 32  
 
 33  
     /**
 34  
      * @param environment  used to locate objects and services during the render
 35  
      * @param spacerAsset  asset for a one-pixel spacer image used as a placeholder for the error marker icon
 36  
      * @param markupWriter
 37  
      */
 38  
     public DefaultValidationDecorator(Environment environment, Asset spacerAsset, MarkupWriter markupWriter)
 39  1176
     {
 40  1176
         this.environment = environment;
 41  1176
         this.spacerAsset = spacerAsset;
 42  1176
         this.markupWriter = markupWriter;
 43  1176
     }
 44  
 
 45  
     @Override
 46  
     public void insideField(Field field)
 47  
     {
 48  690
         if (inError(field)) addErrorClassToCurrentElement();
 49  690
     }
 50  
 
 51  
     @Override
 52  
     public void insideLabel(Field field, Element element)
 53  
     {
 54  582
         if (field == null) return;
 55  
 
 56  580
         if (inError(field)) element.addClassName(CSSClassConstants.ERROR);
 57  580
     }
 58  
 
 59  
     /**
 60  
      * Writes an icon for field after the field.  The icon has the same id as the field, with ":icon" appended. This is
 61  
      * expected by the default client-side JavaScript.  The icon's src is a blank spacer image (this is to allow the
 62  
      * image displayed to be overridden via CSS).     The icon's CSS class is "t-error-icon", with "t-invisible" added
 63  
      * if the field is not in error when rendered.  If client validation is not enabled for the form containing the
 64  
      * field and the field is not in error, then the error icon itself is not rendered.
 65  
      *
 66  
      * @param field which just completed rendering itself
 67  
      */
 68  
     @Override
 69  
     public void afterField(Field field)
 70  
     {
 71  842
         boolean inError = inError(field);
 72  
 
 73  842
         boolean clientValidationEnabled = getFormSupport().isClientValidationEnabled();
 74  
 
 75  842
         if (inError || clientValidationEnabled)
 76  
         {
 77  674
             String iconId = field.getClientId() + "-icon";
 78  
 
 79  674
             String cssClass = inError ? "t-error-icon" : "t-error-icon t-invisible";
 80  
 
 81  674
             markupWriter.element("img",
 82  
                                  "src", spacerAsset.toClientURL(),
 83  
                                  "alt", "",
 84  
                                  "class", cssClass,
 85  
                                  "id", iconId);
 86  674
             markupWriter.end();
 87  
         }
 88  
 
 89  842
     }
 90  
 
 91  
     private FormSupport getFormSupport()
 92  
     {
 93  842
         return environment.peekRequired(FormSupport.class);
 94  
     }
 95  
 
 96  
     private boolean inError(Field field)
 97  
     {
 98  2112
         ValidationTracker tracker = environment.peekRequired(ValidationTracker.class);
 99  
 
 100  2112
         return tracker.inError(field);
 101  
     }
 102  
 
 103  
     private void addErrorClassToCurrentElement()
 104  
     {
 105  46
         markupWriter.getElement().addClassName(CSSClassConstants.ERROR);
 106  46
     }
 107  
 }