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