Coverage Report - org.apache.tapestry.form.Select
 
Classes in this File Line Coverage Branch Coverage Complexity
Select
0%
0/54
0%
0/18
2.273
 
 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.form;
 16  
 
 17  
 import org.apache.hivemind.ApplicationRuntimeException;
 18  
 import org.apache.tapestry.IMarkupWriter;
 19  
 import org.apache.tapestry.IRequestCycle;
 20  
 import org.apache.tapestry.Tapestry;
 21  
 import org.apache.tapestry.valid.ValidatorException;
 22  
 
 23  
 import java.util.HashSet;
 24  
 import java.util.Set;
 25  
 
 26  
 /**
 27  
  * Implements a component that manages an HTML <select> form element. The most common
 28  
  * situation, using a <select> to set a specific property of some object, is best handled
 29  
  * using a {@link PropertySelection}component. [ <a
 30  
  * href="../../../../../components/form/select.html">Component Reference </a>]
 31  
  * <p>
 32  
  * Otherwise, this component is very similar to {@link RadioGroup}. 
 33  
  * <p>
 34  
  * As of 4.0, this component can be validated.
 35  
  * 
 36  
  * @author Howard Lewis Ship
 37  
  * @author Paul Ferraro
 38  
  */
 39  0
 public abstract class Select extends AbstractFormComponent implements ValidatableField
 40  
 {
 41  
     
 42  
     /**
 43  
      * Used by the <code>Select</code> to record itself as a {@link IRequestCycle}attribute, so
 44  
      * that the {@link Option}components it wraps can have access to it.
 45  
      */
 46  
 
 47  
     private static final String ATTRIBUTE_NAME = "org.apache.tapestry.active.Select";
 48  
     
 49  
     private boolean _rewinding;
 50  
 
 51  
     private boolean _rendering;
 52  
 
 53  
     private Set _selections;
 54  
 
 55  
     private int _nextOptionId;
 56  
 
 57  
     public static Select get(IRequestCycle cycle)
 58  
     {
 59  0
         return (Select) cycle.getAttribute(ATTRIBUTE_NAME);
 60  
     }
 61  
 
 62  
     public abstract boolean isMultiple();
 63  
 
 64  
     public boolean isRewinding()
 65  
     {
 66  0
         if (!_rendering)
 67  0
             throw Tapestry.createRenderOnlyPropertyException(this, "rewinding");
 68  
 
 69  0
         return _rewinding;
 70  
     }
 71  
 
 72  
     public String getNextOptionId()
 73  
     {
 74  0
         if (!_rendering)
 75  0
             throw Tapestry.createRenderOnlyPropertyException(this, "nextOptionId");
 76  
 
 77  
         // Return it as a hex value.
 78  
 
 79  0
         return Integer.toString(_nextOptionId++);
 80  
     }
 81  
 
 82  
     public boolean isSelected(String value)
 83  
     {
 84  0
         if (_selections == null)
 85  0
             return false;
 86  
 
 87  0
         return _selections.contains(value);
 88  
     }
 89  
 
 90  
     /**
 91  
      * @see org.apache.tapestry.AbstractComponent#prepareForRender(org.apache.tapestry.IRequestCycle)
 92  
      */
 93  
     protected void prepareForRender(IRequestCycle cycle)
 94  
     {
 95  0
         super.prepareForRender(cycle);
 96  
         
 97  0
         if (cycle.getAttribute(ATTRIBUTE_NAME) != null)
 98  0
             throw new ApplicationRuntimeException(Tapestry.getMessage("Select.may-not-nest"), this, null, null);
 99  
 
 100  0
         cycle.setAttribute(ATTRIBUTE_NAME, this);
 101  
 
 102  0
         _rendering = true;
 103  0
         _nextOptionId = 0;      
 104  0
     }
 105  
 
 106  
     /**
 107  
      * @see org.apache.tapestry.AbstractComponent#cleanupAfterRender(org.apache.tapestry.IRequestCycle)
 108  
      */
 109  
     protected void cleanupAfterRender(IRequestCycle cycle)
 110  
     {
 111  0
         super.cleanupAfterRender(cycle);
 112  
 
 113  0
         _rendering = false;
 114  0
         _selections = null;        
 115  
         
 116  0
         cycle.removeAttribute(ATTRIBUTE_NAME);
 117  0
     }
 118  
 
 119  
     /**
 120  
      * @see org.apache.tapestry.form.AbstractFormComponent#renderFormComponent(org.apache.tapestry.IMarkupWriter, org.apache.tapestry.IRequestCycle)
 121  
      */
 122  
     protected void renderFormComponent(IMarkupWriter writer, IRequestCycle cycle)
 123  
     {
 124  0
         _rewinding = false;
 125  
 
 126  0
         renderDelegatePrefix(writer, cycle);
 127  
 
 128  0
         writer.begin("select");
 129  
 
 130  0
         writer.attribute("name", getName());
 131  
 
 132  0
         if (isMultiple())
 133  0
             writer.attribute("multiple", "multiple");
 134  
 
 135  0
         if (isDisabled())
 136  0
             writer.attribute("disabled", "disabled");
 137  
 
 138  0
         renderIdAttribute(writer, cycle);
 139  
 
 140  0
         renderDelegateAttributes(writer, cycle);
 141  
 
 142  0
         getValidatableFieldSupport().renderContributions(this, writer, cycle);
 143  
         
 144  0
         renderInformalParameters(writer, cycle);
 145  
 
 146  0
         renderBody(writer, cycle);
 147  
 
 148  0
         writer.end();
 149  
 
 150  0
         renderDelegateSuffix(writer, cycle);
 151  0
     }
 152  
 
 153  
     /**
 154  
      * @see org.apache.tapestry.form.AbstractFormComponent#rewindFormComponent(org.apache.tapestry.IMarkupWriter, org.apache.tapestry.IRequestCycle)
 155  
      */
 156  
     protected void rewindFormComponent(IMarkupWriter writer, IRequestCycle cycle)
 157  
     {
 158  0
         _selections = null;
 159  0
         _rewinding = true;
 160  
 
 161  0
         String[] parameters = cycle.getParameters(getName());
 162  
 
 163  
         try
 164  
         {
 165  0
             if (parameters != null)
 166  
             {
 167  0
                 int length = parameters.length;
 168  
     
 169  0
                 _selections = new HashSet((length > 30) ? 101 : 7);
 170  
     
 171  0
                 for (int i = 0; i < length; i++)
 172  0
                     _selections.add(parameters[i]);
 173  
             }
 174  
     
 175  0
             renderBody(writer, cycle);
 176  
             
 177  
             // This is atypical validation - since this component does not explicitly bind to an object
 178  0
             getValidatableFieldSupport().validate(this, writer, cycle, parameters);
 179  
         }
 180  0
         catch (ValidatorException e)
 181  
         {
 182  0
             getForm().getDelegate().record(e);
 183  0
         }
 184  0
     }
 185  
 
 186  
     /**
 187  
      * Injected.
 188  
      */
 189  
     public abstract ValidatableFieldSupport getValidatableFieldSupport();
 190  
 
 191  
     /**
 192  
      * @see org.apache.tapestry.form.AbstractFormComponent#isRequired()
 193  
      */
 194  
     public boolean isRequired()
 195  
     {
 196  0
         return getValidatableFieldSupport().isRequired(this);
 197  
     }
 198  
 }