diff options
author | Aleksi Hietanen <aleksi@vaadin.com> | 2016-08-10 14:18:47 +0300 |
---|---|---|
committer | Vaadin Code Review <review@vaadin.com> | 2016-08-26 07:49:25 +0000 |
commit | 4a6a632b1f57a1ddbb437036cb5d17f98f827b47 (patch) | |
tree | 0a2091302a829adcfe86fd4d77545a271fa5c0b6 /server | |
parent | 24a888a2d1dc198d1d88c9e0dbdde1cfe8b0a7f1 (diff) | |
download | vaadin-framework-4a6a632b1f57a1ddbb437036cb5d17f98f827b47.tar.gz vaadin-framework-4a6a632b1f57a1ddbb437036cb5d17f98f827b47.zip |
Update TextArea to extend AbstractFieldNew (#53)
Change-Id: I72400695f3b015add07be9709e48b5f99ce619cf
Diffstat (limited to 'server')
4 files changed, 263 insertions, 8 deletions
diff --git a/server/src/main/java/com/vaadin/ui/AbstractTextField.java b/server/src/main/java/com/vaadin/ui/AbstractTextField.java index 8043766a11..75c2067759 100644 --- a/server/src/main/java/com/vaadin/ui/AbstractTextField.java +++ b/server/src/main/java/com/vaadin/ui/AbstractTextField.java @@ -26,6 +26,7 @@ import com.vaadin.event.FieldEvents.BlurListener; import com.vaadin.event.FieldEvents.FocusEvent; import com.vaadin.event.FieldEvents.FocusListener; import com.vaadin.shared.Registration; +import com.vaadin.shared.communication.FieldRpc.FocusAndBlurServerRpc; import com.vaadin.shared.ui.textfield.AbstractTextFieldClientRpc; import com.vaadin.shared.ui.textfield.AbstractTextFieldServerRpc; import com.vaadin.shared.ui.textfield.TextFieldState; @@ -43,6 +44,18 @@ public abstract class AbstractTextField extends AbstractField<String> { private final class TextFieldServerRpcImpl implements AbstractTextFieldServerRpc { + + @Override + public void setText(String text, int cursorPosition) { + getUI().getConnectorTracker().getDiffState(AbstractTextField.this) + .put("text", text); + lastKnownCursorPosition = cursorPosition; + setValue(text, true); + } + } + + private final class TextFieldFocusAndBlurRpcImpl + implements FocusAndBlurServerRpc { @Override public void blur() { fireEvent(new BlurEvent(AbstractTextField.this)); @@ -52,14 +65,6 @@ public abstract class AbstractTextField extends AbstractField<String> { public void focus() { fireEvent(new FocusEvent(AbstractTextField.this)); } - - @Override - public void setText(String text, int cursorPosition) { - getUI().getConnectorTracker().getDiffState(AbstractTextField.this) - .put("text", text); - lastKnownCursorPosition = cursorPosition; - setValue(text, true); - } } private int lastKnownCursorPosition = -1; @@ -69,6 +74,7 @@ public abstract class AbstractTextField extends AbstractField<String> { */ protected AbstractTextField() { registerRpc(new TextFieldServerRpcImpl()); + registerRpc(new TextFieldFocusAndBlurRpcImpl()); } @Override diff --git a/server/src/main/java/com/vaadin/ui/TextArea.java b/server/src/main/java/com/vaadin/ui/TextArea.java new file mode 100644 index 0000000000..e1b939d1c8 --- /dev/null +++ b/server/src/main/java/com/vaadin/ui/TextArea.java @@ -0,0 +1,138 @@ +/* + * Copyright 2000-2016 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.ui; + +import org.jsoup.nodes.Element; + +import com.vaadin.shared.ui.textarea.TextAreaServerRpc; +import com.vaadin.shared.ui.textarea.TextAreaState; +import com.vaadin.ui.declarative.DesignContext; +import com.vaadin.ui.declarative.DesignFormatter; + +/** + * A text field that supports multi line editing. + */ +public class TextArea extends AbstractTextField { + + /** + * Constructs an empty TextArea. + */ + public TextArea() { + registerRpc(new TextAreaServerRpc() { + + @Override + public void setHeight(String height) { + TextArea.this.setHeight(height); + } + + @Override + public void setWidth(String width) { + TextArea.this.setWidth(width); + } + }); + clear(); + } + + /** + * Constructs an empty TextArea with given caption. + * + * @param caption + * the caption for the field. + */ + public TextArea(String caption) { + this(); + setCaption(caption); + } + + /** + * Constructs a TextArea with given caption and value. + * + * @param caption + * the caption for the field + * @param value + * the value for the field + */ + public TextArea(String caption, String value) { + this(caption); + setValue(value); + + } + + @Override + protected TextAreaState getState() { + return (TextAreaState) super.getState(); + } + + @Override + protected TextAreaState getState(boolean markAsDirty) { + return (TextAreaState) super.getState(markAsDirty); + } + + /** + * Sets the number of rows in the text area. + * + * @param rows + * the number of rows for this text area. + */ + public void setRows(int rows) { + if (rows < 0) { + rows = 0; + } + getState().rows = rows; + } + + /** + * Gets the number of rows in the text area. + * + * @return number of explicitly set rows. + */ + public int getRows() { + return getState(false).rows; + } + + /** + * Sets the text area's word-wrap mode on or off. + * + * @param wordWrap + * <code>true</code> to use word-wrap mode <code>false</code> + * otherwise. + */ + public void setWordWrap(boolean wordWrap) { + getState().wordWrap = wordWrap; + } + + /** + * Tests if the text area is in word-wrap mode. + * + * @return <code>true</code> if the component is in word-wrap mode, + * <code>false</code> if not. + */ + public boolean isWordWrap() { + return getState(false).wordWrap; + } + + @Override + public void readDesign(Element design, DesignContext designContext) { + super.readDesign(design, designContext); + doSetValue(DesignFormatter.decodeFromTextNode(design.html())); + } + + @Override + public void writeDesign(Element design, DesignContext designContext) { + super.writeDesign(design, designContext); + design.html(DesignFormatter.encodeForTextNode(getValue())); + } +} diff --git a/server/src/test/java/com/vaadin/tests/server/component/textarea/TextAreaDeclarativeTest.java b/server/src/test/java/com/vaadin/tests/server/component/textarea/TextAreaDeclarativeTest.java new file mode 100644 index 0000000000..1b6df09c19 --- /dev/null +++ b/server/src/test/java/com/vaadin/tests/server/component/textarea/TextAreaDeclarativeTest.java @@ -0,0 +1,74 @@ +/* + * Copyright 2000-2016 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.tests.server.component.textarea; + +import java.io.IOException; + +import org.jsoup.nodes.Element; +import org.jsoup.parser.Tag; +import org.junit.Assert; +import org.junit.Test; + +import com.vaadin.tests.design.DeclarativeTestBase; +import com.vaadin.ui.TextArea; +import com.vaadin.ui.declarative.DesignContext; + +/** + * Tests declarative support for implementations of {@link TextArea}. + * + * @since 7.4 + * @author Vaadin Ltd + */ +public class TextAreaDeclarativeTest extends DeclarativeTestBase<TextArea> { + + @Test + public void testTextArea() { + String design = "<vaadin-text-area rows=6 word-wrap=false>Hello World!</vaadin-text-area>"; + TextArea ta = new TextArea(); + ta.setRows(6); + ta.setWordWrap(false); + ta.setValue("Hello World!"); + testRead(design, ta); + testWrite(design, ta); + } + + @Test + public void testHtmlEntities() throws IOException { + String design = "<vaadin-text-area>& Test</vaadin-text-area>"; + TextArea read = read(design); + Assert.assertEquals("& Test", read.getValue()); + + read.setValue("& Test"); + + DesignContext dc = new DesignContext(); + Element root = new Element(Tag.valueOf("vaadin-text-area"), ""); + read.writeDesign(root, dc); + + Assert.assertEquals("&amp; Test", root.html()); + } + + @Test + public void testReadOnlyValue() { + String design = "<vaadin-text-area readonly rows=6 word-wrap=false>Hello World!</vaadin-text-area>"; + TextArea ta = new TextArea(); + ta.setRows(6); + ta.setWordWrap(false); + ta.setValue("Hello World!"); + ta.setReadOnly(true); + testRead(design, ta); + testWrite(design, ta); + } +} diff --git a/server/src/test/java/com/vaadin/ui/TextAreaTest.java b/server/src/test/java/com/vaadin/ui/TextAreaTest.java new file mode 100644 index 0000000000..6c3d5fecd5 --- /dev/null +++ b/server/src/test/java/com/vaadin/ui/TextAreaTest.java @@ -0,0 +1,37 @@ +/* + * Copyright 2000-2016 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.ui; + +import org.junit.Assert; +import org.junit.Test; + +public class TextAreaTest { + @Test + public void initiallyEmpty() { + TextArea textArea = new TextArea(); + Assert.assertTrue(textArea.isEmpty()); + } + + @Test + public void emptyAfterClear() { + TextArea textArea = new TextArea(); + textArea.setValue("foobar"); + Assert.assertFalse(textArea.isEmpty()); + textArea.clear(); + Assert.assertTrue(textArea.isEmpty()); + } + +} |