-
Notifications
You must be signed in to change notification settings - Fork 225
/
DateWidget.java
170 lines (145 loc) · 5.02 KB
/
DateWidget.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package org.openmrs.module.htmlformentry.widget;
import org.openmrs.api.context.Context;
import org.openmrs.module.htmlformentry.FormEntryContext;
import org.openmrs.module.htmlformentry.FormEntryContext.Mode;
import org.openmrs.module.htmlformentry.HtmlFormEntryConstants;
import org.openmrs.module.htmlformentry.HtmlFormEntryUtil;
import org.springframework.util.StringUtils;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
/**
* A widget that allows the selection of a specific day, month, and year. To handle both a date and
* time, see {@see DateTimeWidget}.
*/
public class DateWidget implements Widget {
private Date initialValue;
private String onChangeFunction;
private String dateFormat;
private boolean hidden = false;
public DateWidget() {
}
private SimpleDateFormat dateFormat() {
String df = dateFormat != null ? dateFormat
: Context.getAdministrationService().getGlobalProperty(HtmlFormEntryConstants.GP_DATE_FORMAT);
if (StringUtils.hasText(df)) {
return new SimpleDateFormat(df, Context.getLocale());
} else {
return Context.getDateFormat();
}
}
public String getYearsRange() {
return Context.getAdministrationService().getGlobalProperty(HtmlFormEntryConstants.GP_YEARS_RANGE, "110,20");
}
private String jsDateFormat() {
String ret = dateFormat().toPattern();
if (ret.contains("yyyy"))
ret = ret.replaceAll("yyyy", "yy"); // jquery uses yy for 4-digit years
else if (ret.contains("yy"))
ret = ret.replaceAll("yy", "y"); // jquery uses y for 2-digit years
if (ret.contains("MMMM"))
ret = ret.replaceAll("MMMM", "MM"); // jquery uses MM for long month name
else if (ret.contains("MMM"))
ret = ret.replaceAll("MMM", "M"); // jquery uses M for short month name
else if (ret.contains("MM"))
ret = ret.replaceAll("MM", "mm"); // jquery uses mm for 2-digit month
else
ret = ret.replaceAll("M", "m"); // jquery uses m for month with no leading zero
return ret;
}
private String getLocaleForJquery() {
Locale loc = Context.getLocale();
String ret = loc.getLanguage();
if (StringUtils.hasText(loc.getCountry())) {
ret += "-" + loc.getCountry();
}
return ret;
}
@Override
public String generateHtml(FormEntryContext context) {
if (context.getMode() == Mode.VIEW) {
String toPrint = "";
if (initialValue != null) {
toPrint = dateFormat().format(initialValue);
return WidgetFactory.displayValue(toPrint);
} else {
toPrint = "________";
return WidgetFactory.displayEmptyValue(toPrint);
}
} else {
StringBuilder sb = new StringBuilder();
String fieldName = context.getFieldName(this);
if (!hidden) {
sb.append("<input type=\"text\" size=\"10\" id=\"").append(fieldName).append("-display\"/>");
}
sb.append("<input type=\"hidden\" name=\"").append(fieldName).append("\" id=\"").append(fieldName).append("\"");
if (onChangeFunction != null) {
sb.append(" onChange=\"" + onChangeFunction + "\" ");
}
if (hidden && initialValue != null) {
// set the value here, since it won't be set by the ui widget
sb.append(" value=\"" + new SimpleDateFormat("yyyy-MM-dd").format(initialValue) + "\"");
}
sb.append(" />");
if (!hidden) {
if ("true".equals(
Context.getAdministrationService().getGlobalProperty(HtmlFormEntryConstants.GP_SHOW_DATE_FORMAT))) {
sb.append(" (" + dateFormat().toPattern().toLowerCase() + ")");
}
sb.append("<script>setupDatePicker('" + jsDateFormat() + "', '" + getYearsRange() + "','"
+ getLocaleForJquery() + "', '#" + fieldName + "-display', '#" + fieldName + "'");
if (initialValue != null)
sb.append(", '" + new SimpleDateFormat("yyyy-MM-dd").format(initialValue) + "'");
sb.append(")</script>");
}
return sb.toString();
}
}
@Override
public Date getValue(FormEntryContext context, HttpServletRequest request) {
try {
Date d = (Date) HtmlFormEntryUtil.getParameterAsType(request, context.getFieldName(this), Date.class);
return d;
}
catch (Exception ex) {
throw new IllegalArgumentException("Illegal value");
}
}
@Override
public void setInitialValue(Object value) {
initialValue = (Date) value;
}
public Object getInitialValue() {
return this.initialValue;
}
public void setOnChangeFunction(String onChangeFunction) {
this.onChangeFunction = onChangeFunction;
}
/**
* @param dateFormat the dateFormat to set
*/
public void setDateFormat(String dateFormat) {
this.dateFormat = dateFormat;
}
public void setHidden(boolean hidden) {
this.hidden = hidden;
}
public boolean isHidden() {
return hidden;
}
public String getOnChangeFunction() {
return onChangeFunction;
}
public String getDateFormat() {
return dateFormat;
}
public DateWidget clone() {
DateWidget clone = new DateWidget();
clone.setInitialValue(this.getInitialValue());
clone.setOnChangeFunction(this.getOnChangeFunction());
clone.setHidden(this.isHidden());
clone.setDateFormat(this.getDateFormat());
return clone;
}
}