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