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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Copyright 2000-2014 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.data.Property;
  19. import com.vaadin.legacy.ui.LegacyAbstractTextField;
  20. import com.vaadin.shared.ui.textarea.TextAreaState;
  21. import com.vaadin.ui.declarative.DesignContext;
  22. import com.vaadin.ui.declarative.DesignFormatter;
  23. /**
  24. * A text field that supports multi line editing.
  25. */
  26. public class TextArea extends LegacyAbstractTextField {
  27. /**
  28. * Constructs an empty TextArea.
  29. */
  30. public TextArea() {
  31. setValue("");
  32. }
  33. /**
  34. * Constructs an empty TextArea with given caption.
  35. *
  36. * @param caption
  37. * the caption for the field.
  38. */
  39. public TextArea(String caption) {
  40. this();
  41. setCaption(caption);
  42. }
  43. /**
  44. * Constructs a TextArea with given property data source.
  45. *
  46. * @param dataSource
  47. * the data source for the field
  48. */
  49. public TextArea(Property dataSource) {
  50. this();
  51. setPropertyDataSource(dataSource);
  52. }
  53. /**
  54. * Constructs a TextArea with given caption and property data source.
  55. *
  56. * @param caption
  57. * the caption for the field
  58. * @param dataSource
  59. * the data source for the field
  60. */
  61. public TextArea(String caption, Property dataSource) {
  62. this(dataSource);
  63. setCaption(caption);
  64. }
  65. /**
  66. * Constructs a TextArea with given caption and value.
  67. *
  68. * @param caption
  69. * the caption for the field
  70. * @param value
  71. * the value for the field
  72. */
  73. public TextArea(String caption, String value) {
  74. this(caption);
  75. setValue(value);
  76. }
  77. @Override
  78. protected TextAreaState getState() {
  79. return (TextAreaState) super.getState();
  80. }
  81. @Override
  82. protected TextAreaState getState(boolean markAsDirty) {
  83. return (TextAreaState) super.getState(markAsDirty);
  84. }
  85. /**
  86. * Sets the number of rows in the text area.
  87. *
  88. * @param rows
  89. * the number of rows for this text area.
  90. */
  91. public void setRows(int rows) {
  92. if (rows < 0) {
  93. rows = 0;
  94. }
  95. getState().rows = rows;
  96. }
  97. /**
  98. * Gets the number of rows in the text area.
  99. *
  100. * @return number of explicitly set rows.
  101. */
  102. public int getRows() {
  103. return getState(false).rows;
  104. }
  105. /**
  106. * Sets the text area's word-wrap mode on or off.
  107. *
  108. * @param wordwrap
  109. * the boolean value specifying if the text area should be in
  110. * word-wrap mode.
  111. */
  112. public void setWordwrap(boolean wordwrap) {
  113. getState().wordwrap = wordwrap;
  114. }
  115. /**
  116. * Tests if the text area is in word-wrap mode.
  117. *
  118. * @return <code>true</code> if the component is in word-wrap mode,
  119. * <code>false</code> if not.
  120. */
  121. public boolean isWordwrap() {
  122. return getState(false).wordwrap;
  123. }
  124. /*
  125. * (non-Javadoc)
  126. *
  127. * @see com.vaadin.ui.AbstractField#readDesign(org.jsoup.nodes.Element ,
  128. * com.vaadin.ui.declarative.DesignContext)
  129. */
  130. @Override
  131. public void readDesign(Element design, DesignContext designContext) {
  132. super.readDesign(design, designContext);
  133. setValue(DesignFormatter.decodeFromTextNode(design.html()), false,
  134. true);
  135. }
  136. /*
  137. * (non-Javadoc)
  138. *
  139. * @see com.vaadin.ui.AbstractTextField#writeDesign(org.jsoup.nodes.Element
  140. * , com.vaadin.ui.declarative.DesignContext)
  141. */
  142. @Override
  143. public void writeDesign(Element design, DesignContext designContext) {
  144. super.writeDesign(design, designContext);
  145. design.html(DesignFormatter.encodeForTextNode(getValue()));
  146. }
  147. @Override
  148. public void clear() {
  149. setValue("");
  150. }
  151. }