下面列出了怎么用sun.awt.CausedFocusEvent的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public boolean requestWindowFocus(CausedFocusEvent.Cause cause) {
if (!focusAllowedFor()) {
return false;
}
if (getPlatformWindow().rejectFocusRequest(cause)) {
return false;
}
Window opposite = LWKeyboardFocusManagerPeer.getInstance().
getCurrentFocusedWindow();
changeFocusedWindow(true, opposite);
return true;
}
private boolean doRestoreFocus(Component toFocus, Component vetoedComponent,
boolean clearOnFailure)
{
if (toFocus != vetoedComponent && toFocus.isShowing() && toFocus.canBeFocusOwner() &&
toFocus.requestFocus(false, CausedFocusEvent.Cause.ROLLBACK))
{
return true;
} else {
Component nextFocus = toFocus.getNextFocusCandidate();
if (nextFocus != null && nextFocus != vetoedComponent &&
nextFocus.requestFocusInWindow(CausedFocusEvent.Cause.ROLLBACK))
{
return true;
} else if (clearOnFailure) {
clearGlobalFocusOwnerPriv();
return true;
} else {
return false;
}
}
}
static FocusEvent retargetUnexpectedFocusEvent(FocusEvent fe) {
synchronized (heavyweightRequests) {
// Any other case represents a failure condition which we did
// not expect. We need to clearFocusRequestList() and patch up
// the event as best as possible.
if (removeFirstRequest()) {
return (FocusEvent)retargetFocusEvent(fe);
}
Component source = fe.getComponent();
Component opposite = fe.getOppositeComponent();
boolean temporary = false;
if (fe.getID() == FocusEvent.FOCUS_LOST &&
(opposite == null || isTemporary(opposite, source)))
{
temporary = true;
}
return new CausedFocusEvent(source, fe.getID(), temporary, opposite,
CausedFocusEvent.Cause.NATIVE_SYSTEM);
}
}
public static boolean deliverFocus(Component lightweightChild,
Component target,
boolean temporary,
boolean focusedWindowChangeAllowed,
long time,
CausedFocusEvent.Cause cause)
{
// TODO: do something to eliminate this forwarding
return KeyboardFocusManagerPeerImpl.deliverFocus(lightweightChild,
target,
temporary,
focusedWindowChangeAllowed,
time,
cause,
getNativeFocusOwner());
}
private boolean doRestoreFocus(Component toFocus, Component vetoedComponent,
boolean clearOnFailure)
{
boolean success = true;
if (toFocus != vetoedComponent && toFocus.isShowing() && toFocus.canBeFocusOwner() &&
(success = toFocus.requestFocus(false, CausedFocusEvent.Cause.ROLLBACK)))
{
return true;
} else {
if (!success && getGlobalFocusedWindow() != SunToolkit.getContainingWindow(toFocus)) {
restoreFocusTo = toFocus;
return true;
}
Component nextFocus = toFocus.getNextFocusCandidate();
if (nextFocus != null && nextFocus != vetoedComponent &&
nextFocus.requestFocusInWindow(CausedFocusEvent.Cause.ROLLBACK))
{
return true;
} else if (clearOnFailure) {
clearGlobalFocusOwnerPriv();
return true;
} else {
return false;
}
}
}
private boolean doRestoreFocus(Component toFocus, Component vetoedComponent,
boolean clearOnFailure)
{
if (toFocus != vetoedComponent && toFocus.isShowing() && toFocus.canBeFocusOwner() &&
toFocus.requestFocus(false, CausedFocusEvent.Cause.ROLLBACK))
{
return true;
} else {
Component nextFocus = toFocus.getNextFocusCandidate();
if (nextFocus != null && nextFocus != vetoedComponent &&
nextFocus.requestFocusInWindow(CausedFocusEvent.Cause.ROLLBACK))
{
return true;
} else if (clearOnFailure) {
clearGlobalFocusOwnerPriv();
return true;
} else {
return false;
}
}
}
public static boolean deliverFocus(Component lightweightChild,
Component target,
boolean temporary,
boolean focusedWindowChangeAllowed,
long time,
CausedFocusEvent.Cause cause)
{
// TODO: do something to eliminate this forwarding
return KeyboardFocusManagerPeerImpl.deliverFocus(lightweightChild,
target,
temporary,
focusedWindowChangeAllowed,
time,
cause,
getNativeFocusOwner());
}
static FocusEvent retargetUnexpectedFocusEvent(FocusEvent fe) {
synchronized (heavyweightRequests) {
// Any other case represents a failure condition which we did
// not expect. We need to clearFocusRequestList() and patch up
// the event as best as possible.
if (removeFirstRequest()) {
return (FocusEvent)retargetFocusEvent(fe);
}
Component source = fe.getComponent();
Component opposite = fe.getOppositeComponent();
boolean temporary = false;
if (fe.getID() == FocusEvent.FOCUS_LOST &&
(opposite == null || isTemporary(opposite, source)))
{
temporary = true;
}
return new CausedFocusEvent(source, fe.getID(), temporary, opposite,
CausedFocusEvent.Cause.NATIVE_SYSTEM);
}
}
static FocusEvent retargetUnexpectedFocusEvent(FocusEvent fe) {
synchronized (heavyweightRequests) {
// Any other case represents a failure condition which we did
// not expect. We need to clearFocusRequestList() and patch up
// the event as best as possible.
if (removeFirstRequest()) {
return (FocusEvent)retargetFocusEvent(fe);
}
Component source = fe.getComponent();
Component opposite = fe.getOppositeComponent();
boolean temporary = false;
if (fe.getID() == FocusEvent.FOCUS_LOST &&
(opposite == null || isTemporary(opposite, source)))
{
temporary = true;
}
return new CausedFocusEvent(source, fe.getID(), temporary, opposite,
CausedFocusEvent.Cause.NATIVE_SYSTEM);
}
}
@Override
public boolean requestWindowFocus(CausedFocusEvent.Cause cause) {
if (!focusAllowedFor()) {
return false;
}
if (getPlatformWindow().rejectFocusRequest(cause)) {
return false;
}
Window opposite = LWKeyboardFocusManagerPeer.getInstance().
getCurrentFocusedWindow();
changeFocusedWindow(true, opposite);
return true;
}
public static boolean deliverFocus(Component lightweightChild,
Component target,
boolean temporary,
boolean focusedWindowChangeAllowed,
long time,
CausedFocusEvent.Cause cause)
{
// TODO: do something to eliminate this forwarding
return KeyboardFocusManagerPeerImpl.deliverFocus(lightweightChild,
target,
temporary,
focusedWindowChangeAllowed,
time,
cause,
getNativeFocusOwner());
}
private boolean doRestoreFocus(Component toFocus, Component vetoedComponent,
boolean clearOnFailure)
{
boolean success = true;
if (toFocus != vetoedComponent && toFocus.isShowing() && toFocus.canBeFocusOwner() &&
(success = toFocus.requestFocus(false, CausedFocusEvent.Cause.ROLLBACK)))
{
return true;
} else {
if (!success && getGlobalFocusedWindow() != SunToolkit.getContainingWindow(toFocus)) {
restoreFocusTo = toFocus;
return true;
}
Component nextFocus = toFocus.getNextFocusCandidate();
if (nextFocus != null && nextFocus != vetoedComponent &&
nextFocus.requestFocusInWindow(CausedFocusEvent.Cause.ROLLBACK))
{
return true;
} else if (clearOnFailure) {
clearGlobalFocusOwnerPriv();
return true;
} else {
return false;
}
}
}
@Override
public boolean requestWindowFocus(CausedFocusEvent.Cause cause) {
if (!focusAllowedFor()) {
return false;
}
if (getPlatformWindow().rejectFocusRequest(cause)) {
return false;
}
Window opposite = LWKeyboardFocusManagerPeer.getInstance().
getCurrentFocusedWindow();
changeFocusedWindow(true, opposite);
return true;
}
private boolean doRestoreFocus(Component toFocus, Component vetoedComponent,
boolean clearOnFailure)
{
if (toFocus != vetoedComponent && toFocus.isShowing() && toFocus.canBeFocusOwner() &&
toFocus.requestFocus(false, CausedFocusEvent.Cause.ROLLBACK))
{
return true;
} else {
Component nextFocus = toFocus.getNextFocusCandidate();
if (nextFocus != null && nextFocus != vetoedComponent &&
nextFocus.requestFocusInWindow(CausedFocusEvent.Cause.ROLLBACK))
{
return true;
} else if (clearOnFailure) {
clearGlobalFocusOwnerPriv();
return true;
} else {
return false;
}
}
}
private boolean doRestoreFocus(Component toFocus, Component vetoedComponent,
boolean clearOnFailure)
{
if (toFocus != vetoedComponent && toFocus.isShowing() && toFocus.canBeFocusOwner() &&
toFocus.requestFocus(false, CausedFocusEvent.Cause.ROLLBACK))
{
return true;
} else {
Component nextFocus = toFocus.getNextFocusCandidate();
if (nextFocus != null && nextFocus != vetoedComponent &&
nextFocus.requestFocusInWindow(CausedFocusEvent.Cause.ROLLBACK))
{
return true;
} else if (clearOnFailure) {
clearGlobalFocusOwnerPriv();
return true;
} else {
return false;
}
}
}
@Override
public boolean requestWindowFocus(CausedFocusEvent.Cause cause) {
if (!focusAllowedFor()) {
return false;
}
if (getPlatformWindow().rejectFocusRequest(cause)) {
return false;
}
Window opposite = LWKeyboardFocusManagerPeer.getInstance().
getCurrentFocusedWindow();
changeFocusedWindow(true, opposite);
return true;
}
private boolean doRestoreFocus(Component toFocus, Component vetoedComponent,
boolean clearOnFailure)
{
boolean success = true;
if (toFocus != vetoedComponent && toFocus.isShowing() && toFocus.canBeFocusOwner() &&
(success = toFocus.requestFocus(false, CausedFocusEvent.Cause.ROLLBACK)))
{
return true;
} else {
if (!success && getGlobalFocusedWindow() != SunToolkit.getContainingWindow(toFocus)) {
restoreFocusTo = toFocus;
return true;
}
Component nextFocus = toFocus.getNextFocusCandidate();
if (nextFocus != null && nextFocus != vetoedComponent &&
nextFocus.requestFocusInWindow(CausedFocusEvent.Cause.ROLLBACK))
{
return true;
} else if (clearOnFailure) {
clearGlobalFocusOwnerPriv();
return true;
} else {
return false;
}
}
}
public static boolean deliverFocus(Component lightweightChild,
Component target,
boolean temporary,
boolean focusedWindowChangeAllowed,
long time,
CausedFocusEvent.Cause cause)
{
// TODO: do something to eliminate this forwarding
return KeyboardFocusManagerPeerImpl.deliverFocus(lightweightChild,
target,
temporary,
focusedWindowChangeAllowed,
time,
cause,
getNativeFocusOwner());
}
static FocusEvent retargetUnexpectedFocusEvent(FocusEvent fe) {
synchronized (heavyweightRequests) {
// Any other case represents a failure condition which we did
// not expect. We need to clearFocusRequestList() and patch up
// the event as best as possible.
if (removeFirstRequest()) {
return (FocusEvent)retargetFocusEvent(fe);
}
Component source = fe.getComponent();
Component opposite = fe.getOppositeComponent();
boolean temporary = false;
if (fe.getID() == FocusEvent.FOCUS_LOST &&
(opposite == null || isTemporary(opposite, source)))
{
temporary = true;
}
return new CausedFocusEvent(source, fe.getID(), temporary, opposite,
CausedFocusEvent.Cause.NATIVE_SYSTEM);
}
}
@Override
public boolean requestWindowFocus(CausedFocusEvent.Cause cause) {
if (!focusAllowedFor()) {
return false;
}
if (getPlatformWindow().rejectFocusRequest(cause)) {
return false;
}
Window opposite = LWKeyboardFocusManagerPeer.getInstance().
getCurrentFocusedWindow();
changeFocusedWindow(true, opposite);
return true;
}
private boolean doRestoreFocus(Component toFocus, Component vetoedComponent,
boolean clearOnFailure)
{
boolean success = true;
if (toFocus != vetoedComponent && toFocus.isShowing() && toFocus.canBeFocusOwner() &&
(success = toFocus.requestFocus(false, CausedFocusEvent.Cause.ROLLBACK)))
{
return true;
} else {
if (!success && getGlobalFocusedWindow() != SunToolkit.getContainingWindow(toFocus)) {
restoreFocusTo = toFocus;
return true;
}
Component nextFocus = toFocus.getNextFocusCandidate();
if (nextFocus != null && nextFocus != vetoedComponent &&
nextFocus.requestFocusInWindow(CausedFocusEvent.Cause.ROLLBACK))
{
return true;
} else if (clearOnFailure) {
clearGlobalFocusOwnerPriv();
return true;
} else {
return false;
}
}
}
HeavyweightFocusRequest(Component heavyweight, Component descendant,
boolean temporary, CausedFocusEvent.Cause cause) {
if (log.isLoggable(PlatformLogger.Level.FINE)) {
if (heavyweight == null) {
log.fine("Assertion (heavyweight != null) failed");
}
}
this.heavyweight = heavyweight;
this.lightweightRequests = new LinkedList<LightweightFocusRequest>();
addLightweightRequest(descendant, temporary, cause);
}
@Override
public boolean requestFocus
(Component lightweightChild, boolean temporary,
boolean focusedWindowChangeAllowed, long time, CausedFocusEvent.Cause cause)
{
return false;
}
public int shouldNativelyFocusHeavyweight(Component heavyweight,
Component descendant,
boolean temporary,
boolean focusedWindowChangeAllowed,
long time,
CausedFocusEvent.Cause cause)
{
return KeyboardFocusManager.shouldNativelyFocusHeavyweight(
heavyweight, descendant, temporary, focusedWindowChangeAllowed, time, cause);
}
@Override
public boolean rejectFocusRequest(CausedFocusEvent.Cause cause) {
// Cross-app activation requests are not allowed.
if (cause != CausedFocusEvent.Cause.MOUSE_EVENT &&
!target.isParentWindowActive())
{
focusLogger.fine("the embedder is inactive, so the request is rejected");
return true;
}
return false;
}
public void handleJavaMouseEvent(MouseEvent e) {
switch (e.getID()) {
case MouseEvent.MOUSE_PRESSED:
// Note that Swing requests focus in its own mouse event handler.
if (target == e.getSource() &&
!((Component)target).isFocusOwner() &&
WKeyboardFocusManagerPeer.shouldFocusOnClick((Component)target))
{
WKeyboardFocusManagerPeer.requestFocusFor((Component)target,
CausedFocusEvent.Cause.MOUSE_EVENT);
}
break;
}
}
public void handleJavaMouseEvent(MouseEvent e) {
switch (e.getID()) {
case MouseEvent.MOUSE_PRESSED:
// Note that Swing requests focus in its own mouse event handler.
if (target == e.getSource() &&
!((Component)target).isFocusOwner() &&
WKeyboardFocusManagerPeer.shouldFocusOnClick((Component)target))
{
WKeyboardFocusManagerPeer.requestFocusFor((Component)target,
CausedFocusEvent.Cause.MOUSE_EVENT);
}
break;
}
}
@Override
public boolean rejectFocusRequest(CausedFocusEvent.Cause cause) {
// Cross-app activation requests are not allowed.
if (cause != CausedFocusEvent.Cause.MOUSE_EVENT &&
!target.isParentWindowActive())
{
focusLogger.fine("the embedder is inactive, so the request is rejected");
return true;
}
return false;
}
private boolean restoreFocus(Window aWindow, Component vetoedComponent,
boolean clearOnFailure) {
restoreFocusTo = null;
Component toFocus =
KeyboardFocusManager.getMostRecentFocusOwner(aWindow);
if (toFocus != null && toFocus != vetoedComponent) {
if (getHeavyweight(aWindow) != getNativeFocusOwner()) {
// cannot restore focus synchronously
if (!toFocus.isShowing() || !toFocus.canBeFocusOwner()) {
toFocus = toFocus.getNextFocusCandidate();
}
if (toFocus != null && toFocus != vetoedComponent) {
if (!toFocus.requestFocus(false,
CausedFocusEvent.Cause.ROLLBACK)) {
restoreFocusTo = toFocus;
}
return true;
}
} else if (doRestoreFocus(toFocus, vetoedComponent, false)) {
return true;
}
}
if (clearOnFailure) {
clearGlobalFocusOwnerPriv();
return true;
} else {
return false;
}
}
@Override
public boolean rejectFocusRequest(CausedFocusEvent.Cause cause) {
// Cross-app activation requests are not allowed.
if (cause != CausedFocusEvent.Cause.MOUSE_EVENT &&
!target.isParentWindowActive())
{
focusLogger.fine("the embedder is inactive, so the request is rejected");
return true;
}
return false;
}