Clover coverage report - Code Coverage for tapestry release 4.0-beta-1
Coverage timestamp: Fri Jun 24 2005 14:32:46 EDT
file stats: LOC: 251   Methods: 6
NCLOC: 141   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   
 35    /**
 36    * Provides a Form <tt>java.util.Date</tt> field component for selecting dates. [ <a
 37    * href="../../../../../ComponentReference/DatePicker.html">Component Reference </a>] As of 4.0,
 38    * DatePicker can indicate that it is required, use a custom translator (e.g. for java.sql.Date),
 39    * and perform validation on the submitted date.
 40    *
 41    * @author Paul Geerts
 42    * @author Malcolm Edgar
 43    * @author Paul Ferraro
 44    * @since 2.2
 45    */
 46   
 47    public abstract class DatePicker extends AbstractValidatableField
 48    {
 49    public abstract String getFormat();
 50   
 51    public abstract Date getValue();
 52   
 53    public abstract void setValue(Date value);
 54   
 55    public abstract boolean isDisabled();
 56   
 57    public abstract boolean getIncludeWeek();
 58   
 59    public abstract IAsset getIcon();
 60   
 61    private IScript _script;
 62   
 63    private static final String SYM_NAME = "name";
 64   
 65    private static final String SYM_FORMNAME = "formName";
 66   
 67    private static final String SYM_MONTHNAMES = "monthNames";
 68   
 69    private static final String SYM_SHORT_MONTHNAMES = "shortMonthNames";
 70   
 71    private static final String SYM_WEEKDAYNAMES = "weekDayNames";
 72   
 73    private static final String SYM_SHORT_WEEKDAYNAMES = "shortWeekDayNames";
 74   
 75    private static final String SYM_FIRSTDAYINWEEK = "firstDayInWeek";
 76   
 77    private static final String SYM_MINDAYSINFIRSTWEEK = "minimalDaysInFirstWeek";
 78   
 79    private static final String SYM_FORMAT = "format";
 80   
 81    private static final String SYM_INCL_WEEK = "includeWeek";
 82   
 83    private static final String SYM_VALUE = "value";
 84   
 85    private static final String SYM_BUTTONONCLICKHANDLER = "buttonOnclickHandler";
 86   
 87    /**
 88    * Injected
 89    *
 90    * @since 4.0
 91    */
 92    public abstract IScriptSource getScriptSource();
 93   
 94    /**
 95    * @see org.apache.tapestry.AbstractComponent#finishLoad()
 96    */
 97  0 protected void finishLoad()
 98    {
 99  0 super.finishLoad();
 100   
 101  0 IScriptSource source = getScriptSource();
 102   
 103  0 Resource location = getSpecification().getSpecificationLocation().getRelativeResource(
 104    "DatePicker.script");
 105   
 106  0 _script = source.getScript(location);
 107    }
 108   
 109    /**
 110    * @see org.apache.tapestry.form.validator.AbstractValidatableField#render(org.apache.tapestry.IMarkupWriter,
 111    * org.apache.tapestry.IRequestCycle, java.lang.String)
 112    */
 113  0 public void render(IMarkupWriter writer, IRequestCycle cycle, String value)
 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 Map symbols = new HashMap();
 128   
 129  0 symbols.put(SYM_NAME, name);
 130  0 symbols.put(SYM_FORMAT, format.toPattern());
 131  0 symbols.put(SYM_INCL_WEEK, getIncludeWeek() ? Boolean.TRUE : Boolean.FALSE);
 132   
 133  0 symbols.put(SYM_MONTHNAMES, makeStringList(dfs.getMonths(), 0, 12));
 134  0 symbols.put(SYM_SHORT_MONTHNAMES, makeStringList(dfs.getShortMonths(), 0, 12));
 135  0 symbols.put(SYM_WEEKDAYNAMES, makeStringList(dfs.getWeekdays(), 1, 8));
 136  0 symbols.put(SYM_SHORT_WEEKDAYNAMES, makeStringList(dfs.getShortWeekdays(), 1, 8));
 137  0 symbols.put(SYM_FIRSTDAYINWEEK, new Integer(cal.getFirstDayOfWeek() - 1));
 138  0 symbols.put(SYM_MINDAYSINFIRSTWEEK, new Integer(cal.getMinimalDaysInFirstWeek()));
 139  0 symbols.put(SYM_FORMNAME, getForm().getName());
 140  0 symbols.put(SYM_VALUE, getValue());
 141   
 142  0 _script.execute(cycle, pageRenderSupport, symbols);
 143   
 144  0 renderDelegatePrefix(writer, cycle);
 145   
 146  0 writer.beginEmpty("input");
 147  0 writer.attribute("type", "text");
 148  0 writer.attribute("name", name);
 149  0 writer.attribute("value", value);
 150  0 writer.attribute("title", format.toLocalizedPattern());
 151   
 152  0 if (disabled)
 153  0 writer.attribute("disabled", "disabled");
 154   
 155  0 renderIdAttribute(writer, cycle);
 156   
 157  0 renderDelegateAttributes(writer, cycle);
 158   
 159  0 renderContributions(writer, cycle);
 160   
 161  0 renderInformalParameters(writer, cycle);
 162   
 163  0 writer.printRaw("&nbsp;");
 164   
 165  0 if (!disabled)
 166    {
 167  0 writer.begin("a");
 168  0 writer.attribute("href", (String) symbols.get(SYM_BUTTONONCLICKHANDLER));
 169    }
 170   
 171  0 IAsset icon = getIcon();
 172   
 173  0 writer.beginEmpty("img");
 174  0 writer.attribute("src", icon.buildURL(cycle));
 175  0 writer.attribute("border", 0);
 176   
 177  0 if (!disabled)
 178  0 writer.end();
 179   
 180  0 renderDelegateSuffix(writer, cycle);
 181    }
 182   
 183    /**
 184    * @see org.apache.tapestry.form.AbstractValidatableField#readValue()
 185    */
 186  0 public Object readValue()
 187    {
 188  0 return getValue();
 189    }
 190   
 191    /**
 192    * @see org.apache.tapestry.form.AbstractValidatableField#updateValue(java.lang.Object)
 193    */
 194  0 public void writeValue(Object value)
 195    {
 196  0 setValue((Date) value);
 197    }
 198   
 199    /**
 200    * Create a list of quoted strings. The list is suitable for initializing a JavaScript array.
 201    */
 202  0 private String makeStringList(String[] a, int offset, int length)
 203    {
 204  0 StringBuffer b = new StringBuffer();
 205  0 for (int i = offset; i < length; i++)
 206    {
 207    // JavaScript is sensitive to some UNICODE characters. So for
 208    // the sake of simplicity, we just escape everything
 209  0 b.append('"');
 210  0 char[] ch = a[i].toCharArray();
 211  0 for (int j = 0; j < ch.length; j++)
 212    {
 213  0 if (ch[j] < 128)
 214    {
 215  0 b.append(ch[j]);
 216    }
 217    else
 218    {
 219  0 b.append(escape(ch[j]));
 220    }
 221    }
 222   
 223  0 b.append('"');
 224  0 if (i < length - 1)
 225    {
 226  0 b.append(", ");
 227    }
 228    }
 229  0 return b.toString();
 230   
 231    }
 232   
 233    /**
 234    * Create an escaped Unicode character
 235    *
 236    * @param c
 237    * @return The unicode character in escaped string form
 238    */
 239  0 private static String escape(char c)
 240    {
 241  0 StringBuffer b = new StringBuffer();
 242  0 for (int i = 0; i < 4; i++)
 243    {
 244  0 b.append(Integer.toHexString(c & 0x000F).toUpperCase());
 245  0 c >>>= 4;
 246    }
 247  0 b.append("u\\");
 248  0 return b.reverse().toString();
 249    }
 250   
 251    }