Coverage Report - org.kuali.student.common.ui.client.widgets.focus.FocusGroup
 
Classes in this File Line Coverage Branch Coverage Complexity
FocusGroup
0%
0/25
0%
0/6
1.615
FocusGroup$1
0%
0/13
0%
0/10
1.615
FocusGroup$2
0%
0/4
N/A
1.615
FocusGroup$3
0%
0/4
N/A
1.615
FocusGroup$SyntheticBlurEvent
0%
0/2
N/A
1.615
FocusGroup$SyntheticFocusEvent
0%
0/2
N/A
1.615
 
 1  
 /**
 2  
  * Copyright 2010 The Kuali Foundation Licensed under the
 3  
  * Educational Community License, Version 2.0 (the "License"); you may
 4  
  * not use this file except in compliance with the License. You may
 5  
  * obtain a copy of the License at
 6  
  *
 7  
  * http://www.osedu.org/licenses/ECL-2.0
 8  
  *
 9  
  * Unless required by applicable law or agreed to in writing,
 10  
  * software distributed under the License is distributed on an "AS IS"
 11  
  * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 12  
  * or implied. See the License for the specific language governing
 13  
  * permissions and limitations under the License.
 14  
  */
 15  
 
 16  
 package org.kuali.student.common.ui.client.widgets.focus;
 17  
 
 18  
 import java.util.HashMap;
 19  
 import java.util.Map;
 20  
 
 21  
 import com.google.gwt.event.dom.client.BlurEvent;
 22  
 import com.google.gwt.event.dom.client.BlurHandler;
 23  
 import com.google.gwt.event.dom.client.FocusEvent;
 24  
 import com.google.gwt.event.dom.client.FocusHandler;
 25  
 import com.google.gwt.event.dom.client.HasBlurHandlers;
 26  
 import com.google.gwt.event.dom.client.HasFocusHandlers;
 27  
 import com.google.gwt.event.shared.GwtEvent;
 28  
 import com.google.gwt.event.shared.HandlerManager;
 29  
 import com.google.gwt.event.shared.HandlerRegistration;
 30  
 import com.google.gwt.event.shared.HasHandlers;
 31  
 import com.google.gwt.user.client.Command;
 32  
 import com.google.gwt.user.client.DeferredCommand;
 33  
 import com.google.gwt.user.client.ui.Widget;
 34  
 
 35  0
 public class FocusGroup implements HasBlurHandlers, HasFocusHandlers, HasHandlers {
 36  
         private static class SyntheticBlurEvent extends BlurEvent {
 37  0
                 public SyntheticBlurEvent() {
 38  
                         // do nothing
 39  0
                 }
 40  
         }
 41  
 
 42  
         private static class SyntheticFocusEvent extends FocusEvent {
 43  0
                 public SyntheticFocusEvent() {
 44  
                         // do nothing
 45  0
                 }
 46  
         }
 47  
 
 48  
         private final HandlerManager handlers;
 49  0
         private final Map<Widget, Boolean> focusTrackers = new HashMap<Widget, Boolean>();
 50  0
         private boolean focusEventPending = false;
 51  0
         private boolean suppressed = false;
 52  
         
 53  0
         private boolean focused = false;
 54  
 
 55  0
         private final Command checkFocusState = new Command() {
 56  
                 @Override
 57  
                 public void execute() {
 58  0
                         focusEventPending = false;
 59  0
                         boolean nowFocused = false;
 60  0
                         for (final Boolean b : focusTrackers.values()) {
 61  0
                                 if (b) {
 62  0
                                         nowFocused = true;
 63  0
                                         break;
 64  
                                 }
 65  
                         }
 66  
 
 67  0
                         if (!suppressed && (focused ^ nowFocused)) {
 68  0
                                 if (nowFocused) {
 69  
                                         // we weren't focused, but now we are
 70  
                                         //                                        FocusEvent.fireNativeEvent(lastFocusEvent, FocusGroup.this);
 71  0
                                         handlers.fireEvent(new SyntheticFocusEvent());
 72  
                                 } else {
 73  
                                         // we were focused, now we aren't
 74  
                                         //                                        BlurEvent.fireNativeEvent(lastBlurEvent, FocusGroup.this);
 75  0
                                         handlers.fireEvent(new SyntheticBlurEvent());
 76  
                                 }
 77  
                         }
 78  
 
 79  0
                         focused = nowFocused;
 80  0
                 }
 81  
         };
 82  
 
 83  0
         public FocusGroup(final Widget parentWidget) {
 84  0
                 this.handlers = new HandlerManager(parentWidget);
 85  0
         }
 86  
 
 87  
         @Override
 88  
         public HandlerRegistration addBlurHandler(final BlurHandler handler) {
 89  0
                 return handlers.addHandler(BlurEvent.getType(), handler);
 90  
         }
 91  
 
 92  
         @Override
 93  
         public HandlerRegistration addFocusHandler(final FocusHandler handler) {
 94  0
                 return handlers.addHandler(FocusEvent.getType(), handler);
 95  
         }
 96  
 
 97  
         public void addWidget(final Widget w) {
 98  0
                 if (w instanceof HasBlurHandlers) {
 99  0
                         ((HasBlurHandlers) w).addBlurHandler(new BlurHandler() {
 100  
                                 @Override
 101  
                                 public void onBlur(final BlurEvent event) {
 102  0
                                         focusTrackers.put(w, false);
 103  0
                                         queueCheckFocusState();
 104  0
                                 }
 105  
                         });
 106  
                 }
 107  0
                 if (w instanceof HasFocusHandlers) {
 108  0
                         ((HasFocusHandlers) w).addFocusHandler(new FocusHandler() {
 109  
                                 @Override
 110  
                                 public void onFocus(final FocusEvent event) {
 111  0
                                         focusTrackers.put(w, true);
 112  0
                                         queueCheckFocusState();
 113  0
                                 }
 114  
                         });
 115  
                 }
 116  0
         }
 117  
 
 118  
         @Override
 119  
         public void fireEvent(final GwtEvent<?> event) {
 120  0
                 handlers.fireEvent(event);
 121  0
         }
 122  
 
 123  
         private void queueCheckFocusState() {
 124  0
                 if (!focusEventPending) {
 125  0
                         focusEventPending = true;
 126  0
                         DeferredCommand.addCommand(checkFocusState);
 127  
                 }
 128  0
         }
 129  
 
 130  
         public boolean isSuppressed() {
 131  0
                 return suppressed;
 132  
         }
 133  
 
 134  
         public void setSuppressed(boolean suppressed) {
 135  0
                 this.suppressed = suppressed;
 136  0
         }
 137  
         
 138  
         
 139  
 }