diff options
author | Artur Signell <artur@vaadin.com> | 2016-09-01 14:56:41 +0300 |
---|---|---|
committer | Vaadin Code Review <review@vaadin.com> | 2016-09-12 08:11:33 +0000 |
commit | 78a5468279ddc442ac64d045f5fe4aa79ed9ef6e (patch) | |
tree | 4aabf5ea7495e1b0a3e39dc40ab1813bbe67dd69 /server/src/main/java/com/vaadin | |
parent | ea89e24646cead0eef80dd42a7426fae4e0a6092 (diff) | |
download | vaadin-framework-78a5468279ddc442ac64d045f5fe4aa79ed9ef6e.tar.gz vaadin-framework-78a5468279ddc442ac64d045f5fe4aa79ed9ef6e.zip |
Implement new RichTextArea
Change-Id: I6f430c77caaad6d610133f340eba960f2268897e
Diffstat (limited to 'server/src/main/java/com/vaadin')
5 files changed, 259 insertions, 56 deletions
diff --git a/server/src/main/java/com/vaadin/ui/AbstractField.java b/server/src/main/java/com/vaadin/ui/AbstractField.java index f64cec9e66..4946d3c95b 100644 --- a/server/src/main/java/com/vaadin/ui/AbstractField.java +++ b/server/src/main/java/com/vaadin/ui/AbstractField.java @@ -142,21 +142,26 @@ public abstract class AbstractField<T> extends AbstractComponent * * @param value * the new value to set - * @return {@code true} if this event originates from the client, - * {@code false} otherwise. + * @param userOriginated + * {@code true} if this event originates from the client, + * {@code false} otherwise. + * @return <code>true</code> if the value was updated, <code>false</code> + * otherwise */ - protected void setValue(T value, boolean userOriginated) { + protected boolean setValue(T value, boolean userOriginated) { if (userOriginated && isReadOnly()) { - return; + return false; } if (Objects.equals(value, getValue())) { - return; + return false; } doSetValue(value); if (!userOriginated) { markAsDirty(); } fireEvent(createValueChange(userOriginated)); + + return true; } /** diff --git a/server/src/main/java/com/vaadin/ui/AbstractTextField.java b/server/src/main/java/com/vaadin/ui/AbstractTextField.java index e1b0273c6c..b5119ec92c 100644 --- a/server/src/main/java/com/vaadin/ui/AbstractTextField.java +++ b/server/src/main/java/com/vaadin/ui/AbstractTextField.java @@ -27,10 +27,10 @@ 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.ValueChangeMode; import com.vaadin.shared.ui.textfield.AbstractTextFieldClientRpc; import com.vaadin.shared.ui.textfield.AbstractTextFieldServerRpc; import com.vaadin.shared.ui.textfield.AbstractTextFieldState; -import com.vaadin.shared.ui.textfield.ValueChangeMode; import com.vaadin.ui.declarative.DesignAttributeHandler; import com.vaadin.ui.declarative.DesignContext; @@ -40,7 +40,8 @@ import com.vaadin.ui.declarative.DesignContext; * @author Vaadin Ltd. * @since 8.0 */ -public abstract class AbstractTextField extends AbstractField<String> { +public abstract class AbstractTextField extends AbstractField<String> + implements HasValueChangeMode { private final class AbstractTextFieldServerRpcImpl implements AbstractTextFieldServerRpc { @@ -173,6 +174,7 @@ public abstract class AbstractTextField extends AbstractField<String> { /** * Returns the last known cursor position of the field. * + * @return the last known cursor position */ public int getCursorPosition() { return lastKnownCursorPosition; @@ -212,41 +214,17 @@ public abstract class AbstractTextField extends AbstractField<String> { listener); } - /** - * Sets the mode how the TextField triggers {@link ValueChange}s. - * - * @param mode - * the new mode - * - * @see ValueChangeMode - */ + @Override public void setValueChangeMode(ValueChangeMode mode) { getState().valueChangeMode = mode; } - /** - * Returns the currently set {@link ValueChangeMode}. - * - * @return the mode used to trigger {@link ValueChange}s. - * - * @see ValueChangeMode - */ + @Override public ValueChangeMode getValueChangeMode() { return getState(false).valueChangeMode; } - /** - * Sets how often {@link ValueChange}s are triggered when the - * {@link ValueChangeMode} is set to either {@link ValueChangeMode#LAZY} or - * {@link ValueChangeMode#TIMEOUT}. - * - * @param timeout - * timeout in milliseconds, must be greater or equal to 0 - * @throws IllegalArgumentException - * if given timeout is smaller than 0 - * - * @see ValueChangeMode - */ + @Override public void setValueChangeTimeout(int timeout) { if (timeout < 0) { throw new IllegalArgumentException( @@ -255,15 +233,7 @@ public abstract class AbstractTextField extends AbstractField<String> { getState().valueChangeTimeout = timeout; } - /** - * Returns the currently set timeout, in milliseconds, for how often - * {@link ValueChange}s are triggered if the current {@link ValueChangeMode} - * is set to either {@link ValueChangeMode#LAZY} or - * {@link ValueChangeMode#TIMEOUT}. - * - * @return the timeout in milliseconds of how often {@link ValueChange}s are - * triggered. - */ + @Override public int getValueChangeTimeout() { return getState(false).valueChangeTimeout; } @@ -303,7 +273,8 @@ public abstract class AbstractTextField extends AbstractField<String> { /** * Checks if the field is empty. * - * @return true if the field value is an empty string, false otherwise + * @return <code>true</code> if the field value is an empty string, + * <code>false</code> otherwise */ public boolean isEmpty() { return "".equals(getValue()); diff --git a/server/src/main/java/com/vaadin/ui/HasValueChangeMode.java b/server/src/main/java/com/vaadin/ui/HasValueChangeMode.java new file mode 100644 index 0000000000..0d48a29a72 --- /dev/null +++ b/server/src/main/java/com/vaadin/ui/HasValueChangeMode.java @@ -0,0 +1,69 @@ +/* + * 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 com.vaadin.data.HasValue.ValueChange; +import com.vaadin.shared.ui.ValueChangeMode; + +/** + * Implemented by components which support value change modes. + */ +public interface HasValueChangeMode extends Component { + /** + * Sets the mode how the TextField triggers {@link ValueChange}s. + * + * @param valueChangeMode + * the new mode + * + * @see ValueChangeMode + */ + public void setValueChangeMode(ValueChangeMode valueChangeMode); + + /** + * Returns the currently set {@link ValueChangeMode}. + * + * @return the mode used to trigger {@link ValueChange}s. + * + * @see ValueChangeMode + */ + public ValueChangeMode getValueChangeMode(); + + /** + * Sets how often {@link ValueChange}s are triggered when the + * {@link ValueChangeMode} is set to either {@link ValueChangeMode#LAZY} or + * {@link ValueChangeMode#TIMEOUT}. + * + * @param valueChangeTimeout + * timeout in milliseconds, must be greater or equal to 0 + * @throws IllegalArgumentException + * if given timeout is smaller than 0 + * + * @see ValueChangeMode + */ + public void setValueChangeTimeout(int valueChangeTimeout); + + /** + * Returns the currently set timeout, in milliseconds, for how often + * {@link ValueChange}s are triggered if the current {@link ValueChangeMode} + * is set to either {@link ValueChangeMode#LAZY} or + * {@link ValueChangeMode#TIMEOUT}. + * + * @return the timeout in milliseconds of how often {@link ValueChange}s are + * triggered. + */ + public int getValueChangeTimeout(); + +} diff --git a/server/src/main/java/com/vaadin/ui/PasswordField.java b/server/src/main/java/com/vaadin/ui/PasswordField.java index a3fb4d265a..231236c01f 100644 --- a/server/src/main/java/com/vaadin/ui/PasswordField.java +++ b/server/src/main/java/com/vaadin/ui/PasswordField.java @@ -59,12 +59,6 @@ public class PasswordField extends TextField { setCaption(caption); } - /* - * (non-Javadoc) - * - * @see com.vaadin.ui.AbstractField#readDesign(org.jsoup.nodes.Element , - * com.vaadin.ui.declarative.DesignContext) - */ @Override public void readDesign(Element design, DesignContext designContext) { super.readDesign(design, designContext); @@ -75,12 +69,6 @@ public class PasswordField extends TextField { } } - /* - * (non-Javadoc) - * - * @see com.vaadin.ui.AbstractTextField#writeDesign(org.jsoup.nodes.Element - * , com.vaadin.ui.declarative.DesignContext) - */ @Override public void writeDesign(Element design, DesignContext designContext) { super.writeDesign(design, designContext); diff --git a/server/src/main/java/com/vaadin/ui/RichTextArea.java b/server/src/main/java/com/vaadin/ui/RichTextArea.java new file mode 100644 index 0000000000..49346928be --- /dev/null +++ b/server/src/main/java/com/vaadin/ui/RichTextArea.java @@ -0,0 +1,170 @@ +/* + * 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.ValueChangeMode; +import com.vaadin.shared.ui.richtextarea.RichTextAreaClientRpc; +import com.vaadin.shared.ui.richtextarea.RichTextAreaServerRpc; +import com.vaadin.shared.ui.richtextarea.RichTextAreaState; +import com.vaadin.ui.declarative.DesignContext; + +/** + * A simple RichTextArea to edit HTML format text. + */ +public class RichTextArea extends AbstractField<String> + implements HasValueChangeMode { + + private class RichTextAreaServerRpcImpl implements RichTextAreaServerRpc { + @Override + public void setText(String text) { + getUI().getConnectorTracker().getDiffState(RichTextArea.this) + .put("value", text); + if (!setValue(text, true)) { + // The value was not updated, this could happen if the field has + // been set to readonly on the server and the client does not + // know about it yet. Must re-send the correct state back. + markAsDirty(); + } + } + } + + /** + * Constructs an empty <code>RichTextArea</code> with no caption. + */ + public RichTextArea() { + super(); + registerRpc(new RichTextAreaServerRpcImpl()); + 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> 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) { + this(caption); + setValue(value); + } + + @Override + public void readDesign(Element design, DesignContext designContext) { + super.readDesign(design, designContext); + setValue(design.html()); + } + + @Override + public void writeDesign(Element design, DesignContext designContext) { + super.writeDesign(design, designContext); + design.html(getValue()); + } + + @Override + protected RichTextAreaState getState() { + return (RichTextAreaState) super.getState(); + } + + @Override + protected RichTextAreaState getState(boolean markAsDirty) { + return (RichTextAreaState) super.getState(markAsDirty); + } + + @Override + public void setValue(String value) { + if (value == null) { + setValue("", false); + } else { + setValue(value, false); + } + } + + @Override + public String getValue() { + return getState(false).value; + } + + @Override + protected void doSetValue(String value) { + getState().value = value; + } + + /** + * Selects all text in the rich text area. As a side effect, focuses the + * rich text area. + * + * @since 6.5 + */ + public void selectAll() { + getRpcProxy(RichTextAreaClientRpc.class).selectAll(); + focus(); + } + + @Override + public void setValueChangeMode(ValueChangeMode mode) { + getState().valueChangeMode = mode; + } + + @Override + public ValueChangeMode getValueChangeMode() { + return getState(false).valueChangeMode; + } + + @Override + public void setValueChangeTimeout(int timeout) { + getState().valueChangeTimeout = timeout; + + } + + @Override + public int getValueChangeTimeout() { + return getState(false).valueChangeTimeout; + } + + /** + * Checks if the field is empty. + * + * @return <code>true</code> if the field value is an empty string, + * <code>false</code> otherwise + */ + public boolean isEmpty() { + return getValue().length() == 0; + } + + /** + * Clears the value of this field. + */ + public void clear() { + setValue(""); + } +} |