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.Label;
21: import com.google.gwt.user.client.ui.TextBox;
22: import com.google.gwt.user.client.ui.Widget;
23:
24: /**
25: * Demonstrates how to use {@link com.google.gwt.i18n.client.Messages}.
26: */
27: public class MessagesExampleController {
28: private static final ErrorMessages ERRORS = GWT
29: .create(ErrorMessages.class);
30:
31: public final TextBox txtArg1 = new TextBox();
32: public final TextBox txtArg2 = new TextBox();
33: public final TextBox txtArg3 = new TextBox();
34: public final Label lblFormattedMessage = new Label();
35: public final Label lblMessageTemplate = new Label();
36:
37: private String prevArg1;
38: private String prevArg2;
39: private String prevArg3;
40: private final MessagesExampleConstants constants;
41:
42: public MessagesExampleController(MessagesExampleConstants constants) {
43: this .constants = constants;
44:
45: String messageTemplate = ERRORS.permissionDenied("{0}", "{1}",
46: "{2}");
47: lblMessageTemplate.setText(messageTemplate);
48:
49: KeyboardListenerAdapter listener = new KeyboardListenerAdapter() {
50: @Override
51: public void onKeyUp(Widget sender, char keyCode,
52: int modifiers) {
53: maybeRefreshFormattedMessage();
54: }
55: };
56: txtArg1.addKeyboardListener(listener);
57: txtArg2.addKeyboardListener(listener);
58: txtArg3.addKeyboardListener(listener);
59:
60: txtArg1.setText("amelie");
61: txtArg2.setText("guest");
62: txtArg3.setText("/secure/blueprints.xml");
63:
64: maybeRefreshFormattedMessage();
65: }
66:
67: public MessagesExampleConstants getConstants() {
68: return constants;
69: }
70:
71: private void maybeRefreshFormattedMessage() {
72: String arg1 = txtArg1.getText().trim();
73: String arg2 = txtArg2.getText().trim();
74: String arg3 = txtArg3.getText().trim();
75:
76: if (arg1.equals(prevArg1)) {
77: if (arg2.equals(prevArg2)) {
78: if (arg3.equals(prevArg3)) {
79: // Nothing has changed.
80: return;
81: }
82: }
83: }
84:
85: prevArg1 = arg1;
86: prevArg2 = arg2;
87: prevArg3 = arg3;
88:
89: String formattedMessage = ERRORS.permissionDenied(arg1, arg2,
90: arg3);
91: lblFormattedMessage.setText(formattedMessage);
92: }
93: }
|