1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
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 | |
|
39 | 0 | } |
40 | |
} |
41 | |
|
42 | |
private static class SyntheticFocusEvent extends FocusEvent { |
43 | 0 | public SyntheticFocusEvent() { |
44 | |
|
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 | |
|
70 | |
|
71 | 0 | handlers.fireEvent(new SyntheticFocusEvent()); |
72 | |
} else { |
73 | |
|
74 | |
|
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 | |
} |