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.

FieldEvents.java 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.event;
  5. import java.io.Serializable;
  6. import java.lang.reflect.Method;
  7. import com.vaadin.terminal.gwt.client.EventId;
  8. import com.vaadin.terminal.gwt.client.communication.FieldRpc.FocusAndBlurServerRpc;
  9. import com.vaadin.tools.ReflectTools;
  10. import com.vaadin.ui.Component;
  11. import com.vaadin.ui.Component.Event;
  12. import com.vaadin.ui.Field;
  13. import com.vaadin.ui.Field.ValueChangeEvent;
  14. import com.vaadin.ui.TextField;
  15. /**
  16. * Interface that serves as a wrapper for {@link Field} related events.
  17. */
  18. public interface FieldEvents {
  19. /**
  20. * The interface for adding and removing <code>FocusEvent</code> listeners.
  21. * By implementing this interface a class explicitly announces that it will
  22. * generate a <code>FocusEvent</code> when it receives keyboard focus.
  23. * <p>
  24. * Note: The general Java convention is not to explicitly declare that a
  25. * class generates events, but to directly define the
  26. * <code>addListener</code> and <code>removeListener</code> methods. That
  27. * way the caller of these methods has no real way of finding out if the
  28. * class really will send the events, or if it just defines the methods to
  29. * be able to implement an interface.
  30. * </p>
  31. *
  32. * @since 6.2
  33. * @see FocusListener
  34. * @see FocusEvent
  35. */
  36. public interface FocusNotifier extends Serializable {
  37. /**
  38. * Adds a <code>FocusListener</code> to the Component which gets fired
  39. * when a <code>Field</code> receives keyboard focus.
  40. *
  41. * @param listener
  42. * @see FocusListener
  43. * @since 6.2
  44. */
  45. public void addListener(FocusListener listener);
  46. /**
  47. * Removes a <code>FocusListener</code> from the Component.
  48. *
  49. * @param listener
  50. * @see FocusListener
  51. * @since 6.2
  52. */
  53. public void removeListener(FocusListener listener);
  54. }
  55. /**
  56. * The interface for adding and removing <code>BlurEvent</code> listeners.
  57. * By implementing this interface a class explicitly announces that it will
  58. * generate a <code>BlurEvent</code> when it loses keyboard focus.
  59. * <p>
  60. * Note: The general Java convention is not to explicitly declare that a
  61. * class generates events, but to directly define the
  62. * <code>addListener</code> and <code>removeListener</code> methods. That
  63. * way the caller of these methods has no real way of finding out if the
  64. * class really will send the events, or if it just defines the methods to
  65. * be able to implement an interface.
  66. * </p>
  67. *
  68. * @since 6.2
  69. * @see BlurListener
  70. * @see BlurEvent
  71. */
  72. public interface BlurNotifier extends Serializable {
  73. /**
  74. * Adds a <code>BlurListener</code> to the Component which gets fired
  75. * when a <code>Field</code> loses keyboard focus.
  76. *
  77. * @param listener
  78. * @see BlurListener
  79. * @since 6.2
  80. */
  81. public void addListener(BlurListener listener);
  82. /**
  83. * Removes a <code>BlurListener</code> from the Component.
  84. *
  85. * @param listener
  86. * @see BlurListener
  87. * @since 6.2
  88. */
  89. public void removeListener(BlurListener listener);
  90. }
  91. /**
  92. * <code>FocusEvent</code> class for holding additional event information.
  93. * Fired when a <code>Field</code> receives keyboard focus.
  94. *
  95. * @since 6.2
  96. */
  97. @SuppressWarnings("serial")
  98. public class FocusEvent extends Component.Event {
  99. /**
  100. * Identifier for event that can be used in {@link EventRouter}
  101. */
  102. public static final String EVENT_ID = EventId.FOCUS;
  103. public FocusEvent(Component source) {
  104. super(source);
  105. }
  106. }
  107. /**
  108. * <code>FocusListener</code> interface for listening for
  109. * <code>FocusEvent</code> fired by a <code>Field</code>.
  110. *
  111. * @see FocusEvent
  112. * @since 6.2
  113. */
  114. public interface FocusListener extends ComponentEventListener {
  115. public static final Method focusMethod = ReflectTools.findMethod(
  116. FocusListener.class, "focus", FocusEvent.class);
  117. /**
  118. * Component has been focused
  119. *
  120. * @param event
  121. * Component focus event.
  122. */
  123. public void focus(FocusEvent event);
  124. }
  125. /**
  126. * <code>BlurEvent</code> class for holding additional event information.
  127. * Fired when a <code>Field</code> loses keyboard focus.
  128. *
  129. * @since 6.2
  130. */
  131. @SuppressWarnings("serial")
  132. public class BlurEvent extends Component.Event {
  133. /**
  134. * Identifier for event that can be used in {@link EventRouter}
  135. */
  136. public static final String EVENT_ID = EventId.BLUR;
  137. public BlurEvent(Component source) {
  138. super(source);
  139. }
  140. }
  141. /**
  142. * <code>BlurListener</code> interface for listening for
  143. * <code>BlurEvent</code> fired by a <code>Field</code>.
  144. *
  145. * @see BlurEvent
  146. * @since 6.2
  147. */
  148. public interface BlurListener extends ComponentEventListener {
  149. public static final Method blurMethod = ReflectTools.findMethod(
  150. BlurListener.class, "blur", BlurEvent.class);
  151. /**
  152. * Component has been blurred
  153. *
  154. * @param event
  155. * Component blur event.
  156. */
  157. public void blur(BlurEvent event);
  158. }
  159. /**
  160. * TextChangeEvents are fired when the user is editing the text content of a
  161. * field. Most commonly text change events are triggered by typing text with
  162. * keyboard, but e.g. pasting content from clip board to a text field also
  163. * triggers an event.
  164. * <p>
  165. * TextChangeEvents differ from {@link ValueChangeEvent}s so that they are
  166. * triggered repeatedly while the end user is filling the field.
  167. * ValueChangeEvents are not fired until the user for example hits enter or
  168. * focuses another field. Also note the difference that TextChangeEvents are
  169. * only fired if the change is triggered from the user, while
  170. * ValueChangeEvents are also fired if the field value is set by the
  171. * application code.
  172. * <p>
  173. * The {@link TextChangeNotifier}s implementation may decide when exactly
  174. * TextChangeEvents are fired. TextChangeEvents are not necessary fire for
  175. * example on each key press, but buffered with a small delay. The
  176. * {@link TextField} component supports different modes for triggering
  177. * TextChangeEvents.
  178. *
  179. * @see TextChangeListener
  180. * @see TextChangeNotifier
  181. * @see TextField#setTextChangeEventMode(com.vaadin.ui.TextField.TextChangeEventMode)
  182. * @since 6.5
  183. */
  184. public static abstract class TextChangeEvent extends Component.Event {
  185. public TextChangeEvent(Component source) {
  186. super(source);
  187. }
  188. /**
  189. * @return the text content of the field after the
  190. * {@link TextChangeEvent}
  191. */
  192. public abstract String getText();
  193. /**
  194. * @return the cursor position during after the {@link TextChangeEvent}
  195. */
  196. public abstract int getCursorPosition();
  197. }
  198. /**
  199. * A listener for {@link TextChangeEvent}s.
  200. *
  201. * @since 6.5
  202. */
  203. public interface TextChangeListener extends ComponentEventListener {
  204. public static String EVENT_ID = "ie";
  205. public static Method EVENT_METHOD = ReflectTools.findMethod(
  206. TextChangeListener.class, "textChange", TextChangeEvent.class);
  207. /**
  208. * This method is called repeatedly while the text is edited by a user.
  209. *
  210. * @param event
  211. * the event providing details of the text change
  212. */
  213. public void textChange(TextChangeEvent event);
  214. }
  215. /**
  216. * An interface implemented by a {@link Field} supporting
  217. * {@link TextChangeEvent}s. An example a {@link TextField} supports
  218. * {@link TextChangeListener}s.
  219. */
  220. public interface TextChangeNotifier extends Serializable {
  221. public void addListener(TextChangeListener listener);
  222. public void removeListener(TextChangeListener listener);
  223. }
  224. public static abstract class FocusAndBlurServerRpcImpl implements
  225. FocusAndBlurServerRpc {
  226. private Component component;
  227. public FocusAndBlurServerRpcImpl(Component component) {
  228. this.component = component;
  229. }
  230. protected abstract void fireEvent(Event event);
  231. @Override
  232. public void blur() {
  233. fireEvent(new BlurEvent(component));
  234. }
  235. @Override
  236. public void focus() {
  237. fireEvent(new FocusEvent(component));
  238. }
  239. };
  240. }