diff options
author | Artur Signell <artur@vaadin.com> | 2016-08-18 22:54:48 +0300 |
---|---|---|
committer | Artur Signell <artur@vaadin.com> | 2016-08-20 00:22:06 +0300 |
commit | f23880749d9c2dca3ef4fd31b21fcc94b5220b42 (patch) | |
tree | 51143b1f4a379017419e7a9f81a59e553eb4e06a /compatibility-server/src | |
parent | be6a0cfd847593985b6f226646c576df1906695b (diff) | |
download | vaadin-framework-f23880749d9c2dca3ef4fd31b21fcc94b5220b42.tar.gz vaadin-framework-f23880749d9c2dca3ef4fd31b21fcc94b5220b42.zip |
Move RichTextArea to compatibility package
Change-Id: Ie73adbb0ddaf98aed6554f658625f1d812c3342b
Diffstat (limited to 'compatibility-server/src')
4 files changed, 494 insertions, 0 deletions
diff --git a/compatibility-server/src/main/java/com/vaadin/ui/RichTextArea.java b/compatibility-server/src/main/java/com/vaadin/ui/RichTextArea.java new file mode 100644 index 0000000000..e52db6d043 --- /dev/null +++ b/compatibility-server/src/main/java/com/vaadin/ui/RichTextArea.java @@ -0,0 +1,318 @@ +/* + * 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 java.util.Map; + +import org.jsoup.nodes.Element; + +import com.vaadin.data.Property; +import com.vaadin.server.PaintException; +import com.vaadin.server.PaintTarget; +import com.vaadin.shared.ui.textarea.RichTextAreaState; +import com.vaadin.ui.declarative.DesignContext; +import com.vaadin.v7.ui.LegacyAbstractField; +import com.vaadin.v7.ui.LegacyTextField; + +/** + * A simple RichTextArea to edit HTML format text. + * + * Note, that using {@link LegacyTextField#setMaxLength(int)} method in + * {@link RichTextArea} may produce unexpected results as formatting is counted + * into length of field. + */ +public class RichTextArea extends LegacyAbstractField<String> + implements LegacyComponent { + + /** + * Null representation. + */ + private String nullRepresentation = "null"; + + /** + * Is setting to null from non-null value allowed by setting with null + * representation . + */ + private boolean nullSettingAllowed = false; + + /** + * Temporary flag that indicates all content will be selected after the next + * paint. Reset to false after painted. + */ + private boolean selectAll = false; + + /** + * Constructs an empty <code>RichTextArea</code> with no caption. + */ + public RichTextArea() { + setValue(""); + } + + /** + * + * Constructs an empty <code>RichTextArea</code> with the given caption. + * + * @param caption + * the caption for the editor. + */ + public RichTextArea(String caption) { + this(); + setCaption(caption); + } + + /** + * Constructs a new <code>RichTextArea</code> that's bound to the specified + * <code>Property</code> and has no caption. + * + * @param dataSource + * the data source for the editor value + */ + public RichTextArea(Property dataSource) { + setPropertyDataSource(dataSource); + } + + /** + * Constructs a new <code>RichTextArea</code> that's bound to the specified + * <code>Property</code> and has the given caption. + * + * @param caption + * the caption for the editor. + * @param dataSource + * the data source for the editor value + */ + public RichTextArea(String caption, Property dataSource) { + this(dataSource); + setCaption(caption); + } + + /** + * Constructs a new <code>RichTextArea</code> with the given caption and + * initial text contents. + * + * @param caption + * the caption for the editor. + * @param value + * the initial text content of the editor. + */ + public RichTextArea(String caption, String value) { + setValue(value); + setCaption(caption); + } + + @Override + public void paintContent(PaintTarget target) throws PaintException { + if (selectAll) { + target.addAttribute("selectAll", true); + selectAll = false; + } + + // Adds the content as variable + String value = getValue(); + if (value == null) { + value = getNullRepresentation(); + } + if (value == null) { + throw new IllegalStateException( + "Null values are not allowed if the null-representation is null"); + } + target.addVariable(this, "text", value); + + } + + @Override + public void setReadOnly(boolean readOnly) { + super.setReadOnly(readOnly); + // IE6 cannot support multi-classname selectors properly + // TODO Can be optimized now that support for I6 is dropped + if (readOnly) { + addStyleName("v-richtextarea-readonly"); + } else { + removeStyleName("v-richtextarea-readonly"); + } + } + + /** + * Selects all text in the rich text area. As a side effect, focuses the + * rich text area. + * + * @since 6.5 + */ + public void selectAll() { + /* + * Set selection range functionality is currently being + * planned/developed for GWT RTA. Only selecting all is currently + * supported. Consider moving selectAll and other selection related + * functions to AbstractTextField at that point to share the + * implementation. Some third party components extending + * AbstractTextField might however not want to support them. + */ + selectAll = true; + focus(); + markAsDirty(); + } + + @Override + public void changeVariables(Object source, Map<String, Object> variables) { + // Sets the text + if (variables.containsKey("text") && !isReadOnly()) { + + // Only do the setting if the string representation of the value + // has been updated + String newValue = (String) variables.get("text"); + + final String oldValue = getValue(); + if (newValue != null && (oldValue == null || isNullSettingAllowed()) + && newValue.equals(getNullRepresentation())) { + newValue = null; + } + if (newValue != oldValue + && (newValue == null || !newValue.equals(oldValue))) { + boolean wasModified = isModified(); + setValue(newValue, true); + + // If the modified status changes, + // repaint is needed after all. + if (wasModified != isModified()) { + markAsDirty(); + } + } + } + + } + + @Override + public Class<String> getType() { + return String.class; + } + + /** + * Gets the null-string representation. + * + * <p> + * The null-valued strings are represented on the user interface by + * replacing the null value with this string. If the null representation is + * set null (not 'null' string), painting null value throws exception. + * </p> + * + * <p> + * The default value is string 'null'. + * </p> + * + * @return the String Textual representation for null strings. + * @see LegacyTextField#isNullSettingAllowed() + */ + public String getNullRepresentation() { + return nullRepresentation; + } + + /** + * Is setting nulls with null-string representation allowed. + * + * <p> + * If this property is true, writing null-representation string to text + * field always sets the field value to real null. If this property is + * false, null setting is not made, but the null values are maintained. + * Maintenance of null-values is made by only converting the textfield + * contents to real null, if the text field matches the null-string + * representation and the current value of the field is null. + * </p> + * + * <p> + * By default this setting is false + * </p> + * + * @return boolean Should the null-string represenation be always converted + * to null-values. + * @see LegacyTextField#getNullRepresentation() + */ + public boolean isNullSettingAllowed() { + return nullSettingAllowed; + } + + /** + * Sets the null-string representation. + * + * <p> + * The null-valued strings are represented on the user interface by + * replacing the null value with this string. If the null representation is + * set null (not 'null' string), painting null value throws exception. + * </p> + * + * <p> + * The default value is string 'null' + * </p> + * + * @param nullRepresentation + * Textual representation for null strings. + * @see LegacyTextField#setNullSettingAllowed(boolean) + */ + public void setNullRepresentation(String nullRepresentation) { + this.nullRepresentation = nullRepresentation; + } + + /** + * Sets the null conversion mode. + * + * <p> + * If this property is true, writing null-representation string to text + * field always sets the field value to real null. If this property is + * false, null setting is not made, but the null values are maintained. + * Maintenance of null-values is made by only converting the textfield + * contents to real null, if the text field matches the null-string + * representation and the current value of the field is null. + * </p> + * + * <p> + * By default this setting is false. + * </p> + * + * @param nullSettingAllowed + * Should the null-string represenation be always converted to + * null-values. + * @see LegacyTextField#getNullRepresentation() + */ + public void setNullSettingAllowed(boolean nullSettingAllowed) { + this.nullSettingAllowed = nullSettingAllowed; + } + + @Override + public boolean isEmpty() { + return super.isEmpty() || getValue().length() == 0; + } + + @Override + public void clear() { + setValue(""); + } + + @Override + public void readDesign(Element design, DesignContext designContext) { + super.readDesign(design, designContext); + setValue(design.html(), false, true); + } + + @Override + public void writeDesign(Element design, DesignContext designContext) { + super.writeDesign(design, designContext); + design.html(getValue()); + } + + @Override + protected RichTextAreaState getState() { + return (RichTextAreaState) super.getState(); + } +} diff --git a/compatibility-server/src/test/java/com/vaadin/tests/server/component/richtextarea/RichTextAreaDeclarativeTest.java b/compatibility-server/src/test/java/com/vaadin/tests/server/component/richtextarea/RichTextAreaDeclarativeTest.java new file mode 100644 index 0000000000..eec1781f81 --- /dev/null +++ b/compatibility-server/src/test/java/com/vaadin/tests/server/component/richtextarea/RichTextAreaDeclarativeTest.java @@ -0,0 +1,70 @@ +/* + * 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.richtextarea; + +import org.junit.Test; + +import com.vaadin.tests.design.DeclarativeTestBase; +import com.vaadin.ui.RichTextArea; + +public class RichTextAreaDeclarativeTest + extends DeclarativeTestBase<RichTextArea> { + + private String getBasicDesign() { + return "<vaadin-rich-text-area null-representation='' null-setting-allowed>\n" + + "\n <b>Header</b> <br/>Some text\n " + + "</vaadin-rich-text-area>"; + } + + private RichTextArea getBasicExpected() { + RichTextArea rta = new RichTextArea(); + rta.setNullRepresentation(""); + rta.setNullSettingAllowed(true); + rta.setValue("<b>Header</b> \n<br>Some text"); + return rta; + } + + @Test + public void testBasicRead() { + testRead(getBasicDesign(), getBasicExpected()); + } + + @Test + public void testBasicWrite() { + testWrite(getBasicDesign(), getBasicExpected()); + } + + @Test + public void testReadEmpty() { + testRead("<vaadin-rich-text-area />", new RichTextArea()); + } + + @Test + public void testWriteEmpty() { + testWrite("<vaadin-rich-text-area />", new RichTextArea()); + } + + @Test + public void testReadOnlyValue() { + String design = "<vaadin-rich-text-area readonly style-name='v-richtextarea-readonly'>Hello World!</vaadin-text-area>"; + RichTextArea ta = new RichTextArea(); + ta.setValue("Hello World!"); + ta.setReadOnly(true); + + testRead(design, ta); + testWrite(design, ta); + } +} diff --git a/compatibility-server/src/test/java/com/vaadin/tests/server/component/richtextarea/RichTextAreaStateTest.java b/compatibility-server/src/test/java/com/vaadin/tests/server/component/richtextarea/RichTextAreaStateTest.java new file mode 100644 index 0000000000..feb3b2e7b0 --- /dev/null +++ b/compatibility-server/src/test/java/com/vaadin/tests/server/component/richtextarea/RichTextAreaStateTest.java @@ -0,0 +1,59 @@ +/* + * 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.richtextarea; + +import org.junit.Assert; +import org.junit.Test; + +import com.vaadin.shared.ui.textarea.RichTextAreaState; +import com.vaadin.ui.RichTextArea; + +/** + * Tests for RichTextArea State. + * + */ +public class RichTextAreaStateTest { + @Test + public void getState_areaHasCustomState() { + TestRichTextArea area = new TestRichTextArea(); + RichTextAreaState state = area.getState(); + Assert.assertEquals("Unexpected state class", RichTextAreaState.class, + state.getClass()); + } + + @Test + public void getPrimaryStyleName_areaHasCustomPrimaryStyleName() { + RichTextArea area = new RichTextArea(); + RichTextAreaState state = new RichTextAreaState(); + Assert.assertEquals("Unexpected primary style name", + state.primaryStyleName, area.getPrimaryStyleName()); + } + + @Test + public void areaStateHasCustomPrimaryStyleName() { + RichTextAreaState state = new RichTextAreaState(); + Assert.assertEquals("Unexpected primary style name", "v-richtextarea", + state.primaryStyleName); + } + + private static class TestRichTextArea extends RichTextArea { + + @Override + public RichTextAreaState getState() { + return super.getState(); + } + } +} diff --git a/compatibility-server/src/test/java/com/vaadin/ui/RichTextAreaTest.java b/compatibility-server/src/test/java/com/vaadin/ui/RichTextAreaTest.java new file mode 100644 index 0000000000..8b6b2d011e --- /dev/null +++ b/compatibility-server/src/test/java/com/vaadin/ui/RichTextAreaTest.java @@ -0,0 +1,47 @@ +/* + * 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; + +import com.vaadin.data.util.ObjectProperty; + +public class RichTextAreaTest { + @Test + public void initiallyEmpty() { + RichTextArea tf = new RichTextArea(); + Assert.assertTrue(tf.isEmpty()); + } + + @Test + public void emptyAfterClearUsingPDS() { + RichTextArea tf = new RichTextArea(new ObjectProperty<String>("foo")); + Assert.assertFalse(tf.isEmpty()); + tf.clear(); + Assert.assertTrue(tf.isEmpty()); + } + + @Test + public void emptyAfterClear() { + RichTextArea tf = new RichTextArea(); + tf.setValue("foobar"); + Assert.assertFalse(tf.isEmpty()); + tf.clear(); + Assert.assertTrue(tf.isEmpty()); + } + +} |