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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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/blured, but doesn't fire events if it happens between its content
  36. * (child) elements.
  37. *
  38. * @author Vaadin Ltd
  39. *
  40. */
  41. public class ChildFocusAwareFlowPanel extends FocusableFlowPanel
  42. implements HasAllFocusHandlers {
  43. private class FocusBlurHandler implements BlurHandler, FocusHandler {
  44. private boolean blurOccured;
  45. @Override
  46. public void onBlur(BlurEvent event) {
  47. blurOccured = true;
  48. Scheduler.get().scheduleDeferred(() -> fireBlurEvent(event));
  49. }
  50. @Override
  51. public void onFocus(FocusEvent event) {
  52. if (!blurOccured) {
  53. // no blur occured before this focus event
  54. eventBus.fireEvent(event);
  55. } else {
  56. // blur occured before this focus event
  57. // another component inside the panel was
  58. // blurred => do not fire the focus and set blurOccured to
  59. // false, so
  60. // blur will not be fired, too
  61. blurOccured = false;
  62. }
  63. }
  64. private void fireBlurEvent(BlurEvent event) {
  65. if (blurOccured) {
  66. eventBus.fireEvent(event);
  67. blurOccured = 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> iterator = iterator();
  128. if (iterator.hasNext()) {
  129. Widget widget = iterator.next();
  130. if (widget instanceof Focusable) {
  131. ((Focusable) widget).setFocus(true);
  132. }
  133. }
  134. }
  135. private void addHandlers(Widget widget) {
  136. if (focusRegistrations.containsKey(widget)) {
  137. assert blurRegistrations.containsKey(widget);
  138. return;
  139. }
  140. if (widget instanceof FocusWidget) {
  141. HandlerRegistration focusRegistration = ((FocusWidget) widget)
  142. .addFocusHandler(handler);
  143. HandlerRegistration blurRegistration = ((FocusWidget) widget)
  144. .addBlurHandler(handler);
  145. focusRegistrations.put(widget, focusRegistration);
  146. blurRegistrations.put(widget, blurRegistration);
  147. }
  148. }
  149. private void removeHandlers(Widget widget) {
  150. HandlerRegistration focusRegistration = focusRegistrations
  151. .remove(widget);
  152. if (focusRegistration != null) {
  153. focusRegistration.removeHandler();
  154. }
  155. HandlerRegistration blurRegistration = blurRegistrations.remove(widget);
  156. if (blurRegistration != null) {
  157. blurRegistration.removeHandler();
  158. }
  159. }
  160. }