Coverage Report - org.apache.tapestry.form.DatePicker
 
Classes in this File Line Coverage Branch Coverage Complexity
DatePicker
0%
0/82
0%
0/20
1.733
 
 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 java.text.DateFormatSymbols;
 18  
 import java.text.SimpleDateFormat;
 19  
 import java.util.Calendar;
 20  
 import java.util.Date;
 21  
 import java.util.HashMap;
 22  
 import java.util.Locale;
 23  
 import java.util.Map;
 24  
 
 25  
 import org.apache.tapestry.IAsset;
 26  
 import org.apache.tapestry.IMarkupWriter;
 27  
 import org.apache.tapestry.IRequestCycle;
 28  
 import org.apache.tapestry.IScript;
 29  
 import org.apache.tapestry.PageRenderSupport;
 30  
 import org.apache.tapestry.TapestryUtils;
 31  
 import org.apache.tapestry.form.translator.DateTranslator;
 32  
 import org.apache.tapestry.valid.ValidatorException;
 33  
 
 34  
 /**
 35  
  * Provides a Form <tt>java.util.Date</tt> field component for selecting dates. [ <a
 36  
  * href="../../../../../components/form/datepicker.html">Component Reference </a>] As of 4.0,
 37  
  * DatePicker can indicate that it is required, use a custom translator (e.g. for java.sql.Date),
 38  
  * and perform validation on the submitted date.
 39  
  * <p>
 40  
  * As of 4.0, this component can be configurably translated and validated.
 41  
  * 
 42  
  * @author Paul Geerts
 43  
  * @author Malcolm Edgar
 44  
  * @author Paul Ferraro
 45  
  * @since 2.2
 46  
  */
 47  
 
 48  0
 public abstract class DatePicker extends AbstractFormComponent implements TranslatedField
 49  
 {
 50  
     private static final String SYM_NAME = "name";
 51  
 
 52  
     private static final String SYM_FORMNAME = "formName";
 53  
 
 54  
     private static final String SYM_MONTHNAMES = "monthNames";
 55  
 
 56  
     private static final String SYM_SHORT_MONTHNAMES = "shortMonthNames";
 57  
 
 58  
     private static final String SYM_WEEKDAYNAMES = "weekDayNames";
 59  
 
 60  
     private static final String SYM_SHORT_WEEKDAYNAMES = "shortWeekDayNames";
 61  
 
 62  
     private static final String SYM_FIRSTDAYINWEEK = "firstDayInWeek";
 63  
 
 64  
     private static final String SYM_MINDAYSINFIRSTWEEK = "minimalDaysInFirstWeek";
 65  
 
 66  
     private static final String SYM_FORMAT = "format";
 67  
 
 68  
     private static final String SYM_INCL_WEEK = "includeWeek";
 69  
 
 70  
     private static final String SYM_CLEAR_BUTTON_LABEL = "clearButtonLabel";
 71  
 
 72  
     private static final String SYM_VALUE = "value";
 73  
 
 74  
     private static final String SYM_BUTTONONCLICKHANDLER = "buttonOnclickHandler";
 75  
     
 76  
     public abstract Date getValue();
 77  
 
 78  
     public abstract void setValue(Date value);
 79  
 
 80  
     public abstract boolean isDisabled();
 81  
 
 82  
     public abstract boolean getIncludeWeek();
 83  
 
 84  
     public abstract IAsset getIcon();
 85  
     
 86  
     public abstract String getImageClass();
 87  
     
 88  
     /** 
 89  
      * @since 4.1.1
 90  
      */    
 91  
     public abstract String getTitle();
 92  
 
 93  
     /**
 94  
      * Injected.
 95  
      * 
 96  
      * @since 4.0
 97  
      */
 98  
     public abstract IScript getScript();
 99  
 
 100  
     /**
 101  
      * @see org.apache.tapestry.form.AbstractFormComponent#renderFormComponent(org.apache.tapestry.IMarkupWriter,
 102  
      *      org.apache.tapestry.IRequestCycle)
 103  
      */
 104  
     protected void renderFormComponent(IMarkupWriter writer, IRequestCycle cycle)
 105  
     {
 106  0
         PageRenderSupport pageRenderSupport = TapestryUtils.getPageRenderSupport(cycle, this);
 107  
 
 108  0
         boolean disabled = isDisabled();
 109  0
         DateTranslator translator = (DateTranslator) getTranslator();
 110  0
         Locale locale = getPage().getLocale();
 111  0
         SimpleDateFormat format = translator.getDateFormat(locale);
 112  
 
 113  0
         DateFormatSymbols dfs = format.getDateFormatSymbols();
 114  0
         Calendar cal = Calendar.getInstance(locale);
 115  
 
 116  0
         String name = getName();
 117  
         
 118  0
         String title = getTitle();
 119  0
         if (title == null)
 120  0
             title = format.toLocalizedPattern();
 121  
 
 122  0
         String value = getTranslatedFieldSupport().format(this, getValue());
 123  
 
 124  0
         Map symbols = new HashMap();
 125  
 
 126  0
         symbols.put(SYM_NAME, name);
 127  0
         symbols.put(SYM_FORMAT, format.toPattern());
 128  0
         symbols.put(SYM_INCL_WEEK, getIncludeWeek() ? Boolean.TRUE : Boolean.FALSE);
 129  
 
 130  0
         symbols.put(SYM_MONTHNAMES, makeStringList(dfs.getMonths(), 0, 12));
 131  0
         symbols.put(SYM_SHORT_MONTHNAMES, makeStringList(dfs.getShortMonths(), 0, 12));
 132  0
         symbols.put(SYM_WEEKDAYNAMES, makeStringList(dfs.getWeekdays(), 1, 8));
 133  0
         symbols.put(SYM_SHORT_WEEKDAYNAMES, makeStringList(dfs.getShortWeekdays(), 1, 8));
 134  0
         symbols.put(SYM_FIRSTDAYINWEEK, new Integer(cal.getFirstDayOfWeek() - 1));
 135  0
         symbols.put(SYM_MINDAYSINFIRSTWEEK, new Integer(cal.getMinimalDaysInFirstWeek()));
 136  0
         symbols.put(SYM_CLEAR_BUTTON_LABEL, getMessages().getMessage("clear"));
 137  0
         symbols.put(SYM_FORMNAME, getForm().getName());
 138  0
         symbols.put(SYM_VALUE, getValue());
 139  
         
 140  0
         getScript().execute(this, cycle, pageRenderSupport, symbols);
 141  
         
 142  0
         renderDelegatePrefix(writer, cycle);
 143  
 
 144  0
         writer.beginEmpty("input");
 145  0
         writer.attribute("type", "text");
 146  0
         writer.attribute("name", name);
 147  0
         writer.attribute("value", value);
 148  0
         writer.attribute("title", title);
 149  
 
 150  0
         if (disabled)
 151  0
             writer.attribute("disabled", "disabled");
 152  
 
 153  0
         renderIdAttribute(writer, cycle);
 154  
 
 155  0
         renderDelegateAttributes(writer, cycle);
 156  
 
 157  0
         getTranslatedFieldSupport().renderContributions(this, writer, cycle);
 158  0
         getValidatableFieldSupport().renderContributions(this, writer, cycle);
 159  
 
 160  0
         renderInformalParameters(writer, cycle);
 161  
 
 162  0
         writer.printRaw("&nbsp;");
 163  
 
 164  0
         if (!disabled)
 165  
         {
 166  0
             writer.begin("a");
 167  0
             writer.attribute("href", (String) symbols.get(SYM_BUTTONONCLICKHANDLER));
 168  
         }
 169  
 
 170  0
         IAsset icon = getIcon();
 171  
 
 172  0
         writer.beginEmpty("img");
 173  0
         writer.attribute("src", icon.buildURL());
 174  0
         writer.attribute("alt", getMessages().getMessage("alt"));
 175  0
         writer.attribute("border", 0);
 176  0
         writer.attribute("class", getImageClass());
 177  
         
 178  0
         if (!disabled)
 179  0
             writer.end();
 180  
 
 181  0
         renderDelegateSuffix(writer, cycle);
 182  0
     }
 183  
 
 184  
     /**
 185  
      * @see org.apache.tapestry.form.AbstractFormComponent#rewindFormComponent(org.apache.tapestry.IMarkupWriter,
 186  
      *      org.apache.tapestry.IRequestCycle)
 187  
      */
 188  
     protected void rewindFormComponent(IMarkupWriter writer, IRequestCycle cycle)
 189  
     {
 190  0
         String value = cycle.getParameter(getName());
 191  
 
 192  
         try
 193  
         {
 194  0
             Date date = (Date) getTranslatedFieldSupport().parse(this, value);
 195  
 
 196  0
             getValidatableFieldSupport().validate(this, writer, cycle, date);
 197  
 
 198  0
             setValue(date);
 199  
         }
 200  0
         catch (ValidatorException e)
 201  
         {
 202  0
             getForm().getDelegate().record(e);
 203  0
         }
 204  0
     }
 205  
 
 206  
     /**
 207  
      * Create a list of quoted strings. The list is suitable for initializing a JavaScript array.
 208  
      */
 209  
     private String makeStringList(String[] a, int offset, int length)
 210  
     {
 211  0
         StringBuffer b = new StringBuffer();
 212  0
         for (int i = offset; i < length; i++)
 213  
         {
 214  
             // JavaScript is sensitive to some UNICODE characters. So for
 215  
             // the sake of simplicity, we just escape everything
 216  0
             b.append('"');
 217  0
             char[] ch = a[i].toCharArray();
 218  0
             for (int j = 0; j < ch.length; j++)
 219  
             {
 220  0
                 if (ch[j] < 128)
 221  
                 {
 222  0
                     b.append(ch[j]);
 223  
                 }
 224  
                 else
 225  
                 {
 226  0
                     b.append(escape(ch[j]));
 227  
                 }
 228  
             }
 229  
 
 230  0
             b.append('"');
 231  0
             if (i < length - 1)
 232  
             {
 233  0
                 b.append(", ");
 234  
             }
 235  
         }
 236  0
         return b.toString();
 237  
 
 238  
     }
 239  
 
 240  
     /**
 241  
      * Create an escaped Unicode character.
 242  
      * 
 243  
      * @param c
 244  
      * @return The unicode character in escaped string form
 245  
      */
 246  
     private static String escape(char c)
 247  
     {
 248  0
         char unescapedChar = c;
 249  0
         StringBuffer b = new StringBuffer();
 250  0
         for (int i = 0; i < 4; i++)
 251  
         {
 252  0
             b.append(Integer.toHexString(unescapedChar & 0x000F).toUpperCase());
 253  0
             unescapedChar >>>= 4;
 254  
         }
 255  0
         b.append("u\\");
 256  0
         return b.reverse().toString();
 257  
     }
 258  
 
 259  
     /**
 260  
      * Injected.
 261  
      */
 262  
     public abstract ValidatableFieldSupport getValidatableFieldSupport();
 263  
 
 264  
     /**
 265  
      * Injected.
 266  
      */
 267  
     public abstract TranslatedFieldSupport getTranslatedFieldSupport();
 268  
 
 269  
     /**
 270  
      * @see org.apache.tapestry.form.AbstractFormComponent#isRequired()
 271  
      */
 272  
     public boolean isRequired()
 273  
     {
 274  0
         return getValidatableFieldSupport().isRequired(this);
 275  
     }
 276  
 }