Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

VTextArea.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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
  35. implements DragImageModifier {
  36. public static final String CLASSNAME = "v-textarea";
  37. private EnterDownHandler enterDownHandler = new EnterDownHandler();
  38. private boolean wordWrap = true;
  39. private class EnterDownHandler implements KeyDownHandler {
  40. @Override
  41. public void onKeyDown(KeyDownEvent event) {
  42. // Fix for #12424/13811 - if the key being pressed is enter, we stop
  43. // propagation of the KeyDownEvents if there were no modifier keys
  44. // also pressed. This prevents shortcuts that are bound to only the
  45. // enter key from being processed but allows usage of e.g.
  46. // shift-enter or ctrl-enter.
  47. if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER
  48. && !event.isAnyModifierKeyDown()) {
  49. event.stopPropagation();
  50. }
  51. }
  52. }
  53. public VTextArea() {
  54. super(DOM.createTextArea());
  55. setStyleName(CLASSNAME);
  56. addKeyDownHandler(enterDownHandler);
  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().clearOverflow();
  71. getElement().getStyle().clearWhiteSpace();
  72. } else {
  73. getElement().setAttribute("wrap", "off");
  74. getElement().getStyle().setOverflow(Overflow.AUTO);
  75. getElement().getStyle().setWhiteSpace(WhiteSpace.PRE);
  76. }
  77. if (BrowserInfo.get().isOpera()
  78. || (BrowserInfo.get().isWebkit() && wordWrap)) {
  79. // Opera fails to dynamically update the wrap attribute so we detach
  80. // and reattach the whole TextArea.
  81. // Webkit fails to properly reflow the text when enabling wrapping,
  82. // same workaround
  83. WidgetUtil.detachAttach(getElement());
  84. }
  85. this.wordWrap = wordWrap;
  86. }
  87. @Override
  88. public void modifyDragImage(Element element) {
  89. // Fix for #13557 - drag image doesn't show original text area text.
  90. // It happens because "value" property is not copied into the cloned
  91. // element
  92. String value = getElement().getPropertyString("value");
  93. if (value != null) {
  94. element.setPropertyString("value", value);
  95. }
  96. }
  97. }