Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

VTextArea.java 11KB

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