1 /*
2 * Copyright 2006-2016 The JGUIraffe Team.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License")
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package net.sf.jguiraffe.transform;
17
18 import java.text.DateFormat;
19 import java.util.Locale;
20
21 import org.apache.commons.configuration.Configuration;
22
23 /**
24 * <p>
25 * A specialized transformer that transforms strings into date objects with both
26 * a date and time component.
27 * </p>
28 * <p>
29 * This transformer class allows user to enter a date and a time in a single
30 * input field, such as <code>1/31/2008 10:19</code>. Most of the required
31 * functionality is already implemented by the base class. This class creates an
32 * appropriate <code>DateFormat</code> object that supports parsing time
33 * stamps.
34 * </p>
35 * <p>
36 * For the documentation of the supported error messages refer to the super
37 * class. In addition to the properties defined by
38 * <code>DateTransformerBase</code>, the following properties can be set:
39 * <table border="1">
40 * <tr>
41 * <th>Property</th>
42 * <th>Description</th>
43 * <th>Default</th>
44 * </tr>
45 * <tr>
46 * <td valign="top">timeStyle</td>
47 * <td>Defines the style of the time component. This can be one of the style
48 * constants declared by the <code>java.text.DateFormat</code> class like
49 * <code>SHORT</code> or <code>FULL</code>. While the style for the date
50 * component is set by the inherited <code>style</code> property, with this
51 * property the style for the time component can be set separately.</td>
52 * <td valign="top">SHORT</td>
53 * </tr>
54 * </table>
55 * </p>
56 *
57 * @author Oliver Heger
58 * @version $Id: DateTimeTransformer.java 205 2012-01-29 18:29:57Z oheger $
59 */
60 public class DateTimeTransformer extends DateTransformerBase
61 {
62 /** Constant for the time style property. */
63 protected static final String PROP_TIME_STYLE = "timeStyle";
64
65 /** Stores the formatting style to be used for the time portion. */
66 private int timeStyle;
67
68 /**
69 * Creates a new instance of <code>DateTimeTransformer</code>.
70 */
71 public DateTimeTransformer()
72 {
73 setTimeStyle(DateFormat.SHORT);
74 }
75
76 /**
77 * Returns the style for the time portion.
78 *
79 * @return the style for the time
80 */
81 public int getTimeStyle()
82 {
83 return timeStyle;
84 }
85
86 /**
87 * Sets the style for the time. The styles for the date portion and the time
88 * portion are separately set. Here one of the constants supported by
89 * <code>java.text.DateFormat</code> (e.g. <code>SHORT</code>,
90 * <code>MEDIUM</code> etc.) can be passed in.
91 *
92 * @param timeStyle the style for the time
93 */
94 public void setTimeStyle(int timeStyle)
95 {
96 this.timeStyle = timeStyle;
97 }
98
99 /**
100 * Creates the format object to be used by this transformer. This
101 * implementation returns a date/time instance of <code>DateFormat</code>.
102 *
103 * @param locale the locale
104 * @param style the style for the date portion
105 * @param config the configuration with the properties
106 * @return the format object
107 */
108 @Override
109 protected DateFormat createFormat(Locale locale, int style,
110 Configuration config)
111 {
112 return DateFormat.getDateTimeInstance(style, config.getInt(
113 PROP_TIME_STYLE, getTimeStyle()), locale);
114 }
115 }