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 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright 2000-2016 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.
  68. */
  69. public RichTextArea(String caption, String value) {
  70. this(caption);
  71. setValue(value);
  72. }
  73. @Override
  74. public void readDesign(Element design, DesignContext designContext) {
  75. super.readDesign(design, designContext);
  76. setValue(design.html());
  77. }
  78. @Override
  79. public void writeDesign(Element design, DesignContext designContext) {
  80. super.writeDesign(design, designContext);
  81. design.html(getValue());
  82. }
  83. @Override
  84. protected RichTextAreaState getState() {
  85. return (RichTextAreaState) super.getState();
  86. }
  87. @Override
  88. protected RichTextAreaState getState(boolean markAsDirty) {
  89. return (RichTextAreaState) super.getState(markAsDirty);
  90. }
  91. /**
  92. * Sets the value of this object. If the new value is not equal to
  93. * {@code getValue()}, fires a {@link ValueChangeEvent}. Throws
  94. * {@code NullPointerException} if the value is null.
  95. *
  96. * @param value
  97. * the new value, not {@code null}
  98. * @throws NullPointerException
  99. * if {@code value} is {@code null}
  100. */
  101. @Override
  102. public void setValue(String value) {
  103. Objects.requireNonNull(value, "value cannot be null");
  104. setValue(value, false);
  105. }
  106. @Override
  107. public String getValue() {
  108. return getState(false).value;
  109. }
  110. @Override
  111. public String getEmptyValue() {
  112. return "";
  113. }
  114. @Override
  115. protected void doSetValue(String value) {
  116. getState().value = value;
  117. }
  118. /**
  119. * Selects all text in the rich text area. As a side effect, focuses the
  120. * rich text area.
  121. *
  122. * @since 6.5
  123. */
  124. public void selectAll() {
  125. getRpcProxy(RichTextAreaClientRpc.class).selectAll();
  126. focus();
  127. }
  128. @Override
  129. public void setValueChangeMode(ValueChangeMode mode) {
  130. getState().valueChangeMode = mode;
  131. }
  132. @Override
  133. public ValueChangeMode getValueChangeMode() {
  134. return getState(false).valueChangeMode;
  135. }
  136. @Override
  137. public void setValueChangeTimeout(int timeout) {
  138. getState().valueChangeTimeout = timeout;
  139. }
  140. @Override
  141. public int getValueChangeTimeout() {
  142. return getState(false).valueChangeTimeout;
  143. }
  144. /**
  145. * Clears the value of this field.
  146. */
  147. @Override
  148. public void clear() {
  149. setValue("");
  150. }
  151. }