Coverage Report - org.apache.tapestry5.internal.util.SelectModelRenderer
 
Classes in this File Line Coverage Branch Coverage Complexity
SelectModelRenderer
100%
26/26
100%
8/8
0
 
 1  
 // Copyright 2007, 2008 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.util;
 16  
 
 17  
 import org.apache.tapestry5.*;
 18  
 
 19  
 import java.util.Map;
 20  
 
 21  
 public class SelectModelRenderer implements SelectModelVisitor
 22  
 {
 23  
     private final MarkupWriter writer;
 24  
 
 25  
     private final ValueEncoder encoder;
 26  
 
 27  
     public SelectModelRenderer(final MarkupWriter writer, ValueEncoder encoder)
 28  174
     {
 29  174
         this.writer = writer;
 30  174
         this.encoder = encoder;
 31  174
     }
 32  
 
 33  
     public void beginOptionGroup(OptionGroupModel groupModel)
 34  
     {
 35  8
         writer.element("optgroup", "label", groupModel.getLabel());
 36  
 
 37  8
         writeDisabled(groupModel.isDisabled());
 38  8
         writeAttributes(groupModel.getAttributes());
 39  8
     }
 40  
 
 41  
     public void endOptionGroup(OptionGroupModel groupModel)
 42  
     {
 43  8
         writer.end(); // select
 44  8
     }
 45  
 
 46  
     @SuppressWarnings("unchecked")
 47  
     public void option(OptionModel optionModel)
 48  
     {
 49  736
         Object optionValue = optionModel.getValue();
 50  
 
 51  736
         String clientValue = encoder.toClient(optionValue);
 52  
 
 53  736
         writer.element("option", "value", clientValue);
 54  
 
 55  736
         if (isOptionSelected(optionModel, clientValue)) writer.attributes("selected", "selected");
 56  
 
 57  736
         writeDisabled(optionModel.isDisabled());
 58  736
         writeAttributes(optionModel.getAttributes());
 59  
 
 60  736
         writer.write(optionModel.getLabel());
 61  
 
 62  736
         writer.end();
 63  736
     }
 64  
 
 65  
     private void writeDisabled(boolean disabled)
 66  
     {
 67  744
         if (disabled) writer.attributes("disabled", "disabled");
 68  744
     }
 69  
 
 70  
     private void writeAttributes(Map<String, String> attributes)
 71  
     {
 72  744
         if (attributes == null) return;
 73  
 
 74  6
         for (Map.Entry<String, String> e : attributes.entrySet())
 75  6
             writer.attributes(e.getKey(), e.getValue());
 76  6
     }
 77  
 
 78  
     /**
 79  
      * If true, then the selected attribute will be written. This implementation always returns false.
 80  
      */
 81  
     protected boolean isOptionSelected(OptionModel optionModel, String clientValue)
 82  
     {
 83  238
         return false;
 84  
     }
 85  
 
 86  
 }