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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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.tokka.ui.components.fields;
  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.FieldEvents.TextChangeListener;
  25. import com.vaadin.shared.ui.components.fields.TextFieldServerRpc;
  26. import com.vaadin.shared.ui.components.fields.TextFieldState;
  27. import com.vaadin.tokka.event.Event;
  28. import com.vaadin.tokka.event.Handler;
  29. import com.vaadin.tokka.event.Registration;
  30. import com.vaadin.tokka.ui.components.HasValue;
  31. import com.vaadin.ui.AbstractComponent;
  32. import com.vaadin.ui.declarative.DesignAttributeHandler;
  33. import com.vaadin.ui.declarative.DesignContext;
  34. /**
  35. * Abstract base class for text input components.
  36. *
  37. * @author Vaadin Ltd.
  38. */
  39. public abstract class AbstractTextField extends AbstractComponent
  40. implements HasValue<String> {
  41. public static class TextChangeEvent
  42. extends Event<String> {
  43. public static final String ID = TextChangeListener.EVENT_ID;
  44. public TextChangeEvent(AbstractTextField source, String text,
  45. boolean userOriginated) {
  46. super(source, text, userOriginated);
  47. }
  48. }
  49. protected AbstractTextField() {
  50. registerRpc(new TextFieldServerRpc() {
  51. @Override
  52. public void blur() {
  53. fireEvent(new BlurEvent(AbstractTextField.this));
  54. }
  55. @Override
  56. public void focus() {
  57. fireEvent(new FocusEvent(AbstractTextField.this));
  58. }
  59. @Override
  60. public void setText(String text) {
  61. if (!isReadOnly()) {
  62. setValue(text);
  63. fireEvent(new TextChangeEvent(AbstractTextField.this, text,
  64. true));
  65. }
  66. }
  67. });
  68. }
  69. @Override
  70. protected TextFieldState getState() {
  71. return (TextFieldState) super.getState();
  72. }
  73. @Override
  74. protected TextFieldState getState(boolean markAsDirty) {
  75. return (TextFieldState) super.getState(markAsDirty);
  76. }
  77. /**
  78. * Returns the maximum number of characters in the field. Value -1 is
  79. * considered unlimited. Terminal may however have some technical limits.
  80. *
  81. * @return the maxLength
  82. */
  83. public int getMaxLength() {
  84. return getState(false).maxLength;
  85. }
  86. /**
  87. * Sets the maximum number of characters in the field. Value -1 is
  88. * considered unlimited. Terminal may however have some technical limits.
  89. *
  90. * @param maxLength
  91. * the maxLength to set
  92. */
  93. public void setMaxLength(int maxLength) {
  94. getState().maxLength = maxLength;
  95. }
  96. /**
  97. * Returns the current placeholder text.
  98. *
  99. * @see #setPlaceholder(String)
  100. * @return the placeholder text
  101. */
  102. public String getPlaceholder() {
  103. return getState(false).placeholder;
  104. }
  105. /**
  106. * Sets the placeholder text. The placeholder is text that is displayed when
  107. * the field would otherwise be empty, to prompt the user for input.
  108. *
  109. * @param placeholder
  110. * the placeholder text to set
  111. */
  112. public void setPlaceholder(String placeholder) {
  113. getState().placeholder = placeholder;
  114. }
  115. @Override
  116. public void setValue(String text) {
  117. // TODO Accept nulls or not?
  118. getState().text = text;
  119. }
  120. @Override
  121. public String getValue() {
  122. return getState(false).text;
  123. }
  124. @Override
  125. public Registration onChange(Handler<String> handler) {
  126. return onEvent(TextChangeEvent.ID, TextChangeEvent.class, handler);
  127. }
  128. /**
  129. * Selects all text in the field.
  130. *
  131. */
  132. public void selectAll() {
  133. // TODO Do we need this API? Or provide as extension?
  134. setSelection(0, getValue().length());
  135. }
  136. /**
  137. * Sets the range of text to be selected.
  138. *
  139. * As a side effect the field will become focused.
  140. *
  141. * @param pos
  142. * the position of the first character to be selected
  143. * @param length
  144. * the number of characters to be selected
  145. */
  146. public void setSelection(int start, int length) {
  147. // TODO Do we need this API? Or provide as extension?
  148. getState().selectionStart = start;
  149. getState().selectionLength = length;
  150. focus();
  151. }
  152. /**
  153. * Sets the cursor position in the field. As a side effect the field will
  154. * become focused.
  155. *
  156. * @param pos
  157. * the position for the cursor
  158. */
  159. public void setCursorPosition(int pos) {
  160. setSelection(pos, 0);
  161. }
  162. @Deprecated
  163. public void addFocusListener(FocusListener listener) {
  164. addListener(FocusEvent.EVENT_ID, FocusEvent.class, listener,
  165. FocusListener.focusMethod);
  166. }
  167. @Deprecated
  168. public void removeFocusListener(FocusListener listener) {
  169. removeListener(FocusEvent.EVENT_ID, FocusEvent.class, listener);
  170. }
  171. @Deprecated
  172. public void addBlurListener(BlurListener listener) {
  173. addListener(BlurEvent.EVENT_ID, BlurEvent.class, listener,
  174. BlurListener.blurMethod);
  175. }
  176. @Deprecated
  177. public void removeBlurListener(BlurListener listener) {
  178. removeListener(BlurEvent.EVENT_ID, BlurEvent.class, listener);
  179. }
  180. @Override
  181. public void readDesign(Element design, DesignContext designContext) {
  182. super.readDesign(design, designContext);
  183. Attributes attr = design.attributes();
  184. if (attr.hasKey("maxlength")) {
  185. setMaxLength(DesignAttributeHandler.readAttribute("maxlength", attr,
  186. Integer.class));
  187. }
  188. }
  189. @Override
  190. protected Collection<String> getCustomAttributes() {
  191. Collection<String> customAttributes = super.getCustomAttributes();
  192. customAttributes.add("maxlength");
  193. customAttributes.add("max-length"); // to prevent this appearing in
  194. // output
  195. customAttributes.add("cursor-position");
  196. return customAttributes;
  197. }
  198. @Override
  199. public void writeDesign(Element design, DesignContext designContext) {
  200. super.writeDesign(design, designContext);
  201. AbstractTextField def = (AbstractTextField) designContext
  202. .getDefaultInstance(this);
  203. Attributes attr = design.attributes();
  204. DesignAttributeHandler.writeAttribute("maxlength", attr, getMaxLength(),
  205. def.getMaxLength(), Integer.class);
  206. }
  207. }