1 package fr.improve.struts.taglib.layout.field; 2 3 import java.text.DateFormat ; 4 import java.text.DateFormatSymbols ; 5 import java.text.ParsePosition ; 6 import java.text.SimpleDateFormat ; 7 import java.util.Calendar ; 8 import java.util.Date ; 9 import java.util.HashMap ; 10 import java.util.Locale ; 11 import java.util.Map ; 12 13 import javax.servlet.jsp.JspException ; 14 15 import org.apache.struts.Globals; 16 import org.apache.struts.taglib.html.Constants; 17 import org.apache.struts.taglib.html.FormTag; 18 19 import fr.improve.struts.taglib.layout.el.Expression; 20 import fr.improve.struts.taglib.layout.event.StaticCodeIncludeLayoutEvent; 21 import fr.improve.struts.taglib.layout.util.LayoutUtils; 22 import fr.improve.struts.taglib.layout.util.TagUtils; 23 28 public class DateFieldTag extends TextFieldTag { 29 public static final String CALENDAR_KEY = "fr.improve.struts.taglib.layout.field.DateFieldTag.CLANDER_KEY"; 30 private static final Map dateSymbols = new HashMap (); 31 32 35 protected String patternKey; 36 protected String pattern; 37 protected String startYear; 38 protected String endYear; 39 protected String calendarTooltip; 40 41 public DateFieldTag() { 42 } 44 protected void doAfterValue() throws JspException { 45 if (getFieldDisplayMode() == MODE_EDIT) { 46 Locale lc_locale = findLocale(); 47 StringBuffer buffer = new StringBuffer (); 48 buffer.append("<a HREF=\"javascript://\" onclick=\"showCalendar("); 49 50 Object lc_value = getFieldValue(); 51 Calendar lc_date = parseDate(lc_value==null ? "" : lc_value.toString(), lc_locale); 52 String lc_year = String.valueOf(Calendar.getInstance().get(Calendar.YEAR)); 53 54 buffer.append(lc_date.get(Calendar.YEAR)); 55 buffer.append(","); 56 buffer.append(lc_date.get(Calendar.MONTH) + 1); 57 buffer.append(","); 58 buffer.append(lc_date.get(Calendar.DAY_OF_MONTH)); 59 buffer.append(","); 60 if (pattern==null) { 61 buffer.append("null"); 62 } else { 63 buffer.append("'"); 64 buffer.append(pattern); 65 buffer.append("'"); 66 } 67 buffer.append(",'"); 68 FormTag lc_formTag = (FormTag) pageContext.findAttribute(Constants.FORM_KEY); 69 if (lc_formTag==null) { 70 throw new JspException ("<layout:date> tag is not nested into a <html:form> or <layout:form> tag"); 71 } 72 String lc_formName = lc_formTag.getBeanName(); buffer.append(lc_formName); 74 buffer.append("','"); 75 buffer.append(property); 76 buffer.append("',event,"); 77 buffer.append(startYear==null ? String.valueOf(lc_date.get(Calendar.YEAR)) : Expression.evaluate(startYear,pageContext)); 78 buffer.append(","); 79 buffer.append(endYear==null ? (startYear==null ? String.valueOf(lc_date.get(Calendar.YEAR)+2) : lc_year) : Expression.evaluate(endYear, pageContext)); 80 buffer.append(");"); 81 82 buffer.append("\"><img alt=\""); 83 buildCalendarTooltip(buffer); 84 buffer.append("\" border=\"0\" SRC=\""); 85 86 buffer.append(getSkin().getImageDirectory(pageContext.getRequest())); 87 buffer.append("/"); 88 buffer.append(getSkin().getProperty("layout.calendar")); 89 buffer.append("\"></a>"); 90 91 if (pageContext.getRequest().getAttribute(CALENDAR_KEY)==null) { 92 StringBuffer lc_buffer2 = new StringBuffer (); 94 includeClientCode(lc_buffer2, lc_date, lc_locale); 95 buffer.append(new StaticCodeIncludeLayoutEvent(this, lc_buffer2.toString()).send()); 96 pageContext.getRequest().setAttribute(CALENDAR_KEY, ""); 97 } 98 TagUtils.write(pageContext, buffer.toString()); 99 } 100 } 101 102 106 protected Locale findLocale() { 107 Locale lc_locale = (Locale ) pageContext.findAttribute(Globals.LOCALE_KEY); 108 if (lc_locale==null) { 109 lc_locale = pageContext.getRequest().getLocale(); 110 } 111 return lc_locale; 112 } 113 114 protected void buildCalendarTooltip(StringBuffer buffer) throws JspException { 115 if (calendarTooltip==null) { 116 buffer.append("pick up a date"); 117 } else { 118 buffer.append(LayoutUtils.getLabel(pageContext, bundle, calendarTooltip, null, false)); 119 } 120 } 121 122 125 protected void includeClientCode(StringBuffer out_buffer, Calendar in_calendar, Locale in_locale) { 126 out_buffer.append("<div id=\"slcalcod\" style=\"position:absolute; left:100px; top:100px; z-index:10; visibility:hidden;\"><script>printCalendar("); 127 128 DateFormatSymbols lc_symbols = (DateFormatSymbols ) dateSymbols.get(in_locale); 129 if (lc_symbols==null) { 130 lc_symbols = new DateFormatSymbols (in_locale); 131 dateSymbols.put(in_locale, lc_symbols); 132 } 133 String [] lc_daySymbols = lc_symbols.getShortWeekdays(); 134 String [] lc_monthSymbols = lc_symbols.getShortMonths(); 135 136 out_buffer.append("\"").append(lc_daySymbols[Calendar.SUNDAY]).append("\","); 138 out_buffer.append("\"").append(lc_daySymbols[Calendar.MONDAY]).append("\","); 139 out_buffer.append("\"").append(lc_daySymbols[Calendar.TUESDAY]).append("\","); 140 out_buffer.append("\"").append(lc_daySymbols[Calendar.WEDNESDAY]).append("\","); 141 out_buffer.append("\"").append(lc_daySymbols[Calendar.THURSDAY]).append("\","); 142 out_buffer.append("\"").append(lc_daySymbols[Calendar.FRIDAY]).append("\","); 143 out_buffer.append("\"").append(lc_daySymbols[Calendar.SATURDAY]).append("\","); 144 out_buffer.append(in_calendar.getFirstDayOfWeek()).append(","); 145 146 out_buffer.append("\"").append(lc_monthSymbols[Calendar.JANUARY]).append("\","); 147 out_buffer.append("\"").append(lc_monthSymbols[Calendar.FEBRUARY]).append("\","); 148 out_buffer.append("\"").append(lc_monthSymbols[Calendar.MARCH]).append("\","); 149 out_buffer.append("\"").append(lc_monthSymbols[Calendar.APRIL]).append("\","); 150 out_buffer.append("\"").append(lc_monthSymbols[Calendar.MAY]).append("\","); 151 out_buffer.append("\"").append(lc_monthSymbols[Calendar.JUNE]).append("\","); 152 out_buffer.append("\"").append(lc_monthSymbols[Calendar.JULY]).append("\","); 153 out_buffer.append("\"").append(lc_monthSymbols[Calendar.AUGUST]).append("\","); 154 out_buffer.append("\"").append(lc_monthSymbols[Calendar.SEPTEMBER]).append("\","); 155 out_buffer.append("\"").append(lc_monthSymbols[Calendar.OCTOBER]).append("\","); 156 out_buffer.append("\"").append(lc_monthSymbols[Calendar.NOVEMBER]).append("\","); 157 out_buffer.append("\"").append(lc_monthSymbols[Calendar.DECEMBER]).append("\","); 158 out_buffer.append(in_calendar.get(Calendar.DAY_OF_MONTH)).append(","); 159 out_buffer.append(in_calendar.get(Calendar.MONTH)+1).append(","); 160 out_buffer.append(in_calendar.get(Calendar.YEAR)); 161 out_buffer.append(");</script></div>"); 162 } 163 164 165 public void release() { 166 super.release(); 167 patternKey = null; 169 pattern = null; 170 startYear = null; 171 endYear = null; 172 calendarTooltip = null; 173 } 174 175 178 protected Calendar parseDate(Object in_date, Locale in_locale) throws JspException { 179 Calendar lc_calendar = Calendar.getInstance(in_locale); 180 SimpleDateFormat lc_format = null; 181 if (patternKey==null) { 182 lc_format = (SimpleDateFormat ) SimpleDateFormat.getDateInstance(DateFormat.SHORT, in_locale); 184 pattern = lc_format.toPattern(); 185 } else { 186 pattern = LayoutUtils.getLabel(pageContext, bundle, patternKey, null, true); 188 if (pattern==null) { 189 pattern = LayoutUtils.getLabel(pageContext, patternKey, null); 191 } 192 lc_format = new SimpleDateFormat (pattern); 193 } 194 if (in_date!=null && in_date.toString().length()>0) { 195 ParsePosition lc_position = new ParsePosition (0); 196 Date lc_date = lc_format.parse(in_date.toString(), lc_position); 197 if (lc_position.getIndex()==in_date.toString().length() && lc_date!=null) { 198 lc_calendar.setTime(lc_date); 199 } 200 } 201 return lc_calendar; 202 } 203 207 public void setPatternKey(String patternKey) { 208 this.patternKey = patternKey; 209 } 210 211 public void setEndYear(String in_string) { 212 endYear = in_string; 213 } 214 215 public void setStartYear(String in_string) { 216 startYear = in_string; 217 } 218 219 public void setCalendarTooltip(String in_string) { 220 calendarTooltip = in_string; 221 } 222 223 } 224 | Popular Tags |