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.

VTextArea.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. * Copyright 2000-2013 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.client.ui;
  17. import com.google.gwt.core.client.Scheduler;
  18. import com.google.gwt.dom.client.Element;
  19. import com.google.gwt.dom.client.Style.Overflow;
  20. import com.google.gwt.dom.client.Style.WhiteSpace;
  21. import com.google.gwt.dom.client.TextAreaElement;
  22. import com.google.gwt.event.dom.client.ChangeEvent;
  23. import com.google.gwt.event.dom.client.ChangeHandler;
  24. import com.google.gwt.event.dom.client.KeyDownEvent;
  25. import com.google.gwt.event.dom.client.KeyUpEvent;
  26. import com.google.gwt.event.dom.client.KeyUpHandler;
  27. import com.google.gwt.user.client.Command;
  28. import com.google.gwt.user.client.DOM;
  29. import com.google.gwt.user.client.Event;
  30. import com.vaadin.client.BrowserInfo;
  31. import com.vaadin.client.Util;
  32. import com.vaadin.client.ui.dd.VDragCloneAware;
  33. /**
  34. * This class represents a multiline textfield (textarea).
  35. *
  36. * TODO consider replacing this with a RichTextArea based implementation. IE
  37. * does not support CSS height for textareas in Strict mode :-(
  38. *
  39. * @author Vaadin Ltd.
  40. *
  41. */
  42. public class VTextArea extends VTextField implements VDragCloneAware {
  43. public static final String CLASSNAME = "v-textarea";
  44. private boolean wordwrap = true;
  45. private MaxLengthHandler maxLengthHandler = new MaxLengthHandler();
  46. private boolean browserSupportsMaxLengthAttribute = browserSupportsMaxLengthAttribute();
  47. public VTextArea() {
  48. super(DOM.createTextArea());
  49. setStyleName(CLASSNAME);
  50. if (!browserSupportsMaxLengthAttribute) {
  51. addKeyUpHandler(maxLengthHandler);
  52. addChangeHandler(maxLengthHandler);
  53. sinkEvents(Event.ONPASTE);
  54. }
  55. }
  56. public TextAreaElement getTextAreaElement() {
  57. return super.getElement().cast();
  58. }
  59. public void setRows(int rows) {
  60. getTextAreaElement().setRows(rows);
  61. }
  62. @Override
  63. public void setSelectionRange(int pos, int length) {
  64. super.setSelectionRange(pos, length);
  65. final String value = getValue();
  66. /*
  67. * Align position to index inside string value
  68. */
  69. int index = pos;
  70. if (index < 0) {
  71. index = 0;
  72. }
  73. if (index > value.length()) {
  74. index = value.length();
  75. }
  76. // Get pixels count required to scroll textarea vertically
  77. int scrollTop = getScrollTop(value, index);
  78. int scrollLeft = -1;
  79. /*
  80. * Check if textarea has wrap attribute set to "off". In the latter case
  81. * horizontal scroll is also required.
  82. */
  83. if (!isWordwrap()) {
  84. // Get pixels count required to scroll textarea horizontally
  85. scrollLeft = getScrollLeft(value, index);
  86. }
  87. // Set back original text if previous methods calls changed it
  88. if (!isWordwrap() || index < value.length()) {
  89. setValue(value, false);
  90. }
  91. /*
  92. * Call original method to set cursor position. In most browsers it
  93. * doesn't lead to scrolling.
  94. */
  95. super.setSelectionRange(pos, length);
  96. /*
  97. * Align vertical scroll to middle of textarea view (height) if
  98. * scrolling is reqiured at all.
  99. */
  100. if (scrollTop > 0) {
  101. scrollTop += getElement().getClientHeight() / 2;
  102. }
  103. /*
  104. * Align horizontal scroll to middle of textarea view (widht) if
  105. * scrolling is reqiured at all.
  106. */
  107. if (scrollLeft > 0) {
  108. scrollLeft += getElement().getClientWidth() / 2;
  109. }
  110. /*
  111. * Scroll if computed scrollTop is greater than scroll after cursor
  112. * setting
  113. */
  114. if (getElement().getScrollTop() < scrollTop) {
  115. getElement().setScrollTop(scrollTop);
  116. }
  117. /*
  118. * Scroll if computed scrollLeft is greater than scroll after cursor
  119. * setting
  120. */
  121. if (getElement().getScrollLeft() < scrollLeft) {
  122. getElement().setScrollLeft(scrollLeft);
  123. }
  124. }
  125. /*
  126. * Get horizontal scroll value required to get position visible. Method is
  127. * called only when text wrapping is off. There is need to scroll
  128. * horizontally in case words are wrapped.
  129. */
  130. private int getScrollLeft(String value, int index) {
  131. String beginning = value.substring(0, index);
  132. // Compute beginning of the current line
  133. int begin = beginning.lastIndexOf('\n');
  134. String line = value.substring(begin + 1);
  135. index = index - begin - 1;
  136. if (index < line.length()) {
  137. index++;
  138. }
  139. line = line.substring(0, index);
  140. /*
  141. * Now <code>line</code> contains current line up to index position
  142. */
  143. setValue(line.trim(), false); // Set this line to the textarea.
  144. /*
  145. * Scroll textarea up to the end of the line (maximum possible
  146. * horizontal scrolling value). Now the end line becomes visible.
  147. */
  148. getElement().setScrollLeft(getElement().getScrollWidth());
  149. // Return resulting horizontal scrolling value.
  150. return getElement().getScrollLeft();
  151. }
  152. /*
  153. * Get vertical scroll value required to get position visible
  154. */
  155. private int getScrollTop(String value, int index) {
  156. /*
  157. * Trim text after position and set this trimmed text if index is not
  158. * very end.
  159. */
  160. if (index < value.length()) {
  161. String beginning = value.substring(0, index);
  162. setValue(beginning, false);
  163. }
  164. /*
  165. * Now textarea contains trimmed text and could be scrolled up to the
  166. * top. Scroll it to maximum possible value to get end of the text
  167. * visible.
  168. */
  169. getElement().setScrollTop(getElement().getScrollHeight());
  170. // Return resulting vertical scrolling value.
  171. return getElement().getScrollTop();
  172. }
  173. private class MaxLengthHandler implements KeyUpHandler, ChangeHandler {
  174. @Override
  175. public void onKeyUp(KeyUpEvent event) {
  176. enforceMaxLength();
  177. }
  178. public void onPaste(Event event) {
  179. enforceMaxLength();
  180. }
  181. @Override
  182. public void onChange(ChangeEvent event) {
  183. // Opera does not support paste events so this enforces max length
  184. // for Opera.
  185. enforceMaxLength();
  186. }
  187. }
  188. protected void enforceMaxLength() {
  189. if (getMaxLength() >= 0) {
  190. Scheduler.get().scheduleDeferred(new Command() {
  191. @Override
  192. public void execute() {
  193. if (getText().length() > getMaxLength()) {
  194. setText(getText().substring(0, getMaxLength()));
  195. }
  196. }
  197. });
  198. }
  199. }
  200. protected boolean browserSupportsMaxLengthAttribute() {
  201. BrowserInfo info = BrowserInfo.get();
  202. if (info.isFirefox() && info.isBrowserVersionNewerOrEqual(4, 0)) {
  203. return true;
  204. }
  205. if (info.isSafari() && info.isBrowserVersionNewerOrEqual(5, 0)) {
  206. return true;
  207. }
  208. if (info.isIE() && info.isBrowserVersionNewerOrEqual(10, 0)) {
  209. return true;
  210. }
  211. if (info.isAndroid() && info.isBrowserVersionNewerOrEqual(2, 3)) {
  212. return true;
  213. }
  214. return false;
  215. }
  216. @Override
  217. protected void updateMaxLength(int maxLength) {
  218. if (browserSupportsMaxLengthAttribute) {
  219. super.updateMaxLength(maxLength);
  220. } else {
  221. // Events handled by MaxLengthHandler. This call enforces max length
  222. // when the max length value has changed
  223. enforceMaxLength();
  224. }
  225. }
  226. @Override
  227. public void onBrowserEvent(Event event) {
  228. super.onBrowserEvent(event);
  229. if (event.getTypeInt() == Event.ONPASTE) {
  230. maxLengthHandler.onPaste(event);
  231. }
  232. }
  233. @Override
  234. public int getCursorPos() {
  235. // This is needed so that TextBoxImplIE6 is used to return the correct
  236. // position for old Internet Explorer versions where it has to be
  237. // detected in a different way.
  238. return getImpl().getTextAreaCursorPos(getElement());
  239. }
  240. @Override
  241. protected void setMaxLengthToElement(int newMaxLength) {
  242. // There is no maxlength property for textarea. The maximum length is
  243. // enforced by the KEYUP handler
  244. }
  245. public void setWordwrap(boolean wordwrap) {
  246. if (wordwrap == this.wordwrap) {
  247. return; // No change
  248. }
  249. if (wordwrap) {
  250. getElement().removeAttribute("wrap");
  251. getElement().getStyle().clearOverflow();
  252. getElement().getStyle().clearWhiteSpace();
  253. } else {
  254. getElement().setAttribute("wrap", "off");
  255. getElement().getStyle().setOverflow(Overflow.AUTO);
  256. getElement().getStyle().setWhiteSpace(WhiteSpace.PRE);
  257. }
  258. if (BrowserInfo.get().isOpera()
  259. || (BrowserInfo.get().isWebkit() && wordwrap)) {
  260. // Opera fails to dynamically update the wrap attribute so we detach
  261. // and reattach the whole TextArea.
  262. // Webkit fails to properly reflow the text when enabling wrapping,
  263. // same workaround
  264. Util.detachAttach(getElement());
  265. }
  266. this.wordwrap = wordwrap;
  267. }
  268. @Override
  269. public void onKeyDown(KeyDownEvent event) {
  270. // Overridden to avoid submitting TextArea value on enter in IE. This is
  271. // another reason why widgets should inherit a common abstract
  272. // class instead of directly each other.
  273. }
  274. @Override
  275. public void initDragImageCopy(Element element) {
  276. // Fix for #13557 - drag image doesn't show original text area text.
  277. // It happens because "value" property is not copied into the cloned
  278. // element
  279. String value = getElement().getPropertyString("value");
  280. if (value != null) {
  281. element.setPropertyString("value", value);
  282. }
  283. }
  284. }