1 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 ; 22 23 26 public class DateTimeFormatExampleController extends 27 AbstractFormatExampleController { 28 29 private static final String 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 doGetPattern(String patternKey) { 45 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 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 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 ("Unknown pattern key '" + patternKey 97 + "'"); 98 } 99 100 protected void doParseAndRememberPattern(String pattern) { 101 activeFormat = DateTimeFormat.getFormat(pattern); 102 } 103 104 protected void doParseInput(String toParse, HasText output, HasText error) { 105 error.setText(""); 106 if (!"".equals(toParse)) { 107 try { 108 Date x = new Date (toParse); 109 String s = activeFormat.format(x); 110 output.setText(s); 111 } catch (IllegalArgumentException e) { 112 String errMsg = constants.failedToParseInput(); 113 error.setText(errMsg); 114 } 115 } else { 116 output.setText("<None>"); 117 } 118 } 119 } 120 | Popular Tags |