2
|
1 |
/*
|
2464
|
2 |
* Copyright 2003-2009 Sun Microsystems, Inc. All Rights Reserved.
|
2
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
package sun.awt;
|
|
26 |
|
|
27 |
import java.awt.Component;
|
|
28 |
import java.awt.KeyboardFocusManager;
|
|
29 |
import java.awt.Window;
|
2464
|
30 |
import java.awt.Canvas;
|
|
31 |
import java.awt.Scrollbar;
|
|
32 |
import java.awt.Panel;
|
|
33 |
|
|
34 |
import java.awt.event.FocusEvent;
|
2
|
35 |
|
|
36 |
import java.awt.peer.KeyboardFocusManagerPeer;
|
2464
|
37 |
import java.awt.peer.ComponentPeer;
|
2
|
38 |
|
|
39 |
import java.lang.reflect.InvocationTargetException;
|
|
40 |
import java.lang.reflect.Method;
|
|
41 |
|
2464
|
42 |
import java.util.logging.Level;
|
|
43 |
import java.util.logging.Logger;
|
2
|
44 |
|
2464
|
45 |
public abstract class KeyboardFocusManagerPeerImpl implements KeyboardFocusManagerPeer {
|
|
46 |
|
|
47 |
private static final Logger focusLog = Logger.getLogger("sun.awt.focus.KeyboardFocusManagerPeerImpl");
|
|
48 |
|
|
49 |
private static AWTAccessor.KeyboardFocusManagerAccessor kfmAccessor =
|
|
50 |
AWTAccessor.getKeyboardFocusManagerAccessor();
|
2
|
51 |
|
2464
|
52 |
// The constants are copied from java.awt.KeyboardFocusManager
|
|
53 |
public static final int SNFH_FAILURE = 0;
|
|
54 |
public static final int SNFH_SUCCESS_HANDLED = 1;
|
|
55 |
public static final int SNFH_SUCCESS_PROCEED = 2;
|
|
56 |
|
|
57 |
protected KeyboardFocusManager manager;
|
|
58 |
|
|
59 |
public KeyboardFocusManagerPeerImpl(KeyboardFocusManager manager) {
|
|
60 |
this.manager = manager;
|
2
|
61 |
}
|
|
62 |
|
2464
|
63 |
@Override
|
|
64 |
public void clearGlobalFocusOwner(Window activeWindow) {
|
|
65 |
if (activeWindow != null) {
|
|
66 |
Component focusOwner = activeWindow.getFocusOwner();
|
|
67 |
if (focusLog.isLoggable(Level.FINE)) focusLog.fine("Clearing global focus owner " + focusOwner);
|
|
68 |
if (focusOwner != null) {
|
|
69 |
FocusEvent fl = new CausedFocusEvent(focusOwner, FocusEvent.FOCUS_LOST, false, null,
|
|
70 |
CausedFocusEvent.Cause.CLEAR_GLOBAL_FOCUS_OWNER);
|
|
71 |
SunToolkit.postPriorityEvent(fl);
|
|
72 |
}
|
|
73 |
}
|
2
|
74 |
}
|
|
75 |
|
2464
|
76 |
/*
|
|
77 |
* WARNING: Don't call it on the Toolkit thread.
|
|
78 |
*
|
|
79 |
* Checks if the component:
|
|
80 |
* 1) accepts focus on click (in general)
|
|
81 |
* 2) may be a focus owner (in particular)
|
|
82 |
*/
|
|
83 |
public static boolean shouldFocusOnClick(Component component) {
|
|
84 |
boolean acceptFocusOnClick = false;
|
|
85 |
|
|
86 |
// A component is generally allowed to accept focus on click
|
|
87 |
// if its peer is focusable. There're some exceptions though.
|
|
88 |
|
|
89 |
|
|
90 |
// CANVAS & SCROLLBAR accept focus on click
|
|
91 |
if (component instanceof Canvas ||
|
|
92 |
component instanceof Scrollbar)
|
|
93 |
{
|
|
94 |
acceptFocusOnClick = true;
|
|
95 |
|
|
96 |
// PANEL, empty only, accepts focus on click
|
|
97 |
} else if (component instanceof Panel) {
|
|
98 |
acceptFocusOnClick = (((Panel)component).getComponentCount() == 0);
|
|
99 |
|
|
100 |
|
|
101 |
// Other components
|
|
102 |
} else {
|
|
103 |
ComponentPeer peer = (component != null ? component.getPeer() : null);
|
|
104 |
acceptFocusOnClick = (peer != null ? peer.isFocusable() : false);
|
|
105 |
}
|
|
106 |
return acceptFocusOnClick &&
|
|
107 |
AWTAccessor.getComponentAccessor().canBeFocusOwner(component);
|
2
|
108 |
}
|
|
109 |
|
2464
|
110 |
/*
|
|
111 |
* Posts proper lost/gain focus events to the event queue.
|
|
112 |
*/
|
|
113 |
public static boolean deliverFocus(Component lightweightChild,
|
|
114 |
Component target,
|
|
115 |
boolean temporary,
|
|
116 |
boolean focusedWindowChangeAllowed,
|
|
117 |
long time,
|
|
118 |
CausedFocusEvent.Cause cause,
|
|
119 |
Component currentFocusOwner) // provided by the descendant peers
|
|
120 |
{
|
|
121 |
if (lightweightChild == null) {
|
|
122 |
lightweightChild = (Component)target;
|
|
123 |
}
|
|
124 |
|
|
125 |
Component currentOwner = currentFocusOwner;
|
|
126 |
if (currentOwner != null && currentOwner.getPeer() == null) {
|
|
127 |
currentOwner = null;
|
|
128 |
}
|
|
129 |
if (currentOwner != null) {
|
|
130 |
FocusEvent fl = new CausedFocusEvent(currentOwner, FocusEvent.FOCUS_LOST,
|
|
131 |
false, lightweightChild, cause);
|
|
132 |
|
|
133 |
if (focusLog.isLoggable(Level.FINER)) focusLog.finer("Posting focus event: " + fl);
|
|
134 |
SunToolkit.postPriorityEvent(fl);
|
|
135 |
}
|
|
136 |
|
|
137 |
FocusEvent fg = new CausedFocusEvent(lightweightChild, FocusEvent.FOCUS_GAINED,
|
|
138 |
false, currentOwner, cause);
|
|
139 |
|
|
140 |
if (focusLog.isLoggable(Level.FINER)) focusLog.finer("Posting focus event: " + fg);
|
|
141 |
SunToolkit.postPriorityEvent(fg);
|
|
142 |
return true;
|
2
|
143 |
}
|
|
144 |
|
2464
|
145 |
// WARNING: Don't call it on the Toolkit thread.
|
|
146 |
public static boolean requestFocusFor(Component target, CausedFocusEvent.Cause cause) {
|
|
147 |
return AWTAccessor.getComponentAccessor().requestFocus(target, cause);
|
|
148 |
}
|
|
149 |
|
|
150 |
// WARNING: Don't call it on the Toolkit thread.
|
|
151 |
public static int shouldNativelyFocusHeavyweight(Component heavyweight,
|
|
152 |
Component descendant,
|
|
153 |
boolean temporary,
|
|
154 |
boolean focusedWindowChangeAllowed,
|
|
155 |
long time,
|
|
156 |
CausedFocusEvent.Cause cause)
|
|
157 |
{
|
|
158 |
return kfmAccessor.shouldNativelyFocusHeavyweight(
|
|
159 |
heavyweight, descendant, temporary, focusedWindowChangeAllowed, time, cause);
|
|
160 |
}
|
|
161 |
|
2
|
162 |
public static void removeLastFocusRequest(Component heavyweight) {
|
2464
|
163 |
kfmAccessor.removeLastFocusRequest(heavyweight);
|
|
164 |
}
|
|
165 |
|
|
166 |
// WARNING: Don't call it on the Toolkit thread.
|
|
167 |
public static boolean processSynchronousLightweightTransfer(Component heavyweight,
|
|
168 |
Component descendant,
|
|
169 |
boolean temporary,
|
|
170 |
boolean focusedWindowChangeAllowed,
|
|
171 |
long time)
|
|
172 |
{
|
|
173 |
return kfmAccessor.processSynchronousLightweightTransfer(
|
|
174 |
heavyweight, descendant, temporary, focusedWindowChangeAllowed, time);
|
2
|
175 |
}
|
|
176 |
}
|