Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

FieldEvents.java 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * Copyright 2000-2016 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.server.SerializableConsumer;
  20. import com.vaadin.shared.EventId;
  21. import com.vaadin.shared.Registration;
  22. import com.vaadin.shared.communication.FieldRpc.FocusAndBlurServerRpc;
  23. import com.vaadin.ui.Component;
  24. import com.vaadin.ui.Component.Event;
  25. import com.vaadin.util.ReflectTools;
  26. /**
  27. * Interface that serves as a wrapper for {@link Field} related events.
  28. */
  29. public interface FieldEvents {
  30. /**
  31. * The interface for adding and removing <code>FocusEvent</code> listeners.
  32. * By implementing this interface a class explicitly announces that it will
  33. * generate a <code>FocusEvent</code> when it receives keyboard focus.
  34. *
  35. * @since 6.2
  36. * @see FocusListener
  37. * @see FocusEvent
  38. */
  39. public interface FocusNotifier extends Serializable {
  40. /**
  41. * Adds a <code>FocusListener</code> to the Component which gets fired
  42. * when a <code>Field</code> receives keyboard focus.
  43. *
  44. * @param listener
  45. * the focus listener to add, not null
  46. * @return a registration object for removing the listener
  47. * @see FocusListener
  48. * @see Registration
  49. * @since 8.0
  50. */
  51. public Registration addFocusListener(FocusListener listener);
  52. /**
  53. * Removes a <code>BlurListener</code> from the Component.
  54. *
  55. * @param listener
  56. * @see FocusListener
  57. * @since 6.2
  58. *
  59. * @deprecated As of 8.0, replaced by {@link Registration#remove()} in
  60. * the registration object returned from
  61. * {@link #addFocusListener(FocusListener)}.
  62. */
  63. @Deprecated
  64. public void removeFocusListener(FocusListener listener);
  65. }
  66. /**
  67. * The interface for adding and removing <code>BlurEvent</code> listeners.
  68. * By implementing this interface a class explicitly announces that it will
  69. * generate a <code>BlurEvent</code> when it loses keyboard focus.
  70. *
  71. * @since 6.2
  72. * @see BlurListener
  73. * @see BlurEvent
  74. */
  75. public interface BlurNotifier extends Serializable {
  76. /**
  77. * Adds a <code>BlurListener</code> to the Component which gets fired
  78. * when a <code>Field</code> loses keyboard focus.
  79. *
  80. * @see BlurListener
  81. * @see Registration
  82. * @since 8.0
  83. *
  84. * @param listener
  85. * the blur listener to add, not null
  86. * @return a registration object for removing the listener
  87. */
  88. public Registration addBlurListener(BlurListener listener);
  89. /**
  90. * Removes a <code>BlurListener</code> from the Component.
  91. *
  92. * @see BlurListener
  93. * @since 6.2
  94. *
  95. * @param listener
  96. * the listener to remove
  97. *
  98. * @deprecated As of 8.0, replaced by {@link Registration#remove()} in
  99. * the registration object returned from
  100. * {@link #addFocusListener(FocusListener)}.
  101. */
  102. @Deprecated
  103. public void removeBlurListener(BlurListener listener);
  104. }
  105. /**
  106. * <code>FocusEvent</code> class for holding additional event information.
  107. * Fired when a <code>Field</code> receives keyboard focus.
  108. *
  109. * @since 6.2
  110. */
  111. @SuppressWarnings("serial")
  112. public static class FocusEvent extends Component.Event {
  113. /**
  114. * Identifier for event that can be used in {@link EventRouter}
  115. */
  116. public static final String EVENT_ID = EventId.FOCUS;
  117. public FocusEvent(Component source) {
  118. super(source);
  119. }
  120. }
  121. /**
  122. * <code>FocusListener</code> interface for listening for
  123. * <code>FocusEvent</code> fired by a <code>Field</code>.
  124. *
  125. * @see FocusEvent
  126. * @since 6.2
  127. */
  128. public interface FocusListener extends ConnectorEventListener {
  129. public static final Method focusMethod = ReflectTools
  130. .findMethod(FocusListener.class, "focus", FocusEvent.class);
  131. /**
  132. * Component has been focused
  133. *
  134. * @param event
  135. * Component focus event.
  136. */
  137. public void focus(FocusEvent event);
  138. }
  139. /**
  140. * <code>BlurEvent</code> class for holding additional event information.
  141. * Fired when a <code>Field</code> loses keyboard focus.
  142. *
  143. * @since 6.2
  144. */
  145. @SuppressWarnings("serial")
  146. public static class BlurEvent extends Component.Event {
  147. /**
  148. * Identifier for event that can be used in {@link EventRouter}
  149. */
  150. public static final String EVENT_ID = EventId.BLUR;
  151. public BlurEvent(Component source) {
  152. super(source);
  153. }
  154. }
  155. /**
  156. * <code>BlurListener</code> interface for listening for
  157. * <code>BlurEvent</code> fired by a <code>Field</code>.
  158. *
  159. * @see BlurEvent
  160. * @since 6.2
  161. */
  162. public interface BlurListener extends ConnectorEventListener {
  163. public static final Method blurMethod = ReflectTools
  164. .findMethod(BlurListener.class, "blur", BlurEvent.class);
  165. /**
  166. * Component has been blurred
  167. *
  168. * @param event
  169. * Component blur event.
  170. */
  171. public void blur(BlurEvent event);
  172. }
  173. public static abstract class FocusAndBlurServerRpcImpl
  174. implements FocusAndBlurServerRpc {
  175. private Component component;
  176. public FocusAndBlurServerRpcImpl(Component component) {
  177. this.component = component;
  178. }
  179. protected abstract void fireEvent(Event event);
  180. @Override
  181. public void blur() {
  182. fireEvent(new BlurEvent(component));
  183. }
  184. @Override
  185. public void focus() {
  186. fireEvent(new FocusEvent(component));
  187. }
  188. }
  189. /**
  190. * Focus and blur server RPC implementation which fires focus or blur event
  191. * using a provided event handler.
  192. *
  193. * @author Vaadin Ltd
  194. *
  195. */
  196. public static class FocusAndBlurServerRpcDecorator
  197. extends FocusAndBlurServerRpcImpl {
  198. private final SerializableConsumer<Event> eventHandler;
  199. /**
  200. * Create a new decorator instance.
  201. *
  202. * @param component
  203. * the source events component
  204. * @param eventHandler
  205. * the event handler to delegate event firing
  206. */
  207. public FocusAndBlurServerRpcDecorator(Component component,
  208. SerializableConsumer<Event> eventHandler) {
  209. super(component);
  210. this.eventHandler = eventHandler;
  211. }
  212. @Override
  213. protected void fireEvent(Event event) {
  214. eventHandler.accept(event);
  215. }
  216. }
  217. }