1 /*
2  * Copyright 2008 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.showcase.client.content.text;
17 
18 import com.google.gwt.core.client.GWT;
19 import com.google.gwt.core.client.RunAsyncCallback;
20 import com.google.gwt.event.dom.client.ClickEvent;
21 import com.google.gwt.event.dom.client.ClickHandler;
22 import com.google.gwt.event.dom.client.KeyUpEvent;
23 import com.google.gwt.event.dom.client.KeyUpHandler;
24 import com.google.gwt.i18n.client.Constants;
25 import com.google.gwt.i18n.shared.AnyRtlDirectionEstimator;
26 import com.google.gwt.sample.showcase.client.ContentWidget;
27 import com.google.gwt.sample.showcase.client.ShowcaseAnnotations.ShowcaseData;
28 import com.google.gwt.sample.showcase.client.ShowcaseAnnotations.ShowcaseSource;
29 import com.google.gwt.sample.showcase.client.ShowcaseAnnotations.ShowcaseStyle;
30 import com.google.gwt.user.client.rpc.AsyncCallback;
31 import com.google.gwt.user.client.ui.HTML;
32 import com.google.gwt.user.client.ui.HorizontalPanel;
33 import com.google.gwt.user.client.ui.Label;
34 import com.google.gwt.user.client.ui.PasswordTextBox;
35 import com.google.gwt.user.client.ui.TextArea;
36 import com.google.gwt.user.client.ui.TextBox;
37 import com.google.gwt.user.client.ui.TextBoxBase;
38 import com.google.gwt.user.client.ui.VerticalPanel;
39 import com.google.gwt.user.client.ui.Widget;
40 
41 /**
42  * Example file.
43  */
44 @ShowcaseStyle(/* css style names */{
45     ".gwt-TextBox", ".gwt-PasswordTextBox", ".gwt-TextArea"})
46 public class CwBasicText extends ContentWidget {
47   /**
48    * The constants used in this Content Widget.
49    */
50   @ShowcaseSource
51   public static interface CwConstants extends Constants {
cwBasicTextAreaLabel()52     String cwBasicTextAreaLabel();
53 
cwBasicTextDescription()54     String cwBasicTextDescription();
55 
cwBasicTextName()56     String cwBasicTextName();
57 
cwBasicTextNormalLabel()58     String cwBasicTextNormalLabel();
59 
cwBasicTextPasswordLabel()60     String cwBasicTextPasswordLabel();
61 
cwBasicTextReadOnly()62     String cwBasicTextReadOnly();
63 
cwBasicTextSelected()64     String cwBasicTextSelected();
65   }
66 
67   /**
68    * An instance of the constants.
69    */
70   @ShowcaseData
71   private final CwConstants constants;
72 
73   /**
74    * Constructor.
75    *
76    * @param constants the constants
77    */
CwBasicText(CwConstants constants)78   public CwBasicText(CwConstants constants) {
79     super(
80         constants.cwBasicTextName(), constants.cwBasicTextDescription(), true);
81     this.constants = constants;
82   }
83 
84   /**
85    * Initialize this example.
86    */
87   @ShowcaseSource
88   @Override
onInitialize()89   public Widget onInitialize() {
90     // Create a panel to layout the widgets
91     VerticalPanel vpanel = new VerticalPanel();
92     vpanel.setSpacing(5);
93 
94     // Add a normal and disabled text box
95     TextBox normalText = new TextBox();
96     normalText.ensureDebugId("cwBasicText-textbox");
97     // Set the normal text box to automatically adjust its direction according
98     // to the input text. Use the Any-RTL heuristic, which sets an RTL direction
99     // iff the text contains at least one RTL character.
100     normalText.setDirectionEstimator(AnyRtlDirectionEstimator.get());
101     TextBox disabledText = new TextBox();
102     disabledText.ensureDebugId("cwBasicText-textbox-disabled");
103     disabledText.setText(constants.cwBasicTextReadOnly());
104     disabledText.setEnabled(false);
105     vpanel.add(new HTML(constants.cwBasicTextNormalLabel()));
106     vpanel.add(createTextExample(normalText, true));
107     vpanel.add(createTextExample(disabledText, false));
108 
109     // Add a normal and disabled password text box
110     PasswordTextBox normalPassword = new PasswordTextBox();
111     normalPassword.ensureDebugId("cwBasicText-password");
112     PasswordTextBox disabledPassword = new PasswordTextBox();
113     disabledPassword.ensureDebugId("cwBasicText-password-disabled");
114     disabledPassword.setText(constants.cwBasicTextReadOnly());
115     disabledPassword.setEnabled(false);
116     vpanel.add(new HTML("<br><br>" + constants.cwBasicTextPasswordLabel()));
117     vpanel.add(createTextExample(normalPassword, true));
118     vpanel.add(createTextExample(disabledPassword, false));
119 
120     // Add a text area
121     TextArea textArea = new TextArea();
122     textArea.ensureDebugId("cwBasicText-textarea");
123     textArea.setVisibleLines(5);
124     vpanel.add(new HTML("<br><br>" + constants.cwBasicTextAreaLabel()));
125     vpanel.add(createTextExample(textArea, true));
126 
127     // Return the panel
128     return vpanel;
129   }
130 
131   @Override
asyncOnInitialize(final AsyncCallback<Widget> callback)132   protected void asyncOnInitialize(final AsyncCallback<Widget> callback) {
133     GWT.runAsync(CwBasicText.class, new RunAsyncCallback() {
134 
135       public void onFailure(Throwable caught) {
136         callback.onFailure(caught);
137       }
138 
139       public void onSuccess() {
140         callback.onSuccess(onInitialize());
141       }
142     });
143   }
144 
145   /**
146    * Create a TextBox example that includes the text box and an optional handler
147    * that updates a Label with the currently selected text.
148    *
149    * @param textBox the text box to handle
150    * @param addSelection add handlers to update label
151    * @return the Label that will be updated
152    */
153   @ShowcaseSource
createTextExample( final TextBoxBase textBox, boolean addSelection)154   private HorizontalPanel createTextExample(
155       final TextBoxBase textBox, boolean addSelection) {
156     // Add the text box and label to a panel
157     HorizontalPanel hPanel = new HorizontalPanel();
158     hPanel.setSpacing(4);
159     hPanel.add(textBox);
160 
161     // Add handlers
162     if (addSelection) {
163       // Create the new label
164       final Label label = new Label(constants.cwBasicTextSelected() + ": 0, 0");
165 
166       // Add a KeyUpHandler
167       textBox.addKeyUpHandler(new KeyUpHandler() {
168         public void onKeyUp(KeyUpEvent event) {
169           updateSelectionLabel(textBox, label);
170         }
171       });
172 
173       // Add a ClickHandler
174       textBox.addClickHandler(new ClickHandler() {
175         public void onClick(ClickEvent event) {
176           updateSelectionLabel(textBox, label);
177         }
178       });
179 
180       // Add the label to the box
181       hPanel.add(label);
182     }
183 
184     // Return the panel
185     return hPanel;
186   }
187 
188   /**
189    * Update the text in one of the selection labels.
190    *
191    * @param textBox the text box
192    * @param label the label to update
193    */
194   @ShowcaseSource
updateSelectionLabel(TextBoxBase textBox, Label label)195   private void updateSelectionLabel(TextBoxBase textBox, Label label) {
196     label.setText(
197         constants.cwBasicTextSelected() + ": " + textBox.getCursorPos() + ", "
198             + textBox.getSelectionLength());
199   }
200 }
201