001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.sample.i18n.client;
017:
018: import com.google.gwt.i18n.client.DateTimeFormat;
019: import com.google.gwt.user.client.ui.HasText;
020:
021: import java.util.Date;
022:
023: /**
024: * Demonstrates how to use {@link DateTimeFormat}.
025: */
026: public class DateTimeFormatExampleController extends
027: AbstractFormatExampleController {
028:
029: private static final String DEFAULT_INPUT = "13 September 1999";
030: private DateTimeFormat activeFormat;
031: private final DateTimeFormatExampleConstants constants;
032:
033: public DateTimeFormatExampleController(
034:
035: final DateTimeFormatExampleConstants constants) {
036: super (DEFAULT_INPUT, constants.dateTimeFormatPatterns());
037: this .constants = constants;
038: }
039:
040: public DateTimeFormatExampleConstants getConstants() {
041: return constants;
042: }
043:
044: @Override
045: protected String doGetPattern(String patternKey) {
046: // Date + Time
047: if ("fullDateTime".equals(patternKey)) {
048: return DateTimeFormat.getFullDateTimeFormat().getPattern();
049: }
050:
051: if ("longDateTime".equals(patternKey)) {
052: return DateTimeFormat.getLongDateTimeFormat().getPattern();
053: }
054:
055: if ("mediumDateTime".equals(patternKey)) {
056: return DateTimeFormat.getMediumDateTimeFormat()
057: .getPattern();
058: }
059:
060: if ("shortDateTime".equals(patternKey)) {
061: return DateTimeFormat.getShortDateTimeFormat().getPattern();
062: }
063:
064: // Date only
065: if ("fullDate".equals(patternKey)) {
066: return DateTimeFormat.getFullDateFormat().getPattern();
067: }
068:
069: if ("longDate".equals(patternKey)) {
070: return DateTimeFormat.getLongDateFormat().getPattern();
071: }
072:
073: if ("mediumDate".equals(patternKey)) {
074: return DateTimeFormat.getMediumDateFormat().getPattern();
075: }
076:
077: if ("shortDate".equals(patternKey)) {
078: return DateTimeFormat.getShortDateFormat().getPattern();
079: }
080:
081: // Time only
082: if ("fullTime".equals(patternKey)) {
083: return DateTimeFormat.getFullTimeFormat().getPattern();
084: }
085:
086: if ("longTime".equals(patternKey)) {
087: return DateTimeFormat.getLongTimeFormat().getPattern();
088: }
089:
090: if ("mediumTime".equals(patternKey)) {
091: return DateTimeFormat.getMediumTimeFormat().getPattern();
092: }
093:
094: if ("shortTime".equals(patternKey)) {
095: return DateTimeFormat.getShortTimeFormat().getPattern();
096: }
097:
098: throw new IllegalArgumentException("Unknown pattern key '"
099: + patternKey + "'");
100: }
101:
102: @Override
103: protected void doParseAndRememberPattern(String pattern) {
104: activeFormat = DateTimeFormat.getFormat(pattern);
105: }
106:
107: @Override
108: protected void doParseInput(String toParse, HasText output,
109: HasText error) {
110: error.setText("");
111: if (!"".equals(toParse)) {
112: try {
113: Date x = new Date(toParse);
114: String s = activeFormat.format(x);
115: output.setText(s);
116: } catch (IllegalArgumentException e) {
117: String errMsg = constants.failedToParseInput();
118: error.setText(errMsg);
119: }
120: } else {
121: output.setText("<None>");
122: }
123: }
124: }
|