James Moger
2015-12-10 63e4d9660de3f4c615a8e9444aaf3c3de128e77c
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
package com.gitblit.wicket;
 
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
 
import org.apache.wicket.Session;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.form.AbstractTextComponent.ITextFormatProvider;
import org.apache.wicket.model.IModel;
import org.apache.wicket.util.convert.IConverter;
import org.apache.wicket.util.convert.converters.DateConverter;
 
public class Html5DateField extends TextField<Date> implements ITextFormatProvider {
 
    private static final long serialVersionUID = 1L;
 
    private static final String DEFAULT_PATTERN = "MM/dd/yyyy";
 
    private String datePattern = null;
 
    private IConverter converter = null;
 
    /**
     * Creates a new Html5DateField, without a specified pattern. This is the same as calling
     * <code>new Html5DateField(id, Date.class)</code>
     * 
     * @param id
     *            The id of the date field
     */
    public Html5DateField(String id)
    {
        this(id, null, defaultDatePattern());
    }
 
    /**
     * Creates a new Html5DateField, without a specified pattern. This is the same as calling
     * <code>new Html5DateField(id, object, Date.class)</code>
     * 
     * @param id
     *            The id of the date field
     * @param model
     *            The model
     */
    public Html5DateField(String id, IModel<Date> model)
    {
        this(id, model, defaultDatePattern());
    }
 
    /**
     * Creates a new Html5DateField bound with a specific <code>SimpleDateFormat</code> pattern.
     * 
     * @param id
     *            The id of the date field
     * @param datePattern
     *            A <code>SimpleDateFormat</code> pattern
     * 
     */
    public Html5DateField(String id, String datePattern)
    {
        this(id, null, datePattern);
    }
 
    /**
     * Creates a new DateTextField bound with a specific <code>SimpleDateFormat</code> pattern.
     * 
     * @param id
     *            The id of the date field
     * @param model
     *            The model
     * @param datePattern
     *            A <code>SimpleDateFormat</code> pattern
     */
    public Html5DateField(String id, IModel<Date> model, String datePattern)
    {
        super(id, model, Date.class);
        this.datePattern = datePattern;
        converter = new DateConverter()
        {
            private static final long serialVersionUID = 1L;
 
            /**
             * @see org.apache.wicket.util.convert.converters.DateConverter#getDateFormat(java.util.Locale)
             */
            @Override
            public DateFormat getDateFormat(Locale locale)
            {
                if (locale == null)
                {
                    locale = Locale.getDefault();
                }
                return new SimpleDateFormat(Html5DateField.this.datePattern, locale);
            }
        };
    }
 
    /**
     * Returns the default converter if created without pattern; otherwise it returns a
     * pattern-specific converter.
     * 
     * @param type
     *            The type for which the converter should work
     * 
     * @return A pattern-specific converter
     */
    @Override
    public IConverter getConverter(Class<?> type)
    {
        if (converter == null)
        {
            return super.getConverter(type);
        }
        else
        {
            return converter;
        }
    }
 
    /**
     * Returns the date pattern.
     * 
     * @see org.apache.wicket.markup.html.form.AbstractTextComponent.ITextFormatProvider#getTextFormat()
     */
    public String getTextFormat()
    {
        return datePattern;
    }
 
    /**
     * Try to get datePattern from user session locale. If it is not possible, it will return
     * {@link #DEFAULT_PATTERN}
     * 
     * @return date pattern
     */
    private static String defaultDatePattern()
    {
        Locale locale = Session.get().getLocale();
        if (locale != null)
        {
            DateFormat format = DateFormat.getDateInstance(DateFormat.SHORT, locale);
            if (format instanceof SimpleDateFormat)
            {
                return ((SimpleDateFormat)format).toPattern();
            }
        }
        return DEFAULT_PATTERN;
    }
    
    @Override
    protected String getInputType()
    {
        return "date";
    }
    
}