001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.sample.kitchensink.client;
017:
018: import com.google.gwt.user.client.DOM;
019: import com.google.gwt.user.client.ui.ClickListener;
020: import com.google.gwt.user.client.ui.HTML;
021: import com.google.gwt.user.client.ui.HorizontalPanel;
022: import com.google.gwt.user.client.ui.KeyboardListenerAdapter;
023: import com.google.gwt.user.client.ui.PasswordTextBox;
024: import com.google.gwt.user.client.ui.RichTextArea;
025: import com.google.gwt.user.client.ui.TextArea;
026: import com.google.gwt.user.client.ui.TextBox;
027: import com.google.gwt.user.client.ui.TextBoxBase;
028: import com.google.gwt.user.client.ui.VerticalPanel;
029: import com.google.gwt.user.client.ui.Widget;
030:
031: /**
032: * Demonstrates the various text widgets.
033: */
034: public class Text extends Sink {
035:
036: public static SinkInfo init() {
037: return new SinkInfo(
038: "Text",
039: "<h2>Basic and Rich Text</h2>"
040: + "<p>GWT includes the standard complement of text-entry widgets, each of which "
041: + "supports keyboard and selection events you can use to control text entry. "
042: + "In particular, notice that the selection range for each widget is "
043: + "updated whenever you press a key.</p>"
044: + "<p>Also notice the rich-text area to the right. This is supported on "
045: + "all major browsers, and will fall back gracefully to the level of "
046: + "functionality supported on each.</p>") {
047:
048: @Override
049: public Sink createInstance() {
050: return new Text();
051: }
052:
053: @Override
054: public String getColor() {
055: return "#2fba10";
056: }
057: };
058: }
059:
060: private PasswordTextBox passwordText = new PasswordTextBox();
061: private TextArea textArea = new TextArea();
062: private TextBox textBox = new TextBox();
063:
064: public Text() {
065: TextBox readOnlyTextBox = new TextBox();
066: readOnlyTextBox.setReadOnly(true);
067: readOnlyTextBox.setText("read only");
068:
069: VerticalPanel vp = new VerticalPanel();
070: vp.setSpacing(8);
071: vp.add(new HTML("Normal text box:"));
072: vp.add(createTextThing(textBox, true));
073: vp.add(createTextThing(readOnlyTextBox, false));
074: vp.add(new HTML("Password text box:"));
075: vp.add(createTextThing(passwordText, true));
076: vp.add(new HTML("Text area:"));
077: vp.add(createTextThing(textArea, true));
078:
079: textArea.setVisibleLines(5);
080:
081: Widget richText = createRichText();
082: richText.setWidth("32em");
083:
084: HorizontalPanel hp = new HorizontalPanel();
085: hp.add(vp);
086: hp.add(richText);
087: hp.setCellHorizontalAlignment(vp, HorizontalPanel.ALIGN_LEFT);
088: hp.setCellHorizontalAlignment(richText,
089: HorizontalPanel.ALIGN_RIGHT);
090:
091: initWidget(hp);
092: hp.setWidth("100%");
093: }
094:
095: @Override
096: public void onShow() {
097: }
098:
099: private Widget createRichText() {
100: RichTextArea area = new RichTextArea();
101: RichTextToolbar tb = new RichTextToolbar(area);
102:
103: VerticalPanel p = new VerticalPanel();
104: p.add(tb);
105: p.add(area);
106:
107: area.setHeight("14em");
108: area.setWidth("100%");
109: tb.setWidth("100%");
110: p.setWidth("100%");
111: DOM.setStyleAttribute(p.getElement(), "margin-right", "4px");
112: return p;
113: }
114:
115: private Widget createTextThing(final TextBoxBase textBox,
116: boolean addSelection) {
117: HorizontalPanel p = new HorizontalPanel();
118: p.setSpacing(4);
119:
120: textBox.setWidth("20em");
121: p.add(textBox);
122:
123: if (addSelection) {
124: final HTML echo = new HTML();
125:
126: p.add(echo);
127: textBox.addKeyboardListener(new KeyboardListenerAdapter() {
128: @Override
129: public void onKeyUp(Widget sender, char keyCode,
130: int modifiers) {
131: updateText(textBox, echo);
132: }
133: });
134:
135: textBox.addClickListener(new ClickListener() {
136: public void onClick(Widget sender) {
137: updateText(textBox, echo);
138: }
139: });
140:
141: updateText(textBox, echo);
142: }
143: return p;
144: }
145:
146: private void updateText(TextBoxBase text, HTML echo) {
147: echo.setHTML("Selection: " + text.getCursorPos() + ", "
148: + text.getSelectionLength());
149: }
150: }
|