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

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