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.

AbstractTextField.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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.ui;
  17. import java.util.Collection;
  18. import org.jsoup.nodes.Attributes;
  19. import org.jsoup.nodes.Element;
  20. import com.vaadin.event.FieldEvents.BlurEvent;
  21. import com.vaadin.event.FieldEvents.BlurListener;
  22. import com.vaadin.event.FieldEvents.FocusEvent;
  23. import com.vaadin.event.FieldEvents.FocusListener;
  24. import com.vaadin.event.Registration;
  25. import com.vaadin.shared.ui.textfield.TextFieldServerRpc;
  26. import com.vaadin.shared.ui.textfield.TextFieldState;
  27. import com.vaadin.shared.ui.textfield.ValueChangeMode;
  28. import com.vaadin.ui.declarative.DesignAttributeHandler;
  29. import com.vaadin.ui.declarative.DesignContext;
  30. /**
  31. * Abstract base class for text input components.
  32. *
  33. * @author Vaadin Ltd.
  34. * @since 8.0
  35. */
  36. public abstract class AbstractTextField extends AbstractField<String> {
  37. protected AbstractTextField() {
  38. registerRpc(new TextFieldServerRpc() {
  39. @Override
  40. public void blur() {
  41. fireEvent(new BlurEvent(AbstractTextField.this));
  42. }
  43. @Override
  44. public void focus() {
  45. fireEvent(new FocusEvent(AbstractTextField.this));
  46. }
  47. @Override
  48. public void setText(String text, int cursorPosition) {
  49. getUI().getConnectorTracker()
  50. .getDiffState(AbstractTextField.this).put("text", text);
  51. getUI().getConnectorTracker()
  52. .getDiffState(AbstractTextField.this)
  53. .put("cursorPosition", cursorPosition);
  54. getState(false).cursorPosition = cursorPosition;
  55. setValue(text, true);
  56. }
  57. });
  58. }
  59. @Override
  60. public void setValue(String value) {
  61. if (value == null) {
  62. setValue("", false);
  63. } else {
  64. setValue(value, false);
  65. }
  66. }
  67. /**
  68. * Returns the maximum number of characters in the field. Value -1 is
  69. * considered unlimited. Terminal may however have some technical limits.
  70. *
  71. * @return the maxLength
  72. */
  73. public int getMaxLength() {
  74. return getState(false).maxLength;
  75. }
  76. /**
  77. * Sets the maximum number of characters in the field. Value -1 is
  78. * considered unlimited. Terminal may however have some technical limits.
  79. *
  80. * @param maxLength
  81. * the maxLength to set
  82. */
  83. public void setMaxLength(int maxLength) {
  84. getState().maxLength = maxLength;
  85. }
  86. /**
  87. * Returns the current placeholder text.
  88. *
  89. * @see #setPlaceholder(String)
  90. * @return the placeholder text
  91. */
  92. public String getPlaceholder() {
  93. return getState(false).placeholder;
  94. }
  95. /**
  96. * Sets the placeholder text. The placeholder is text that is displayed when
  97. * the field would otherwise be empty, to prompt the user for input.
  98. *
  99. * @param placeholder
  100. * the placeholder text to set
  101. */
  102. public void setPlaceholder(String placeholder) {
  103. getState().placeholder = placeholder;
  104. }
  105. @Override
  106. public String getValue() {
  107. return getState(false).text;
  108. }
  109. /**
  110. * Selects all text in the field.
  111. */
  112. public void selectAll() {
  113. setSelection(0, getValue().length());
  114. }
  115. /**
  116. * Sets the range of text to be selected.
  117. *
  118. * As a side effect the field will become focused.
  119. *
  120. * @param pos
  121. * the position of the first character to be selected
  122. * @param length
  123. * the number of characters to be selected
  124. */
  125. public void setSelection(int start, int length) {
  126. getState().selectionStart = start;
  127. getState().selectionLength = length;
  128. focus();
  129. }
  130. /**
  131. * Sets the cursor position in the field. As a side effect the field will
  132. * become focused.
  133. *
  134. * @param pos
  135. * the position for the cursor
  136. */
  137. public void setCursorPosition(int pos) {
  138. getState().cursorPosition = pos;
  139. focus();
  140. }
  141. /**
  142. * Returns the last known cursor position of the field.
  143. *
  144. */
  145. public int getCursorPosition() {
  146. return getState(false).cursorPosition;
  147. }
  148. /**
  149. * Adds a {@link FocusListener} to this component, which gets fired when
  150. * this component receives keyboard focus.
  151. *
  152. * @param listener
  153. * the focus listener
  154. * @return a registration for the listener
  155. *
  156. * @see Registration
  157. */
  158. public Registration addFocusListener(FocusListener listener) {
  159. addListener(FocusEvent.EVENT_ID, FocusEvent.class, listener,
  160. FocusListener.focusMethod);
  161. return () -> removeListener(FocusEvent.EVENT_ID, FocusEvent.class,
  162. listener);
  163. }
  164. /**
  165. * Adds a {@link BlurListener} to this component, which gets fired when this
  166. * component loses keyboard focus.
  167. *
  168. * @param listener
  169. * the blur listener
  170. * @return a registration for the listener
  171. *
  172. * @see Registration
  173. */
  174. public Registration addBlurListener(BlurListener listener) {
  175. addListener(BlurEvent.EVENT_ID, BlurEvent.class, listener,
  176. BlurListener.blurMethod);
  177. return () -> removeListener(BlurEvent.EVENT_ID, BlurEvent.class,
  178. listener);
  179. }
  180. /**
  181. * Gets the number of columns in the editor. If the number of columns is set
  182. * 0, the actual number of displayed columns is determined implicitly by the
  183. * adapter.
  184. *
  185. * @return the number of columns in the editor.
  186. */
  187. public int getColumns() {
  188. return getState(false).columns;
  189. }
  190. /**
  191. * Sets the number of columns in the editor. If the number of columns is set
  192. * 0, the actual number of displayed columns is determined implicitly by the
  193. * adapter.
  194. *
  195. * @param columns
  196. * the number of columns to set.
  197. */
  198. public void setColumns(int columns) {
  199. if (columns < 0) {
  200. columns = 0;
  201. }
  202. getState().columns = columns;
  203. }
  204. /**
  205. * Sets the mode how the TextField triggers {@link ValueChange}s.
  206. *
  207. * @param mode
  208. * the new mode
  209. *
  210. * @see ValueChangeMode
  211. */
  212. public void setValueChangeMode(ValueChangeMode mode) {
  213. getState().valueChangeMode = mode;
  214. }
  215. /**
  216. * Returns the currently set {@link ValueChangeMode}.
  217. *
  218. * @return the mode used to trigger {@link ValueChange}s.
  219. *
  220. * @see ValueChangeMode
  221. */
  222. public ValueChangeMode getValueChangeMode() {
  223. return getState(false).valueChangeMode;
  224. }
  225. /**
  226. * Sets how often {@link ValueChange}s are triggered when the
  227. * {@link ValueChangeMode} is set to either {@link ValueChangeMode#LAZY} or
  228. * {@link ValueChangeMode#TIMEOUT}.
  229. *
  230. * @param timeout
  231. * timeout in milliseconds, must be greater or equal to 0
  232. * @throws IllegalArgumentException
  233. * if given timeout is smaller than 0
  234. *
  235. * @see ValueChangeMode
  236. */
  237. public void setValueChangeTimeout(int timeout) {
  238. if (timeout < 0)
  239. throw new IllegalArgumentException(
  240. "Timeout must be greater than 0");
  241. getState().valueChangeTimeout = timeout;
  242. }
  243. /**
  244. * Returns the currently set timeout, in milliseconds, for how often
  245. * {@link ValueChange}s are triggered if the current {@link ValueChangeMode}
  246. * is set to either {@link ValueChangeMode#LAZY} or
  247. * {@link ValueChangeMode#TIMEOUT}.
  248. *
  249. * @return the timeout in milliseconds of how often {@link ValueChange}s are
  250. * triggered.
  251. */
  252. public int getValueChangeTimeout() {
  253. return getState(false).valueChangeTimeout;
  254. }
  255. @Override
  256. public void readDesign(Element design, DesignContext designContext) {
  257. super.readDesign(design, designContext);
  258. Attributes attr = design.attributes();
  259. if (attr.hasKey("maxlength")) {
  260. setMaxLength(DesignAttributeHandler.readAttribute("maxlength", attr,
  261. Integer.class));
  262. }
  263. }
  264. @Override
  265. protected TextFieldState getState() {
  266. return (TextFieldState) super.getState();
  267. }
  268. @Override
  269. protected TextFieldState getState(boolean markAsDirty) {
  270. return (TextFieldState) super.getState(markAsDirty);
  271. }
  272. @Override
  273. protected void doSetValue(String value) {
  274. getState().text = value;
  275. }
  276. /**
  277. * Clears the value of this field.
  278. */
  279. public void clear() {
  280. setValue("");
  281. }
  282. /**
  283. * Checks if the field is empty.
  284. *
  285. * @return true if the field value is an empty string, false otherwise
  286. */
  287. public boolean isEmpty() {
  288. return "".equals(getValue());
  289. }
  290. @Override
  291. public void writeDesign(Element design, DesignContext designContext) {
  292. super.writeDesign(design, designContext);
  293. AbstractTextField def = (AbstractTextField) designContext
  294. .getDefaultInstance(this);
  295. Attributes attr = design.attributes();
  296. DesignAttributeHandler.writeAttribute("maxlength", attr, getMaxLength(),
  297. def.getMaxLength(), Integer.class);
  298. }
  299. @Override
  300. protected Collection<String> getCustomAttributes() {
  301. Collection<String> customAttributes = super.getCustomAttributes();
  302. customAttributes.add("maxlength");
  303. customAttributes.add("max-length"); // to prevent this appearing in
  304. // output
  305. customAttributes.add("cursor-position");
  306. return customAttributes;
  307. }
  308. }