changeset 4895:e00e98bc0c0e

7125044: [macosx] Test failure because Component.transferFocus() works differently in applet and application Reviewed-by: art
author ant
date Wed, 14 Mar 2012 16:49:50 +0300
parents 91ede930328c
children 54b91467ed95
files src/share/classes/java/awt/Dialog.java src/share/classes/java/awt/Frame.java src/share/classes/java/awt/Window.java src/share/classes/javax/swing/JApplet.java src/share/classes/javax/swing/JDialog.java src/share/classes/javax/swing/JFrame.java src/share/classes/javax/swing/JInternalFrame.java src/share/classes/javax/swing/JWindow.java src/share/classes/javax/swing/UIManager.java src/share/classes/sun/awt/SunToolkit.java test/java/awt/Focus/FocusTraversalPolicy/InitialFTP.java test/java/awt/Focus/FocusTraversalPolicy/InitialFTP_AWT.java test/java/awt/Focus/FocusTraversalPolicy/InitialFTP_Swing.java
diffstat 13 files changed, 158 insertions(+), 61 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/awt/Dialog.java	Wed Mar 14 12:31:25 2012 +0400
+++ b/src/share/classes/java/awt/Dialog.java	Wed Mar 14 16:49:50 2012 +0300
@@ -670,7 +670,7 @@
 
         this.title = title;
         setModalityType(modalityType);
-        SunToolkit.checkAndSetPolicy(this, false);
+        SunToolkit.checkAndSetPolicy(this);
     }
 
     /**
@@ -721,7 +721,7 @@
 
         this.title = title;
         setModalityType(modalityType);
-        SunToolkit.checkAndSetPolicy(this, false);
+        SunToolkit.checkAndSetPolicy(this);
     }
 
     /**
--- a/src/share/classes/java/awt/Frame.java	Wed Mar 14 12:31:25 2012 +0400
+++ b/src/share/classes/java/awt/Frame.java	Wed Mar 14 16:49:50 2012 +0300
@@ -449,7 +449,7 @@
 
     private void init(String title, GraphicsConfiguration gc) {
         this.title = title;
-        SunToolkit.checkAndSetPolicy(this, false);
+        SunToolkit.checkAndSetPolicy(this);
     }
 
     /**
--- a/src/share/classes/java/awt/Window.java	Wed Mar 14 12:31:25 2012 +0400
+++ b/src/share/classes/java/awt/Window.java	Wed Mar 14 16:49:50 2012 +0300
@@ -506,7 +506,7 @@
 
         modalExclusionType = Dialog.ModalExclusionType.NO_EXCLUDE;
 
-        SunToolkit.checkAndSetPolicy(this, false);
+        SunToolkit.checkAndSetPolicy(this);
     }
 
     /**
--- a/src/share/classes/javax/swing/JApplet.java	Wed Mar 14 12:31:25 2012 +0400
+++ b/src/share/classes/javax/swing/JApplet.java	Wed Mar 14 16:49:50 2012 +0300
@@ -149,7 +149,7 @@
         setRootPaneCheckingEnabled(true);
 
         setFocusTraversalPolicyProvider(true);
-        sun.awt.SunToolkit.checkAndSetPolicy(this, true);
+        sun.awt.SunToolkit.checkAndSetPolicy(this);
 
         enableEvents(AWTEvent.KEY_EVENT_MASK);
     }
--- a/src/share/classes/javax/swing/JDialog.java	Wed Mar 14 12:31:25 2012 +0400
+++ b/src/share/classes/javax/swing/JDialog.java	Wed Mar 14 16:49:50 2012 +0300
@@ -654,7 +654,7 @@
                 getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
             }
         }
-        sun.awt.SunToolkit.checkAndSetPolicy(this, true);
+        sun.awt.SunToolkit.checkAndSetPolicy(this);
     }
 
     /**
--- a/src/share/classes/javax/swing/JFrame.java	Wed Mar 14 12:31:25 2012 +0400
+++ b/src/share/classes/javax/swing/JFrame.java	Wed Mar 14 16:49:50 2012 +0300
@@ -266,7 +266,7 @@
                 getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
             }
         }
-        sun.awt.SunToolkit.checkAndSetPolicy(this, true);
+        sun.awt.SunToolkit.checkAndSetPolicy(this);
     }
 
     /**
--- a/src/share/classes/javax/swing/JInternalFrame.java	Wed Mar 14 12:31:25 2012 +0400
+++ b/src/share/classes/javax/swing/JInternalFrame.java	Wed Mar 14 16:49:50 2012 +0300
@@ -349,7 +349,7 @@
         setRootPaneCheckingEnabled(true);
         desktopIcon = new JDesktopIcon(this);
         updateUI();
-        sun.awt.SunToolkit.checkAndSetPolicy(this, true);
+        sun.awt.SunToolkit.checkAndSetPolicy(this);
         addPropertyChangeListenerIfNecessary();
     }
 
--- a/src/share/classes/javax/swing/JWindow.java	Wed Mar 14 12:31:25 2012 +0400
+++ b/src/share/classes/javax/swing/JWindow.java	Wed Mar 14 16:49:50 2012 +0300
@@ -263,7 +263,7 @@
         setLocale( JComponent.getDefaultLocale() );
         setRootPane(createRootPane());
         setRootPaneCheckingEnabled(true);
-        sun.awt.SunToolkit.checkAndSetPolicy(this, true);
+        sun.awt.SunToolkit.checkAndSetPolicy(this);
     }
 
     /**
--- a/src/share/classes/javax/swing/UIManager.java	Wed Mar 14 12:31:25 2012 +0400
+++ b/src/share/classes/javax/swing/UIManager.java	Wed Mar 14 16:49:50 2012 +0300
@@ -191,6 +191,7 @@
         private UIDefaults[] tables = new UIDefaults[2];
 
         boolean initialized = false;
+        boolean focusPolicyInitialized = false;
         MultiUIDefaults multiUIDefaults = new MultiUIDefaults(tables);
         LookAndFeel lookAndFeel;
         LookAndFeel multiLookAndFeel = null;
@@ -1000,6 +1001,7 @@
      */
     public static ComponentUI getUI(JComponent target) {
         maybeInitialize();
+        maybeInitializeFocusPolicy(target);
         ComponentUI ui = null;
         LookAndFeel multiLAF = getLAFState().multiLookAndFeel;
         if (multiLAF != null) {
@@ -1422,6 +1424,27 @@
         }
     }
 
