01: /*
02: * Copyright 2007 Google Inc.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
05: * use this file except in compliance with the License. You may obtain a copy of
06: * the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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.core.client.GWT;
19: import com.google.gwt.user.client.ui.KeyboardListenerAdapter;
20: import com.google.gwt.user.client.ui.TextBox;
21: import com.google.gwt.user.client.ui.Widget;
22:
23: import java.util.MissingResourceException;
24:
25: /**
26: * Demonstrates {@link com.google.gwt.i18n.client.ConstantsWithLookup}.
27: */
28: public class ConstantsWithLookupExampleController {
29:
30: private static final String DEFAULT_INPUT = "red";
31: private static final ColorConstants COLORS = GWT
32: .create(ColorConstants.class);
33:
34: public final TextBox txtInput = new TextBox();
35: public final TextBox txtResult = new TextBox();
36: private String prevText;
37: private final ConstantsWithLookupExampleConstants constants;
38:
39: public ConstantsWithLookupExampleController(
40: final ConstantsWithLookupExampleConstants constants) {
41:
42: this .constants = constants;
43: txtResult.setText(constants.noInputResult());
44: txtResult.setReadOnly(true);
45:
46: txtInput.addKeyboardListener(new KeyboardListenerAdapter() {
47: @Override
48: public void onKeyUp(Widget sender, char keyCode,
49: int modifiers) {
50: maybeRefreshLookup(constants);
51: }
52: });
53:
54: txtInput.setText(DEFAULT_INPUT);
55: maybeRefreshLookup(constants);
56: }
57:
58: public ConstantsWithLookupExampleConstants getConstants() {
59: return constants;
60: }
61:
62: private void maybeRefreshLookup(
63: final ConstantsWithLookupExampleConstants constants) {
64: final String currText = txtInput.getText().trim();
65: if (!currText.equals(prevText)) {
66: prevText = currText;
67: if ("".equals(currText)) {
68: txtResult.setText(constants.noInputResult());
69: } else {
70: try {
71: String color = COLORS.getString(currText);
72: txtResult.setText(color);
73: } catch (MissingResourceException e) {
74: txtResult.setText(constants.noMatchingResult());
75: }
76: }
77: }
78: }
79: }
|