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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. /**
  54. * The interface for adding and removing <code>BlurEvent</code> listeners.
  55. * By implementing this interface a class explicitly announces that it will
  56. * generate a <code>BlurEvent</code> when it loses keyboard focus.
  57. *
  58. * @since 6.2
  59. * @see BlurListener
  60. * @see BlurEvent
  61. */
  62. public interface BlurNotifier extends Serializable {
  63. /**
  64. * Adds a <code>BlurListener</code> to the Component which gets fired
  65. * when a <code>Field</code> loses keyboard focus.
  66. *
  67. * @see BlurListener
  68. * @see Registration
  69. * @since 8.0
  70. *
  71. * @param listener
  72. * the blur listener to add, not null
  73. * @return a registration object for removing the listener
  74. */
  75. public Registration addBlurListener(BlurListener listener);
  76. }
  77. /**
  78. * <code>FocusEvent</code> class for holding additional event information.
  79. * Fired when a <code>Field</code> receives keyboard focus.
  80. *
  81. * @since 6.2
  82. */
  83. @SuppressWarnings("serial")
  84. public static class FocusEvent extends Component.Event {
  85. /**
  86. * Identifier for event that can be used in {@link EventRouter}
  87. */
  88. public static final String EVENT_ID = EventId.FOCUS;
  89. public FocusEvent(Component source) {
  90. super(source);
  91. }
  92. }
  93. /**
  94. * <code>FocusListener</code> interface for listening for
  95. * <code>FocusEvent</code> fired by a <code>Field</code>.
  96. *
  97. * @see FocusEvent
  98. * @since 6.2
  99. */
  100. @FunctionalInterface
  101. public interface FocusListener extends ConnectorEventListener {
  102. public static final Method focusMethod = ReflectTools
  103. .findMethod(FocusListener.class, "focus", FocusEvent.class);
  104. /**
  105. * Component has been focused
  106. *
  107. * @param event
  108. * Component focus event.
  109. */
  110. public void focus(FocusEvent event);
  111. }
  112. /**
  113. * <code>BlurEvent</code> class for holding additional event information.
  114. * Fired when a <code>Field</code> loses keyboard focus.
  115. *
  116. * @since 6.2
  117. */
  118. @SuppressWarnings("serial")
  119. public static class BlurEvent extends Component.Event {
  120. /**
  121. * Identifier for event that can be used in {@link EventRouter}
  122. */
  123. public static final String EVENT_ID = EventId.BLUR;
  124. public BlurEvent(Component source) {
  125. super(source);
  126. }
  127. }
  128. /**
  129. * <code>BlurListener</code> interface for listening for
  130. * <code>BlurEvent</code> fired by a <code>Field</code>.
  131. *
  132. * @see BlurEvent
  133. * @since 6.2
  134. */
  135. @FunctionalInterface
  136. public interface BlurListener extends ConnectorEventListener {
  137. public static final Method blurMethod = ReflectTools
  138. .findMethod(BlurListener.class, "blur", BlurEvent.class);
  139. /**
  140. * Component has been blurred
  141. *
  142. * @param event
  143. * Component blur event.
  144. */
  145. public void blur(BlurEvent event);
  146. }
  147. public static abstract class FocusAndBlurServerRpcImpl
  148. implements FocusAndBlurServerRpc {
  149. private final Component component;
  150. public FocusAndBlurServerRpcImpl(Component component) {
  151. this.component = component;
  152. }
  153. protected abstract void fireEvent(Event event);
  154. @Override
  155. public void blur() {
  156. fireEvent(new BlurEvent(component));
  157. }
  158. @Override
  159. public void focus() {
  160. fireEvent(new FocusEvent(component));
  161. }
  162. }
  163. /**
  164. * Focus and blur server RPC implementation which fires focus or blur event
  165. * using a provided event handler.
  166. *
  167. * @author Vaadin Ltd
  168. *
  169. */
  170. public static class FocusAndBlurServerRpcDecorator
  171. extends FocusAndBlurServerRpcImpl {
  172. private final SerializableConsumer<Event> eventHandler;
  173. /**
  174. * Create a new decorator instance.
  175. *
  176. * @param component
  177. * the source events component
  178. * @param eventHandler
  179. * the event handler to delegate event firing
  180. */
  181. public FocusAndBlurServerRpcDecorator(Component component,
  182. SerializableConsumer<Event> eventHandler) {
  183. super(component);
  184. this.eventHandler = eventHandler;
  185. }
  186. @Override
  187. protected void fireEvent(Event event) {
  188. eventHandler.accept(event);
  189. }
  190. }
  191. }