6824395: Several Swing core components prevent using them in wrapper classes
authoralexp
Mon, 24 Aug 2009 19:22:38 +0400
changeset 3737 83fb4621a129
parent 3736 b82a1864410d
child 3738 837612df050f
6824395: Several Swing core components prevent using them in wrapper classes Reviewed-by: peterz
jdk/src/share/classes/javax/swing/JEditorPane.java
jdk/src/share/classes/javax/swing/JLayer.java
jdk/src/share/classes/javax/swing/JList.java
jdk/src/share/classes/javax/swing/JTable.java
jdk/src/share/classes/javax/swing/JTextField.java
jdk/src/share/classes/javax/swing/JTree.java
jdk/src/share/classes/javax/swing/plaf/LayerUI.java
jdk/src/share/classes/javax/swing/text/JTextComponent.java
jdk/src/share/classes/sun/swing/SwingUtilities2.java
jdk/test/javax/swing/JLayer/6824395/bug6824395.java
jdk/test/javax/swing/JLayer/SerializationTest/SerializationTest.java
--- a/jdk/src/share/classes/javax/swing/JEditorPane.java	Mon Aug 24 18:21:47 2009 +0400
+++ b/jdk/src/share/classes/javax/swing/JEditorPane.java	Mon Aug 24 19:22:38 2009 +0400
@@ -24,6 +24,8 @@
  */
 package javax.swing;
 
+import sun.swing.SwingUtilities2;
+
 import java.awt.*;
 import java.awt.event.*;
 import java.lang.reflect.*;