+    /*
+     * Sets default swing focus traversal policy.
+     */
+    private static void maybeInitializeFocusPolicy(JComponent comp) {
+        // Check for JRootPane which indicates that a swing toplevel
+        // is coming, in which case a swing default focus policy
+        // should be instatiated. See 7125044.
+        if (comp instanceof JRootPane) {
+            synchronized (classLock) {
+                if (!getLAFState().focusPolicyInitialized) {
+                    getLAFState().focusPolicyInitialized = true;
+
+                    if (FocusManager.isFocusManagerEnabled()) {
+                        KeyboardFocusManager.getCurrentKeyboardFocusManager().
+                            setDefaultFocusTraversalPolicy(
+                                new LayoutFocusTraversalPolicy());
+                    }
+                }
+            }
+        }
+    }
 
     /*
      * Only called by maybeInitialize().
@@ -1433,17 +1456,6 @@
         initializeAuxiliaryLAFs(swingProps);
         initializeInstalledLAFs(swingProps);
 
-        // Enable the Swing default LayoutManager.
-        String toolkitName = Toolkit.getDefaultToolkit().getClass().getName();
-        // don't set default policy if this is XAWT.
-        if (!"sun.awt.X11.XToolkit".equals(toolkitName)) {
-            if (FocusManager.isFocusManagerEnabled()) {
-                KeyboardFocusManager.getCurrentKeyboardFocusManager().
-                    setDefaultFocusTraversalPolicy(
-                        new LayoutFocusTraversalPolicy());
-            }
-        }
-
         // Install Swing's PaintEventDispatcher
         if (RepaintManager.HANDLE_TOP_LEVEL_PAINT) {
             sun.awt.PaintEventDispatcher.setPaintEventDispatcher(
--- a/src/share/classes/sun/awt/SunToolkit.java	Wed Mar 14 12:31:25 2012 +0400
+++ b/src/share/classes/sun/awt/SunToolkit.java	Wed Mar 14 16:49:50 2012 +0300
@@ -458,48 +458,11 @@
         AWTAccessor.getWindowAccessor().setLWRequestStatus(changed, status);
     };
 
-    public static void checkAndSetPolicy(Container cont, boolean isSwingCont)
-    {
-        FocusTraversalPolicy defaultPolicy = KeyboardFocusManager
-            .getCurrentKeyboardFocusManager().getDefaultFocusTraversalPolicy();
+    public static void checkAndSetPolicy(Container cont) {
+        FocusTraversalPolicy defaultPolicy = KeyboardFocusManager.
+            getCurrentKeyboardFocusManager().
+                getDefaultFocusTraversalPolicy();
 
-        String toolkitName = Toolkit.getDefaultToolkit().getClass().getName();
-        // if this is not XAWT then use default policy
-        // because Swing change it
-        if (!"sun.awt.X11.XToolkit".equals(toolkitName)) {
-            cont.setFocusTraversalPolicy(defaultPolicy);
-            return;
-        }
-
-        String policyName = defaultPolicy.getClass().getName();
-
-        if (DefaultFocusTraversalPolicy.class != defaultPolicy.getClass()) {
-            // Policy was changed
-            // Check if it is awt policy or swing policy
-            // If it is Swing policy we shouldn't use it in AWT frames
-            // If it is AWT policy  we shouldn't use it in Swing frames
-            // Otherwise we should use this policy
-            if (policyName.startsWith("java.awt.")) {
-                // AWT
-                if (isSwingCont) {
-                    // Can't use AWT policy in Swing windows - should use Swing's one.
-                    defaultPolicy = createLayoutPolicy();
-                } else {
-                    // New awt policy.
-                }
-            } else if (policyName.startsWith("javax.swing.")) {
-                if (isSwingCont) {
-                    // New Swing's policy
-                } else {
-                    defaultPolicy = new DefaultFocusTraversalPolicy();
-                }
-            }
-        } else {
-            // Policy is default, use different default policy for swing
-            if (isSwingCont) {
-                defaultPolicy = createLayoutPolicy();
-            }
-        }
         cont.setFocusTraversalPolicy(defaultPolicy);
     }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/java/awt/Focus/FocusTraversalPolicy/InitialFTP.java	Wed Mar 14 16:49:50 2012 +0300
@@ -0,0 +1,26 @@
+/*
+  @test
+  @bug       7125044
+  @summary   Tests defaut focus traversal policy in AWT & Swing toplevel windows.
+  @author    anton.tarasov@sun.com: area=awt.focus
+  @run       main InitialFTP_AWT
+  @run       main InitialFTP_Swing
+*/
+
+import java.awt.FocusTraversalPolicy;
+import java.awt.Window;
+
+public class InitialFTP {
+    public static void test(Window win, Class<? extends FocusTraversalPolicy> expectedPolicy) {
+        FocusTraversalPolicy ftp = win.getFocusTraversalPolicy();
+
+        System.out.println("==============" + "\n" +
+                           "Tested window:    " + win + "\n" +
+                           "Expected policy:  " + expectedPolicy + "\n" +
+                           "Effective policy: " + ftp.getClass());
+
+        if (!expectedPolicy.equals(ftp.getClass())) {
+            throw new RuntimeException("Test failed: wrong effective focus policy");
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/java/awt/Focus/FocusTraversalPolicy/InitialFTP_AWT.java	Wed Mar 14 16:49:50 2012 +0300
@@ -0,0 +1,50 @@
+/*
+  @bug       7125044
+  @summary   Tests default focus traversal policy in AWT toplevel windows.
+  @author    anton.tarasov@sun.com: area=awt.focus
+*/
+
+import java.awt.Button;
+import java.awt.DefaultFocusTraversalPolicy;
+import java.awt.FlowLayout;
+import java.awt.FocusTraversalPolicy;
+import java.awt.Frame;
+import java.awt.List;
+import java.awt.TextArea;
+import java.awt.Window;
+
+public class InitialFTP_AWT {
+    public static void main(String[] args) {
+        AWTFrame f0 = new AWTFrame("frame0");
+        f0.setVisible(true);
+
+        InitialFTP.test(f0, DefaultFocusTraversalPolicy.class);
+
+        AWTFrame f1 = new AWTFrame("frame1");
+        f1.setVisible(true);
+
+        InitialFTP.test(f1, DefaultFocusTraversalPolicy.class);
+
+        System.out.println("Test passed.");
+    }
+}
+
+class AWTFrame extends Frame {
+    Button button = new Button("button");
+    TextArea text = new TextArea("qwerty");
+    List list = new List();
+
+    public AWTFrame(String title) {
+        super(title);
+
+        list.add("one");
+        list.add("two");
+        list.add("three");
+
+        this.setLayout(new FlowLayout());
+        this.add(button);
+        this.add(text);
+        this.add(list);
+        this.pack();
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/java/awt/Focus/FocusTraversalPolicy/InitialFTP_Swing.java	Wed Mar 14 16:49:50 2012 +0300
@@ -0,0 +1,46 @@
+/*
+  @bug       7125044
+  @summary   Tests default focus traversal policy in Swing toplevel windows.
+  @author    anton.tarasov@sun.com: area=awt.focus
+*/
+
+import java.awt.FlowLayout;
+import java.awt.FocusTraversalPolicy;
+import java.awt.Window;
+import javax.swing.JButton;
+import javax.swing.JFrame;
+import javax.swing.JList;
+import javax.swing.JTextArea;
+import javax.swing.LayoutFocusTraversalPolicy;
+
+public class InitialFTP_Swing {
+    public static void main(String[] args) {
+        SwingFrame f0 = new SwingFrame("frame0");
+        f0.setVisible(true);
+
+        InitialFTP.test(f0, LayoutFocusTraversalPolicy.class);
+
+        SwingFrame f1 = new SwingFrame("frame1");
+        f1.setVisible(true);
+
+        InitialFTP.test(f1, LayoutFocusTraversalPolicy.class);
+
+        System.out.println("Test passed.");
+    }
+}
+
+class SwingFrame extends JFrame {
+    JButton button = new JButton("button");
+    JTextArea text = new JTextArea("qwerty");
+    JList list = new JList(new String[] {"one", "two", "three"});
+
+    public SwingFrame(String title) {
+        super(title);
+
+        this.setLayout(new FlowLayout());
+        this.add(button);
+        this.add(text);
+        this.add(list);
+        this.pack();
+    }
+}