Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

AbstractTextField.java 6.8KB

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