@@ -1323,8 +1325,8 @@
      */
     public Dimension getPreferredSize() {
         Dimension d = super.getPreferredSize();
-        if (getParent() instanceof JViewport) {
-            JViewport port = (JViewport)getParent();
+        JViewport port = SwingUtilities2.getViewport(this);
+        if (port != null) {
             TextUI ui = getUI();
             int prefWidth = d.width;
             int prefHeight = d.height;
@@ -1445,8 +1447,8 @@
      * match its own, false otherwise
      */
     public boolean getScrollableTracksViewportWidth() {
-        if (getParent() instanceof JViewport) {
-            JViewport port = (JViewport)getParent();
+        JViewport port = SwingUtilities2.getViewport(this);
+        if (port != null) {
             TextUI ui = getUI();
             int w = port.getWidth();
             Dimension min = ui.getMinimumSize(this);
@@ -1467,8 +1469,8 @@
      *          false otherwise
      */
     public boolean getScrollableTracksViewportHeight() {
-        if (getParent() instanceof JViewport) {
-            JViewport port = (JViewport)getParent();
+        JViewport port = SwingUtilities2.getViewport(this);
+        if (port != null) {
             TextUI ui = getUI();
             int h = port.getHeight();
             Dimension min = ui.getMinimumSize(this);
--- a/jdk/src/share/classes/javax/swing/JLayer.java	Mon Aug 24 18:21:47 2009 +0400
+++ b/jdk/src/share/classes/javax/swing/JLayer.java	Mon Aug 24 19:22:38 2009 +0400
@@ -495,9 +495,6 @@
         if (getUI() != null) {
             return getUI().getScrollableTracksViewportHeight(this);
         }
-        if (getParent() instanceof JViewport) {
-            return ((getParent()).getHeight() > getPreferredSize().height);
-        }
         return false;
     }
 
@@ -518,9 +515,6 @@
         if (getUI() != null) {
             return getUI().getScrollableTracksViewportWidth(this);
         }
-        if (getParent() instanceof JViewport) {
-            return ((getParent()).getWidth() > getPreferredSize().width);
-        }
         return false;
     }
 
--- a/jdk/src/share/classes/javax/swing/JList.java	Mon Aug 24 18:21:47 2009 +0400
+++ b/jdk/src/share/classes/javax/swing/JList.java	Mon Aug 24 19:22:38 2009 +0400
@@ -2722,8 +2722,9 @@
                                       getVisibleRowCount() <= 0) {
             return true;
         }
-        if (getParent() instanceof JViewport) {
-            return (getParent().getWidth() > getPreferredSize().width);
+        JViewport port = SwingUtilities2.getViewport(this);
+        if (port != null) {
+            return port.getWidth() > getPreferredSize().width;
         }
         return false;
     }
@@ -2747,8 +2748,9 @@
                      getVisibleRowCount() <= 0) {
             return true;
         }
-        if (getParent() instanceof JViewport) {
-            return (getParent().getHeight() > getPreferredSize().height);
+        JViewport port = SwingUtilities2.getViewport(this);
+        if (port != null) {
+            return port.getHeight() > getPreferredSize().height;
         }
         return false;
     }
--- a/jdk/src/share/classes/javax/swing/JTable.java	Mon Aug 24 18:21:47 2009 +0400
+++ b/jdk/src/share/classes/javax/swing/JTable.java	Mon Aug 24 19:22:38 2009 +0400
@@ -718,9 +718,9 @@
      * @see #addNotify
      */
     protected void configureEnclosingScrollPane() {
-        Container p = getParent();
-        if (p instanceof JViewport) {
-            Container gp = p.getParent();
+        JViewport port = SwingUtilities2.getViewport(this);
+        if (port != null) {
+            Container gp = port.getParent();
             if (gp instanceof JScrollPane) {
                 JScrollPane scrollPane = (JScrollPane)gp;
                 // Make certain we are the viewPort's view and not, for
@@ -750,9 +750,9 @@
      * from configureEnclosingScrollPane() and updateUI() in a safe manor.
      */
     private void configureEnclosingScrollPaneUI() {
-        Container p = getParent();
-        if (p instanceof JViewport) {
-            Container gp = p.getParent();
+        JViewport port = SwingUtilities2.getViewport(this);
+        if (port != null) {
+            Container gp = port.getParent();
             if (gp instanceof JScrollPane) {
                 JScrollPane scrollPane = (JScrollPane)gp;
                 // Make certain we are the viewPort's view and not, for
@@ -819,9 +819,9 @@
      * @since 1.3
      */
     protected void unconfigureEnclosingScrollPane() {
-        Container p = getParent();
-        if (p instanceof JViewport) {
-            Container gp = p.getParent();
+        JViewport port = SwingUtilities2.getViewport(this);
+        if (port != null) {
+            Container gp = port.getParent();
             if (gp instanceof JScrollPane) {
                 JScrollPane scrollPane = (JScrollPane)gp;
                 // Make certain we are the viewPort's view and not, for
@@ -5215,9 +5215,10 @@
      * @see #getFillsViewportHeight
      */
     public boolean getScrollableTracksViewportHeight() {
+        JViewport port = SwingUtilities2.getViewport(this);
         return getFillsViewportHeight()
-               && getParent() instanceof JViewport
-               && (getParent().getHeight() > getPreferredSize().height);
+               && port != null
+               && port.getHeight() > getPreferredSize().height;
     }
 
     /**
--- a/jdk/src/share/classes/javax/swing/JTextField.java	Mon Aug 24 18:21:47 2009 +0400
+++ b/jdk/src/share/classes/javax/swing/JTextField.java	Mon Aug 24 19:22:38 2009 +0400
@@ -24,6 +24,8 @@
  */
 package javax.swing;
 
+import sun.swing.SwingUtilities2;
+
 import java.awt.*;
 import java.awt.event.*;
 import java.beans.*;
@@ -288,11 +290,7 @@
      * @see JComponent#isValidateRoot
      */
     public boolean isValidateRoot() {
-        Component parent = getParent();
-        if (parent instanceof JViewport) {
-            return false;
-        }
-        return true;
+        return SwingUtilities2.getViewport(this) == null;
     }
 
 
--- a/jdk/src/share/classes/javax/swing/JTree.java	Mon Aug 24 18:21:47 2009 +0400
+++ b/jdk/src/share/classes/javax/swing/JTree.java	Mon Aug 24 19:22:38 2009 +0400
@@ -3498,8 +3498,9 @@
      * @see Scrollable#getScrollableTracksViewportWidth
      */
     public boolean getScrollableTracksViewportWidth() {
-        if (getParent() instanceof JViewport) {
-            return getParent().getWidth() > getPreferredSize().width;
+        JViewport port = SwingUtilities2.getViewport(this);
+        if (port != null) {
+            return port.getWidth() > getPreferredSize().width;
         }
         return false;
     }
@@ -3514,8 +3515,9 @@
      * @see Scrollable#getScrollableTracksViewportHeight
      */
     public boolean getScrollableTracksViewportHeight() {
-        if (getParent() instanceof JViewport) {
-            return getParent().getHeight() > getPreferredSize().height;
+        JViewport port = SwingUtilities2.getViewport(this);
+        if (port != null) {
+            return port.getHeight() > getPreferredSize().height;
         }
         return false;
     }
--- a/jdk/src/share/classes/javax/swing/plaf/LayerUI.java	Mon Aug 24 18:21:47 2009 +0400
+++ b/jdk/src/share/classes/javax/swing/plaf/LayerUI.java	Mon Aug 24 19:22:38 2009 +0400
@@ -303,9 +303,6 @@
         if (l.getView() instanceof Scrollable) {
             return ((Scrollable)l.getView()).getScrollableTracksViewportHeight();
         }
-        if (l.getParent() instanceof JViewport) {
-            return (((JViewport)l.getParent()).getHeight() > l.getPreferredSize().height);
-        }
         return false;
     }
 
@@ -322,9 +319,6 @@
         if (l.getView() instanceof Scrollable) {
             return ((Scrollable)l.getView()).getScrollableTracksViewportWidth();
         }
-        if (l.getParent() instanceof JViewport) {
-            return (((JViewport)l.getParent()).getWidth() > l.getPreferredSize().width);
-        }
         return false;
     }
 
--- a/jdk/src/share/classes/javax/swing/text/JTextComponent.java	Mon Aug 24 18:21:47 2009 +0400
+++ b/jdk/src/share/classes/javax/swing/text/JTextComponent.java	Mon Aug 24 19:22:38 2009 +0400
@@ -2069,8 +2069,9 @@
      *   width to match its own
      */
     public boolean getScrollableTracksViewportWidth() {
-        if (getParent() instanceof JViewport) {
-            return (getParent().getWidth() > getPreferredSize().width);
+        JViewport port = SwingUtilities2.getViewport(this);
+        if (port != null) {
+            return port.getWidth() > getPreferredSize().width;
         }
         return false;
     }
@@ -2089,8 +2090,9 @@
      *   to match its own
      */
     public boolean getScrollableTracksViewportHeight() {
-        if (getParent() instanceof JViewport) {
-            return (getParent().getHeight() > getPreferredSize().height);
+        JViewport port = SwingUtilities2.getViewport(this);
+        if (port != null) {
+            return (port.getHeight() > getPreferredSize().height);
         }
         return false;
     }
--- a/jdk/src/share/classes/sun/swing/SwingUtilities2.java	Mon Aug 24 18:21:47 2009 +0400
+++ b/jdk/src/share/classes/sun/swing/SwingUtilities2.java	Mon Aug 24 19:22:38 2009 +0400
@@ -1844,4 +1844,22 @@
                                          boolean three) {
         return liesIn(rect, p, false, false, three);
     }
+
+    /**
+     * Returns the {@code JViewport} instance for the {@code component}
+     * or {@code null}.
+     *
+     * @return the {@code JViewport} instance for the {@code component}
+     * or {@code null}
+     * @throws NullPointerException if {@code component} is {@code null}
+     */
+    public static JViewport getViewport(Component component) {
+        do {
+            component = component.getParent();
+            if (component instanceof JViewport) {
+                return (JViewport) component;
+            }
+        } while(component instanceof JLayer);
+        return null;
+    }
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/javax/swing/JLayer/6824395/bug6824395.java	Mon Aug 24 19:22:38 2009 +0400
@@ -0,0 +1,80 @@
+/*
+ * Copyright 2009 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+ /*
+ * @test
+ * @summary Checks that JLayer inside JViewport works is correctly laid out
+ * @author Alexander Potochkin
+ * @run main bug6824395
+ */
+
+
+import sun.awt.SunToolkit;
+
+import javax.swing.*;
+import javax.swing.plaf.LayerUI;
+import java.awt.*;
+
+public class bug6824395 {
+
+    static JScrollPane scrollPane;
+
+    public static void main(String[] args) throws Exception {
+        SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
+        SwingUtilities.invokeAndWait(new Runnable() {
+            public void run() {
+                JFrame frame = new JFrame("testing");
+                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+                JEditorPane editorPane = new JEditorPane();
+                String str = "hello\n";
+                for(int i = 0; i<5; i++) {
+                    str += str;
+                }
+
+                editorPane.setText(str);
+
+                JLayer<JEditorPane> editorPaneLayer = new JLayer<JEditorPane>(editorPane);
+                LayerUI<JComponent> layerUI = new LayerUI<JComponent>();
+                editorPaneLayer.setUI(layerUI);
+
+                scrollPane = new JScrollPane(editorPaneLayer);
+
+                scrollPane.setPreferredSize(new Dimension(200, 250));
+                frame.add(scrollPane);
+
+                frame.setSize(200, 200);
+                frame.pack();
+                frame.setVisible(true);
+            }
+        });
+        toolkit.realSync();
+        SwingUtilities.invokeAndWait(new Runnable() {
+            public void run() {
+                if (scrollPane.getViewportBorderBounds().width != scrollPane.getViewport().getView().getWidth()) {
+                    throw new RuntimeException("Wrong component's width!");
+                }
+            }
+        });
+    }
+}
--- a/jdk/test/javax/swing/JLayer/SerializationTest/SerializationTest.java	Mon Aug 24 18:21:47 2009 +0400
+++ b/jdk/test/javax/swing/JLayer/SerializationTest/SerializationTest.java	Mon Aug 24 19:22:38 2009 +0400
@@ -1,3 +1,26 @@
+/*
+ * Copyright 2008 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
 /*
  * @test
  * @summary Makes sure that JLayer is synchronizable
@@ -50,4 +73,4 @@
             return "TestLayerUI";
         }
     }
-}
\ No newline at end of file
+}