8080395: consider making sun.awt.CausedFocusEvent functionality public

Reviewed-by: alexsch, prr
This commit is contained in:
Semyon Sadetsky 2016-04-15 09:46:31 +03:00
parent eca0d89af6
commit 40685b4271
34 changed files with 617 additions and 199 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -903,7 +903,7 @@ public abstract class LWComponentPeer<T extends Component, D extends JComponent>
@Override @Override
public boolean requestFocus(Component lightweightChild, boolean temporary, public boolean requestFocus(Component lightweightChild, boolean temporary,
boolean focusedWindowChangeAllowed, long time, boolean focusedWindowChangeAllowed, long time,
CausedFocusEvent.Cause cause) FocusEvent.Cause cause)
{ {
if (focusLog.isLoggable(PlatformLogger.Level.FINEST)) { if (focusLog.isLoggable(PlatformLogger.Level.FINEST)) {
focusLog.finest("lightweightChild=" + lightweightChild + ", temporary=" + temporary + focusLog.finest("lightweightChild=" + lightweightChild + ", temporary=" + temporary +
@ -1278,7 +1278,7 @@ public abstract class LWComponentPeer<T extends Component, D extends JComponent>
assert (e.getSource() == target); assert (e.getSource() == target);
if (!target.isFocusOwner() && LWKeyboardFocusManagerPeer.shouldFocusOnClick(target)) { if (!target.isFocusOwner() && LWKeyboardFocusManagerPeer.shouldFocusOnClick(target)) {
LWKeyboardFocusManagerPeer.requestFocusFor(target, CausedFocusEvent.Cause.MOUSE_EVENT); LWKeyboardFocusManagerPeer.requestFocusFor(target, FocusEvent.Cause.MOUSE_EVENT);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -31,8 +31,8 @@ import java.awt.Point;
import java.awt.Rectangle; import java.awt.Rectangle;
import java.awt.Window; import java.awt.Window;
import java.awt.dnd.DropTarget; import java.awt.dnd.DropTarget;
import java.awt.event.FocusEvent;
import sun.awt.CausedFocusEvent;
import sun.awt.LightweightFrame; import sun.awt.LightweightFrame;
import sun.swing.JLightweightFrame; import sun.swing.JLightweightFrame;
import sun.swing.SwingAccessor; import sun.swing.SwingAccessor;
@ -60,7 +60,7 @@ public class LWLightweightFramePeer extends LWWindowPeer {
} }
@Override @Override
public boolean requestWindowFocus(CausedFocusEvent.Cause cause) { public boolean requestWindowFocus(FocusEvent.Cause cause) {
if (!focusAllowedFor()) { if (!focusAllowedFor()) {
return false; return false;
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -256,14 +256,14 @@ public class LWWindowPeer
if (!getTarget().isAutoRequestFocus()) { if (!getTarget().isAutoRequestFocus()) {
return; return;
} else { } else {
requestWindowFocus(CausedFocusEvent.Cause.ACTIVATION); requestWindowFocus(FocusEvent.Cause.ACTIVATION);
} }
// Focus the owner in case this window is focused. // Focus the owner in case this window is focused.
} else if (kfmPeer.getCurrentFocusedWindow() == getTarget()) { } else if (kfmPeer.getCurrentFocusedWindow() == getTarget()) {
// Transfer focus to the owner. // Transfer focus to the owner.
LWWindowPeer owner = getOwnerFrameDialog(LWWindowPeer.this); LWWindowPeer owner = getOwnerFrameDialog(LWWindowPeer.this);
if (owner != null) { if (owner != null) {
owner.requestWindowFocus(CausedFocusEvent.Cause.ACTIVATION); owner.requestWindowFocus(FocusEvent.Cause.ACTIVATION);
} }
} }
} }
@ -848,7 +848,7 @@ public class LWWindowPeer
// 2. An active but not focused owner frame/dialog is clicked. // 2. An active but not focused owner frame/dialog is clicked.
// The mouse event then will trigger a focus request "in window" to the component, so the window // The mouse event then will trigger a focus request "in window" to the component, so the window
// should gain focus before. // should gain focus before.
requestWindowFocus(CausedFocusEvent.Cause.MOUSE_EVENT); requestWindowFocus(FocusEvent.Cause.MOUSE_EVENT);
mouseDownTarget[targetIdx] = targetPeer; mouseDownTarget[targetIdx] = targetPeer;
} else if (id == MouseEvent.MOUSE_DRAGGED) { } else if (id == MouseEvent.MOUSE_DRAGGED) {
@ -1199,7 +1199,7 @@ public class LWWindowPeer
* Requests platform to set native focus on a frame/dialog. * Requests platform to set native focus on a frame/dialog.
* In case of a simple window, triggers appropriate java focus change. * In case of a simple window, triggers appropriate java focus change.
*/ */
public boolean requestWindowFocus(CausedFocusEvent.Cause cause) { public boolean requestWindowFocus(FocusEvent.Cause cause) {
if (focusLog.isLoggable(PlatformLogger.Level.FINE)) { if (focusLog.isLoggable(PlatformLogger.Level.FINE)) {
focusLog.fine("requesting native focus to " + this); focusLog.fine("requesting native focus to " + this);
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -26,8 +26,8 @@
package sun.lwawt; package sun.lwawt;
import java.awt.*; import java.awt.*;
import java.awt.event.FocusEvent;
import sun.awt.CausedFocusEvent;
import sun.java2d.SurfaceData; import sun.java2d.SurfaceData;
// TODO Is it worth to generify this interface, like that: // TODO Is it worth to generify this interface, like that:
@ -114,7 +114,7 @@ public interface PlatformWindow {
public void updateFocusableWindowState(); public void updateFocusableWindowState();
public boolean rejectFocusRequest(CausedFocusEvent.Cause cause); public boolean rejectFocusRequest(FocusEvent.Cause cause);
public boolean requestWindowFocus(); public boolean requestWindowFocus();

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -26,6 +26,7 @@
package sun.lwawt.macosx; package sun.lwawt.macosx;
import java.awt.*; import java.awt.*;
import java.awt.event.FocusEvent.Cause;
import java.awt.peer.*; import java.awt.peer.*;
import java.awt.BufferCapabilities.FlipContents; import java.awt.BufferCapabilities.FlipContents;
import java.awt.event.*; import java.awt.event.*;
@ -34,7 +35,6 @@ import java.security.AccessController;
import java.util.List; import java.util.List;
import java.io.*; import java.io.*;
import sun.awt.CausedFocusEvent.Cause;
import sun.awt.AWTAccessor; import sun.awt.AWTAccessor;
import sun.java2d.pipe.Region; import sun.java2d.pipe.Region;
import sun.security.action.GetBooleanAction; import sun.security.action.GetBooleanAction;

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -26,7 +26,8 @@
package sun.lwawt.macosx; package sun.lwawt.macosx;
import java.awt.*; import java.awt.*;
import sun.awt.CausedFocusEvent; import java.awt.event.FocusEvent;
import sun.java2d.SurfaceData; import sun.java2d.SurfaceData;
import sun.java2d.opengl.CGLLayer; import sun.java2d.opengl.CGLLayer;
import sun.lwawt.LWWindowPeer; import sun.lwawt.LWWindowPeer;
@ -133,9 +134,9 @@ public class CPlatformEmbeddedFrame implements PlatformWindow {
public void updateFocusableWindowState() {} public void updateFocusableWindowState() {}
@Override @Override
public boolean rejectFocusRequest(CausedFocusEvent.Cause cause) { public boolean rejectFocusRequest(FocusEvent.Cause cause) {
// Cross-app activation requests are not allowed. // Cross-app activation requests are not allowed.
if (cause != CausedFocusEvent.Cause.MOUSE_EVENT && if (cause != FocusEvent.Cause.MOUSE_EVENT &&
!target.isParentWindowActive()) !target.isParentWindowActive())
{ {
focusLogger.fine("the embedder is inactive, so the request is rejected"); focusLogger.fine("the embedder is inactive, so the request is rejected");

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -37,7 +37,7 @@ import java.awt.Rectangle;
import java.awt.Window; import java.awt.Window;
import sun.awt.CGraphicsDevice; import sun.awt.CGraphicsDevice;
import sun.awt.CGraphicsEnvironment; import sun.awt.CGraphicsEnvironment;
import sun.awt.CausedFocusEvent; import java.awt.event.FocusEvent;
import sun.awt.LightweightFrame; import sun.awt.LightweightFrame;
import sun.java2d.SurfaceData; import sun.java2d.SurfaceData;
import sun.lwawt.LWLightweightFramePeer; import sun.lwawt.LWLightweightFramePeer;
@ -134,7 +134,7 @@ public class CPlatformLWWindow extends CPlatformWindow {
} }
@Override @Override
public boolean rejectFocusRequest(CausedFocusEvent.Cause cause) { public boolean rejectFocusRequest(FocusEvent.Cause cause) {
return false; return false;
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -706,9 +706,9 @@ public class CPlatformWindow extends CFRetainedResource implements PlatformWindo
} }
@Override @Override
public boolean rejectFocusRequest(CausedFocusEvent.Cause cause) { public boolean rejectFocusRequest(FocusEvent.Cause cause) {
// Cross-app activation requests are not allowed. // Cross-app activation requests are not allowed.
if (cause != CausedFocusEvent.Cause.MOUSE_EVENT && if (cause != FocusEvent.Cause.MOUSE_EVENT &&
!((LWCToolkit)Toolkit.getDefaultToolkit()).isApplicationActive()) !((LWCToolkit)Toolkit.getDefaultToolkit()).isApplicationActive())
{ {
focusLogger.fine("the app is inactive, so the request is rejected"); focusLogger.fine("the app is inactive, so the request is rejected");

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -33,7 +33,7 @@ import java.awt.Insets;
import java.awt.MenuBar; import java.awt.MenuBar;
import java.awt.Point; import java.awt.Point;
import java.awt.Window; import java.awt.Window;
import sun.awt.CausedFocusEvent.Cause; import java.awt.event.FocusEvent.Cause;
import sun.java2d.SurfaceData; import sun.java2d.SurfaceData;
import sun.lwawt.LWWindowPeer; import sun.lwawt.LWWindowPeer;
import sun.lwawt.PlatformWindow; import sun.lwawt.PlatformWindow;

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 1995, 2015, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1995, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -68,7 +68,6 @@ import sun.awt.AWTAccessor;
import sun.awt.ConstrainableGraphics; import sun.awt.ConstrainableGraphics;
import sun.awt.SubRegionShowable; import sun.awt.SubRegionShowable;
import sun.awt.SunToolkit; import sun.awt.SunToolkit;
import sun.awt.CausedFocusEvent;
import sun.awt.EmbeddedFrame; import sun.awt.EmbeddedFrame;
import sun.awt.dnd.SunDropTargetEvent; import sun.awt.dnd.SunDropTargetEvent;
import sun.awt.im.CompositionArea; import sun.awt.im.CompositionArea;
@ -878,7 +877,7 @@ public abstract class Component implements ImageObserver, MenuContainer,
{ {
comp.setGraphicsConfiguration(gc); comp.setGraphicsConfiguration(gc);
} }
public boolean requestFocus(Component comp, CausedFocusEvent.Cause cause) { public boolean requestFocus(Component comp, FocusEvent.Cause cause) {
return comp.requestFocus(cause); return comp.requestFocus(cause);
} }
public boolean canBeFocusOwner(Component comp) { public boolean canBeFocusOwner(Component comp) {
@ -7538,7 +7537,7 @@ public abstract class Component implements ImageObserver, MenuContainer,
requestFocusHelper(false, true); requestFocusHelper(false, true);
} }
boolean requestFocus(CausedFocusEvent.Cause cause) { boolean requestFocus(FocusEvent.Cause cause) {
return requestFocusHelper(false, true, cause); return requestFocusHelper(false, true, cause);
} }
@ -7605,7 +7604,7 @@ public abstract class Component implements ImageObserver, MenuContainer,
return requestFocusHelper(temporary, true); return requestFocusHelper(temporary, true);
} }
boolean requestFocus(boolean temporary, CausedFocusEvent.Cause cause) { boolean requestFocus(boolean temporary, FocusEvent.Cause cause) {
return requestFocusHelper(temporary, true, cause); return requestFocusHelper(temporary, true, cause);
} }
/** /**
@ -7656,7 +7655,7 @@ public abstract class Component implements ImageObserver, MenuContainer,
return requestFocusHelper(false, false); return requestFocusHelper(false, false);
} }
boolean requestFocusInWindow(CausedFocusEvent.Cause cause) { boolean requestFocusInWindow(FocusEvent.Cause cause) {
return requestFocusHelper(false, false, cause); return requestFocusHelper(false, false, cause);
} }
@ -7721,18 +7720,18 @@ public abstract class Component implements ImageObserver, MenuContainer,
return requestFocusHelper(temporary, false); return requestFocusHelper(temporary, false);
} }
boolean requestFocusInWindow(boolean temporary, CausedFocusEvent.Cause cause) { boolean requestFocusInWindow(boolean temporary, FocusEvent.Cause cause) {
return requestFocusHelper(temporary, false, cause); return requestFocusHelper(temporary, false, cause);
} }
final boolean requestFocusHelper(boolean temporary, final boolean requestFocusHelper(boolean temporary,
boolean focusedWindowChangeAllowed) { boolean focusedWindowChangeAllowed) {
return requestFocusHelper(temporary, focusedWindowChangeAllowed, CausedFocusEvent.Cause.UNKNOWN); return requestFocusHelper(temporary, focusedWindowChangeAllowed, FocusEvent.Cause.UNKNOWN);
} }
final boolean requestFocusHelper(boolean temporary, final boolean requestFocusHelper(boolean temporary,
boolean focusedWindowChangeAllowed, boolean focusedWindowChangeAllowed,
CausedFocusEvent.Cause cause) FocusEvent.Cause cause)
{ {
// 1) Check if the event being dispatched is a system-generated mouse event. // 1) Check if the event being dispatched is a system-generated mouse event.
AWTEvent currentEvent = EventQueue.getCurrentEvent(); AWTEvent currentEvent = EventQueue.getCurrentEvent();
@ -7820,7 +7819,7 @@ public abstract class Component implements ImageObserver, MenuContainer,
private boolean isRequestFocusAccepted(boolean temporary, private boolean isRequestFocusAccepted(boolean temporary,
boolean focusedWindowChangeAllowed, boolean focusedWindowChangeAllowed,
CausedFocusEvent.Cause cause) FocusEvent.Cause cause)
{ {
if (!isFocusable() || !isVisible()) { if (!isFocusable() || !isVisible()) {
if (focusLog.isLoggable(PlatformLogger.Level.FINEST)) { if (focusLog.isLoggable(PlatformLogger.Level.FINEST)) {
@ -7867,7 +7866,7 @@ public abstract class Component implements ImageObserver, MenuContainer,
return true; return true;
} }
if (CausedFocusEvent.Cause.ACTIVATION == cause) { if (FocusEvent.Cause.ACTIVATION == cause) {
// we shouldn't call RequestFocusController in case we are // we shouldn't call RequestFocusController in case we are
// in activation. We do request focus on component which // in activation. We do request focus on component which
// has got temporary focus lost and then on component which is // has got temporary focus lost and then on component which is
@ -7899,7 +7898,7 @@ public abstract class Component implements ImageObserver, MenuContainer,
private static class DummyRequestFocusController implements RequestFocusController { private static class DummyRequestFocusController implements RequestFocusController {
public boolean acceptRequestFocus(Component from, Component to, public boolean acceptRequestFocus(Component from, Component to,
boolean temporary, boolean focusedWindowChangeAllowed, boolean temporary, boolean focusedWindowChangeAllowed,
CausedFocusEvent.Cause cause) FocusEvent.Cause cause)
{ {
return true; return true;
} }
@ -7983,7 +7982,7 @@ public abstract class Component implements ImageObserver, MenuContainer,
Component toFocus = getNextFocusCandidate(); Component toFocus = getNextFocusCandidate();
boolean res = false; boolean res = false;
if (toFocus != null && !toFocus.isFocusOwner() && toFocus != this) { if (toFocus != null && !toFocus.isFocusOwner() && toFocus != this) {
res = toFocus.requestFocusInWindow(CausedFocusEvent.Cause.TRAVERSAL_FORWARD); res = toFocus.requestFocusInWindow(FocusEvent.Cause.TRAVERSAL_FORWARD);
} }
if (clearOnFailure && !res) { if (clearOnFailure && !res) {
if (focusLog.isLoggable(PlatformLogger.Level.FINER)) { if (focusLog.isLoggable(PlatformLogger.Level.FINER)) {
@ -8063,7 +8062,7 @@ public abstract class Component implements ImageObserver, MenuContainer,
toFocus = policy.getDefaultComponent(rootAncestor); toFocus = policy.getDefaultComponent(rootAncestor);
} }
if (toFocus != null) { if (toFocus != null) {
res = toFocus.requestFocusInWindow(CausedFocusEvent.Cause.TRAVERSAL_BACKWARD); res = toFocus.requestFocusInWindow(FocusEvent.Cause.TRAVERSAL_BACKWARD);
} }
} }
if (clearOnFailure && !res) { if (clearOnFailure && !res) {
@ -8108,7 +8107,7 @@ public abstract class Component implements ImageObserver, MenuContainer,
KeyboardFocusManager.getCurrentKeyboardFocusManager(). KeyboardFocusManager.getCurrentKeyboardFocusManager().
setGlobalCurrentFocusCycleRootPriv(fcr); setGlobalCurrentFocusCycleRootPriv(fcr);
rootAncestor.requestFocus(CausedFocusEvent.Cause.TRAVERSAL_UP); rootAncestor.requestFocus(FocusEvent.Cause.TRAVERSAL_UP);
} else { } else {
Window window = getContainingWindow(); Window window = getContainingWindow();
@ -8118,7 +8117,7 @@ public abstract class Component implements ImageObserver, MenuContainer,
if (toFocus != null) { if (toFocus != null) {
KeyboardFocusManager.getCurrentKeyboardFocusManager(). KeyboardFocusManager.getCurrentKeyboardFocusManager().
setGlobalCurrentFocusCycleRootPriv(window); setGlobalCurrentFocusCycleRootPriv(window);
toFocus.requestFocus(CausedFocusEvent.Cause.TRAVERSAL_UP); toFocus.requestFocus(FocusEvent.Cause.TRAVERSAL_UP);
} }
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 1995, 2015, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1995, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -55,7 +55,6 @@ import sun.util.logging.PlatformLogger;
import sun.awt.AppContext; import sun.awt.AppContext;
import sun.awt.AWTAccessor; import sun.awt.AWTAccessor;
import sun.awt.CausedFocusEvent;
import sun.awt.PeerEvent; import sun.awt.PeerEvent;
import sun.awt.SunToolkit; import sun.awt.SunToolkit;
@ -3515,7 +3514,7 @@ public class Container extends Component {
Component toFocus = getFocusTraversalPolicy(). Component toFocus = getFocusTraversalPolicy().
getDefaultComponent(this); getDefaultComponent(this);
if (toFocus != null) { if (toFocus != null) {
toFocus.requestFocus(CausedFocusEvent.Cause.TRAVERSAL_DOWN); toFocus.requestFocus(FocusEvent.Cause.TRAVERSAL_DOWN);
} }
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -40,7 +40,6 @@ import sun.util.logging.PlatformLogger;
import sun.awt.AppContext; import sun.awt.AppContext;
import sun.awt.SunToolkit; import sun.awt.SunToolkit;
import sun.awt.AWTAccessor; import sun.awt.AWTAccessor;
import sun.awt.CausedFocusEvent;
import sun.awt.TimedWindowEvent; import sun.awt.TimedWindowEvent;
/** /**
@ -165,13 +164,13 @@ public class DefaultKeyboardFocusManager extends KeyboardFocusManager {
boolean clearOnFailure) boolean clearOnFailure)
{ {
if (toFocus != vetoedComponent && toFocus.isShowing() && toFocus.canBeFocusOwner() && if (toFocus != vetoedComponent && toFocus.isShowing() && toFocus.canBeFocusOwner() &&
toFocus.requestFocus(false, CausedFocusEvent.Cause.ROLLBACK)) toFocus.requestFocus(false, FocusEvent.Cause.ROLLBACK))
{ {
return true; return true;
} else { } else {
Component nextFocus = toFocus.getNextFocusCandidate(); Component nextFocus = toFocus.getNextFocusCandidate();
if (nextFocus != null && nextFocus != vetoedComponent && if (nextFocus != null && nextFocus != vetoedComponent &&
nextFocus.requestFocusInWindow(CausedFocusEvent.Cause.ROLLBACK)) nextFocus.requestFocusInWindow(FocusEvent.Cause.ROLLBACK))
{ {
return true; return true;
} else if (clearOnFailure) { } else if (clearOnFailure) {
@ -431,13 +430,13 @@ public class DefaultKeyboardFocusManager extends KeyboardFocusManager {
tempLost, toFocus); tempLost, toFocus);
} }
if (tempLost != null) { if (tempLost != null) {
tempLost.requestFocusInWindow(CausedFocusEvent.Cause.ACTIVATION); tempLost.requestFocusInWindow(FocusEvent.Cause.ACTIVATION);
} }
if (toFocus != null && toFocus != tempLost) { if (toFocus != null && toFocus != tempLost) {
// If there is a component which requested focus when this window // If there is a component which requested focus when this window
// was inactive it expects to receive focus after activation. // was inactive it expects to receive focus after activation.
toFocus.requestFocusInWindow(CausedFocusEvent.Cause.ACTIVATION); toFocus.requestFocusInWindow(FocusEvent.Cause.ACTIVATION);
} }
} }
@ -490,8 +489,6 @@ public class DefaultKeyboardFocusManager extends KeyboardFocusManager {
case FocusEvent.FOCUS_GAINED: { case FocusEvent.FOCUS_GAINED: {
FocusEvent fe = (FocusEvent)e; FocusEvent fe = (FocusEvent)e;
CausedFocusEvent.Cause cause = (fe instanceof CausedFocusEvent) ?
((CausedFocusEvent)fe).getCause() : CausedFocusEvent.Cause.UNKNOWN;
Component oldFocusOwner = getGlobalFocusOwner(); Component oldFocusOwner = getGlobalFocusOwner();
Component newFocusOwner = fe.getComponent(); Component newFocusOwner = fe.getComponent();
if (oldFocusOwner == newFocusOwner) { if (oldFocusOwner == newFocusOwner) {
@ -509,10 +506,10 @@ public class DefaultKeyboardFocusManager extends KeyboardFocusManager {
if (oldFocusOwner != null) { if (oldFocusOwner != null) {
boolean isEventDispatched = boolean isEventDispatched =
sendMessage(oldFocusOwner, sendMessage(oldFocusOwner,
new CausedFocusEvent(oldFocusOwner, new FocusEvent(oldFocusOwner,
FocusEvent.FOCUS_LOST, FocusEvent.FOCUS_LOST,
fe.isTemporary(), fe.isTemporary(),
newFocusOwner, cause)); newFocusOwner, fe.getCause()));
// Failed to dispatch, clear by ourselves // Failed to dispatch, clear by ourselves
if (!isEventDispatched) { if (!isEventDispatched) {
setGlobalFocusOwner(null); setGlobalFocusOwner(null);
@ -552,7 +549,7 @@ public class DefaultKeyboardFocusManager extends KeyboardFocusManager {
// Refuse focus on a disabled component if the focus event // Refuse focus on a disabled component if the focus event
// isn't of UNKNOWN reason (i.e. not a result of a direct request // isn't of UNKNOWN reason (i.e. not a result of a direct request
// but traversal, activation or system generated). // but traversal, activation or system generated).
(newFocusOwner.isEnabled() || cause.equals(CausedFocusEvent.Cause.UNKNOWN)))) (newFocusOwner.isEnabled() || fe.getCause().equals(FocusEvent.Cause.UNKNOWN))))
{ {
// we should not accept focus on such component, so reject it. // we should not accept focus on such component, so reject it.
dequeueKeyEvents(-1, newFocusOwner); dequeueKeyEvents(-1, newFocusOwner);
@ -601,10 +598,10 @@ public class DefaultKeyboardFocusManager extends KeyboardFocusManager {
Component realOppositeComponent = this.realOppositeComponentWR.get(); Component realOppositeComponent = this.realOppositeComponentWR.get();
if (realOppositeComponent != null && if (realOppositeComponent != null &&
realOppositeComponent != fe.getOppositeComponent()) { realOppositeComponent != fe.getOppositeComponent()) {
fe = new CausedFocusEvent(newFocusOwner, fe = new FocusEvent(newFocusOwner,
FocusEvent.FOCUS_GAINED, FocusEvent.FOCUS_GAINED,
fe.isTemporary(), fe.isTemporary(),
realOppositeComponent, cause); realOppositeComponent, fe.getCause());
((AWTEvent) fe).isPosted = true; ((AWTEvent) fe).isPosted = true;
} }
return typeAheadAssertions(newFocusOwner, fe); return typeAheadAssertions(newFocusOwner, fe);
@ -729,10 +726,10 @@ public class DefaultKeyboardFocusManager extends KeyboardFocusManager {
oppositeComp = oppositeWindow; oppositeComp = oppositeWindow;
} }
sendMessage(currentFocusOwner, sendMessage(currentFocusOwner,
new CausedFocusEvent(currentFocusOwner, new FocusEvent(currentFocusOwner,
FocusEvent.FOCUS_LOST, FocusEvent.FOCUS_LOST,
true, true,
oppositeComp, CausedFocusEvent.Cause.ACTIVATION)); oppositeComp, FocusEvent.Cause.ACTIVATION));
} }
setGlobalFocusedWindow(null); setGlobalFocusedWindow(null);

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -57,7 +57,6 @@ import sun.util.logging.PlatformLogger;
import sun.awt.AppContext; import sun.awt.AppContext;
import sun.awt.SunToolkit; import sun.awt.SunToolkit;
import sun.awt.CausedFocusEvent;
import sun.awt.KeyboardFocusManagerPeerProvider; import sun.awt.KeyboardFocusManagerPeerProvider;
import sun.awt.AWTAccessor; import sun.awt.AWTAccessor;
@ -124,7 +123,7 @@ public abstract class KeyboardFocusManager
boolean temporary, boolean temporary,
boolean focusedWindowChangeAllowed, boolean focusedWindowChangeAllowed,
long time, long time,
CausedFocusEvent.Cause cause) FocusEvent.Cause cause)
{ {
return KeyboardFocusManager.shouldNativelyFocusHeavyweight( return KeyboardFocusManager.shouldNativelyFocusHeavyweight(
heavyweight, descendant, temporary, focusedWindowChangeAllowed, time, cause); heavyweight, descendant, temporary, focusedWindowChangeAllowed, time, cause);
@ -2164,9 +2163,9 @@ public abstract class KeyboardFocusManager
private static final class LightweightFocusRequest { private static final class LightweightFocusRequest {
final Component component; final Component component;
final boolean temporary; final boolean temporary;
final CausedFocusEvent.Cause cause; final FocusEvent.Cause cause;
LightweightFocusRequest(Component component, boolean temporary, CausedFocusEvent.Cause cause) { LightweightFocusRequest(Component component, boolean temporary, FocusEvent.Cause cause) {
this.component = component; this.component = component;
this.temporary = temporary; this.temporary = temporary;
this.cause = cause; this.cause = cause;
@ -2190,7 +2189,7 @@ public abstract class KeyboardFocusManager
} }
HeavyweightFocusRequest(Component heavyweight, Component descendant, HeavyweightFocusRequest(Component heavyweight, Component descendant,
boolean temporary, CausedFocusEvent.Cause cause) { boolean temporary, FocusEvent.Cause cause) {
if (log.isLoggable(PlatformLogger.Level.FINE)) { if (log.isLoggable(PlatformLogger.Level.FINE)) {
if (heavyweight == null) { if (heavyweight == null) {
log.fine("Assertion (heavyweight != null) failed"); log.fine("Assertion (heavyweight != null) failed");
@ -2202,7 +2201,7 @@ public abstract class KeyboardFocusManager
addLightweightRequest(descendant, temporary, cause); addLightweightRequest(descendant, temporary, cause);
} }
boolean addLightweightRequest(Component descendant, boolean addLightweightRequest(Component descendant,
boolean temporary, CausedFocusEvent.Cause cause) { boolean temporary, FocusEvent.Cause cause) {
if (log.isLoggable(PlatformLogger.Level.FINE)) { if (log.isLoggable(PlatformLogger.Level.FINE)) {
if (this == HeavyweightFocusRequest.CLEAR_GLOBAL_FOCUS_OWNER) { if (this == HeavyweightFocusRequest.CLEAR_GLOBAL_FOCUS_OWNER) {
log.fine("Assertion (this != HeavyweightFocusRequest.CLEAR_GLOBAL_FOCUS_OWNER) failed"); log.fine("Assertion (this != HeavyweightFocusRequest.CLEAR_GLOBAL_FOCUS_OWNER) failed");
@ -2314,7 +2313,7 @@ public abstract class KeyboardFocusManager
hwFocusRequest = hwFocusRequest =
new HeavyweightFocusRequest(heavyweight, descendant, new HeavyweightFocusRequest(heavyweight, descendant,
temporary, CausedFocusEvent.Cause.UNKNOWN); temporary, FocusEvent.Cause.UNKNOWN);
heavyweightRequests.add(hwFocusRequest); heavyweightRequests.add(hwFocusRequest);
if (currentFocusOwner != null) { if (currentFocusOwner != null) {
@ -2379,7 +2378,7 @@ public abstract class KeyboardFocusManager
*/ */
static int shouldNativelyFocusHeavyweight static int shouldNativelyFocusHeavyweight
(Component heavyweight, Component descendant, boolean temporary, (Component heavyweight, Component descendant, boolean temporary,
boolean focusedWindowChangeAllowed, long time, CausedFocusEvent.Cause cause) boolean focusedWindowChangeAllowed, long time, FocusEvent.Cause cause)
{ {
if (log.isLoggable(PlatformLogger.Level.FINE)) { if (log.isLoggable(PlatformLogger.Level.FINE)) {
if (heavyweight == null) { if (heavyweight == null) {
@ -2445,7 +2444,7 @@ public abstract class KeyboardFocusManager
if (currentFocusOwner != null) { if (currentFocusOwner != null) {
FocusEvent currentFocusOwnerEvent = FocusEvent currentFocusOwnerEvent =
new CausedFocusEvent(currentFocusOwner, new FocusEvent(currentFocusOwner,
FocusEvent.FOCUS_LOST, FocusEvent.FOCUS_LOST,
temporary, descendant, cause); temporary, descendant, cause);
// Fix 5028014. Rolled out. // Fix 5028014. Rolled out.
@ -2454,7 +2453,7 @@ public abstract class KeyboardFocusManager
currentFocusOwnerEvent); currentFocusOwnerEvent);
} }
FocusEvent newFocusOwnerEvent = FocusEvent newFocusOwnerEvent =
new CausedFocusEvent(descendant, FocusEvent.FOCUS_GAINED, new FocusEvent(descendant, FocusEvent.FOCUS_GAINED,
temporary, currentFocusOwner, cause); temporary, currentFocusOwner, cause);
// Fix 5028014. Rolled out. // Fix 5028014. Rolled out.
// SunToolkit.postPriorityEvent(newFocusOwnerEvent); // SunToolkit.postPriorityEvent(newFocusOwnerEvent);
@ -2670,13 +2669,13 @@ public abstract class KeyboardFocusManager
* lw requests. * lw requests.
*/ */
if (currentFocusOwner != null) { if (currentFocusOwner != null) {
currentFocusOwnerEvent = new CausedFocusEvent(currentFocusOwner, currentFocusOwnerEvent = new FocusEvent(currentFocusOwner,
FocusEvent.FOCUS_LOST, FocusEvent.FOCUS_LOST,
lwFocusRequest.temporary, lwFocusRequest.temporary,
lwFocusRequest.component, lwFocusRequest.cause); lwFocusRequest.component, lwFocusRequest.cause);
} }
FocusEvent newFocusOwnerEvent = FocusEvent newFocusOwnerEvent =
new CausedFocusEvent(lwFocusRequest.component, new FocusEvent(lwFocusRequest.component,
FocusEvent.FOCUS_GAINED, FocusEvent.FOCUS_GAINED,
lwFocusRequest.temporary, lwFocusRequest.temporary,
currentFocusOwner == null ? lastFocusOwner : currentFocusOwner, currentFocusOwner == null ? lastFocusOwner : currentFocusOwner,
@ -2726,8 +2725,8 @@ public abstract class KeyboardFocusManager
{ {
temporary = true; temporary = true;
} }
return new CausedFocusEvent(source, fe.getID(), temporary, opposite, return new FocusEvent(source, fe.getID(), temporary, opposite,
CausedFocusEvent.Cause.NATIVE_SYSTEM); FocusEvent.Cause.UNEXPECTED);
} }
} }
@ -2802,7 +2801,7 @@ public abstract class KeyboardFocusManager
// 'opposite' will be fixed by // 'opposite' will be fixed by
// DefaultKeyboardFocusManager.realOppositeComponent // DefaultKeyboardFocusManager.realOppositeComponent
return new CausedFocusEvent(newSource, return new FocusEvent(newSource,
FocusEvent.FOCUS_GAINED, temporary, FocusEvent.FOCUS_GAINED, temporary,
opposite, lwFocusRequest.cause); opposite, lwFocusRequest.cause);
} }
@ -2815,8 +2814,8 @@ public abstract class KeyboardFocusManager
// If it arrives as the result of activation we should skip it // If it arrives as the result of activation we should skip it
// This event will not have appropriate request record and // This event will not have appropriate request record and
// on arrival there will be already some focus owner set. // on arrival there will be already some focus owner set.
return new CausedFocusEvent(currentFocusOwner, FocusEvent.FOCUS_GAINED, false, return new FocusEvent(currentFocusOwner, FocusEvent.FOCUS_GAINED, false,
null, CausedFocusEvent.Cause.ACTIVATION); null, FocusEvent.Cause.ACTIVATION);
} }
return retargetUnexpectedFocusEvent(fe); return retargetUnexpectedFocusEvent(fe);
@ -2839,9 +2838,9 @@ public abstract class KeyboardFocusManager
if (currentFocusOwner != null) { if (currentFocusOwner != null) {
// Call to KeyboardFocusManager.clearGlobalFocusOwner() // Call to KeyboardFocusManager.clearGlobalFocusOwner()
heavyweightRequests.removeFirst(); heavyweightRequests.removeFirst();
return new CausedFocusEvent(currentFocusOwner, return new FocusEvent(currentFocusOwner,
FocusEvent.FOCUS_LOST, false, null, FocusEvent.FOCUS_LOST, false, null,
CausedFocusEvent.Cause.CLEAR_GLOBAL_FOCUS_OWNER); FocusEvent.Cause.CLEAR_GLOBAL_FOCUS_OWNER);
} }
// Otherwise, fall through to failure case below // Otherwise, fall through to failure case below
@ -2850,9 +2849,9 @@ public abstract class KeyboardFocusManager
{ {
// Focus leaving application // Focus leaving application
if (currentFocusOwner != null) { if (currentFocusOwner != null) {
return new CausedFocusEvent(currentFocusOwner, return new FocusEvent(currentFocusOwner,
FocusEvent.FOCUS_LOST, FocusEvent.FOCUS_LOST,
true, null, CausedFocusEvent.Cause.ACTIVATION); true, null, FocusEvent.Cause.ACTIVATION);
} else { } else {
return fe; return fe;
} }
@ -2878,15 +2877,15 @@ public abstract class KeyboardFocusManager
? true ? true
: lwFocusRequest.temporary; : lwFocusRequest.temporary;
return new CausedFocusEvent(currentFocusOwner, FocusEvent.FOCUS_LOST, return new FocusEvent(currentFocusOwner, FocusEvent.FOCUS_LOST,
temporary, lwFocusRequest.component, lwFocusRequest.cause); temporary, lwFocusRequest.component, lwFocusRequest.cause);
} else if (focusedWindowChanged(opposite, currentFocusOwner)) { } else if (focusedWindowChanged(opposite, currentFocusOwner)) {
// If top-level changed there might be no focus request in a list // If top-level changed there might be no focus request in a list
// But we know the opposite, we now it is temporary - dispatch the event. // But we know the opposite, we now it is temporary - dispatch the event.
if (!fe.isTemporary() && currentFocusOwner != null) { if (!fe.isTemporary() && currentFocusOwner != null) {
// Create copy of the event with only difference in temporary parameter. // Create copy of the event with only difference in temporary parameter.
fe = new CausedFocusEvent(currentFocusOwner, FocusEvent.FOCUS_LOST, fe = new FocusEvent(currentFocusOwner, FocusEvent.FOCUS_LOST,
true, opposite, CausedFocusEvent.Cause.ACTIVATION); true, opposite, FocusEvent.Cause.ACTIVATION);
} }
return fe; return fe;
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 1995, 2015, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1995, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -52,7 +52,6 @@ import javax.accessibility.*;
import sun.awt.AWTAccessor; import sun.awt.AWTAccessor;
import sun.awt.AWTPermissions; import sun.awt.AWTPermissions;
import sun.awt.AppContext; import sun.awt.AppContext;
import sun.awt.CausedFocusEvent;
import sun.awt.DebugSettings; import sun.awt.DebugSettings;
import sun.awt.SunToolkit; import sun.awt.SunToolkit;
import sun.awt.util.IdentityArrayList; import sun.awt.util.IdentityArrayList;
@ -2599,7 +2598,7 @@ public class Window extends Container implements Accessible {
{ {
Component toFocus = Component toFocus =
KeyboardFocusManager.getMostRecentFocusOwner(owner); KeyboardFocusManager.getMostRecentFocusOwner(owner);
if (toFocus != null && toFocus.requestFocus(false, CausedFocusEvent.Cause.ACTIVATION)) { if (toFocus != null && toFocus.requestFocus(false, FocusEvent.Cause.ACTIVATION)) {
return; return;
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1996, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -26,6 +26,9 @@
package java.awt.event; package java.awt.event;
import java.awt.Component; import java.awt.Component;
import java.io.ObjectStreamException;
import sun.awt.AWTAccessor;
import sun.awt.AppContext; import sun.awt.AppContext;
import sun.awt.SunToolkit; import sun.awt.SunToolkit;
@ -51,6 +54,10 @@ import sun.awt.SunToolkit;
* the FOCUS_GAINED and FOCUS_LOST event ids; the level may be distinguished in * the FOCUS_GAINED and FOCUS_LOST event ids; the level may be distinguished in
* the event using the isTemporary() method. * the event using the isTemporary() method.
* <p> * <p>
* Every {@code FocusEvent} records its cause - the reason why this event was
* generated. The cause is assigned during the focus event creation and may be
* retrieved by calling {@link #getCause}.
* <p>
* An unspecified behavior will be caused if the {@code id} parameter * An unspecified behavior will be caused if the {@code id} parameter
* of any particular {@code FocusEvent} instance is not * of any particular {@code FocusEvent} instance is not
* in the range from {@code FOCUS_FIRST} to {@code FOCUS_LAST}. * in the range from {@code FOCUS_FIRST} to {@code FOCUS_LAST}.
@ -65,6 +72,61 @@ import sun.awt.SunToolkit;
*/ */
public class FocusEvent extends ComponentEvent { public class FocusEvent extends ComponentEvent {
/**
* This enum represents the cause of a {@code FocusEvent}- the reason why it
* occurred. Possible reasons include mouse events, keyboard focus
* traversal, window activation.
* If no cause is provided then the reason is {@code UNKNOWN}.
*
* @since 9
*/
public enum Cause {
/**
* The default value.
*/
UNKNOWN,
/**
* An activating mouse event.
*/
MOUSE_EVENT,
/**
* A focus traversal action with unspecified direction.
*/
TRAVERSAL,
/**
* An up-cycle focus traversal action.
*/
TRAVERSAL_UP,
/**
* A down-cycle focus traversal action.
*/
TRAVERSAL_DOWN,
/**
* A forward focus traversal action.
*/
TRAVERSAL_FORWARD,
/**
* A backward focus traversal action.
*/
TRAVERSAL_BACKWARD,
/**
* Restoring focus after a focus request has been rejected.
*/
ROLLBACK,
/**
* A system action causing an unexpected focus change.
*/
UNEXPECTED,
/**
* An activation of a toplevel window.
*/
ACTIVATION,
/**
* Clearing global focus owner.
*/
CLEAR_GLOBAL_FOCUS_OWNER
}
/** /**
* The first number in the range of ids used for focus events. * The first number in the range of ids used for focus events.
*/ */
@ -85,6 +147,16 @@ public class FocusEvent extends ComponentEvent {
*/ */
public static final int FOCUS_LOST = 1 + FOCUS_FIRST; //Event.LOST_FOCUS public static final int FOCUS_LOST = 1 + FOCUS_FIRST; //Event.LOST_FOCUS
/**
* A focus event has the reason why this event was generated.
* The cause is set during the focus event creation.
*
* @serial
* @see #getCause()
* @since 9
*/
private final Cause cause;
/** /**
* A focus event can have two different levels, permanent and temporary. * A focus event can have two different levels, permanent and temporary.
* It will be set to true if some operation takes away the focus * It will be set to true if some operation takes away the focus
@ -115,7 +187,8 @@ public class FocusEvent extends ComponentEvent {
/** /**
* Constructs a {@code FocusEvent} object with the * Constructs a {@code FocusEvent} object with the
* specified temporary state and opposite {@code Component}. * specified temporary state, opposite {@code Component} and the
* {@code Cause.UNKNOWN} cause.
* The opposite {@code Component} is the other * The opposite {@code Component} is the other
* {@code Component} involved in this focus change. * {@code Component} involved in this focus change.
* For a {@code FOCUS_GAINED} event, this is the * For a {@code FOCUS_GAINED} event, this is the
@ -142,13 +215,57 @@ public class FocusEvent extends ComponentEvent {
* @see #getID() * @see #getID()
* @see #isTemporary() * @see #isTemporary()
* @see #getOppositeComponent() * @see #getOppositeComponent()
* @see Cause#UNKNOWN
* @since 1.4 * @since 1.4
*/ */
public FocusEvent(Component source, int id, boolean temporary, public FocusEvent(Component source, int id, boolean temporary,
Component opposite) { Component opposite) {
this(source, id, temporary, opposite, Cause.UNKNOWN);
}
/**
* Constructs a {@code FocusEvent} object with the
* specified temporary state, opposite {@code Component} and the cause.
* The opposite {@code Component} is the other
* {@code Component} involved in this focus change.
* For a {@code FOCUS_GAINED} event, this is the
* {@code Component} that lost focus. For a
* {@code FOCUS_LOST} event, this is the {@code Component}
* that gained focus. If this focus change occurs with a native
* application, with a Java application in a different VM,
* or with no other {@code Component}, then the opposite
* {@code Component} is {@code null}.
* <p> This method throws an
* {@code IllegalArgumentException} if {@code source} or {@code cause}
* is {@code null}.
*
* @param source The {@code Component} that originated the event
* @param id An integer indicating the type of event.
* For information on allowable values, see
* the class description for {@link FocusEvent}
* @param temporary Equals {@code true} if the focus change is temporary;
* {@code false} otherwise
* @param opposite The other Component involved in the focus change,
* or {@code null}
* @param cause The focus event cause.
* @throws IllegalArgumentException if {@code source} equals {@code null}
* or if {@code cause} equals {@code null}
* @see #getSource()
* @see #getID()
* @see #isTemporary()
* @see #getOppositeComponent()
* @see Cause
* @since 9
*/
public FocusEvent(Component source, int id, boolean temporary,
Component opposite, Cause cause) {
super(source, id); super(source, id);
if (cause == null) {
throw new IllegalArgumentException("null cause");
}
this.temporary = temporary; this.temporary = temporary;
this.opposite = opposite; this.opposite = opposite;
this.cause = cause;
} }
/** /**
@ -220,8 +337,8 @@ public class FocusEvent extends ComponentEvent {
return (SunToolkit.targetToAppContext(opposite) == return (SunToolkit.targetToAppContext(opposite) ==
AppContext.getAppContext()) AppContext.getAppContext())
? opposite ? opposite
: null; : null;
} }
/** /**
@ -233,17 +350,56 @@ public class FocusEvent extends ComponentEvent {
public String paramString() { public String paramString() {
String typeStr; String typeStr;
switch(id) { switch(id) {
case FOCUS_GAINED: case FOCUS_GAINED:
typeStr = "FOCUS_GAINED"; typeStr = "FOCUS_GAINED";
break; break;
case FOCUS_LOST: case FOCUS_LOST:
typeStr = "FOCUS_LOST"; typeStr = "FOCUS_LOST";
break; break;
default: default:
typeStr = "unknown type"; typeStr = "unknown type";
} }
return typeStr + (temporary ? ",temporary" : ",permanent") + return typeStr + (temporary ? ",temporary" : ",permanent") +
",opposite=" + getOppositeComponent(); ",opposite=" + getOppositeComponent() + ",cause=" + getCause();
} }
} /**
* Returns the event cause.
*
* @return one of {@link Cause} values
* @since 9
*/
public final Cause getCause() {
return cause;
}
/**
* Checks if this deserialized {@code FocusEvent} instance is compatible
* with the current specification which implies that focus event has
* non-null {@code cause} value. If the check fails a new {@code FocusEvent}
* instance is returned which {@code cause} field equals to
* {@link Cause#UNKNOWN} and its other fields have the same values as in
* this {@code FocusEvent} instance.
*
* @serial
* @see #cause
* @since 9
*/
@SuppressWarnings("serial")
Object readResolve() throws ObjectStreamException {
if (cause != null) {
return this;
}
FocusEvent focusEvent = new FocusEvent(new Component(){}, getID(),
isTemporary(), getOppositeComponent());
focusEvent.setSource(null);
focusEvent.consumed = consumed;
AWTAccessor.AWTEventAccessor accessor =
AWTAccessor.getAWTEventAccessor();
accessor.setBData(focusEvent, accessor.getBData(this));
return focusEvent;
}
}

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 1995, 2014, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1995, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -27,12 +27,12 @@ package java.awt.peer;
import java.awt.*; import java.awt.*;
import java.awt.event.PaintEvent; import java.awt.event.PaintEvent;
import java.awt.event.FocusEvent.Cause;
import java.awt.image.ColorModel; import java.awt.image.ColorModel;
import java.awt.image.ImageObserver; import java.awt.image.ImageObserver;
import java.awt.image.ImageProducer; import java.awt.image.ImageProducer;
import java.awt.image.VolatileImage; import java.awt.image.VolatileImage;
import sun.awt.CausedFocusEvent;
import sun.java2d.pipe.Region; import sun.java2d.pipe.Region;
@ -343,7 +343,7 @@ public interface ComponentPeer {
*/ */
boolean requestFocus(Component lightweightChild, boolean temporary, boolean requestFocus(Component lightweightChild, boolean temporary,
boolean focusedWindowChangeAllowed, long time, boolean focusedWindowChangeAllowed, long time,
CausedFocusEvent.Cause cause); Cause cause);
/** /**
* Returns {@code true} when the component takes part in the focus * Returns {@code true} when the component takes part in the focus

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -3558,7 +3558,7 @@ public abstract class JComponent extends Container implements Serializable,
new sun.awt.RequestFocusController() { new sun.awt.RequestFocusController() {
public boolean acceptRequestFocus(Component from, Component to, public boolean acceptRequestFocus(Component from, Component to,
boolean temporary, boolean focusedWindowChangeAllowed, boolean temporary, boolean focusedWindowChangeAllowed,
sun.awt.CausedFocusEvent.Cause cause) FocusEvent.Cause cause)
{ {
if ((to == null) || !(to instanceof JComponent)) { if ((to == null) || !(to instanceof JComponent)) {
return true; return true;

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2008, 2015, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2008, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -29,6 +29,7 @@ import jdk.internal.misc.Unsafe;
import javax.accessibility.AccessibleContext; import javax.accessibility.AccessibleContext;
import java.awt.*; import java.awt.*;
import java.awt.event.FocusEvent.Cause;
import java.awt.dnd.DragSourceContext; import java.awt.dnd.DragSourceContext;
import java.awt.dnd.DropTargetContext; import java.awt.dnd.DropTargetContext;
import java.awt.dnd.peer.DragSourceContextPeer; import java.awt.dnd.peer.DragSourceContextPeer;
@ -104,7 +105,7 @@ public final class AWTAccessor {
/* /*
* Requests focus to the component. * Requests focus to the component.
*/ */
boolean requestFocus(Component comp, CausedFocusEvent.Cause cause); boolean requestFocus(Component comp, Cause cause);
/* /*
* Determines if the component can gain focus. * Determines if the component can gain focus.
*/ */
@ -438,7 +439,7 @@ public final class AWTAccessor {
boolean temporary, boolean temporary,
boolean focusedWindowChangeAllowed, boolean focusedWindowChangeAllowed,
long time, long time,
CausedFocusEvent.Cause cause); Cause cause);
/** /**
* Delivers focus for the lightweight descendant of the heavyweight * Delivers focus for the lightweight descendant of the heavyweight
* synchronously. * synchronously.

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -27,17 +27,18 @@ package sun.awt;
import java.awt.event.FocusEvent; import java.awt.event.FocusEvent;
import java.awt.Component; import java.awt.Component;
import java.io.ObjectStreamException;
import java.lang.reflect.Field;
import java.security.AccessController;
import java.security.PrivilegedAction;
/** /**
* This class represents FocusEvents with a known "cause" - reason why this event happened. It can * This class exists for deserialization compatibility only.
* be mouse press, traversal, activation, and so on - all causes are described as Cause enum. The
* event with the cause can be constructed in two ways - explicitly through constructor of
* CausedFocusEvent class or implicitly, by calling appropriate requestFocusXXX method with "cause"
* parameter. The default cause is UNKNOWN.
*/ */
@SuppressWarnings("serial") class CausedFocusEvent extends FocusEvent {
public class CausedFocusEvent extends FocusEvent { private static final long serialVersionUID = -3647309088427840738L;
public enum Cause {
private enum Cause {
UNKNOWN, UNKNOWN,
MOUSE_EVENT, MOUSE_EVENT,
TRAVERSAL, TRAVERSAL,
@ -51,39 +52,82 @@ public class CausedFocusEvent extends FocusEvent {
NATIVE_SYSTEM, NATIVE_SYSTEM,
ACTIVATION, ACTIVATION,
CLEAR_GLOBAL_FOCUS_OWNER, CLEAR_GLOBAL_FOCUS_OWNER,
RETARGETED RETARGETED;
}; };
@SuppressWarnings("serial")
private static final Component dummy = new Component(){};
private final Cause cause; private final Cause cause;
public Cause getCause() { private CausedFocusEvent(Component source, int id, boolean temporary,
return cause;
}
public String toString() {
return "java.awt.FocusEvent[" + super.paramString() + ",cause=" + cause + "] on " + getSource();
}
public CausedFocusEvent(Component source, int id, boolean temporary,
Component opposite, Cause cause) { Component opposite, Cause cause) {
super(source, id, temporary, opposite); super(source, id, temporary, opposite);
if (cause == null) { throw new IllegalStateException();
cause = Cause.UNKNOWN;
}
this.cause = cause;
} }
/** Object readResolve() throws ObjectStreamException {
* Retargets the original focus event to the new target. If the FocusEvent.Cause newCause;
* original focus event is CausedFocusEvent, it remains such and switch (cause) {
* cause is copied. Otherwise, new CausedFocusEvent is created, case UNKNOWN:
* with cause as RETARGETED. newCause = FocusEvent.Cause.UNKNOWN;
* @return retargeted event, or null if e is null break;
*/ case MOUSE_EVENT:
public static FocusEvent retarget(FocusEvent e, Component newSource) { newCause = FocusEvent.Cause.MOUSE_EVENT;
if (e == null) return null; break;
case TRAVERSAL:
newCause = FocusEvent.Cause.TRAVERSAL;
break;
case TRAVERSAL_UP:
newCause = FocusEvent.Cause.TRAVERSAL_UP;
break;
case TRAVERSAL_DOWN:
newCause = FocusEvent.Cause.TRAVERSAL_DOWN;
break;
case TRAVERSAL_FORWARD:
newCause = FocusEvent.Cause.TRAVERSAL_FORWARD;
break;
case TRAVERSAL_BACKWARD:
newCause = FocusEvent.Cause.TRAVERSAL_BACKWARD;
break;
case ROLLBACK:
newCause = FocusEvent.Cause.ROLLBACK;
break;
case NATIVE_SYSTEM:
newCause = FocusEvent.Cause.UNEXPECTED;
break;
case ACTIVATION:
newCause = FocusEvent.Cause.ACTIVATION;
break;
case CLEAR_GLOBAL_FOCUS_OWNER:
newCause = FocusEvent.Cause.CLEAR_GLOBAL_FOCUS_OWNER;
break;
default:
newCause = FocusEvent.Cause.UNKNOWN;
}
return new CausedFocusEvent(newSource, e.getID(), e.isTemporary(), e.getOppositeComponent(), FocusEvent focusEvent = new FocusEvent(dummy, getID(), isTemporary(),
(e instanceof CausedFocusEvent) ? ((CausedFocusEvent)e).getCause() : Cause.RETARGETED); getOppositeComponent(), newCause);
focusEvent.setSource(null);
try {
final Field consumedField = FocusEvent.class.getField("consumed");
AccessController.doPrivileged(new PrivilegedAction<Object>() {
@Override
public Object run() {
consumedField.setAccessible(true);
try {
consumedField.set(focusEvent, consumed);
} catch (IllegalAccessException e) {
}
return null;
}
});
} catch (NoSuchFieldException e) {
}
AWTAccessor.AWTEventAccessor accessor =
AWTAccessor.getAWTEventAccessor();
accessor.setBData(focusEvent, accessor.getBData(this));
return focusEvent;
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -60,8 +60,8 @@ public abstract class KeyboardFocusManagerPeerImpl implements KeyboardFocusManag
focusLog.fine("Clearing global focus owner " + focusOwner); focusLog.fine("Clearing global focus owner " + focusOwner);
} }
if (focusOwner != null) { if (focusOwner != null) {
FocusEvent fl = new CausedFocusEvent(focusOwner, FocusEvent.FOCUS_LOST, false, null, FocusEvent fl = new FocusEvent(focusOwner, FocusEvent.FOCUS_LOST, false, null,
CausedFocusEvent.Cause.CLEAR_GLOBAL_FOCUS_OWNER); FocusEvent.Cause.CLEAR_GLOBAL_FOCUS_OWNER);
SunToolkit.postPriorityEvent(fl); SunToolkit.postPriorityEvent(fl);
} }
} }
@ -110,7 +110,7 @@ public abstract class KeyboardFocusManagerPeerImpl implements KeyboardFocusManag
boolean temporary, boolean temporary,
boolean focusedWindowChangeAllowed, boolean focusedWindowChangeAllowed,
long time, long time,
CausedFocusEvent.Cause cause, FocusEvent.Cause cause,
Component currentFocusOwner) // provided by the descendant peers Component currentFocusOwner) // provided by the descendant peers
{ {
if (lightweightChild == null) { if (lightweightChild == null) {
@ -122,7 +122,7 @@ public abstract class KeyboardFocusManagerPeerImpl implements KeyboardFocusManag
currentOwner = null; currentOwner = null;
} }
if (currentOwner != null) { if (currentOwner != null) {
FocusEvent fl = new CausedFocusEvent(currentOwner, FocusEvent.FOCUS_LOST, FocusEvent fl = new FocusEvent(currentOwner, FocusEvent.FOCUS_LOST,
false, lightweightChild, cause); false, lightweightChild, cause);
if (focusLog.isLoggable(PlatformLogger.Level.FINER)) { if (focusLog.isLoggable(PlatformLogger.Level.FINER)) {
@ -131,7 +131,7 @@ public abstract class KeyboardFocusManagerPeerImpl implements KeyboardFocusManag
SunToolkit.postEvent(SunToolkit.targetToAppContext(currentOwner), fl); SunToolkit.postEvent(SunToolkit.targetToAppContext(currentOwner), fl);
} }
FocusEvent fg = new CausedFocusEvent(lightweightChild, FocusEvent.FOCUS_GAINED, FocusEvent fg = new FocusEvent(lightweightChild, FocusEvent.FOCUS_GAINED,
false, currentOwner, cause); false, currentOwner, cause);
if (focusLog.isLoggable(PlatformLogger.Level.FINER)) { if (focusLog.isLoggable(PlatformLogger.Level.FINER)) {
@ -142,7 +142,7 @@ public abstract class KeyboardFocusManagerPeerImpl implements KeyboardFocusManag
} }
// WARNING: Don't call it on the Toolkit thread. // WARNING: Don't call it on the Toolkit thread.
public static boolean requestFocusFor(Component target, CausedFocusEvent.Cause cause) { public static boolean requestFocusFor(Component target, FocusEvent.Cause cause) {
return AWTAccessor.getComponentAccessor().requestFocus(target, cause); return AWTAccessor.getComponentAccessor().requestFocus(target, cause);
} }
@ -152,7 +152,7 @@ public abstract class KeyboardFocusManagerPeerImpl implements KeyboardFocusManag
boolean temporary, boolean temporary,
boolean focusedWindowChangeAllowed, boolean focusedWindowChangeAllowed,
long time, long time,
CausedFocusEvent.Cause cause) FocusEvent.Cause cause)
{ {
return KfmAccessor.instance.shouldNativelyFocusHeavyweight( return KfmAccessor.instance.shouldNativelyFocusHeavyweight(
heavyweight, descendant, temporary, focusedWindowChangeAllowed, heavyweight, descendant, temporary, focusedWindowChangeAllowed,

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -37,7 +37,7 @@ import java.awt.Graphics;
import java.awt.GraphicsConfiguration; import java.awt.GraphicsConfiguration;
import java.awt.Image; import java.awt.Image;
import java.awt.Insets; import java.awt.Insets;
import java.awt.MenuBar; import java.awt.event.FocusEvent.Cause;
import java.awt.Point; import java.awt.Point;
import java.awt.Event; import java.awt.Event;
import java.awt.event.PaintEvent; import java.awt.event.PaintEvent;
@ -178,7 +178,7 @@ public class NullComponentPeer implements LightweightPeer,
public boolean requestFocus public boolean requestFocus
(Component lightweightChild, boolean temporary, (Component lightweightChild, boolean temporary,
boolean focusedWindowChangeAllowed, long time, CausedFocusEvent.Cause cause) { boolean focusedWindowChangeAllowed, long time, Cause cause) {
return false; return false;
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2006, 2016 Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -25,10 +25,11 @@
package sun.awt; package sun.awt;
import java.awt.Component; import java.awt.Component;
import java.awt.event.FocusEvent.Cause;
public interface RequestFocusController public interface RequestFocusController
{ {
public boolean acceptRequestFocus(Component from, Component to, public boolean acceptRequestFocus(Component from, Component to,
boolean temporary, boolean focusedWindowChangeAllowed, boolean temporary, boolean focusedWindowChangeAllowed,
CausedFocusEvent.Cause cause); Cause cause);
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2002, 2015, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2002, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -287,7 +287,7 @@ public class XComponentPeer extends XWindow implements ComponentPeer, DropTarget
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public final boolean requestFocus(Component lightweightChild, boolean temporary, public final boolean requestFocus(Component lightweightChild, boolean temporary,
boolean focusedWindowChangeAllowed, long time, boolean focusedWindowChangeAllowed, long time,
CausedFocusEvent.Cause cause) FocusEvent.Cause cause)
{ {
if (XKeyboardFocusManagerPeer. if (XKeyboardFocusManagerPeer.
processSynchronousLightweightTransfer(target, lightweightChild, temporary, processSynchronousLightweightTransfer(target, lightweightChild, temporary,
@ -527,7 +527,7 @@ public class XComponentPeer extends XWindow implements ComponentPeer, DropTarget
// WindowEvent wfg = new WindowEvent(parentWindow, WindowEvent.WINDOW_GAINED_FOCUS); // WindowEvent wfg = new WindowEvent(parentWindow, WindowEvent.WINDOW_GAINED_FOCUS);
// parentWindow.dispatchEvent(wfg); // parentWindow.dispatchEvent(wfg);
// } // }
XKeyboardFocusManagerPeer.requestFocusFor(target, CausedFocusEvent.Cause.MOUSE_EVENT); XKeyboardFocusManagerPeer.requestFocusFor(target, FocusEvent.Cause.MOUSE_EVENT);
} }
break; break;
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -430,13 +430,10 @@ public class XEmbedCanvasPeer extends XCanvasPeer implements WindowFocusListener
if (isXEmbedActive()) { if (isXEmbedActive()) {
xembedLog.fine("Forwarding FOCUS_GAINED"); xembedLog.fine("Forwarding FOCUS_GAINED");
int flavor = XEMBED_FOCUS_CURRENT; int flavor = XEMBED_FOCUS_CURRENT;
if (e instanceof CausedFocusEvent) { if (e.getCause() == FocusEvent.Cause.TRAVERSAL_FORWARD) {
CausedFocusEvent ce = (CausedFocusEvent)e; flavor = XEMBED_FOCUS_FIRST;
if (ce.getCause() == CausedFocusEvent.Cause.TRAVERSAL_FORWARD) { } else if (e.getCause() == FocusEvent.Cause.TRAVERSAL_BACKWARD) {
flavor = XEMBED_FOCUS_FIRST; flavor = XEMBED_FOCUS_LAST;
} else if (ce.getCause() == CausedFocusEvent.Cause.TRAVERSAL_BACKWARD) {
flavor = XEMBED_FOCUS_LAST;
}
} }
xembed.sendMessage(xembed.handle, XEMBED_FOCUS_IN, flavor, 0, 0); xembed.sendMessage(xembed.handle, XEMBED_FOCUS_IN, flavor, 0, 0);
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -195,7 +195,7 @@ public class XEmbedChildProxyPeer implements ComponentPeer, XEventDispatcher{
boolean temporary, boolean temporary,
boolean focusedWindowChangeAllowed, boolean focusedWindowChangeAllowed,
long time, long time,
CausedFocusEvent.Cause cause) FocusEvent.Cause cause)
{ {
int result = XKeyboardFocusManagerPeer int result = XKeyboardFocusManagerPeer
.shouldNativelyFocusHeavyweight(proxy, lightweightChild, .shouldNativelyFocusHeavyweight(proxy, lightweightChild,

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -28,7 +28,7 @@ import java.awt.Component;
import java.awt.Window; import java.awt.Window;
import sun.awt.AWTAccessor; import sun.awt.AWTAccessor;
import sun.awt.CausedFocusEvent; import java.awt.event.FocusEvent;
import sun.awt.KeyboardFocusManagerPeerImpl; import sun.awt.KeyboardFocusManagerPeerImpl;
import sun.util.logging.PlatformLogger; import sun.util.logging.PlatformLogger;
@ -101,7 +101,7 @@ public class XKeyboardFocusManagerPeer extends KeyboardFocusManagerPeerImpl {
boolean temporary, boolean temporary,
boolean focusedWindowChangeAllowed, boolean focusedWindowChangeAllowed,
long time, long time,
CausedFocusEvent.Cause cause) FocusEvent.Cause cause)
{ {
return KeyboardFocusManagerPeerImpl.deliverFocus(lightweightChild, return KeyboardFocusManagerPeerImpl.deliverFocus(lightweightChild,
target, target,

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -59,7 +59,6 @@ import javax.swing.text.JTextComponent;
import javax.swing.plaf.BorderUIResource; import javax.swing.plaf.BorderUIResource;
import java.awt.im.InputMethodRequests; import java.awt.im.InputMethodRequests;
import sun.awt.CausedFocusEvent;
import sun.awt.AWTAccessor; import sun.awt.AWTAccessor;
import sun.awt.SunToolkit; import sun.awt.SunToolkit;
@ -945,14 +944,16 @@ final class XTextAreaPeer extends XComponentPeer implements TextAreaPeer {
void forwardFocusGained( FocusEvent e) { void forwardFocusGained( FocusEvent e) {
isFocused = true; isFocused = true;
FocusEvent fe = CausedFocusEvent.retarget(e, this); FocusEvent fe = new FocusEvent(this, e.getID(), e.isTemporary(),
e.getOppositeComponent(), e.getCause());
super.processFocusEvent(fe); super.processFocusEvent(fe);
} }
void forwardFocusLost( FocusEvent e) { void forwardFocusLost( FocusEvent e) {
isFocused = false; isFocused = false;
FocusEvent fe = CausedFocusEvent.retarget(e, this); FocusEvent fe = new FocusEvent(this, e.getID(), e.isTemporary(),
e.getOppositeComponent(), e.getCause());
super.processFocusEvent(fe); super.processFocusEvent(fe);
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -54,7 +54,6 @@ import java.awt.im.InputMethodRequests;
import sun.util.logging.PlatformLogger; import sun.util.logging.PlatformLogger;
import sun.awt.CausedFocusEvent;
import sun.awt.AWTAccessor; import sun.awt.AWTAccessor;
final class XTextFieldPeer extends XComponentPeer implements TextFieldPeer { final class XTextFieldPeer extends XComponentPeer implements TextFieldPeer {
@ -618,13 +617,15 @@ final class XTextFieldPeer extends XComponentPeer implements TextFieldPeer {
void forwardFocusGained( FocusEvent e) { void forwardFocusGained( FocusEvent e) {
isFocused = true; isFocused = true;
FocusEvent fe = CausedFocusEvent.retarget(e, this); FocusEvent fe = new FocusEvent(this, e.getID(), e.isTemporary(),
e.getOppositeComponent(), e.getCause());
super.processFocusEvent(fe); super.processFocusEvent(fe);
} }
void forwardFocusLost( FocusEvent e) { void forwardFocusLost( FocusEvent e) {
isFocused = false; isFocused = false;
FocusEvent fe = CausedFocusEvent.retarget(e, this); FocusEvent fe = new FocusEvent(this, e.getID(), e.isTemporary(),
e.getOppositeComponent(), e.getCause());
super.processFocusEvent(fe); super.processFocusEvent(fe);
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 1996, 2015, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1996, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -28,7 +28,6 @@ import java.awt.*;
import java.awt.peer.*; import java.awt.peer.*;
import java.awt.image.VolatileImage; import java.awt.image.VolatileImage;
import sun.awt.RepaintArea; import sun.awt.RepaintArea;
import sun.awt.CausedFocusEvent;
import sun.awt.image.SunVolatileImage; import sun.awt.image.SunVolatileImage;
import sun.awt.image.ToolkitImage; import sun.awt.image.ToolkitImage;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
@ -321,7 +320,7 @@ public abstract class WComponentPeer extends WObjectPeer
WKeyboardFocusManagerPeer.shouldFocusOnClick((Component)target)) WKeyboardFocusManagerPeer.shouldFocusOnClick((Component)target))
{ {
WKeyboardFocusManagerPeer.requestFocusFor((Component)target, WKeyboardFocusManagerPeer.requestFocusFor((Component)target,
CausedFocusEvent.Cause.MOUSE_EVENT); FocusEvent.Cause.MOUSE_EVENT);
} }
break; break;
} }
@ -687,7 +686,7 @@ public abstract class WComponentPeer extends WObjectPeer
@Override @Override
public boolean requestFocus(Component lightweightChild, boolean temporary, public boolean requestFocus(Component lightweightChild, boolean temporary,
boolean focusedWindowChangeAllowed, long time, boolean focusedWindowChangeAllowed, long time,
CausedFocusEvent.Cause cause) FocusEvent.Cause cause)
{ {
if (WKeyboardFocusManagerPeer. if (WKeyboardFocusManagerPeer.
processSynchronousLightweightTransfer((Component)target, lightweightChild, temporary, processSynchronousLightweightTransfer((Component)target, lightweightChild, temporary,

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 1996, 2015, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1996, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -25,6 +25,7 @@
package sun.awt.windows; package sun.awt.windows;
import java.awt.*; import java.awt.*;
import java.awt.event.FocusEvent.Cause;
import java.awt.dnd.DropTarget; import java.awt.dnd.DropTarget;
import java.awt.peer.*; import java.awt.peer.*;
import java.io.File; import java.io.File;
@ -34,7 +35,6 @@ import java.security.PrivilegedAction;
import java.util.ResourceBundle; import java.util.ResourceBundle;
import java.util.MissingResourceException; import java.util.MissingResourceException;
import java.util.Vector; import java.util.Vector;
import sun.awt.CausedFocusEvent;
import sun.awt.AWTAccessor; import sun.awt.AWTAccessor;
final class WFileDialogPeer extends WWindowPeer implements FileDialogPeer { final class WFileDialogPeer extends WWindowPeer implements FileDialogPeer {
@ -282,7 +282,7 @@ final class WFileDialogPeer extends WWindowPeer implements FileDialogPeer {
@Override @Override
public boolean requestFocus public boolean requestFocus
(Component lightweightChild, boolean temporary, (Component lightweightChild, boolean temporary,
boolean focusedWindowChangeAllowed, long time, CausedFocusEvent.Cause cause) boolean focusedWindowChangeAllowed, long time, Cause cause)
{ {
return false; return false;
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2009, 2015, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2009, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -32,7 +32,7 @@ import java.awt.peer.ComponentPeer;
import sun.awt.AWTAccessor; import sun.awt.AWTAccessor;
import sun.awt.AWTAccessor.ComponentAccessor; import sun.awt.AWTAccessor.ComponentAccessor;
import sun.awt.KeyboardFocusManagerPeerImpl; import sun.awt.KeyboardFocusManagerPeerImpl;
import sun.awt.CausedFocusEvent; import java.awt.event.FocusEvent.Cause;
final class WKeyboardFocusManagerPeer extends KeyboardFocusManagerPeerImpl { final class WKeyboardFocusManagerPeer extends KeyboardFocusManagerPeerImpl {
static native void setNativeFocusOwner(ComponentPeer peer); static native void setNativeFocusOwner(ComponentPeer peer);
@ -75,7 +75,7 @@ final class WKeyboardFocusManagerPeer extends KeyboardFocusManagerPeerImpl {
boolean temporary, boolean temporary,
boolean focusedWindowChangeAllowed, boolean focusedWindowChangeAllowed,
long time, long time,
CausedFocusEvent.Cause cause) Cause cause)
{ {
// TODO: do something to eliminate this forwarding // TODO: do something to eliminate this forwarding
return KeyboardFocusManagerPeerImpl.deliverFocus(lightweightChild, return KeyboardFocusManagerPeerImpl.deliverFocus(lightweightChild,

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 1999, 2015, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1999, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -26,11 +26,11 @@
package sun.awt.windows; package sun.awt.windows;
import java.awt.*; import java.awt.*;
import java.awt.event.FocusEvent.Cause;
import java.awt.peer.DialogPeer; import java.awt.peer.DialogPeer;
import java.awt.peer.ComponentPeer; import java.awt.peer.ComponentPeer;
import java.awt.dnd.DropTarget; import java.awt.dnd.DropTarget;
import java.util.Vector; import java.util.Vector;
import sun.awt.CausedFocusEvent;
import sun.awt.AWTAccessor; import sun.awt.AWTAccessor;
class WPrintDialogPeer extends WWindowPeer implements DialogPeer { class WPrintDialogPeer extends WWindowPeer implements DialogPeer {
@ -153,7 +153,7 @@ class WPrintDialogPeer extends WWindowPeer implements DialogPeer {
@Override @Override
public boolean requestFocus public boolean requestFocus
(Component lightweightChild, boolean temporary, (Component lightweightChild, boolean temporary,
boolean focusedWindowChangeAllowed, long time, CausedFocusEvent.Cause cause) boolean focusedWindowChangeAllowed, long time, Cause cause)
{ {
return false; return false;

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 1996, 2015, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1996, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -300,11 +300,11 @@ public class WWindowPeer extends WPanelPeer implements WindowPeer,
return getNativeWindowSize(); return getNativeWindowSize();
} }
public boolean requestWindowFocus(CausedFocusEvent.Cause cause) { public boolean requestWindowFocus(FocusEvent.Cause cause) {
if (!focusAllowedFor()) { if (!focusAllowedFor()) {
return false; return false;
} }
return requestWindowFocus(cause == CausedFocusEvent.Cause.MOUSE_EVENT); return requestWindowFocus(cause == FocusEvent.Cause.MOUSE_EVENT);
} }
private native boolean requestWindowFocus(boolean isMouseEventCause); private native boolean requestWindowFocus(boolean isMouseEventCause);

View File

@ -0,0 +1,224 @@
/*
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
@test
@bug 8080395
@summary consider making sun.awt.CausedFocusEvent functionality public
@run main FocusCauseTest
*/
import java.awt.*;
import java.awt.event.FocusEvent.Cause;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.lang.IllegalArgumentException;
import java.lang.Override;
import java.lang.RuntimeException;
import java.util.Arrays;
public class FocusCauseTest {
private static Cause[] causes1 = {Cause.ACTIVATION,
Cause.UNKNOWN, Cause.UNKNOWN, Cause.TRAVERSAL_FORWARD,
Cause.TRAVERSAL_FORWARD, Cause.TRAVERSAL_BACKWARD,
Cause.TRAVERSAL_BACKWARD, Cause.TRAVERSAL_UP,
Cause.TRAVERSAL_DOWN, Cause.CLEAR_GLOBAL_FOCUS_OWNER};
private static Cause[] causes2 = new Cause[10];
private static int cnt;
static byte[] data =
{-84, -19, 0, 5, 115, 114, 0, 24, 115, 117, 110, 46, 97, 119,
116, 46, 67, 97, 117, 115, 101, 100, 70, 111, 99, 117, 115, 69, 118,
101, 110, 116, -51, 98, 39, -75, 86, 52, 107, 30, 2, 0, 1, 76, 0, 5,
99, 97, 117, 115, 101, 116, 0, 32, 76, 115, 117, 110, 47, 97, 119,
116, 47, 67, 97, 117, 115, 101, 100, 70, 111, 99, 117, 115, 69, 118,
101, 110, 116, 36, 67, 97, 117, 115, 101, 59, 120, 114, 0, 25, 106,
97, 118, 97, 46, 97, 119, 116, 46, 101, 118, 101, 110, 116, 46, 70,
111, 99, 117, 115, 69, 118, 101, 110, 116, 7, 68, -65, 75, 55, -113,
98, -52, 2, 0, 1, 90, 0, 9, 116, 101, 109, 112, 111, 114, 97, 114,
121, 120, 114, 0, 29, 106, 97, 118, 97, 46, 97, 119, 116, 46, 101,
118, 101, 110, 116, 46, 67, 111, 109, 112, 111, 110, 101, 110, 116,
69, 118, 101, 110, 116, 112, 109, -6, -107, 79, -87, -38, 69, 2, 0,
0, 120, 114, 0, 17, 106, 97, 118, 97, 46, 97, 119, 116, 46, 65, 87,
84, 69, 118, 101, 110, 116, -26, -85, 45, -31, 24, -33, -118, -61,
2, 0, 3, 90, 0, 8, 99, 111, 110, 115, 117, 109, 101, 100, 73, 0, 2,
105, 100, 91, 0, 5, 98, 100, 97, 116, 97, 116, 0, 2, 91, 66, 120,
114, 0, 21, 106, 97, 118, 97, 46, 117, 116, 105, 108, 46, 69, 118,
101, 110, 116, 79, 98, 106, 101, 99, 116, 76, -115, 9, 78, 24, 109,
125, -88, 2, 0, 0, 120, 112, 0, 0, 0, 3, -20, 112, 0, 126, 114, 0,
30, 115, 117, 110, 46, 97, 119, 116, 46, 67, 97, 117, 115, 101, 100,
70, 111, 99, 117, 115, 69, 118, 101, 110, 116, 36, 67, 97, 117, 115,
101, 0, 0, 0, 0, 0, 0, 0, 0, 18, 0, 0, 120, 114, 0, 14, 106, 97,
118, 97, 46, 108, 97, 110, 103, 46, 69, 110, 117, 109, 0, 0, 0, 0,
0, 0, 0, 0, 18, 0, 0, 120, 112, 116, 0};
static byte[] dataOld =
{-84, -19, 0, 5, 115, 114, 0, 25, 106, 97, 118, 97, 46, 97, 119,
116, 46, 101, 118, 101, 110, 116, 46, 70, 111, 99, 117, 115, 69,
118, 101, 110, 116, 7, 68, -65, 75, 55, -113, 98, -52, 2, 0, 1, 90,
0, 9, 116, 101, 109, 112, 111, 114, 97, 114, 121, 120, 114, 0, 29,
106, 97, 118, 97, 46, 97, 119, 116, 46, 101, 118, 101, 110, 116, 46,
67, 111, 109, 112, 111, 110, 101, 110, 116, 69, 118, 101, 110, 116,
112, 109, -6, -107, 79, -87, -38, 69, 2, 0, 0, 120, 114, 0, 17, 106,
97, 118, 97, 46, 97, 119, 116, 46, 65, 87, 84, 69, 118, 101, 110,
116, -26, -85, 45, -31, 24, -33, -118, -61, 2, 0, 3, 90, 0, 8, 99,
111, 110, 115, 117, 109, 101, 100, 73, 0, 2, 105, 100, 91, 0, 5, 98,
100, 97, 116, 97, 116, 0, 2, 91, 66, 120, 114, 0, 21, 106, 97, 118,
97, 46, 117, 116, 105, 108, 46, 69, 118, 101, 110, 116, 79, 98, 106,
101, 99, 116, 76, -115, 9, 78, 24, 109, 125, -88, 2, 0, 0, 120, 112,
0, 0, 0, 0, 100, 112, 0};
static String[] causesIn = {"UNKNOWN", "MOUSE_EVENT", "TRAVERSAL",
"TRAVERSAL_UP", "TRAVERSAL_DOWN", "TRAVERSAL_FORWARD",
"TRAVERSAL_BACKWARD", "MANUAL_REQUEST", "AUTOMATIC_TRAVERSE"
,"ROLLBACK", "NATIVE_SYSTEM", "ACTIVATION",
"CLEAR_GLOBAL_FOCUS_OWNER", "RETARGETED"};
static FocusEvent.Cause[] causesOut = {FocusEvent.Cause.UNKNOWN,
FocusEvent.Cause.MOUSE_EVENT,
FocusEvent.Cause.TRAVERSAL, FocusEvent.Cause.TRAVERSAL_UP,
FocusEvent.Cause.TRAVERSAL_DOWN, FocusEvent.Cause.TRAVERSAL_FORWARD,
FocusEvent.Cause.TRAVERSAL_BACKWARD, FocusEvent.Cause.UNKNOWN,
FocusEvent.Cause.UNKNOWN, FocusEvent.Cause.ROLLBACK,
FocusEvent.Cause.UNEXPECTED, FocusEvent.Cause.ACTIVATION,
FocusEvent.Cause.CLEAR_GLOBAL_FOCUS_OWNER, FocusEvent.Cause.UNKNOWN
};
public static void main(String[] args) throws Exception {
testCauses();
testNullCause();
testCausedFocusEventDeserialization();
testFocusEventDeserialization();
System.out.println("ok");
}
private static void testNullCause() {
try {
new FocusEvent(new Frame(), FocusEvent.FOCUS_GAINED, true,
null, null);
throw new RuntimeException("Exception is not thrown when the " +
"cause is null");
} catch (IllegalArgumentException e) {
}
}
private static void testCauses() throws Exception {
cnt = 0;
Frame frame = new Frame();
TextField comp1 = new TextField();
comp1.addFocusListener(new FocusListener() {
@Override
public void focusGained(FocusEvent e) {
System.out.println(e.getCause());
causes2[cnt++] = e.getCause();
}
@Override
public void focusLost(FocusEvent e) {
System.out.println(e.getCause());
causes2[cnt++] = e.getCause();
}
});
TextField comp2 = new TextField();
comp2.addFocusListener(new FocusListener() {
@Override
public void focusGained(FocusEvent e) {
System.out.println(e.getCause());
causes2[cnt++] = e.getCause();
}
@Override
public void focusLost(FocusEvent e) {
System.out.println(e.getCause());
causes2[cnt++] = e.getCause();
}
});
frame.add(comp1, BorderLayout.NORTH);
frame.add(comp2, BorderLayout.SOUTH);
frame.setVisible(true);
Robot robot = new Robot();
robot.delay(200);
robot.waitForIdle();
comp2.requestFocus();
robot.waitForIdle();
comp2.transferFocus();
robot.waitForIdle();
comp1.transferFocusBackward();
robot.waitForIdle();
comp2.transferFocusUpCycle();
robot.waitForIdle();
frame.transferFocusDownCycle();
robot.waitForIdle();
frame.dispose();
robot.waitForIdle();
if (!Arrays.equals(causes1, causes2)) {
throw new RuntimeException("wrong cause " + causes2);
}
}
private static void testCausedFocusEventDeserialization() throws
Exception {
for (int i = 0; i < causesIn.length; i++) {
final String causeIn = causesIn[i];
ObjectInputStream oi = new ObjectInputStream(new InputStream() {
int cnt = 0;
@Override
public int read() throws IOException {
if(cnt < data.length) {
return data[cnt++];
} else if(cnt == data.length){
cnt++;
return causeIn.length();
} else if(cnt - data.length - 1 < causeIn.length()) {
return causeIn.getBytes()[cnt++ - data.length - 1];
}
return -1;
}
});
FocusEvent ev = (FocusEvent) oi.readObject();
System.out.println(ev);
if(ev.getCause() != causesOut[i]) {
throw new RuntimeException("Wrong cause read :" +ev.getCause());
}
}
}
private static void testFocusEventDeserialization() throws
Exception {
ObjectInputStream oi = new ObjectInputStream(
new ByteArrayInputStream(dataOld));
FocusEvent ev = (FocusEvent)oi.readObject();
if(ev.getCause() != FocusEvent.Cause.UNKNOWN) {
throw new RuntimeException("Wrong cause in deserialized FocusEvent "
+ ev.getCause());
}
}
}