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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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.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. super.addFocusHandler(handler);
  82. super.addBlurHandler(handler);
  83. }
  84. @Override
  85. public void add(Widget widget) {
  86. super.add(widget);
  87. addHandlers(widget);
  88. }
  89. @Override
  90. public void clear() {
  91. super.clear();
  92. focusRegistrations.clear();
  93. blurRegistrations.clear();
  94. }
  95. @Override
  96. public void insert(Widget widget, int beforeIndex) {
  97. super.insert(widget, beforeIndex);
  98. addHandlers(widget);
  99. }
  100. @Override
  101. public boolean remove(int index) {
  102. Widget widget = getWidget(index);
  103. boolean isRemoved = super.remove(index);
  104. if (isRemoved) {
  105. removeHandlers(widget);
  106. }
  107. return isRemoved;
  108. }
  109. @Override
  110. public boolean remove(Widget widget) {
  111. boolean isRemoved = super.remove(widget);
  112. if (isRemoved) {
  113. removeHandlers(widget);
  114. }
  115. return isRemoved;
  116. }
  117. @Override
  118. public HandlerRegistration addFocusHandler(FocusHandler handler) {
  119. return eventBus.addHandler(FocusEvent.getType(), handler);
  120. }
  121. @Override
  122. public HandlerRegistration addBlurHandler(BlurHandler handler) {
  123. return eventBus.addHandler(BlurEvent.getType(), handler);
  124. }
  125. @Override
  126. public void focus() {
  127. Iterator<Widget> it = iterator();
  128. if (it.hasNext()) {
  129. Widget child = it.next();
  130. if (child instanceof Focusable) {
  131. ((Focusable) child).setFocus(true);
  132. }
  133. }
  134. }
  135. /**
  136. * Put focus in the first child Widget that can be focused and is not
  137. * disabled.
  138. */
  139. public void focusFirstEnabledChild() {
  140. for (int i = 0; i < getWidgetCount(); i++) {
  141. Widget widget = getWidget(i);
  142. if (!(widget instanceof FocusWidget)) {
  143. continue;
  144. }
  145. FocusWidget focusableChild = (FocusWidget) widget;
  146. if (focusableChild.isEnabled()) {
  147. focusableChild.setFocus(true);
  148. break;
  149. }
  150. }
  151. }
  152. private void addHandlers(Widget widget) {
  153. if (focusRegistrations.containsKey(widget)) {
  154. assert blurRegistrations.containsKey(widget);
  155. return;
  156. }
  157. if (widget instanceof FocusWidget) {
  158. HandlerRegistration focusRegistration = ((FocusWidget) widget)
  159. .addFocusHandler(handler);
  160. HandlerRegistration blurRegistration = ((FocusWidget) widget)
  161. .addBlurHandler(handler);
  162. focusRegistrations.put(widget, focusRegistration);
  163. blurRegistrations.put(widget, blurRegistration);
  164. }
  165. }
  166. private void removeHandlers(Widget widget) {
  167. HandlerRegistration focusRegistration = focusRegistrations
  168. .remove(widget);
  169. if (focusRegistration != null) {
  170. focusRegistration.removeHandler();
  171. }
  172. HandlerRegistration blurRegistration = blurRegistrations.remove(widget);
  173. if (blurRegistration != null) {
  174. blurRegistration.removeHandler();
  175. }
  176. }
  177. }