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.

TextField.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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.ui;
  17. import org.jsoup.nodes.Attributes;
  18. import org.jsoup.nodes.Element;
  19. import com.vaadin.shared.ui.textfield.TextFieldState;
  20. import com.vaadin.ui.declarative.DesignAttributeHandler;
  21. import com.vaadin.ui.declarative.DesignContext;
  22. /**
  23. * A component for editing textual data that fits on a single line. For a
  24. * multi-line textarea, see the {@link TextArea} component.
  25. *
  26. * @author Vaadin Ltd.
  27. */
  28. @SuppressWarnings("serial")
  29. public class TextField extends AbstractTextField {
  30. /**
  31. * Constructs an empty <code>TextField</code> with no caption.
  32. */
  33. public TextField() {
  34. clear();
  35. }
  36. /**
  37. * Constructs an empty <code>TextField</code> with given caption.
  38. *
  39. * @param caption
  40. * the caption <code>String</code> for the editor.
  41. */
  42. public TextField(String caption) {
  43. this();
  44. setCaption(caption);
  45. }
  46. /**
  47. * Constructs a new <code>TextField</code> with the given caption and
  48. * initial text contents.
  49. *
  50. * @param caption
  51. * the caption <code>String</code> for the editor.
  52. * @param value
  53. * the initial text content of the editor, not {@code null}
  54. */
  55. public TextField(String caption, String value) {
  56. setValue(value);
  57. setCaption(caption);
  58. }
  59. /**
  60. * Constructs a new {@code TextField} with a value change listener. The
  61. * listener is called when the value of this {@code TextField} is changed
  62. * either by the user or programmatically.
  63. *
  64. * @param valueChangeListener
  65. * the value change listener, not {@code null}
  66. */
  67. public TextField(ValueChangeListener<String> valueChangeListener) {
  68. addValueChangeListener(valueChangeListener);
  69. }
  70. /**
  71. * Constructs a new {@code TextField} with the given caption and a value
  72. * change listener.
  73. * <p>
  74. * The listener is called when the value of this {@code TextField} is
  75. * changed either by the user or programmatically.
  76. *
  77. * @param caption
  78. * the caption {@code String} for the editor.
  79. * @param valueChangeListener
  80. * the value change listener, not {@code null}
  81. */
  82. public TextField(String caption,
  83. ValueChangeListener<String> valueChangeListener) {
  84. this(valueChangeListener);
  85. setCaption(caption);
  86. }
  87. /**
  88. * Constructs a new {@code TextField} with the given caption, initial text
  89. * contents and a value change listener.
  90. * <p>
  91. * The listener is called when the value of this {@code TextField} is
  92. * changed either by the user or programmatically.
  93. *
  94. * @param caption
  95. * the caption {@code String} for the editor.
  96. * @param value
  97. * the initial text content of the editor, not {@code null}
  98. * @param valueChangeListener
  99. * the value change listener, not {@code null}
  100. */
  101. public TextField(String caption, String value,
  102. ValueChangeListener<String> valueChangeListener) {
  103. this(caption, value);
  104. addValueChangeListener(valueChangeListener);
  105. }
  106. @Override
  107. protected TextFieldState getState() {
  108. return (TextFieldState) super.getState();
  109. }
  110. @Override
  111. protected TextFieldState getState(boolean markAsDirty) {
  112. return (TextFieldState) super.getState(markAsDirty);
  113. }
  114. @Override
  115. public void readDesign(Element design, DesignContext designContext) {
  116. super.readDesign(design, designContext);
  117. Attributes attr = design.attributes();
  118. if (attr.hasKey("value")) {
  119. String text = DesignAttributeHandler.readAttribute("value", attr,
  120. String.class);
  121. doSetValue(text);
  122. }
  123. }
  124. @Override
  125. public void writeDesign(Element design, DesignContext designContext) {
  126. super.writeDesign(design, designContext);
  127. AbstractTextField def = designContext.getDefaultInstance(this);
  128. Attributes attr = design.attributes();
  129. DesignAttributeHandler.writeAttribute("value", attr, getValue(),
  130. def.getValue(), String.class, designContext);
  131. }
  132. }