KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > sample > i18n > client > DateTimeFormatExampleController


1 /*
2  * Copyright 2007 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * 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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package com.google.gwt.sample.i18n.client;
17
18 import com.google.gwt.i18n.client.DateTimeFormat;
19 import com.google.gwt.user.client.ui.HasText;
20
21 import java.util.Date JavaDoc;
22
23 /**
24  * Demonstrates how to use {@link DateTimeFormat}.
25  */

26 public class DateTimeFormatExampleController extends
27     AbstractFormatExampleController {
28
29   private static final String JavaDoc DEFAULT_INPUT = "13 September 1999";
30   private DateTimeFormat activeFormat;
31   private final DateTimeFormatExampleConstants constants;
32
33   public DateTimeFormatExampleController(
34
35   final DateTimeFormatExampleConstants constants) {
36     super(DEFAULT_INPUT, constants.dateTimeFormatPatterns());
37     this.constants = constants;
38   }
39
40   public DateTimeFormatExampleConstants getConstants() {
41     return constants;
42   }
43
44   protected String JavaDoc doGetPattern(String JavaDoc patternKey) {
45     // Date + Time
46
if ("fullDateTime".equals(patternKey)) {
47       return DateTimeFormat.getFullDateTimeFormat().getPattern();
48     }
49
50     if ("longDateTime".equals(patternKey)) {
51       return DateTimeFormat.getLongDateTimeFormat().getPattern();
52     }
53
54     if ("mediumDateTime".equals(patternKey)) {
55       return DateTimeFormat.getMediumDateTimeFormat().getPattern();
56     }
57     
58     if ("shortDateTime".equals(patternKey)) {
59       return DateTimeFormat.getShortDateTimeFormat().getPattern();
60     }
61
62     // Date only
63
if ("fullDate".equals(patternKey)) {
64       return DateTimeFormat.getFullDateFormat().getPattern();
65     }
66
67     if ("longDate".equals(patternKey)) {
68       return DateTimeFormat.getLongDateFormat().getPattern();
69     }
70
71     if ("mediumDate".equals(patternKey)) {
72       return DateTimeFormat.getMediumDateFormat().getPattern();
73     }
74     
75     if ("shortDate".equals(patternKey)) {
76       return DateTimeFormat.getShortDateFormat().getPattern();
77     }
78
79     // Time only
80
if ("fullTime".equals(patternKey)) {
81       return DateTimeFormat.getFullTimeFormat().getPattern();
82     }
83
84     if ("longTime".equals(patternKey)) {
85       return DateTimeFormat.getLongTimeFormat().getPattern();
86     }
87
88     if ("mediumTime".equals(patternKey)) {
89       return DateTimeFormat.getMediumTimeFormat().getPattern();
90     }
91     
92     if ("shortTime".equals(patternKey)) {
93       return DateTimeFormat.getShortTimeFormat().getPattern();
94     }
95     
96     throw new IllegalArgumentException JavaDoc("Unknown pattern key '" + patternKey
97         + "'");
98   }
99
100   protected void doParseAndRememberPattern(String JavaDoc pattern) {
101     activeFormat = DateTimeFormat.getFormat(pattern);
102   }
103
104   protected void doParseInput(String JavaDoc toParse, HasText output, HasText error) {
105     error.setText("");
106     if (!"".equals(toParse)) {
107       try {
108         Date JavaDoc x = new Date JavaDoc(toParse);
109         String JavaDoc s = activeFormat.format(x);
110         output.setText(s);
111       } catch (IllegalArgumentException JavaDoc e) {
112         String JavaDoc errMsg = constants.failedToParseInput();
113         error.setText(errMsg);
114       }
115     } else {
116       output.setText("<None>");
117     }
118   }
119 }
120
Popular Tags