View Javadoc

1   /*
2    * $Id: TimeTools.java,v 1.5 2007/02/02 17:33:31 oeuillot Exp $
3    */
4   package org.rcfaces.core.internal.tools;
5   
6   import java.text.DateFormat;
7   import java.text.ParseException;
8   import java.util.Collections;
9   import java.util.Date;
10  import java.util.Locale;
11  
12  import javax.faces.FacesException;
13  import javax.faces.component.UIComponent;
14  import javax.faces.context.FacesContext;
15  
16  import org.rcfaces.core.internal.renderkit.IComponentRenderContext;
17  import org.rcfaces.core.lang.Time;
18  
19  /**
20   * 
21   * @author Olivier Oeuillot (latest modification by $Author: oeuillot $)
22   * @version $Revision: 1.5 $ $Date: 2007/02/02 17:33:31 $
23   */
24  public class TimeTools {
25      private static final String REVISION = "$Revision: 1.5 $";
26  
27      public static Object parseValue(FacesContext context,
28              UIComponent component, String value, boolean literalValue) {
29  
30          DateFormat dateFormat = getShortTimeFormat(component, literalValue);
31  
32          Date d;
33          try {
34              synchronized (dateFormat) {
35                  d = dateFormat.parse(value);
36  
37                  return new Time(d, dateFormat.getCalendar());
38              }
39  
40          } catch (ParseException e) {
41              throw new FacesException("Can not parse time '" + value + "'.", e);
42          }
43      }
44  
45      public static String formatValue(UIComponent component, Time time,
46              boolean literalValue) {
47          DateFormat dateFormat = getShortTimeFormat(component, literalValue);
48  
49          synchronized (dateFormat) {
50              Date date = time.getDate(dateFormat.getCalendar());
51  
52              return dateFormat.format(date);
53          }
54      }
55  
56      private static DateFormat getShortTimeFormat(UIComponent component,
57              boolean literalValue) {
58          return (DateFormat) LocaleTools.getDefaultFormat(component,
59                  LocaleTools.TIME_TYPE, literalValue);
60      }
61  
62      public static String getDefaultTimeFormatPattern(Locale locale) {
63          return LocaleTools.getDefaultPattern(locale, LocaleTools.TIME_TYPE);
64      }
65  
66      public static String normalizeTimeFormat(
67              IComponentRenderContext componentRenderContext, String format) {
68          return LocaleTools.normalizeFormat(componentRenderContext, format,
69                  LocaleTools.TIME_TYPE, Collections.EMPTY_MAP);
70      }
71  
72  }