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

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