KickJava   Java API By Example, From Geeks To Geeks.

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


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.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 JavaDoc;
24
25 /**
26  * Demonstrates {@link ConstantsWithLookup}.
27  */

28 public class ConstantsWithLookupExampleController {
29
30   private static final String JavaDoc DEFAULT_INPUT = "red";
31   private static final ColorConstants COLORS = (ColorConstants) GWT.create(ColorConstants.class);
32
33   public final TextBox txtInput = new TextBox();
34   public final TextBox txtResult = new TextBox();
35   private String JavaDoc prevText;
36   private final ConstantsWithLookupExampleConstants constants;
37
38   public ConstantsWithLookupExampleController(
39       final ConstantsWithLookupExampleConstants constants) {
40
41     this.constants = constants;
42     txtResult.setText(constants.noInputResult());
43     txtResult.setReadOnly(true);
44
45     txtInput.addKeyboardListener(new KeyboardListenerAdapter() {
46       public void onKeyUp(Widget sender, char keyCode, int modifiers) {
47         maybeRefreshLookup(constants);
48       }
49     });
50
51     txtInput.setText(DEFAULT_INPUT);
52     maybeRefreshLookup(constants);
53   }
54
55   public ConstantsWithLookupExampleConstants getConstants() {
56     return constants;
57   }
58
59   private void maybeRefreshLookup(
60       final ConstantsWithLookupExampleConstants constants) {
61     final String JavaDoc currText = txtInput.getText().trim();
62     if (!currText.equals(prevText)) {
63       prevText = currText;
64       if ("".equals(currText)) {
65         txtResult.setText(constants.noInputResult());
66       } else {
67         try {
68           String JavaDoc color = COLORS.getString(currText);
69           txtResult.setText(color);
70         } catch (MissingResourceException JavaDoc e) {
71           txtResult.setText(constants.noMatchingResult());
72         }
73       }
74     }
75   }
76 }
77
Popular Tags