You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RichTextArea.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * Copyright 2000-2018 Vaadin Ltd.
  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.vaadin.ui;
  17. import java.util.Objects;
  18. import org.jsoup.nodes.Element;
  19. import com.vaadin.shared.ui.ValueChangeMode;
  20. import com.vaadin.shared.ui.richtextarea.RichTextAreaClientRpc;
  21. import com.vaadin.shared.ui.richtextarea.RichTextAreaServerRpc;
  22. import com.vaadin.shared.ui.richtextarea.RichTextAreaState;
  23. import com.vaadin.ui.declarative.DesignContext;
  24. import elemental.json.Json;
  25. /**
  26. * A simple RichTextArea to edit HTML format text.
  27. */
  28. public class RichTextArea extends AbstractField<String>
  29. implements HasValueChangeMode {
  30. private class RichTextAreaServerRpcImpl implements RichTextAreaServerRpc {
  31. @Override
  32. public void setText(String text) {
  33. updateDiffstate("value", Json.create(text));
  34. if (!setValue(text, true)) {
  35. // The value was not updated, this could happen if the field has
  36. // been set to readonly on the server and the client does not
  37. // know about it yet. Must re-send the correct state back.
  38. markAsDirty();
  39. }
  40. }
  41. }
  42. /**
  43. * Constructs an empty <code>RichTextArea</code> with no caption.
  44. */
  45. public RichTextArea() {
  46. super();
  47. registerRpc(new RichTextAreaServerRpcImpl());
  48. setValue("");
  49. }
  50. /**
  51. * Constructs an empty <code>RichTextArea</code> with the given caption.
  52. *
  53. * @param caption
  54. * the caption for the editor.
  55. */
  56. public RichTextArea(String caption) {
  57. this();
  58. setCaption(caption);
  59. }
  60. /**
  61. * Constructs a new <code>RichTextArea</code> with the given caption and
  62. * initial text contents.
  63. *
  64. * @param caption
  65. * the caption for the editor.
  66. * @param value
  67. * the initial text content of the editor, not {@code null}
  68. */
  69. public RichTextArea(String caption, String value) {
  70. this(caption);
  71. setValue(value);
  72. }
  73. /**
  74. * Constructs a new {@code RichTextArea} with a value change listener.
  75. * <p>
  76. * The listener is called when the value of this {@code TextField} is
  77. * changed either by the user or programmatically.
  78. *
  79. * @param valueChangeListener
  80. * the value change listener, not {@code null}
  81. * @since 8.0
  82. */
  83. public RichTextArea(ValueChangeListener<String> valueChangeListener) {
  84. addValueChangeListener(valueChangeListener);
  85. }
  86. /**
  87. * Constructs a new {@code RichTextArea} with the given caption and a value
  88. * change listener.
  89. * <p>
  90. * The listener is called when the value of this {@code TextField} is
  91. * changed either by the user or programmatically.
  92. *
  93. * @param caption
  94. * the caption for the field
  95. * @param valueChangeListener
  96. * the value change listener, not {@code null}
  97. * @since 8.0
  98. */
  99. public RichTextArea(String caption,
  100. ValueChangeListener<String> valueChangeListener) {
  101. this(valueChangeListener);
  102. setCaption(caption);
  103. }
  104. /**
  105. * Constructs a new {@code RichTextArea} with the given caption, initial
  106. * text contents and a value change listener.
  107. * <p>
  108. * The listener is called when the value of this {@code RichTextArea} is
  109. * changed either by the user or programmatically.
  110. *
  111. * @param caption
  112. * the caption for the field
  113. * @param value
  114. * the value for the field, not {@code null}
  115. * @param valueChangeListener
  116. * the value change listener, not {@code null}
  117. * @since 8.0
  118. */
  119. public RichTextArea(String caption, String value,
  120. ValueChangeListener<String> valueChangeListener) {
  121. this(caption, value);
  122. addValueChangeListener(valueChangeListener);
  123. }
  124. @Override
  125. public void readDesign(Element design, DesignContext designContext) {
  126. super.readDesign(design, designContext);
  127. setValue(design.html());
  128. }
  129. @Override
  130. public void writeDesign(Element design, DesignContext designContext) {
  131. super.writeDesign(design, designContext);
  132. design.html(getValue());
  133. }
  134. @Override
  135. protected RichTextAreaState getState() {
  136. return (RichTextAreaState) super.getState();
  137. }
  138. @Override
  139. protected RichTextAreaState getState(boolean markAsDirty) {
  140. return (RichTextAreaState) super.getState(markAsDirty);
  141. }
  142. /**
  143. * Sets the value of this object. If the new value is not equal to
  144. * {@code getValue()}, fires a {@link ValueChangeEvent}. Throws
  145. * {@code NullPointerException} if the value is null.
  146. *
  147. * @param value
  148. * the new value, not {@code null}
  149. * @throws NullPointerException
  150. * if {@code value} is {@code null}
  151. */
  152. @Override
  153. public void setValue(String value) {
  154. Objects.requireNonNull(value, "value cannot be null");
  155. setValue(value, false);
  156. }
  157. @Override
  158. public String getValue() {
  159. return getState(false).value;
  160. }
  161. @Override
  162. public String getEmptyValue() {
  163. return "";
  164. }
  165. @Override
  166. protected void doSetValue(String value) {
  167. getState().value = value;
  168. }
  169. /**
  170. * Selects all text in the rich text area. As a side effect, focuses the
  171. * rich text area.
  172. *
  173. * @since 6.5
  174. */
  175. public void selectAll() {
  176. getRpcProxy(RichTextAreaClientRpc.class).selectAll();
  177. focus();
  178. }
  179. @Override
  180. public void setValueChangeMode(ValueChangeMode mode) {
  181. getState().valueChangeMode = mode;
  182. }
  183. @Override
  184. public ValueChangeMode getValueChangeMode() {
  185. return getState(false).valueChangeMode;
  186. }
  187. @Override
  188. public void setValueChangeTimeout(int timeout) {
  189. getState().valueChangeTimeout = timeout;
  190. }
  191. @Override
  192. public int getValueChangeTimeout() {
  193. return getState(false).valueChangeTimeout;
  194. }
  195. }