Clover coverage report - Code Coverage for tapestry release 4.0-beta-2
Coverage timestamp: Sat Jul 9 2005 22:02:17 EDT
file stats: LOC: 249   Methods: 6
NCLOC: 140   Classes: 1
30 day Evaluation License registered to hlship@comcast.net Your 30 day evaluation period has expired. Please visit http://www.cenqua.com to obtain a licensed version of Clover
 
 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 Date getValue();
 50   
 51    public abstract void setValue(Date value);
 52   
 53    public abstract boolean isDisabled();
 54   
 55    public abstract boolean getIncludeWeek();
 56   
 57    public abstract IAsset getIcon();
 58   
 59    private IScript _script;
 60   
 61    private static final String SYM_NAME = "name";
 62   
 63    private static final String SYM_FORMNAME = "formName";
 64   
 65    private static final String SYM_MONTHNAMES = "monthNames";
 66   
 67    private static final String SYM_SHORT_MONTHNAMES = "shortMonthNames";
 68   
 69    private static final String SYM_WEEKDAYNAMES = "weekDayNames";
 70   
 71    private static final String SYM_SHORT_WEEKDAYNAMES = "shortWeekDayNames";
 72   
 73    private static final String SYM_FIRSTDAYINWEEK = "firstDayInWeek";
 74   
 75    private static final String SYM_MINDAYSINFIRSTWEEK = "minimalDaysInFirstWeek";
 76   
 77    private static final String SYM_FORMAT = "format";
 78   
 79    private static final String SYM_INCL_WEEK = "includeWeek";
 80   
 81    private static final String SYM_VALUE = "value";
 82   
 83    private static final String SYM_BUTTONONCLICKHANDLER = "buttonOnclickHandler";
 84   
 85    /**
 86    * Injected
 87    *
 88    * @since 4.0
 89    */
 90    public abstract IScriptSource getScriptSource();
 91   
 92    /**
 93    * @see org.apache.tapestry.AbstractComponent#finishLoad()
 94    */
 95  0 protected void finishLoad()
 96    {
 97  0 super.finishLoad();
 98   
 99  0 IScriptSource source = getScriptSource();
 100   
 101  0 Resource location = getSpecification().getSpecificationLocation().getRelativeResource(
 102    "DatePicker.script");
 103   
 104  0 _script = source.getScript(location);
 105    }
 106   
 107    /**
 108    * @see org.apache.tapestry.form.validator.AbstractValidatableField#render(org.apache.tapestry.IMarkupWriter,
 109    * org.apache.tapestry.IRequestCycle, java.lang.String)
 110    */
 111  0 public void render(IMarkupWriter writer, IRequestCycle cycle, String value)
 112    {
 113  0 PageRenderSupport pageRenderSupport = TapestryUtils.getPageRenderSupport(cycle, this);
 114   
 115  0 boolean disabled = isDisabled();
 116  0 DateTranslator translator = (DateTranslator) getTranslator();
 117  0 Locale locale = getPage().getLocale();
 118  0 SimpleDateFormat format = translator.getDateFormat(locale);
 119   
 120  0 DateFormatSymbols dfs = format.getDateFormatSymbols();
 121  0 Calendar cal = Calendar.getInstance(locale);
 122   
 123  0 String name = getName();
 124   
 125  0 Map symbols = new HashMap();
 126   
 127  0 symbols.put(SYM_NAME, name);
 128  0 symbols.put(SYM_FORMAT, format.toPattern());
 129  0 symbols.put(SYM_INCL_WEEK, getIncludeWeek() ? Boolean.TRUE : Boolean.FALSE);
 130   
 131  0 symbols.put(SYM_MONTHNAMES, makeStringList(dfs.getMonths(), 0, 12));
 132  0 symbols.put(SYM_SHORT_MONTHNAMES, makeStringList(dfs.getShortMonths(), 0, 12));
 133  0 symbols.put(SYM_WEEKDAYNAMES, makeStringList(dfs.getWeekdays(), 1, 8));
 134  0 symbols.put(SYM_SHORT_WEEKDAYNAMES, makeStringList(dfs.getShortWeekdays(), 1, 8));
 135  0 symbols.put(SYM_FIRSTDAYINWEEK, new Integer(cal.getFirstDayOfWeek() - 1));
 136  0 symbols.put(SYM_MINDAYSINFIRSTWEEK, new Integer(cal.getMinimalDaysInFirstWeek()));
 137  0 symbols.put(SYM_FORMNAME, getForm().getName());
 138  0 symbols.put(SYM_VALUE, getValue());
 139   
 140  0 _script.execute(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", format.toLocalizedPattern());
 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 renderContributions(writer, cycle);
 158   
 159  0 renderInformalParameters(writer, cycle);
 160   
 161  0 writer.printRaw("&nbsp;");
 162   
 163  0 if (!disabled)
 164    {
 165  0 writer.begin("a");
 166  0 writer.attribute("href", (String) symbols.get(SYM_BUTTONONCLICKHANDLER));
 167    }
 168   
 169  0 IAsset icon = getIcon();
 170   
 171  0 writer.beginEmpty("img");
 172  0 writer.attribute("src", icon.buildURL(cycle));
 173  0 writer.attribute("border", 0);
 174   
 175  0 if (!disabled)
 176  0 writer.end();
 177   
 178  0 renderDelegateSuffix(writer, cycle);
 179    }
 180   
 181    /**
 182    * @see org.apache.tapestry.form.AbstractValidatableField#readValue()
 183    */
 184  0 public Object readValue()
 185    {
 186  0 return getValue();
 187    }
 188   
 189    /**
 190    * @see org.apache.tapestry.form.AbstractValidatableField#updateValue(java.lang.Object)
 191    */
 192  0 public void writeValue(Object value)
 193    {
 194  0 setValue((Date) value);
 195    }
 196   
 197    /**
 198    * Create a list of quoted strings. The list is suitable for initializing a JavaScript array.
 199    */
 200  0 private String makeStringList(String[] a, int offset, int length)
 201    {
 202  0 StringBuffer b = new StringBuffer();
 203  0 for (int i = offset; i < length; i++)
 204    {
 205    // JavaScript is sensitive to some UNICODE characters. So for
 206    // the sake of simplicity, we just escape everything
 207  0 b.append('"');
 208  0 char[] ch = a[i].toCharArray();
 209  0 for (int j = 0; j < ch.length; j++)
 210    {
 211  0 if (ch[j] < 128)
 212    {
 213  0 b.append(ch[j]);
 214    }
 215    else
 216    {
 217  0 b.append(escape(ch[j]));
 218    }
 219    }
 220   
 221  0 b.append('"');
 222  0 if (i < length - 1)
 223    {
 224  0 b.append(", ");
 225    }
 226    }
 227  0 return b.toString();
 228   
 229    }
 230   
 231    /**
 232    * Create an escaped Unicode character
 233    *
 234    * @param c
 235    * @return The unicode character in escaped string form
 236    */
 237  0 private static String escape(char c)
 238    {
 239  0 StringBuffer b = new StringBuffer();
 240  0 for (int i = 0; i < 4; i++)
 241    {
 242  0 b.append(Integer.toHexString(c & 0x000F).toUpperCase());
 243  0 c >>>= 4;
 244    }
 245  0 b.append("u\\");
 246  0 return b.reverse().toString();
 247    }
 248   
 249    }