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.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright 2000-2021 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. getElement().getStyle().setOverflowX(Overflow.HIDDEN);
  57. }
  58. public TextAreaElement getTextAreaElement() {
  59. return super.getElement().cast();
  60. }
  61. public void setRows(int rows) {
  62. getTextAreaElement().setRows(rows);
  63. }
  64. public void setWordWrap(boolean wordWrap) {
  65. if (wordWrap == this.wordWrap) {
  66. return;
  67. }
  68. if (wordWrap) {
  69. getElement().removeAttribute("wrap");
  70. getElement().getStyle().clearOverflowY();
  71. getElement().getStyle().setOverflowX(Overflow.HIDDEN);
  72. getElement().getStyle().clearWhiteSpace();
  73. } else {
  74. getElement().setAttribute("wrap", "off");
  75. getElement().getStyle().setOverflow(Overflow.AUTO);
  76. getElement().getStyle().setWhiteSpace(WhiteSpace.PRE);
  77. }
  78. if (BrowserInfo.get().isOpera()
  79. || (BrowserInfo.get().isWebkit() && wordWrap)) {
  80. // Opera fails to dynamically update the wrap attribute so we detach
  81. // and reattach the whole TextArea.
  82. // Webkit fails to properly reflow the text when enabling wrapping,
  83. // same workaround
  84. WidgetUtil.detachAttach(getElement());
  85. }
  86. this.wordWrap = wordWrap;
  87. }
  88. @Override
  89. public void modifyDragImage(Element element) {
  90. // Fix for #13557 - drag image doesn't show original text area text.
  91. // It happens because "value" property is not copied into the cloned
  92. // element
  93. String value = getElement().getPropertyString("value");
  94. if (value != null) {
  95. element.setPropertyString("value", value);
  96. }
  97. }
  98. }