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.core.client.EntryPoint;
019: import com.google.gwt.core.client.GWT;
020: import com.google.gwt.i18n.client.Dictionary;
021: import com.google.gwt.user.client.DOM;
022: import com.google.gwt.user.client.Element;
023: import com.google.gwt.user.client.ui.FlexTable;
024: import com.google.gwt.user.client.ui.ListBox;
025: import com.google.gwt.user.client.ui.RootPanel;
026: import com.google.gwt.user.client.ui.TextBox;
027: import com.google.gwt.user.client.ui.Widget;
028:
029: import java.util.Iterator;
030: import java.util.NoSuchElementException;
031:
032: /**
033: * A sample module that demonstrates how to localize text resources.
034: */
035: public class I18N implements EntryPoint {
036:
037: /**
038: * Attaches logical controllers to various elements on the page. Note that
039: * this module is logic only; it assumes the HTML handles all layout and
040: * styling.
041: *
042: * @see com.google.gwt.core.client.EntryPoint#onModuleLoad()
043: */
044: public void onModuleLoad() {
045: // An example that demonstrates NumberFormat.
046: NumberFormatExampleConstants numberFormatConstants = GWT
047: .create(NumberFormatExampleConstants.class);
048: NumberFormatExampleController numberFormatController = new NumberFormatExampleController(
049: numberFormatConstants);
050: initNumberFormatExample(numberFormatController);
051:
052: // An example that demonstrates DateTimeFormat.
053: DateTimeFormatExampleConstants dateTimeFormatConstants = GWT
054: .create(DateTimeFormatExampleConstants.class);
055: DateTimeFormatExampleController dateTimeFormatController = new DateTimeFormatExampleController(
056: dateTimeFormatConstants);
057: initDateTimeFormatExample(dateTimeFormatController);
058:
059: // An example that demonstrates Constants.
060: initConstantsExample();
061:
062: // An example that demonstrates ConstantsWithLookup.
063: ConstantsWithLookupExampleConstants constantsWithLookupConstants = GWT
064: .create(ConstantsWithLookupExampleConstants.class);
065: ConstantsWithLookupExampleController constantsWithLookupController = new ConstantsWithLookupExampleController(
066: constantsWithLookupConstants);
067: initConstantsWithLookupExample(constantsWithLookupController);
068:
069: // An example that demonstrates Messages.
070: MessagesExampleConstants messagesExampleConstants = GWT
071: .create(MessagesExampleConstants.class);
072: MessagesExampleController messagesController = new MessagesExampleController(
073: messagesExampleConstants);
074: initMessagesExample(messagesController);
075:
076: // An example that demonstrates Dictionary.
077: initDictionaryExample();
078:
079: TextBox initiallyFocusedTextBox = numberFormatController.txtInput;
080: initiallyFocusedTextBox.setFocus(true);
081: initiallyFocusedTextBox.selectAll();
082: }
083:
084: private void bindElement(String id, String text) {
085: Element elem = DOM.getElementById(id);
086: if (elem == null) {
087: throw new NoSuchElementException(id);
088: }
089: DOM.setInnerText(elem, text);
090: }
091:
092: private void bindElement(String id, Widget widget) {
093: RootPanel rp = RootPanel.get(id);
094: if (rp == null) {
095: throw new NoSuchElementException(id);
096: }
097: rp.clear();
098: rp.add(widget);
099: }
100:
101: private void initConstantsExample() {
102: ConstantsExampleConstants constants = GWT
103: .create(ConstantsExampleConstants.class);
104: ListBox colorChoices = new ListBox();
105:
106: for (String color : constants.colorMap().values()) {
107: colorChoices.addItem(color);
108: }
109:
110: TextBox txtFirstName = new TextBox();
111: TextBox txtLastName = new TextBox();
112:
113: bindElement("constantsFirstNameCaption", constants.firstName());
114: bindElement("constantsFirstNameText", txtFirstName);
115: bindElement("constantsLastNameCaption", constants.lastName());
116: bindElement("constantsLastNameText", txtLastName);
117: bindElement("constantsFavoriteColorCaption", constants
118: .favoriteColor());
119: bindElement("constantsFavoriteColorList", colorChoices);
120:
121: txtFirstName.setText("Amelie");
122: txtLastName.setText("Crutcher");
123: }
124:
125: private void initConstantsWithLookupExample(
126: ConstantsWithLookupExampleController controller) {
127: ConstantsWithLookupExampleConstants constants = controller
128: .getConstants();
129: bindElement("constantsWithLookupInputCaption", constants
130: .input());
131: bindElement("constantsWithLookupInputText", controller.txtInput);
132: bindElement("constantsWithLookupResultsCaption", constants
133: .result());
134: bindElement("constantsWithLookupResultsText",
135: controller.txtResult);
136: }
137:
138: private void initDateTimeFormatExample(
139: DateTimeFormatExampleController controller) {
140: DateTimeFormatExampleConstants constants = controller
141: .getConstants();
142: bindElement("dateTimeFormatPatternCaption", constants.pattern());
143: bindElement("dateTimeFormatPatternList",
144: controller.lstSamplePatterns);
145: bindElement("dateTimeFormatPatternText",
146: controller.txtCurrentPattern);
147: bindElement("dateTimeFormatPatternError",
148: controller.lblPatternError);
149: bindElement("dateTimeFormatInputCaption", constants
150: .inputValue());
151: bindElement("dateTimeFormatInputText", controller.txtInput);
152: bindElement("dateTimeFormatInputError",
153: controller.lblParseError);
154: bindElement("dateTimeFormatOutputCaption", constants
155: .formattedOutput());
156: bindElement("dateTimeFormatOutputText",
157: controller.lblFormattedOutput);
158: }
159:
160: private void initDictionaryExample() {
161: FlexTable t = new FlexTable();
162: t.setStyleName("i18n-dictionary");
163: bindElement("dictionaryExample", t);
164:
165: Dictionary userInfo = Dictionary.getDictionary("userInfo");
166: Iterator<String> s = userInfo.keySet().iterator();
167: for (int i = 0; s.hasNext(); i++) {
168: String key = s.next();
169: t.setText(0, i, key);
170: t.setText(1, i, userInfo.get(key));
171: }
172: t.getRowFormatter().setStyleName(0,
173: "i18n-dictionary-header-row");
174: }
175:
176: private void initMessagesExample(
177: MessagesExampleController controller) {
178: MessagesExampleConstants constants = controller.getConstants();
179: bindElement("messagesTemplateCaption", constants
180: .messageTemplate());
181: bindElement("messagesTemplateText",
182: controller.lblMessageTemplate);
183: bindElement("messagesArg1Caption", constants.arg1());
184: bindElement("messagesArg1Text", controller.txtArg1);
185: bindElement("messagesArg2Caption", constants.arg2());
186: bindElement("messagesArg2Text", controller.txtArg2);
187: bindElement("messagesArg3Caption", constants.arg3());
188: bindElement("messagesArg3Text", controller.txtArg3);
189: bindElement("messagesFormattedOutputCaption", constants
190: .formattedMessage());
191: bindElement("messagesFormattedOutputText",
192: controller.lblFormattedMessage);
193: }
194:
195: private void initNumberFormatExample(
196: NumberFormatExampleController controller) {
197: NumberFormatExampleConstants constants = controller
198: .getConstants();
199: bindElement("numberFormatPatternCaption", constants.pattern());
200: bindElement("numberFormatPatternList",
201: controller.lstSamplePatterns);
202: bindElement("numberFormatPatternText",
203: controller.txtCurrentPattern);
204: bindElement("numberFormatPatternError",
205: controller.lblPatternError);
206: bindElement("numberFormatInputCaption", constants.inputValue());
207: bindElement("numberFormatInputText", controller.txtInput);
208: bindElement("numberFormatInputError", controller.lblParseError);
209: bindElement("numberFormatOutputCaption", constants
210: .formattedOutput());
211: bindElement("numberFormatOutputText",
212: controller.lblFormattedOutput);
213: }
214: }
|