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.

TextArea.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 org.jsoup.nodes.Element;
  18. import com.vaadin.shared.ui.textarea.TextAreaServerRpc;
  19. import com.vaadin.shared.ui.textarea.TextAreaState;
  20. import com.vaadin.ui.declarative.DesignContext;
  21. import com.vaadin.ui.declarative.DesignFormatter;
  22. /**
  23. * A text field that supports multi line editing.
  24. */
  25. public class TextArea extends AbstractTextField {
  26. /**
  27. * Constructs an empty TextArea.
  28. */
  29. public TextArea() {
  30. registerRpc(new TextAreaServerRpc() {
  31. @Override
  32. public void setHeight(String height) {
  33. TextArea.this.setHeight(height);
  34. }
  35. @Override
  36. public void setWidth(String width) {
  37. TextArea.this.setWidth(width);
  38. }
  39. });
  40. clear();
  41. }
  42. /**
  43. * Constructs an empty TextArea with given caption.
  44. *
  45. * @param caption
  46. * the caption for the field.
  47. */
  48. public TextArea(String caption) {
  49. this();
  50. setCaption(caption);
  51. }
  52. /**
  53. * Constructs a TextArea with given caption and value.
  54. *
  55. * @param caption
  56. * the caption for the field
  57. * @param value
  58. * the value for the field, not {@code null}
  59. */
  60. public TextArea(String caption, String value) {
  61. this(caption);
  62. setValue(value);
  63. }
  64. /**
  65. * Constructs a new {@code TextArea} with a value change listener.
  66. * <p>
  67. * The listener is called when the value of this {@code TextArea} is changed
  68. * either by the user or programmatically.
  69. *
  70. * @param valueChangeListener
  71. * the value change listener, not {@code null}
  72. * @since 8.0
  73. */
  74. public TextArea(ValueChangeListener<String> valueChangeListener) {
  75. addValueChangeListener(valueChangeListener);
  76. }
  77. /**
  78. * Constructs a new {@code TextArea} with the given caption and a value
  79. * change listener.
  80. * <p>
  81. * The listener is called when the value of this {@code TextArea} is changed
  82. * either by the user or programmatically.
  83. *
  84. * @param caption
  85. * the caption for the field
  86. * @param valueChangeListener
  87. * the value change listener, not {@code null}
  88. * @since 8.0
  89. */
  90. public TextArea(String caption,
  91. ValueChangeListener<String> valueChangeListener) {
  92. this(valueChangeListener);
  93. setCaption(caption);
  94. }
  95. /**
  96. * Constructs a new {@code TextArea} with the given caption, initial text
  97. * contents and a value change listener.
  98. * <p>
  99. * The listener is called when the value of this {@code TextArea} is changed
  100. * either by the user or programmatically.
  101. *
  102. * @param caption
  103. * the caption for the field
  104. * @param value
  105. * the value for the field, not {@code null}
  106. * @param valueChangeListener
  107. * the value change listener, not {@code null}
  108. * @since 8.0
  109. */
  110. public TextArea(String caption, String value,
  111. ValueChangeListener<String> valueChangeListener) {
  112. this(caption, value);
  113. addValueChangeListener(valueChangeListener);
  114. }
  115. @Override
  116. protected TextAreaState getState() {
  117. return (TextAreaState) super.getState();
  118. }
  119. @Override
  120. protected TextAreaState getState(boolean markAsDirty) {
  121. return (TextAreaState) super.getState(markAsDirty);
  122. }
  123. /**
  124. * Sets the number of rows in the text area.
  125. *
  126. * @param rows
  127. * the number of rows for this text area.
  128. */
  129. public void setRows(int rows) {
  130. if (rows < 0) {
  131. rows = 0;
  132. }
  133. getState().rows = rows;
  134. }
  135. /**
  136. * Gets the number of rows in the text area.
  137. *
  138. * @return number of explicitly set rows.
  139. */
  140. public int getRows() {
  141. return getState(false).rows;
  142. }
  143. /**
  144. * Sets the text area's word-wrap mode on or off.
  145. *
  146. * @param wordWrap
  147. * <code>true</code> to use word-wrap mode <code>false</code>
  148. * otherwise.
  149. */
  150. public void setWordWrap(boolean wordWrap) {
  151. getState().wordWrap = wordWrap;
  152. }
  153. /**
  154. * Tests if the text area is in word-wrap mode.
  155. *
  156. * @return <code>true</code> if the component is in word-wrap mode,
  157. * <code>false</code> if not.
  158. */
  159. public boolean isWordWrap() {
  160. return getState(false).wordWrap;
  161. }
  162. @Override
  163. public void readDesign(Element design, DesignContext designContext) {
  164. super.readDesign(design, designContext);
  165. doSetValue(DesignFormatter.decodeFromTextNode(design.html()));
  166. }
  167. @Override
  168. public void writeDesign(Element design, DesignContext designContext) {
  169. super.writeDesign(design, designContext);
  170. design.html(DesignFormatter.encodeForTextNode(getValue()));
  171. }
  172. }