1 24 package org.riotfamily.forms.element; 25 26 import java.net.URL ; 27 import java.text.DateFormat ; 28 import java.text.ParseException ; 29 import java.text.SimpleDateFormat ; 30 import java.util.Date ; 31 import java.util.regex.Matcher ; 32 import java.util.regex.Pattern ; 33 34 import org.riotfamily.common.util.FormatUtils; 35 import org.riotfamily.forms.DHTMLElement; 36 import org.riotfamily.forms.ErrorUtils; 37 import org.riotfamily.forms.MessageUtils; 38 import org.riotfamily.forms.TemplateUtils; 39 import org.riotfamily.forms.resource.FormResource; 40 import org.riotfamily.forms.resource.ResourceElement; 41 import org.riotfamily.forms.resource.ScriptResource; 42 import org.riotfamily.forms.resource.StylesheetResource; 43 import org.springframework.beans.propertyeditors.CustomDateEditor; 44 import org.springframework.util.StringUtils; 45 46 50 public class Calendar extends AbstractTextElement implements ResourceElement, 51 DHTMLElement { 52 53 57 private static String [] conversions = new String [] { 58 "'(.)", "$1", 59 "%", "%%", 60 "(?<!%)MMMM", "%B", 61 "(?<!%)MMM", "%b", 62 "(?<!%)MM?", "%m", 63 "(?<!%)dd", "%d", 64 "(?<!%)d", "%e", 65 "(?<!%)yyyy", "%Y", 66 "(?<!%)yy", "%y", 67 "(?<!%)hh", "%I", 68 "(?<!%)h", "%i", 69 "(?<!%)HH", "%H", 70 "(?<!%)H", "%h", 71 "(?<!%)mm?", "%M", 72 "(?<!%)ss?", "%S", 73 "(?<!%)EE?", "%a", 74 "(?<!%)w", "%W", 75 "(?<!%)a", "%P"}; 76 77 78 private static Pattern [] patterns; 79 80 static { 81 patterns = new Pattern [conversions.length / 2]; 82 for (int i = 0; i < patterns.length; i++) { 83 patterns[i] = Pattern.compile(conversions[i * 2]); 84 } 85 } 86 87 private String formatPattern = "yyyy-MM-dd"; 88 89 private String formatKey; 90 91 private String defaultValue; 92 93 private String jsFormatPattern; 94 95 private DateFormat dateFormat; 96 97 private ScriptResource resource; 98 99 100 public Calendar() { 101 setStyleClass("text calendar-input"); 102 } 103 104 public String getFormatPattern() { 105 return formatPattern; 106 } 107 108 112 public void setFormatPattern(String formatPattern) { 113 this.formatPattern = formatPattern; 114 } 115 116 public String getJsFormatPattern() { 117 return jsFormatPattern; 118 } 119 120 123 public void setFormatKey(String formatKey) { 124 this.formatKey = formatKey; 125 } 126 127 public void setDefaultValue(String defaultValue) { 128 this.defaultValue = defaultValue; 129 } 130 131 protected void afterFormSet() { 132 if (formatKey != null) { 133 formatPattern = MessageUtils.getMessage(this, formatKey); 134 } 135 jsFormatPattern = formatPattern; 136 for (int i = 0; i < patterns.length; i++) { 137 Matcher matcher = patterns[i].matcher(jsFormatPattern); 138 jsFormatPattern = matcher.replaceAll(conversions[i * 2 + 1]); 139 } 140 dateFormat = new SimpleDateFormat (formatPattern); 141 setPropertyEditor(new CustomDateEditor(dateFormat, false)); 142 } 143 144 public boolean isShowTime() { 145 return formatPattern != null && (formatPattern.indexOf('H') != -1 || 146 formatPattern.indexOf('h') != -1); 147 } 148 149 public void setValue(Object value) { 150 if (value == null && defaultValue != null) { 151 value = getDefaultDate(); 152 } 153 super.setValue(value); 154 } 155 156 protected void validate(boolean formSubmitted) { 157 super.validate(formSubmitted); 158 if (StringUtils.hasText(getText())) { 159 try { 160 dateFormat.parse(getText()); 161 } 162 catch (ParseException e) { 163 ErrorUtils.reject(this, "error.calendar.invalidDateFormat"); 164 } 165 } 166 } 167 168 protected void afterFormContextSet() { 169 String lang = getFormContext().getLocale().getLanguage().toLowerCase(); 170 URL languageScript = getClass().getResource( 171 "/org/riotfamily/resources/jscalendar/lang/calendar-" 172 + lang + ".js"); 173 174 if (languageScript == null) { 175 lang = "en"; 176 } 177 178 resource = new ScriptResource("jscalendar/calendar-setup.js", "Calendar.setup", new FormResource[] { 179 new ScriptResource("jscalendar/lang/calendar-" + lang + ".js", "Calendar._DN", 180 new ScriptResource("jscalendar/calendar.js", "Calendar") 181 ), 182 new StylesheetResource("jscalendar/calendar.css") 183 }); 184 } 185 186 public FormResource getResource() { 187 return resource; 188 } 189 190 public String getInitScript() { 191 return TemplateUtils.getInitScript(this); 192 } 193 194 protected Date getDefaultDate() { 195 Date date = FormatUtils.parseDate(defaultValue); 196 if (date == null) { 197 try { 198 date = dateFormat.parse(defaultValue); 199 } 200 catch (ParseException e) { 201 } 202 } 203 return date; 204 } 205 } 206 | Popular Tags |