51 import java.util.LinkedList; |
51 import java.util.LinkedList; |
52 import java.util.Set; |
52 import java.util.Set; |
53 import java.util.StringTokenizer; |
53 import java.util.StringTokenizer; |
54 import java.util.WeakHashMap; |
54 import java.util.WeakHashMap; |
55 |
55 |
56 import java.util.logging.Level; |
56 import sun.util.logging.PlatformLogger; |
57 import java.util.logging.Logger; |
|
58 |
57 |
59 import sun.awt.AppContext; |
58 import sun.awt.AppContext; |
60 import sun.awt.HeadlessToolkit; |
59 import sun.awt.HeadlessToolkit; |
61 import sun.awt.SunToolkit; |
60 import sun.awt.SunToolkit; |
62 import sun.awt.CausedFocusEvent; |
61 import sun.awt.CausedFocusEvent; |
109 public abstract class KeyboardFocusManager |
108 public abstract class KeyboardFocusManager |
110 implements KeyEventDispatcher, KeyEventPostProcessor |
109 implements KeyEventDispatcher, KeyEventPostProcessor |
111 { |
110 { |
112 |
111 |
113 // Shared focus engine logger |
112 // Shared focus engine logger |
114 private static final Logger focusLog = Logger.getLogger("java.awt.focus.KeyboardFocusManager"); |
113 private static final PlatformLogger focusLog = PlatformLogger.getLogger("java.awt.focus.KeyboardFocusManager"); |
115 |
114 |
116 static { |
115 static { |
117 /* ensure that the necessary native libraries are loaded */ |
116 /* ensure that the necessary native libraries are loaded */ |
118 Toolkit.loadLibraries(); |
117 Toolkit.loadLibraries(); |
119 if (!GraphicsEnvironment.isHeadless()) { |
118 if (!GraphicsEnvironment.isHeadless()) { |
502 protected Component getGlobalFocusOwner() throws SecurityException { |
501 protected Component getGlobalFocusOwner() throws SecurityException { |
503 synchronized (KeyboardFocusManager.class) { |
502 synchronized (KeyboardFocusManager.class) { |
504 if (this == getCurrentKeyboardFocusManager()) { |
503 if (this == getCurrentKeyboardFocusManager()) { |
505 return focusOwner; |
504 return focusOwner; |
506 } else { |
505 } else { |
507 if (focusLog.isLoggable(Level.FINER)) { |
506 if (focusLog.isLoggable(PlatformLogger.FINER)) { |
508 focusLog.log(Level.FINER, "This manager is " + this + ", current is " + getCurrentKeyboardFocusManager()); |
507 focusLog.finer("This manager is " + this + ", current is " + getCurrentKeyboardFocusManager()); |
509 } |
508 } |
510 throw new SecurityException(notPrivileged); |
509 throw new SecurityException(notPrivileged); |
511 } |
510 } |
512 } |
511 } |
513 } |
512 } |
607 Component getNativeFocusOwner() { |
606 Component getNativeFocusOwner() { |
608 return peer.getCurrentFocusOwner(); |
607 return peer.getCurrentFocusOwner(); |
609 } |
608 } |
610 |
609 |
611 void setNativeFocusOwner(Component comp) { |
610 void setNativeFocusOwner(Component comp) { |
612 if (focusLog.isLoggable(Level.FINEST)) { |
611 if (focusLog.isLoggable(PlatformLogger.FINEST)) { |
613 focusLog.log(Level.FINEST, "Calling peer {0} setCurrentFocusOwner for {1}", |
612 focusLog.finest("Calling peer {0} setCurrentFocusOwner for {1}", |
614 new Object[] {String.valueOf(peer), String.valueOf(comp)}); |
613 String.valueOf(peer), String.valueOf(comp)); |
615 } |
614 } |
616 peer.setCurrentFocusOwner(comp); |
615 peer.setCurrentFocusOwner(comp); |
617 } |
616 } |
618 |
617 |
619 Window getNativeFocusedWindow() { |
618 Window getNativeFocusedWindow() { |
671 { |
670 { |
672 synchronized (KeyboardFocusManager.class) { |
671 synchronized (KeyboardFocusManager.class) { |
673 if (this == getCurrentKeyboardFocusManager()) { |
672 if (this == getCurrentKeyboardFocusManager()) { |
674 return permanentFocusOwner; |
673 return permanentFocusOwner; |
675 } else { |
674 } else { |
676 if (focusLog.isLoggable(Level.FINER)) { |
675 if (focusLog.isLoggable(PlatformLogger.FINER)) { |
677 focusLog.log(Level.FINER, "This manager is " + this + ", current is " + getCurrentKeyboardFocusManager()); |
676 focusLog.finer("This manager is " + this + ", current is " + getCurrentKeyboardFocusManager()); |
678 } |
677 } |
679 throw new SecurityException(notPrivileged); |
678 throw new SecurityException(notPrivileged); |
680 } |
679 } |
681 } |
680 } |
682 } |
681 } |
779 protected Window getGlobalFocusedWindow() throws SecurityException { |
778 protected Window getGlobalFocusedWindow() throws SecurityException { |
780 synchronized (KeyboardFocusManager.class) { |
779 synchronized (KeyboardFocusManager.class) { |
781 if (this == getCurrentKeyboardFocusManager()) { |
780 if (this == getCurrentKeyboardFocusManager()) { |
782 return focusedWindow; |
781 return focusedWindow; |
783 } else { |
782 } else { |
784 if (focusLog.isLoggable(Level.FINER)) { |
783 if (focusLog.isLoggable(PlatformLogger.FINER)) { |
785 focusLog.log(Level.FINER, "This manager is " + this + ", current is " + getCurrentKeyboardFocusManager()); |
784 focusLog.finer("This manager is " + this + ", current is " + getCurrentKeyboardFocusManager()); |
786 } |
785 } |
787 throw new SecurityException(notPrivileged); |
786 throw new SecurityException(notPrivileged); |
788 } |
787 } |
789 } |
788 } |
790 } |
789 } |
883 protected Window getGlobalActiveWindow() throws SecurityException { |
882 protected Window getGlobalActiveWindow() throws SecurityException { |
884 synchronized (KeyboardFocusManager.class) { |
883 synchronized (KeyboardFocusManager.class) { |
885 if (this == getCurrentKeyboardFocusManager()) { |
884 if (this == getCurrentKeyboardFocusManager()) { |
886 return activeWindow; |
885 return activeWindow; |
887 } else { |
886 } else { |
888 if (focusLog.isLoggable(Level.FINER)) { |
887 if (focusLog.isLoggable(PlatformLogger.FINER)) { |
889 focusLog.log(Level.FINER, "This manager is " + this + ", current is " + getCurrentKeyboardFocusManager()); |
888 focusLog.finer("This manager is " + this + ", current is " + getCurrentKeyboardFocusManager()); |
890 } |
889 } |
891 throw new SecurityException(notPrivileged); |
890 throw new SecurityException(notPrivileged); |
892 } |
891 } |
893 } |
892 } |
894 } |
893 } |
917 */ |
916 */ |
918 protected void setGlobalActiveWindow(Window activeWindow) { |
917 protected void setGlobalActiveWindow(Window activeWindow) { |
919 Window oldActiveWindow; |
918 Window oldActiveWindow; |
920 synchronized (KeyboardFocusManager.class) { |
919 synchronized (KeyboardFocusManager.class) { |
921 oldActiveWindow = getActiveWindow(); |
920 oldActiveWindow = getActiveWindow(); |
922 if (focusLog.isLoggable(Level.FINER)) { |
921 if (focusLog.isLoggable(PlatformLogger.FINER)) { |
923 focusLog.log(Level.FINER, "Setting global active window to " + activeWindow + ", old active " + oldActiveWindow); |
922 focusLog.finer("Setting global active window to " + activeWindow + ", old active " + oldActiveWindow); |
924 } |
923 } |
925 |
924 |
926 try { |
925 try { |
927 fireVetoableChange("activeWindow", oldActiveWindow, |
926 fireVetoableChange("activeWindow", oldActiveWindow, |
928 activeWindow); |
927 activeWindow); |
2147 lightweightRequests = null; |
2146 lightweightRequests = null; |
2148 } |
2147 } |
2149 |
2148 |
2150 HeavyweightFocusRequest(Component heavyweight, Component descendant, |
2149 HeavyweightFocusRequest(Component heavyweight, Component descendant, |
2151 boolean temporary, CausedFocusEvent.Cause cause) { |
2150 boolean temporary, CausedFocusEvent.Cause cause) { |
2152 if (log.isLoggable(Level.FINE)) { |
2151 if (log.isLoggable(PlatformLogger.FINE)) { |
2153 if (heavyweight == null) { |
2152 if (heavyweight == null) { |
2154 log.log(Level.FINE, "Assertion (heavyweight != null) failed"); |
2153 log.fine("Assertion (heavyweight != null) failed"); |
2155 } |
2154 } |
2156 } |
2155 } |
2157 |
2156 |
2158 this.heavyweight = heavyweight; |
2157 this.heavyweight = heavyweight; |
2159 this.lightweightRequests = new LinkedList<LightweightFocusRequest>(); |
2158 this.lightweightRequests = new LinkedList<LightweightFocusRequest>(); |
2160 addLightweightRequest(descendant, temporary, cause); |
2159 addLightweightRequest(descendant, temporary, cause); |
2161 } |
2160 } |
2162 boolean addLightweightRequest(Component descendant, |
2161 boolean addLightweightRequest(Component descendant, |
2163 boolean temporary, CausedFocusEvent.Cause cause) { |
2162 boolean temporary, CausedFocusEvent.Cause cause) { |
2164 if (log.isLoggable(Level.FINE)) { |
2163 if (log.isLoggable(PlatformLogger.FINE)) { |
2165 if (this == HeavyweightFocusRequest.CLEAR_GLOBAL_FOCUS_OWNER) { |
2164 if (this == HeavyweightFocusRequest.CLEAR_GLOBAL_FOCUS_OWNER) { |
2166 log.log(Level.FINE, "Assertion (this != HeavyweightFocusRequest.CLEAR_GLOBAL_FOCUS_OWNER) failed"); |
2165 log.fine("Assertion (this != HeavyweightFocusRequest.CLEAR_GLOBAL_FOCUS_OWNER) failed"); |
2167 } |
2166 } |
2168 if (descendant == null) { |
2167 if (descendant == null) { |
2169 log.log(Level.FINE, "Assertion (descendant != null) failed"); |
2168 log.fine("Assertion (descendant != null) failed"); |
2170 } |
2169 } |
2171 } |
2170 } |
2172 |
2171 |
2173 Component lastDescendant = ((lightweightRequests.size() > 0) |
2172 Component lastDescendant = ((lightweightRequests.size() > 0) |
2174 ? lightweightRequests.getLast().component |
2173 ? lightweightRequests.getLast().component |
2337 */ |
2336 */ |
2338 static int shouldNativelyFocusHeavyweight |
2337 static int shouldNativelyFocusHeavyweight |
2339 (Component heavyweight, Component descendant, boolean temporary, |
2338 (Component heavyweight, Component descendant, boolean temporary, |
2340 boolean focusedWindowChangeAllowed, long time, CausedFocusEvent.Cause cause) |
2339 boolean focusedWindowChangeAllowed, long time, CausedFocusEvent.Cause cause) |
2341 { |
2340 { |
2342 if (log.isLoggable(Level.FINE)) { |
2341 if (log.isLoggable(PlatformLogger.FINE)) { |
2343 if (heavyweight == null) { |
2342 if (heavyweight == null) { |
2344 log.log(Level.FINE, "Assertion (heavyweight != null) failed"); |
2343 log.fine("Assertion (heavyweight != null) failed"); |
2345 } |
2344 } |
2346 if (time == 0) { |
2345 if (time == 0) { |
2347 log.log(Level.FINE, "Assertion (time != 0) failed"); |
2346 log.fine("Assertion (time != 0) failed"); |
2348 } |
2347 } |
2349 } |
2348 } |
2350 |
2349 |
2351 if (descendant == null) { |
2350 if (descendant == null) { |
2352 // Focus transfers from a lightweight child back to the |
2351 // Focus transfers from a lightweight child back to the |
2359 getCurrentKeyboardFocusManager(SunToolkit.targetToAppContext(descendant)); |
2358 getCurrentKeyboardFocusManager(SunToolkit.targetToAppContext(descendant)); |
2360 KeyboardFocusManager thisManager = getCurrentKeyboardFocusManager(); |
2359 KeyboardFocusManager thisManager = getCurrentKeyboardFocusManager(); |
2361 Component currentFocusOwner = thisManager.getGlobalFocusOwner(); |
2360 Component currentFocusOwner = thisManager.getGlobalFocusOwner(); |
2362 Component nativeFocusOwner = thisManager.getNativeFocusOwner(); |
2361 Component nativeFocusOwner = thisManager.getNativeFocusOwner(); |
2363 Window nativeFocusedWindow = thisManager.getNativeFocusedWindow(); |
2362 Window nativeFocusedWindow = thisManager.getNativeFocusedWindow(); |
2364 if (focusLog.isLoggable(Level.FINER)) { |
2363 if (focusLog.isLoggable(PlatformLogger.FINER)) { |
2365 focusLog.log(Level.FINER, "SNFH for {0} in {1}", |
2364 focusLog.finer("SNFH for {0} in {1}", |
2366 new Object[] {String.valueOf(descendant), String.valueOf(heavyweight)}); |
2365 String.valueOf(descendant), String.valueOf(heavyweight)); |
2367 } |
2366 } |
2368 if (focusLog.isLoggable(Level.FINEST)) { |
2367 if (focusLog.isLoggable(PlatformLogger.FINEST)) { |
2369 focusLog.log(Level.FINEST, "0. Current focus owner {0}", |
2368 focusLog.finest("0. Current focus owner {0}", |
2370 String.valueOf(currentFocusOwner)); |
2369 String.valueOf(currentFocusOwner)); |
2371 focusLog.log(Level.FINEST, "0. Native focus owner {0}", |
2370 focusLog.finest("0. Native focus owner {0}", |
2372 String.valueOf(nativeFocusOwner)); |
2371 String.valueOf(nativeFocusOwner)); |
2373 focusLog.log(Level.FINEST, "0. Native focused window {0}", |
2372 focusLog.finest("0. Native focused window {0}", |
2374 String.valueOf(nativeFocusedWindow)); |
2373 String.valueOf(nativeFocusedWindow)); |
2375 } |
2374 } |
2376 synchronized (heavyweightRequests) { |
2375 synchronized (heavyweightRequests) { |
2377 HeavyweightFocusRequest hwFocusRequest = getLastHWRequest(); |
2376 HeavyweightFocusRequest hwFocusRequest = getLastHWRequest(); |
2378 if (focusLog.isLoggable(Level.FINEST)) { |
2377 if (focusLog.isLoggable(PlatformLogger.FINEST)) { |
2379 focusLog.log(Level.FINEST, "Request {0}", String.valueOf(hwFocusRequest)); |
2378 focusLog.finest("Request {0}", String.valueOf(hwFocusRequest)); |
2380 } |
2379 } |
2381 if (hwFocusRequest == null && |
2380 if (hwFocusRequest == null && |
2382 heavyweight == nativeFocusOwner) |
2381 heavyweight == nativeFocusOwner) |
2383 { |
2382 { |
2384 if (descendant == currentFocusOwner) { |
2383 if (descendant == currentFocusOwner) { |
2385 // Redundant request. |
2384 // Redundant request. |
2386 if (focusLog.isLoggable(Level.FINEST)) |
2385 if (focusLog.isLoggable(PlatformLogger.FINEST)) |
2387 focusLog.log(Level.FINEST, "1. SNFH_FAILURE for {0}", |
2386 focusLog.finest("1. SNFH_FAILURE for {0}", |
2388 String.valueOf(descendant)); |
2387 String.valueOf(descendant)); |
2389 return SNFH_FAILURE; |
2388 return SNFH_FAILURE; |
2390 } |
2389 } |
2391 |
2390 |
2392 // 'heavyweight' owns the native focus and there are no pending |
2391 // 'heavyweight' owns the native focus and there are no pending |
2393 // requests. 'heavyweight' must be a Container and |
2392 // requests. 'heavyweight' must be a Container and |
2415 temporary, currentFocusOwner, cause); |
2414 temporary, currentFocusOwner, cause); |
2416 // Fix 5028014. Rolled out. |
2415 // Fix 5028014. Rolled out. |
2417 // SunToolkit.postPriorityEvent(newFocusOwnerEvent); |
2416 // SunToolkit.postPriorityEvent(newFocusOwnerEvent); |
2418 SunToolkit.postEvent(descendant.appContext, newFocusOwnerEvent); |
2417 SunToolkit.postEvent(descendant.appContext, newFocusOwnerEvent); |
2419 |
2418 |
2420 if (focusLog.isLoggable(Level.FINEST)) |
2419 if (focusLog.isLoggable(PlatformLogger.FINEST)) |
2421 focusLog.log(Level.FINEST, "2. SNFH_HANDLED for {0}", String.valueOf(descendant)); |
2420 focusLog.finest("2. SNFH_HANDLED for {0}", String.valueOf(descendant)); |
2422 return SNFH_SUCCESS_HANDLED; |
2421 return SNFH_SUCCESS_HANDLED; |
2423 } else if (hwFocusRequest != null && |
2422 } else if (hwFocusRequest != null && |
2424 hwFocusRequest.heavyweight == heavyweight) { |
2423 hwFocusRequest.heavyweight == heavyweight) { |
2425 // 'heavyweight' doesn't have the native focus right now, but |
2424 // 'heavyweight' doesn't have the native focus right now, but |
2426 // if all pending requests were completed, it would. Add |
2425 // if all pending requests were completed, it would. Add |
2429 if (hwFocusRequest.addLightweightRequest(descendant, |
2428 if (hwFocusRequest.addLightweightRequest(descendant, |
2430 temporary, cause)) { |
2429 temporary, cause)) { |
2431 manager.enqueueKeyEvents(time, descendant); |
2430 manager.enqueueKeyEvents(time, descendant); |
2432 } |
2431 } |
2433 |
2432 |
2434 if (focusLog.isLoggable(Level.FINEST)) |
2433 if (focusLog.isLoggable(PlatformLogger.FINEST)) |
2435 focusLog.finest("3. SNFH_HANDLED for lightweight" + |
2434 focusLog.finest("3. SNFH_HANDLED for lightweight" + |
2436 descendant + " in " + heavyweight); |
2435 descendant + " in " + heavyweight); |
2437 return SNFH_SUCCESS_HANDLED; |
2436 return SNFH_SUCCESS_HANDLED; |
2438 } else { |
2437 } else { |
2439 if (!focusedWindowChangeAllowed) { |
2438 if (!focusedWindowChangeAllowed) { |
2452 } |
2451 } |
2453 if (focusedWindowChanged(heavyweight, |
2452 if (focusedWindowChanged(heavyweight, |
2454 (hwFocusRequest != null) |
2453 (hwFocusRequest != null) |
2455 ? hwFocusRequest.heavyweight |
2454 ? hwFocusRequest.heavyweight |
2456 : nativeFocusedWindow)) { |
2455 : nativeFocusedWindow)) { |
2457 if (focusLog.isLoggable(Level.FINEST)) |
2456 if (focusLog.isLoggable(PlatformLogger.FINEST)) |
2458 focusLog.finest("4. SNFH_FAILURE for " + descendant); |
2457 focusLog.finest("4. SNFH_FAILURE for " + descendant); |
2459 return SNFH_FAILURE; |
2458 return SNFH_FAILURE; |
2460 } |
2459 } |
2461 } |
2460 } |
2462 |
2461 |
2463 manager.enqueueKeyEvents(time, descendant); |
2462 manager.enqueueKeyEvents(time, descendant); |
2464 heavyweightRequests.add |
2463 heavyweightRequests.add |
2465 (new HeavyweightFocusRequest(heavyweight, descendant, |
2464 (new HeavyweightFocusRequest(heavyweight, descendant, |
2466 temporary, cause)); |
2465 temporary, cause)); |
2467 if (focusLog.isLoggable(Level.FINEST)) |
2466 if (focusLog.isLoggable(PlatformLogger.FINEST)) |
2468 focusLog.finest("5. SNFH_PROCEED for " + descendant); |
2467 focusLog.finest("5. SNFH_PROCEED for " + descendant); |
2469 return SNFH_SUCCESS_PROCEED; |
2468 return SNFH_SUCCESS_PROCEED; |
2470 } |
2469 } |
2471 } |
2470 } |
2472 } |
2471 } |
2853 if (clearingCurrentLightweightRequests) { |
2852 if (clearingCurrentLightweightRequests) { |
2854 return event; |
2853 return event; |
2855 } |
2854 } |
2856 |
2855 |
2857 KeyboardFocusManager manager = getCurrentKeyboardFocusManager(); |
2856 KeyboardFocusManager manager = getCurrentKeyboardFocusManager(); |
2858 if (focusLog.isLoggable(Level.FINER)) { |
2857 if (focusLog.isLoggable(PlatformLogger.FINER)) { |
2859 if (event instanceof FocusEvent || event instanceof WindowEvent) { |
2858 if (event instanceof FocusEvent || event instanceof WindowEvent) { |
2860 focusLog.log(Level.FINER, ">>> {0}", new Object[] {String.valueOf(event)}); |
2859 focusLog.finer(">>> {0}", String.valueOf(event)); |
2861 } |
2860 } |
2862 if (focusLog.isLoggable(Level.FINER) && event instanceof KeyEvent) { |
2861 if (focusLog.isLoggable(PlatformLogger.FINER) && event instanceof KeyEvent) { |
2863 focusLog.log(Level.FINER, " focus owner is {0}", |
2862 focusLog.finer(" focus owner is {0}", |
2864 new Object[] {String.valueOf(manager.getGlobalFocusOwner())}); |
2863 String.valueOf(manager.getGlobalFocusOwner())); |
2865 focusLog.log(Level.FINER, ">>> {0}", new Object[] {String.valueOf(event)}); |
2864 focusLog.finer(">>> {0}", String.valueOf(event)); |
2866 } |
2865 } |
2867 } |
2866 } |
2868 |
2867 |
2869 synchronized(heavyweightRequests) { |
2868 synchronized(heavyweightRequests) { |
2870 /* |
2869 /* |
2944 } |
2943 } |
2945 return (heavyweightRequests.size() > 0); |
2944 return (heavyweightRequests.size() > 0); |
2946 } |
2945 } |
2947 } |
2946 } |
2948 static void removeLastFocusRequest(Component heavyweight) { |
2947 static void removeLastFocusRequest(Component heavyweight) { |
2949 if (log.isLoggable(Level.FINE)) { |
2948 if (log.isLoggable(PlatformLogger.FINE)) { |
2950 if (heavyweight == null) { |
2949 if (heavyweight == null) { |
2951 log.log(Level.FINE, "Assertion (heavyweight != null) failed"); |
2950 log.fine("Assertion (heavyweight != null) failed"); |
2952 } |
2951 } |
2953 } |
2952 } |
2954 |
2953 |
2955 KeyboardFocusManager manager = |
2954 KeyboardFocusManager manager = |
2956 KeyboardFocusManager.getCurrentKeyboardFocusManager(); |
2955 KeyboardFocusManager.getCurrentKeyboardFocusManager(); |