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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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.client.ui;
  17. import com.google.gwt.dom.client.Element;
  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.KeyCodes;
  22. import com.google.gwt.event.dom.client.KeyDownEvent;
  23. import com.google.gwt.event.dom.client.KeyDownHandler;
  24. import com.google.gwt.user.client.DOM;
  25. import com.vaadin.client.BrowserInfo;
  26. import com.vaadin.client.WidgetUtil;
  27. import com.vaadin.client.ui.dd.DragImageModifier;
  28. /**
  29. * This class represents a multiline textfield (textarea).
  30. *
  31. * @author Vaadin Ltd.
  32. *
  33. */
  34. public class VTextArea extends VTextField implements DragImageModifier {
  35. public static final String CLASSNAME = "v-textarea";
  36. private EnterDownHandler enterDownHandler = new EnterDownHandler();
  37. private boolean wordWrap = true;
  38. private class EnterDownHandler implements KeyDownHandler {
  39. @Override
  40. public void onKeyDown(KeyDownEvent event) {
  41. // Fix for #12424/13811 - if the key being pressed is enter, we stop
  42. // propagation of the KeyDownEvents if there were no modifier keys
  43. // also pressed. This prevents shortcuts that are bound to only the
  44. // enter key from being processed but allows usage of e.g.
  45. // shift-enter or ctrl-enter.
  46. if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER
  47. && !event.isAnyModifierKeyDown()) {
  48. event.stopPropagation();
  49. }
  50. }
  51. }
  52. public VTextArea() {
  53. super(DOM.createTextArea());
  54. setStyleName(CLASSNAME);
  55. addKeyDownHandler(enterDownHandler);
  56. }
  57. public TextAreaElement getTextAreaElement() {
  58. return super.getElement().cast();
  59. }
  60. public void setRows(int rows) {
  61. getTextAreaElement().setRows(rows);
  62. }
  63. public void setWordWrap(boolean wordWrap) {
  64. if (wordWrap == this.wordWrap) {
  65. return;
  66. }
  67. if (wordWrap) {
  68. getElement().removeAttribute("wrap");
  69. getElement().getStyle().clearOverflow();
  70. getElement().getStyle().clearWhiteSpace();
  71. } else {
  72. getElement().setAttribute("wrap", "off");
  73. getElement().getStyle().setOverflow(Overflow.AUTO);
  74. getElement().getStyle().setWhiteSpace(WhiteSpace.PRE);
  75. }
  76. if (BrowserInfo.get().isOpera()
  77. || (BrowserInfo.get().isWebkit() && wordWrap)) {
  78. // Opera fails to dynamically update the wrap attribute so we detach
  79. // and reattach the whole TextArea.
  80. // Webkit fails to properly reflow the text when enabling wrapping,
  81. // same workaround
  82. WidgetUtil.detachAttach(getElement());
  83. }
  84. this.wordWrap = wordWrap;
  85. }
  86. @Override
  87. public void modifyDragImage(Element element) {
  88. // Fix for #13557 - drag image doesn't show original text area text.
  89. // It happens because "value" property is not copied into the cloned
  90. // element
  91. String value = getElement().getPropertyString("value");
  92. if (value != null) {
  93. element.setPropertyString("value", value);
  94. }
  95. }
  96. }