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

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