Coverage Report - org.apache.tapestry.dojo.form.GTimePicker
 
Classes in this File Line Coverage Branch Coverage Complexity
GTimePicker
80% 
100% 
2
 
 1  
 package org.apache.tapestry.dojo.form;
 2  
 
 3  
 import org.apache.tapestry.IMarkupWriter;
 4  
 import org.apache.tapestry.IRequestCycle;
 5  
 import org.apache.tapestry.IScript;
 6  
 import org.apache.tapestry.TapestryUtils;
 7  
 import org.apache.tapestry.form.TranslatedField;
 8  
 import org.apache.tapestry.form.TranslatedFieldSupport;
 9  
 import org.apache.tapestry.form.ValidatableFieldSupport;
 10  
 import org.apache.tapestry.json.JSONLiteral;
 11  
 import org.apache.tapestry.json.JSONObject;
 12  
 import org.apache.tapestry.valid.ValidatorException;
 13  
 
 14  
 import java.util.*;
 15  
 
 16  
 /**
 17  
  * Implementation of an html form input field that has a dynamic drop down selection list of
 18  
  * time segments displayed in the {@link org.apache.tapestry.IPage}'s {@link java.util.Locale}.
 19  
  */
 20  1
 public abstract class GTimePicker  extends AbstractFormWidget implements TranslatedField
 21  
 {
 22  
     /**
 23  
      * For a full day - broken up in to half hour segments.
 24  
      */
 25  
     static final int TIME_SEGMENT_LENGTH = 48;
 26  
 
 27  
     /**
 28  
      * Core value used to place input in to.
 29  
      * @return The current bound value, may be null.
 30  
      */
 31  
     public abstract Object getValue();
 32  
 
 33  
     public abstract void setValue(Object value);
 34  
 
 35  
     public abstract boolean isDisabled();
 36  
 
 37  
     /**
 38  
      * {@inheritDoc}
 39  
      */
 40  
     protected void renderFormWidget(IMarkupWriter writer, IRequestCycle cycle)
 41  
     {
 42  1
         String value = getTranslatedFieldSupport().format(this, getValue());
 43  
 
 44  1
         renderDelegatePrefix(writer, cycle);
 45  
 
 46  1
         writer.beginEmpty("input");
 47  
 
 48  1
         writer.attribute("type", "text");
 49  
         
 50  1
         writer.attribute("autocomplete", "off");
 51  
 
 52  1
         writer.attribute("name", getName());
 53  
 
 54  1
         if (isDisabled())
 55  0
             writer.attribute("disabled", "disabled");
 56  
 
 57  1
         if (value != null)
 58  1
             writer.attribute("value", value);
 59  
 
 60  1
         renderIdAttribute(writer, cycle);
 61  
 
 62  1
         renderDelegateAttributes(writer, cycle);
 63  
 
 64  1
         getTranslatedFieldSupport().renderContributions(this, writer, cycle);
 65  1
         getValidatableFieldSupport().renderContributions(this, writer, cycle);
 66  
 
 67  1
         renderInformalParameters(writer, cycle);
 68  
 
 69  1
         writer.closeTag();
 70  
 
 71  1
         renderDelegateSuffix(writer, cycle);
 72  
 
 73  
         // Build up options value list
 74  
 
 75  1
         Locale locale = getPage().getLocale();
 76  
         
 77  1
         GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance(getPage().getLocale());
 78  1
         cal.set(Calendar.HOUR, 0);
 79  1
         cal.set(Calendar.AM_PM, Calendar.AM);
 80  
 
 81  1
         StringBuffer optStr = new StringBuffer("[");
 82  
 
 83  1
         int selectedIndex = -1;
 84  
         
 85  49
         for(int i=0, hour=0; i < TIME_SEGMENT_LENGTH; i++)
 86  
         {
 87  48
             if (i != 0)
 88  
             {
 89  47
                 optStr.append(",");
 90  
             }
 91  
             
 92  48
             if (i == 24)
 93  
             {
 94  1
                 hour = 0;
 95  1
                 cal.set(Calendar.AM_PM, Calendar.PM);
 96  
             }
 97  
             
 98  48
             cal.set(Calendar.HOUR,  hour);
 99  48
             cal.set(Calendar.MINUTE, (i % 2 > 0) ? 30 : 0);
 100  
 
 101  48
             String option = getTranslator().format(this, locale, cal.getTime());
 102  
 
 103  48
             optStr.append("\"").append(option).append("\"");
 104  
             
 105  48
             if (selectedIndex < 0 && value != null && value.equals(option))
 106  
             {
 107  0
                 selectedIndex = i;
 108  
             }
 109  
 
 110  48
             if (i % 2 > 0)
 111  
             {
 112  24
                 hour++;
 113  
             }
 114  
         }
 115  
 
 116  1
         optStr.append("]");
 117  
         
 118  
         // now create widget parms
 119  
 
 120  1
         JSONObject json = new JSONObject();
 121  1
         json.put("inputNodeId", getClientId());
 122  1
         json.put("optionValues", new JSONLiteral(optStr.toString()));
 123  
 
 124  1
         if (selectedIndex > -1)
 125  
         {
 126  0
             json.put("selectedIndex", selectedIndex);
 127  
         }
 128  
 
 129  1
         Map parms = new HashMap();
 130  1
         parms.put("clientId", getClientId());
 131  1
         parms.put("props", json.toString());
 132  1
         parms.put("widget", this);
 133  
 
 134  1
         getScript().execute(this, cycle, TapestryUtils.getPageRenderSupport(cycle, this), parms);
 135  1
     }
 136  
 
 137  
     /**
 138  
      * @see org.apache.tapestry.form.AbstractFormComponent#rewindFormComponent(org.apache.tapestry.IMarkupWriter, org.apache.tapestry.IRequestCycle)
 139  
      */
 140  
     protected void rewindFormWidget(IMarkupWriter writer, IRequestCycle cycle)
 141  
     {
 142  0
         String value = cycle.getParameter(getName());
 143  
 
 144  
         try
 145  
         {
 146  0
             Object translated = getTranslatedFieldSupport().parse(this, value);
 147  
 
 148  0
             getValidatableFieldSupport().validate(this, writer, cycle, translated);
 149  
 
 150  0
             setValue(translated);
 151  
         }
 152  0
         catch (ValidatorException e)
 153  
         {
 154  0
             getForm().getDelegate().record(e);
 155  0
         }
 156  0
     }
 157  
 
 158  
     /**
 159  
      * @see org.apache.tapestry.form.AbstractFormComponent#isRequired()
 160  
      */
 161  
     public boolean isRequired()
 162  
     {
 163  0
         return getValidatableFieldSupport().isRequired(this);
 164  
     }
 165  
 
 166  
     /** Injected. */
 167  
     public abstract IScript getScript();
 168  
 
 169  
     /** Injected. */
 170  
     public abstract TranslatedFieldSupport getTranslatedFieldSupport();
 171  
 
 172  
     /** Injected. */
 173  
     public abstract ValidatableFieldSupport getValidatableFieldSupport();
 174  
 
 175  
 }