KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > sample > kitchensink > client > Text


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.kitchensink.client;
17
18 import com.google.gwt.user.client.DOM;
19 import com.google.gwt.user.client.ui.ClickListener;
20 import com.google.gwt.user.client.ui.HTML;
21 import com.google.gwt.user.client.ui.HorizontalPanel;
22 import com.google.gwt.user.client.ui.KeyboardListenerAdapter;
23 import com.google.gwt.user.client.ui.PasswordTextBox;
24 import com.google.gwt.user.client.ui.RichTextArea;
25 import com.google.gwt.user.client.ui.TextArea;
26 import com.google.gwt.user.client.ui.TextBox;
27 import com.google.gwt.user.client.ui.TextBoxBase;
28 import com.google.gwt.user.client.ui.VerticalPanel;
29 import com.google.gwt.user.client.ui.Widget;
30
31 /**
32  * Demonstrates the various text widgets.
33  */

34 public class Text extends Sink {
35
36   public static SinkInfo init() {
37     return new SinkInfo(
38         "Text",
39         "<h2>Basic and Rich Text</h2>"
40             + "<p>GWT includes the standard complement of text-entry widgets, each of which "
41             + "supports keyboard and selection events you can use to control text entry. "
42             + "In particular, notice that the selection range for each widget is "
43             + "updated whenever you press a key.</p>"
44             + "<p>Also notice the rich-text area to the right. This is supported on "
45             + "all major browsers, and will fall back gracefully to the level of "
46             + "functionality supported on each.</p>") {
47
48       public Sink createInstance() {
49         return new Text();
50       }
51
52       public String JavaDoc getColor() {
53         return "#2fba10";
54       }
55     };
56   }
57
58   private PasswordTextBox passwordText = new PasswordTextBox();
59   private TextArea textArea = new TextArea();
60   private TextBox textBox = new TextBox();
61
62   public Text() {
63     TextBox readOnlyTextBox = new TextBox();
64     readOnlyTextBox.setReadOnly(true);
65     readOnlyTextBox.setText("read only");
66
67     VerticalPanel vp = new VerticalPanel();
68     vp.setSpacing(8);
69     vp.add(new HTML("Normal text box:"));
70     vp.add(createTextThing(textBox, true));
71     vp.add(createTextThing(readOnlyTextBox, false));
72     vp.add(new HTML("Password text box:"));
73     vp.add(createTextThing(passwordText, true));
74     vp.add(new HTML("Text area:"));
75     vp.add(createTextThing(textArea, true));
76
77     textArea.setVisibleLines(5);
78
79     Widget richText = createRichText();
80     richText.setWidth("32em");
81
82     HorizontalPanel hp = new HorizontalPanel();
83     hp.add(vp);
84     hp.add(richText);
85     hp.setCellHorizontalAlignment(vp, HorizontalPanel.ALIGN_LEFT);
86     hp.setCellHorizontalAlignment(richText, HorizontalPanel.ALIGN_RIGHT);
87
88     initWidget(hp);
89     hp.setWidth("100%");
90   }
91
92   public void onShow() {
93   }
94
95   private Widget createRichText() {
96     RichTextArea area = new RichTextArea();
97     RichTextToolbar tb = new RichTextToolbar(area);
98
99     VerticalPanel p = new VerticalPanel();
100     p.add(tb);
101     p.add(area);
102
103     area.setHeight("14em");
104     area.setWidth("100%");
105     tb.setWidth("100%");
106     p.setWidth("100%");
107     DOM.setStyleAttribute(p.getElement(), "margin-right", "4px");
108     return p;
109   }
110
111   private Widget createTextThing(final TextBoxBase textBox,
112       boolean addSelection) {
113     HorizontalPanel p = new HorizontalPanel();
114     p.setSpacing(4);
115
116     textBox.setWidth("20em");
117     p.add(textBox);
118
119     if (addSelection) {
120       final HTML echo = new HTML();
121   
122       p.add(echo);
123       textBox.addKeyboardListener(new KeyboardListenerAdapter() {
124         public void onKeyUp(Widget sender, char keyCode, int modifiers) {
125           updateText(textBox, echo);
126         }
127       });
128   
129       textBox.addClickListener(new ClickListener() {
130         public void onClick(Widget sender) {
131           updateText(textBox, echo);
132         }
133       });
134   
135       updateText(textBox, echo);
136     }
137     return p;
138   }
139
140   private void updateText(TextBoxBase text, HTML echo) {
141     echo.setHTML("Selection: " + text.getCursorPos() + ", "
142         + text.getSelectionLength());
143   }
144 }
145
Popular Tags