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.

ConnectorFocusAndBlurHandler.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright 2000-2018 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.client.ui;
  17. import com.google.gwt.event.dom.client.BlurEvent;
  18. import com.google.gwt.event.dom.client.BlurHandler;
  19. import com.google.gwt.event.dom.client.FocusEvent;
  20. import com.google.gwt.event.dom.client.FocusHandler;
  21. import com.google.gwt.event.dom.client.HasAllFocusHandlers;
  22. import com.google.gwt.event.shared.HandlerRegistration;
  23. import com.google.gwt.user.client.ui.Widget;
  24. import com.vaadin.client.EventHelper;
  25. import com.vaadin.client.communication.StateChangeEvent;
  26. import com.vaadin.client.communication.StateChangeEvent.StateChangeHandler;
  27. import com.vaadin.shared.EventId;
  28. import com.vaadin.shared.communication.FieldRpc.FocusAndBlurServerRpc;
  29. /**
  30. * A handler for focus and blur events which uses {@link FocusAndBlurServerRpc}
  31. * to transmit received events to the server. Events are only handled if there
  32. * is a corresponding listener on the server side.
  33. *
  34. * @since 7.6
  35. * @author Vaadin Ltd
  36. */
  37. public class ConnectorFocusAndBlurHandler
  38. implements StateChangeHandler, FocusHandler, BlurHandler {
  39. private final AbstractComponentConnector connector;
  40. private final Widget widget;
  41. private HandlerRegistration focusRegistration = null;
  42. private HandlerRegistration blurRegistration = null;
  43. private HandlerRegistration stateChangeRegistration = null;
  44. /**
  45. * Add focus/blur handlers to the widget of the {@code connector}.
  46. *
  47. * @param connector
  48. * connector whose widget is a target to add focus/blur handlers
  49. * @return ConnectorFocusAndBlurHandler instance to remove all registered
  50. * handlers
  51. */
  52. public static ConnectorFocusAndBlurHandler addHandlers(
  53. AbstractComponentConnector connector) {
  54. return addHandlers(connector, connector.getWidget());
  55. }
  56. /**
  57. * Add focus/blur handlers to the widget and a state change handler for the
  58. * {@code connector}.
  59. *
  60. * @param connector
  61. * connector to register state change handler
  62. * @param widget
  63. * widget to register focus/blur handler
  64. * @return ConnectorFocusAndBlurHandler instance to remove all registered
  65. * handlers
  66. */
  67. public static ConnectorFocusAndBlurHandler addHandlers(
  68. AbstractComponentConnector connector, Widget widget) {
  69. ConnectorFocusAndBlurHandler handler = new ConnectorFocusAndBlurHandler(
  70. connector, widget);
  71. handler.stateChangeRegistration = connector
  72. .addStateChangeHandler("registeredEventListeners", handler);
  73. return handler;
  74. }
  75. private ConnectorFocusAndBlurHandler(AbstractComponentConnector connector,
  76. Widget widget) {
  77. this.connector = connector;
  78. this.widget = widget;
  79. }
  80. @Override
  81. public void onStateChanged(StateChangeEvent stateChangeEvent) {
  82. if (widget instanceof HasAllFocusHandlers) {
  83. HasAllFocusHandlers focusHandlers = (HasAllFocusHandlers) widget;
  84. focusRegistration = EventHelper.updateHandler(connector,
  85. EventId.FOCUS, focusRegistration,
  86. () -> focusHandlers.addFocusHandler(this));
  87. blurRegistration = EventHelper.updateHandler(connector,
  88. EventId.BLUR, blurRegistration,
  89. () -> focusHandlers.addBlurHandler(this));
  90. } else {
  91. focusRegistration = EventHelper.updateHandler(connector, this,
  92. EventId.FOCUS, focusRegistration, FocusEvent.getType(),
  93. widget);
  94. blurRegistration = EventHelper.updateHandler(connector, this,
  95. EventId.BLUR, blurRegistration, BlurEvent.getType(),
  96. widget);
  97. }
  98. }
  99. @Override
  100. public void onFocus(FocusEvent event) {
  101. // updateHandler ensures that this is called only when
  102. // there is a listener on the server side
  103. getRpc().focus();
  104. }
  105. @Override
  106. public void onBlur(BlurEvent event) {
  107. // updateHandler ensures that this is called only when
  108. // there is a listener on the server side
  109. getRpc().blur();
  110. }
  111. /**
  112. * Remove all handlers from the widget and the connector.
  113. */
  114. public void removeHandlers() {
  115. if (focusRegistration != null) {
  116. focusRegistration.removeHandler();
  117. }
  118. if (blurRegistration != null) {
  119. blurRegistration.removeHandler();
  120. }
  121. if (stateChangeRegistration != null) {
  122. stateChangeRegistration.removeHandler();
  123. }
  124. }
  125. private FocusAndBlurServerRpc getRpc() {
  126. return connector.getRpcProxy(FocusAndBlurServerRpc.class);
  127. }
  128. }