summaryrefslogtreecommitdiffstats
path: root/libjava/classpath/java/awt/DefaultKeyboardFocusManager.java
diff options
context:
space:
mode:
Diffstat (limited to 'libjava/classpath/java/awt/DefaultKeyboardFocusManager.java')
-rw-r--r--libjava/classpath/java/awt/DefaultKeyboardFocusManager.java140
1 files changed, 101 insertions, 39 deletions
diff --git a/libjava/classpath/java/awt/DefaultKeyboardFocusManager.java b/libjava/classpath/java/awt/DefaultKeyboardFocusManager.java
index 037cb834c40..9fea99b7839 100644
--- a/libjava/classpath/java/awt/DefaultKeyboardFocusManager.java
+++ b/libjava/classpath/java/awt/DefaultKeyboardFocusManager.java
@@ -163,7 +163,13 @@ public class DefaultKeyboardFocusManager extends KeyboardFocusManager
if (e.id == WindowEvent.WINDOW_ACTIVATED)
setGlobalActiveWindow (target);
else if (e.id == WindowEvent.WINDOW_GAINED_FOCUS)
- setGlobalFocusedWindow (target);
+ {
+ setGlobalFocusedWindow (target);
+ FocusTraversalPolicy p = target.getFocusTraversalPolicy();
+ Component toFocus = p.getInitialComponent(target);
+ if (toFocus != null)
+ toFocus.requestFocusInWindow();
+ }
else if (e.id != WindowEvent.WINDOW_LOST_FOCUS
&& e.id != WindowEvent.WINDOW_DEACTIVATED)
return false;
@@ -173,51 +179,18 @@ public class DefaultKeyboardFocusManager extends KeyboardFocusManager
}
else if (e instanceof FocusEvent)
{
- Component target = (Component) e.getSource ();
+ FocusEvent fe = (FocusEvent) e;
+ Component target = fe.getComponent ();
+ boolean retval = false;
if (e.id == FocusEvent.FOCUS_GAINED)
{
- if (! (target instanceof Window))
- {
- if (((FocusEvent) e).isTemporary ())
- setGlobalFocusOwner (target);
- else
- setGlobalPermanentFocusOwner (target);
- }
-
- // Keep track of this window's focus owner.
-
- // Find the target Component's top-level ancestor. target
- // may be a window.
- Container parent = target.getParent ();
-
- while (parent != null
- && !(parent instanceof Window))
- parent = parent.getParent ();
-
- // If the parent is null and target is not a window, then target is an
- // unanchored component and so we don't want to set the focus owner.
- if (! (parent == null && ! (target instanceof Window)))
- {
- Window toplevel = parent == null ?
- (Window) target : (Window) parent;
-
- Component focusOwner = getFocusOwner ();
- if (focusOwner != null
- && ! (focusOwner instanceof Window))
- toplevel.setFocusOwner (focusOwner);
- }
+ retval = handleFocusGained(fe);
}
else if (e.id == FocusEvent.FOCUS_LOST)
{
- if (((FocusEvent) e).isTemporary ())
- setGlobalFocusOwner (null);
- else
- setGlobalPermanentFocusOwner (null);
+ retval = handleFocusLost(fe);
}
-
- redispatchEvent(target, e);
-
return true;
}
else if (e instanceof KeyEvent)
@@ -256,6 +229,95 @@ public class DefaultKeyboardFocusManager extends KeyboardFocusManager
return false;
}
+ /**
+ * Handles FOCUS_GAINED events in {@link #dispatchEvent(AWTEvent)}.
+ *
+ * @param fe the focus event
+ */
+ private boolean handleFocusGained(FocusEvent fe)
+ {
+ Component target = fe.getComponent ();
+
+ // If old focus owner != new focus owner, notify old focus
+ // owner that it has lost focus.
+ Component oldFocusOwner = getGlobalFocusOwner();
+ if (oldFocusOwner != null && oldFocusOwner != target)
+ {
+ FocusEvent lost = new FocusEvent(oldFocusOwner,
+ FocusEvent.FOCUS_LOST,
+ fe.isTemporary(), target);
+ oldFocusOwner.dispatchEvent(lost);
+ }
+
+ setGlobalFocusOwner (target);
+ if (target != getGlobalFocusOwner())
+ {
+ // Focus transfer was rejected, like when the target is not
+ // focusable.
+ dequeueKeyEvents(-1, target);
+ // FIXME: Restore focus somehow.
+ }
+ else
+ {
+ if (! fe.isTemporary())
+ {
+ setGlobalPermanentFocusOwner (target);
+ if (target != getGlobalPermanentFocusOwner())
+ {
+ // Focus transfer was rejected, like when the target is not
+ // focusable.
+ dequeueKeyEvents(-1, target);
+ // FIXME: Restore focus somehow.
+ }
+ else
+ {
+ redispatchEvent(target, fe);
+ }
+ }
+ }
+
+ return true;
+ }
+
+ /**
+ * Handles FOCUS_LOST events for {@link #dispatchEvent(AWTEvent)}.
+ *
+ * @param fe the focus event
+ *
+ * @return if the event has been handled
+ */
+ private boolean handleFocusLost(FocusEvent fe)
+ {
+ Component currentFocus = getGlobalFocusOwner();
+ if (currentFocus != fe.getOppositeComponent())
+ {
+ setGlobalFocusOwner(null);
+ if (getGlobalFocusOwner() != null)
+ {
+ // TODO: Is this possible? If so, then we should try to restore
+ // the focus.
+ }
+ else
+ {
+ if (! fe.isTemporary())
+ {
+ setGlobalPermanentFocusOwner(null);
+ if (getGlobalPermanentFocusOwner() != null)
+ {
+ // TODO: Is this possible? If so, then we should try to
+ // restore the focus.
+ }
+ else
+ {
+ fe.setSource(currentFocus);
+ redispatchEvent(currentFocus, fe);
+ }
+ }
+ }
+ }
+ return true;
+ }
+
private boolean enqueueKeyEvent (KeyEvent e)
{
Iterator i = delayRequests.iterator ();
OpenPOWER on IntegriCloud