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.

ChildFocusAwareFlowPanel.java 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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.widgets;
  17. import java.util.HashMap;
  18. import java.util.Iterator;
  19. import java.util.Map;
  20. import com.google.gwt.core.client.Scheduler;
  21. import com.google.gwt.dom.client.Style.OutlineStyle;
  22. import com.google.gwt.event.dom.client.BlurEvent;
  23. import com.google.gwt.event.dom.client.BlurHandler;
  24. import com.google.gwt.event.dom.client.FocusEvent;
  25. import com.google.gwt.event.dom.client.FocusHandler;
  26. import com.google.gwt.event.dom.client.HasAllFocusHandlers;
  27. import com.google.gwt.event.shared.HandlerManager;
  28. import com.google.gwt.event.shared.HandlerRegistration;
  29. import com.google.gwt.user.client.ui.FocusWidget;
  30. import com.google.gwt.user.client.ui.Focusable;
  31. import com.google.gwt.user.client.ui.Widget;
  32. import com.vaadin.client.ui.FocusableFlowPanel;
  33. /**
  34. * Focusable flow panel which fires focus/blur events if it or any of its child
  35. * is focused/blurred, but doesn't fire events if it happens between its content
  36. * (child) elements.
  37. *
  38. * @author Vaadin Ltd
  39. * @since 8.0
  40. */
  41. public class ChildFocusAwareFlowPanel extends FocusableFlowPanel
  42. implements HasAllFocusHandlers {
  43. private class FocusBlurHandler implements BlurHandler, FocusHandler {
  44. private boolean blurOccurred;
  45. @Override
  46. public void onBlur(BlurEvent event) {
  47. blurOccurred = true;
  48. Scheduler.get().scheduleDeferred(() -> fireBlurEvent(event));
  49. }
  50. @Override
  51. public void onFocus(FocusEvent event) {
  52. if (!blurOccurred) {
  53. // no blur occurred before this focus event
  54. eventBus.fireEvent(event);
  55. } else {
  56. // blur occurred before this focus event
  57. // another component inside the panel was
  58. // blurred => do not fire the focus and set blurOccurred to
  59. // false, so
  60. // blur will not be fired, too
  61. blurOccurred = false;
  62. }
  63. }
  64. private void fireBlurEvent(BlurEvent event) {
  65. if (blurOccurred) {
  66. eventBus.fireEvent(event);
  67. blurOccurred = false;
  68. }
  69. }
  70. }
  71. private final HandlerManager eventBus;
  72. private final FocusBlurHandler handler = new FocusBlurHandler();
  73. private final Map<Widget, HandlerRegistration> focusRegistrations = new HashMap<>();
  74. private final Map<Widget, HandlerRegistration> blurRegistrations = new HashMap<>();
  75. /**
  76. * Creates a new panel instance.
  77. */
  78. public ChildFocusAwareFlowPanel() {
  79. eventBus = new HandlerManager(this);
  80. getElement().getStyle().setOutlineStyle(OutlineStyle.NONE);
  81. // The panel itself should not be focused.
  82. getElement().setTabIndex(-1);
  83. super.addFocusHandler(handler);
  84. super.addBlurHandler(handler);
  85. }
  86. @Override
  87. public void add(Widget widget) {
  88. super.add(widget);
  89. addHandlers(widget);
  90. }
  91. @Override
  92. public void clear() {
  93. super.clear();
  94. focusRegistrations.clear();
  95. blurRegistrations.clear();
  96. }
  97. @Override
  98. public void insert(Widget widget, int beforeIndex) {
  99. super.insert(widget, beforeIndex);
  100. addHandlers(widget);
  101. }
  102. @Override
  103. public boolean remove(int index) {
  104. Widget widget = getWidget(index);
  105. boolean isRemoved = super.remove(index);
  106. if (isRemoved) {
  107. removeHandlers(widget);
  108. }
  109. return isRemoved;
  110. }
  111. @Override
  112. public boolean remove(Widget widget) {
  113. boolean isRemoved = super.remove(widget);
  114. if (isRemoved) {
  115. removeHandlers(widget);
  116. }
  117. return isRemoved;
  118. }
  119. @Override
  120. public HandlerRegistration addFocusHandler(FocusHandler handler) {
  121. return eventBus.addHandler(FocusEvent.getType(), handler);
  122. }
  123. @Override
  124. public HandlerRegistration addBlurHandler(BlurHandler handler) {
  125. return eventBus.addHandler(BlurEvent.getType(), handler);
  126. }
  127. @Override
  128. public void focus() {
  129. Iterator<Widget> it = iterator();
  130. if (it.hasNext()) {
  131. Widget child = it.next();
  132. if (child instanceof Focusable) {
  133. ((Focusable) child).setFocus(true);
  134. }
  135. }
  136. }
  137. /**
  138. * Put focus in the first child Widget that can be focused and is not
  139. * disabled.
  140. */
  141. public void focusFirstEnabledChild() {
  142. for (int i = 0; i < getWidgetCount(); i++) {
  143. Widget widget = getWidget(i);
  144. if (!(widget instanceof FocusWidget)) {
  145. continue;
  146. }
  147. FocusWidget focusableChild = (FocusWidget) widget;
  148. if (focusableChild.isEnabled()) {
  149. focusableChild.setFocus(true);
  150. break;
  151. }
  152. }
  153. }
  154. private void addHandlers(Widget widget) {
  155. if (focusRegistrations.containsKey(widget)) {
  156. assert blurRegistrations.containsKey(widget);
  157. return;
  158. }
  159. if (widget instanceof FocusWidget) {
  160. HandlerRegistration focusRegistration = ((FocusWidget) widget)
  161. .addFocusHandler(handler);
  162. HandlerRegistration blurRegistration = ((FocusWidget) widget)
  163. .addBlurHandler(handler);
  164. focusRegistrations.put(widget, focusRegistration);
  165. blurRegistrations.put(widget, blurRegistration);
  166. }
  167. }
  168. private void removeHandlers(Widget widget) {
  169. HandlerRegistration focusRegistration = focusRegistrations
  170. .remove(widget);
  171. if (focusRegistration != null) {
  172. focusRegistration.removeHandler();
  173. }
  174. HandlerRegistration blurRegistration = blurRegistrations.remove(widget);
  175. if (blurRegistration != null) {
  176. blurRegistration.removeHandler();
  177. }
  178. }
  179. }