1 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 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 90 public abstract IScriptSource getScriptSource(); 91 92 95 protected void finishLoad() 96 { 97 super.finishLoad(); 98 99 IScriptSource source = getScriptSource(); 100 101 Resource location = getSpecification().getSpecificationLocation().getRelativeResource( 102 "DatePicker.script"); 103 104 _script = source.getScript(location); 105 } 106 107 111 public void render(IMarkupWriter writer, IRequestCycle cycle, String value) 112 { 113 PageRenderSupport pageRenderSupport = TapestryUtils.getPageRenderSupport(cycle, this); 114 115 boolean disabled = isDisabled(); 116 DateTranslator translator = (DateTranslator) getTranslator(); 117 Locale locale = getPage().getLocale(); 118 SimpleDateFormat format = translator.getDateFormat(locale); 119 120 DateFormatSymbols dfs = format.getDateFormatSymbols(); 121 Calendar cal = Calendar.getInstance(locale); 122 123 String name = getName(); 124 125 Map symbols = new HashMap (); 126 127 symbols.put(SYM_NAME, name); 128 symbols.put(SYM_FORMAT, format.toPattern()); 129 symbols.put(SYM_INCL_WEEK, getIncludeWeek() ? Boolean.TRUE : Boolean.FALSE); 130 131 symbols.put(SYM_MONTHNAMES, makeStringList(dfs.getMonths(), 0, 12)); 132 symbols.put(SYM_SHORT_MONTHNAMES, makeStringList(dfs.getShortMonths(), 0, 12)); 133 symbols.put(SYM_WEEKDAYNAMES, makeStringList(dfs.getWeekdays(), 1, 8)); 134 symbols.put(SYM_SHORT_WEEKDAYNAMES, makeStringList(dfs.getShortWeekdays(), 1, 8)); 135 symbols.put(SYM_FIRSTDAYINWEEK, new Integer (cal.getFirstDayOfWeek() - 1)); 136 symbols.put(SYM_MINDAYSINFIRSTWEEK, new Integer (cal.getMinimalDaysInFirstWeek())); 137 symbols.put(SYM_FORMNAME, getForm().getName()); 138 symbols.put(SYM_VALUE, getValue()); 139 140 _script.execute(cycle, pageRenderSupport, symbols); 141 142 renderDelegatePrefix(writer, cycle); 143 144 writer.beginEmpty("input"); 145 writer.attribute("type", "text"); 146 writer.attribute("name", name); 147 writer.attribute("value", value); 148 writer.attribute("title", format.toLocalizedPattern()); 149 150 if (disabled) 151 writer.attribute("disabled", "disabled"); 152 153 renderIdAttribute(writer, cycle); 154 155 renderDelegateAttributes(writer, cycle); 156 157 renderContributions(writer, cycle); 158 159 renderInformalParameters(writer, cycle); 160 161 writer.printRaw(" "); 162 163 if (!disabled) 164 { 165 writer.begin("a"); 166 writer.attribute("href", (String ) symbols.get(SYM_BUTTONONCLICKHANDLER)); 167 } 168 169 IAsset icon = getIcon(); 170 171 writer.beginEmpty("img"); 172 writer.attribute("src", icon.buildURL(cycle)); 173 writer.attribute("border", 0); 174 175 if (!disabled) 176 writer.end(); 177 178 renderDelegateSuffix(writer, cycle); 179 } 180 181 184 public Object readValue() 185 { 186 return getValue(); 187 } 188 189 192 public void writeValue(Object value) 193 { 194 setValue((Date ) value); 195 } 196 197 200 private String makeStringList(String [] a, int offset, int length) 201 { 202 StringBuffer b = new StringBuffer (); 203 for (int i = offset; i < length; i++) 204 { 205 b.append('"'); 208 char[] ch = a[i].toCharArray(); 209 for (int j = 0; j < ch.length; j++) 210 { 211 if (ch[j] < 128) 212 { 213 b.append(ch[j]); 214 } 215 else 216 { 217 b.append(escape(ch[j])); 218 } 219 } 220 221 b.append('"'); 222 if (i < length - 1) 223 { 224 b.append(", "); 225 } 226 } 227 return b.toString(); 228 229 } 230 231 237 private static String escape(char c) 238 { 239 StringBuffer b = new StringBuffer (); 240 for (int i = 0; i < 4; i++) 241 { 242 b.append(Integer.toHexString(c & 0x000F).toUpperCase()); 243 c >>>= 4; 244 } 245 b.append("u\\"); 246 return b.reverse().toString(); 247 } 248 249 } | Popular Tags |