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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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.v7.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.ui.dd.DragImageModifier;
  35. /**
  36. * This class represents a multiline textfield (textarea).
  37. *
  38. * TODO consider replacing this with a RichTextArea based implementation. IE
  39. * does not support CSS height for textareas in Strict mode :-(
  40. *
  41. * @author Vaadin Ltd.
  42. *
  43. */
  44. @Deprecated
  45. public class VTextArea extends VTextField 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.isSafariOrIOS()) {
  213. return true;
  214. }
  215. // Vaadin 8 no longer supports IE10
  216. if (info.isIE11() || info.isEdge()) {
  217. return true;
  218. }
  219. if (info.isAndroid()) {
  220. return true;
  221. }
  222. return false;
  223. }
  224. @Override
  225. protected void updateMaxLength(int maxLength) {
  226. if (browserSupportsMaxLengthAttribute) {
  227. super.updateMaxLength(maxLength);
  228. } else {
  229. // Events handled by MaxLengthHandler. This call enforces max length
  230. // when the max length value has changed
  231. enforceMaxLength();
  232. }
  233. }
  234. @Override
  235. public void onBrowserEvent(Event event) {
  236. super.onBrowserEvent(event);
  237. if (event.getTypeInt() == Event.ONPASTE) {
  238. maxLengthHandler.onPaste(event);
  239. }
  240. }
  241. private class EnterDownHandler implements KeyDownHandler {
  242. @Override
  243. public void onKeyDown(KeyDownEvent event) {
  244. // Fix for #12424/13811 - if the key being pressed is enter, we stop
  245. // propagation of the KeyDownEvents if there were no modifier keys
  246. // also pressed. This prevents shortcuts that are bound to only the
  247. // enter key from being processed but allows usage of e.g.
  248. // shift-enter or ctrl-enter.
  249. if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER
  250. && !event.isAnyModifierKeyDown()) {
  251. event.stopPropagation();
  252. }
  253. }
  254. }
  255. @Override
  256. public int getCursorPos() {
  257. // This is needed so that TextBoxImplIE6 is used to return the correct
  258. // position for old Internet Explorer versions where it has to be
  259. // detected in a different way.
  260. return getImpl().getTextAreaCursorPos(getElement());
  261. }
  262. @Override
  263. protected void setMaxLengthToElement(int newMaxLength) {
  264. // There is no maxlength property for textarea. The maximum length is
  265. // enforced by the KEYUP handler
  266. }
  267. public void setWordwrap(boolean wordwrap) {
  268. if (wordwrap == this.wordwrap) {
  269. return; // No change
  270. }
  271. if (wordwrap) {
  272. getElement().removeAttribute("wrap");
  273. getElement().getStyle().clearOverflow();
  274. getElement().getStyle().clearWhiteSpace();
  275. } else {
  276. getElement().setAttribute("wrap", "off");
  277. getElement().getStyle().setOverflow(Overflow.AUTO);
  278. getElement().getStyle().setWhiteSpace(WhiteSpace.PRE);
  279. }
  280. if (BrowserInfo.get().isOpera()
  281. || (BrowserInfo.get().isWebkit() && wordwrap)) {
  282. // Opera fails to dynamically update the wrap attribute so we detach
  283. // and reattach the whole TextArea.
  284. // Webkit fails to properly reflow the text when enabling wrapping,
  285. // same workaround
  286. WidgetUtil.detachAttach(getElement());
  287. }
  288. this.wordwrap = wordwrap;
  289. }
  290. @Override
  291. public void onKeyDown(KeyDownEvent event) {
  292. // Overridden to avoid submitting TextArea value on enter in IE. This is
  293. // another reason why widgets should inherit a common abstract
  294. // class instead of directly each other.
  295. // This method is overridden only for IE and Firefox.
  296. }
  297. @Override
  298. public void modifyDragImage(Element element) {
  299. // Fix for #13557 - drag image doesn't show original text area text.
  300. // It happens because "value" property is not copied into the cloned
  301. // element
  302. String value = getElement().getPropertyString("value");
  303. if (value != null) {
  304. element.setPropertyString("value", value);
  305. }
  306. }
  307. }