Clover coverage report - Code Coverage for tapestry release 4.0-rc-2
Coverage timestamp: Sat Dec 17 2005 09:39:46 PST
file stats: LOC: 262   Methods: 5
NCLOC: 146   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
DatePicker.java 0% 0% 0% 0%
coverage
 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="../../../../../ComponentReference/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    public abstract class DatePicker extends AbstractFormComponent implements TranslatedField
 49    {
 50    public abstract Date getValue();
 51   
 52    public abstract void setValue(Date value);
 53   
 54    public abstract boolean isDisabled();
 55   
 56    public abstract boolean getIncludeWeek();
 57   
 58    public abstract IAsset getIcon();
 59   
 60    private static final String SYM_NAME = "name";
 61   
 62    private static final String SYM_FORMNAME = "formName";
 63   
 64    private static final String SYM_MONTHNAMES = "monthNames";
 65   
 66    private static final String SYM_SHORT_MONTHNAMES = "shortMonthNames";
 67   
 68    private static final String SYM_WEEKDAYNAMES = "weekDayNames";
 69   
 70    private static final String SYM_SHORT_WEEKDAYNAMES = "shortWeekDayNames";
 71   
 72    private static final String SYM_FIRSTDAYINWEEK = "firstDayInWeek";
 73   
 74    private static final String SYM_MINDAYSINFIRSTWEEK = "minimalDaysInFirstWeek";
 75   
 76    private static final String SYM_FORMAT = "format";
 77   
 78    private static final String SYM_INCL_WEEK = "includeWeek";
 79   
 80    private static final String SYM_CLEAR_BUTTON_LABEL = "clearButtonLabel";
 81   
 82    private static final String SYM_VALUE = "value";
 83   
 84    private static final String SYM_BUTTONONCLICKHANDLER = "buttonOnclickHandler";
 85   
 86    /**
 87    * Injected
 88    *
 89    * @since 4.0
 90    */
 91    public abstract IScript getScript();
 92   
 93    /**
 94    * @see org.apache.tapestry.form.AbstractFormComponent#renderFormComponent(org.apache.tapestry.IMarkupWriter,
 95    * org.apache.tapestry.IRequestCycle)
 96    */
 97  0 protected void renderFormComponent(IMarkupWriter writer, IRequestCycle cycle)
 98    {
 99  0 PageRenderSupport pageRenderSupport = TapestryUtils.getPageRenderSupport(cycle, this);
 100   
 101  0 boolean disabled = isDisabled();
 102  0 DateTranslator translator = (DateTranslator) getTranslator();
 103  0 Locale locale = getPage().getLocale();
 104  0 SimpleDateFormat format = translator.getDateFormat(locale);
 105   
 106  0 DateFormatSymbols dfs = format.getDateFormatSymbols();
 107  0 Calendar cal = Calendar.getInstance(locale);
 108   
 109  0 String name = getName();
 110   
 111  0 String value = getTranslatedFieldSupport().format(this, getValue());
 112   
 113  0 Map symbols = new HashMap();
 114   
 115  0 symbols.put(SYM_NAME, name);
 116  0 symbols.put(SYM_FORMAT, format.toPattern());
 117  0 symbols.put(SYM_INCL_WEEK, getIncludeWeek() ? Boolean.TRUE : Boolean.FALSE);
 118   
 119  0 symbols.put(SYM_MONTHNAMES, makeStringList(dfs.getMonths(), 0, 12));
 120  0 symbols.put(SYM_SHORT_MONTHNAMES, makeStringList(dfs.getShortMonths(), 0, 12));
 121  0 symbols.put(SYM_WEEKDAYNAMES, makeStringList(dfs.getWeekdays(), 1, 8));
 122  0 symbols.put(SYM_SHORT_WEEKDAYNAMES, makeStringList(dfs.getShortWeekdays(), 1, 8));
 123  0 symbols.put(SYM_FIRSTDAYINWEEK, new Integer(cal.getFirstDayOfWeek() - 1));
 124  0 symbols.put(SYM_MINDAYSINFIRSTWEEK, new Integer(cal.getMinimalDaysInFirstWeek()));
 125  0 symbols.put(SYM_CLEAR_BUTTON_LABEL, getMessages().getMessage("clear"));
 126  0 symbols.put(SYM_FORMNAME, getForm().getName());
 127  0 symbols.put(SYM_VALUE, getValue());
 128   
 129  0 getScript().execute(cycle, pageRenderSupport, symbols);
 130   
 131  0 renderDelegatePrefix(writer, cycle);
 132   
 133  0 writer.beginEmpty("input");
 134  0 writer.attribute("type", "text");
 135  0 writer.attribute("name", name);
 136  0 writer.attribute("value", value);
 137  0 writer.attribute("title", format.toLocalizedPattern());
 138   
 139  0 if (disabled)
 140  0 writer.attribute("disabled", "disabled");
 141   
 142  0 renderIdAttribute(writer, cycle);
 143   
 144  0 renderDelegateAttributes(writer, cycle);
 145   
 146  0 getTranslatedFieldSupport().renderContributions(this, writer, cycle);
 147  0 getValidatableFieldSupport().renderContributions(this, writer, cycle);
 148   
 149  0 renderInformalParameters(writer, cycle);
 150   
 151  0 writer.printRaw("&nbsp;");
 152   
 153  0 if (!disabled)
 154    {
 155  0 writer.begin("a");
 156  0 writer.attribute("href", (String) symbols.get(SYM_BUTTONONCLICKHANDLER));
 157    }
 158   
 159  0 IAsset icon = getIcon();
 160   
 161  0 writer.beginEmpty("img");
 162  0 writer.attribute("src", icon.buildURL());
 163  0 writer.attribute("border", 0);
 164   
 165  0 if (!disabled)
 166  0 writer.end();
 167   
 168  0 renderDelegateSuffix(writer, cycle);
 169    }
 170   
 171    /**
 172    * @see org.apache.tapestry.form.AbstractFormComponent#rewindFormComponent(org.apache.tapestry.IMarkupWriter,
 173    * org.apache.tapestry.IRequestCycle)
 174    */
 175  0 protected void rewindFormComponent(IMarkupWriter writer, IRequestCycle cycle)
 176    {
 177  0 String value = cycle.getParameter(getName());
 178   
 179  0 try
 180    {
 181  0 Date date = (Date) getTranslatedFieldSupport().parse(this, value);
 182   
 183  0 getValidatableFieldSupport().validate(this, writer, cycle, date);
 184   
 185  0 setValue(date);
 186    }
 187    catch (ValidatorException e)
 188    {
 189  0 getForm().getDelegate().record(e);
 190    }
 191    }
 192   
 193    /**
 194    * Create a list of quoted strings. The list is suitable for initializing a JavaScript array.
 195    */
 196  0 private String makeStringList(String[] a, int offset, int length)
 197    {
 198  0 StringBuffer b = new StringBuffer();
 199  0 for (int i = offset; i < length; i++)
 200    {
 201    // JavaScript is sensitive to some UNICODE characters. So for
 202    // the sake of simplicity, we just escape everything
 203  0 b.append('"');
 204  0 char[] ch = a[i].toCharArray();
 205  0 for (int j = 0; j < ch.length; j++)
 206    {
 207  0 if (ch[j] < 128)
 208    {
 209  0 b.append(ch[j]);
 210    }
 211    else
 212    {
 213  0 b.append(escape(ch[j]));
 214    }
 215    }
 216   
 217  0 b.append('"');
 218  0 if (i < length - 1)
 219    {
 220  0 b.append(", ");
 221    }
 222    }
 223  0 return b.toString();
 224   
 225    }
 226   
 227    /**
 228    * Create an escaped Unicode character
 229    *
 230    * @param c
 231    * @return The unicode character in escaped string form
 232    */
 233  0 private static String escape(char c)
 234    {
 235  0 StringBuffer b = new StringBuffer();
 236  0 for (int i = 0; i < 4; i++)
 237    {
 238  0 b.append(Integer.toHexString(c & 0x000F).toUpperCase());
 239  0 c >>>= 4;
 240    }
 241  0 b.append("u\\");
 242  0 return b.reverse().toString();
 243    }
 244   
 245    /**
 246    * Injected.
 247    */
 248    public abstract ValidatableFieldSupport getValidatableFieldSupport();
 249   
 250    /**
 251    * Injected.
 252    */
 253    public abstract TranslatedFieldSupport getTranslatedFieldSupport();
 254   
 255    /**
 256    * @see org.apache.tapestry.form.AbstractFormComponent#isRequired()
 257    */
 258  0 public boolean isRequired()
 259    {
 260  0 return getValidatableFieldSupport().isRequired(this);
 261    }
 262    }