6878399: public SwingUtilities.getParentViewport() is required
authoralexp
Fri, 06 Nov 2009 22:40:58 +0300
changeset 4278 b7a976422d27
parent 4277 9b3f8c82d5c0
child 4279 67bbd3f37f62
6878399: public SwingUtilities.getParentViewport() is required Reviewed-by: peterz
jdk/src/share/classes/javax/swing/JEditorPane.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/SwingUtilities.java
jdk/src/share/classes/javax/swing/text/JTextComponent.java
jdk/src/share/classes/sun/swing/SwingUtilities2.java
--- a/jdk/src/share/classes/javax/swing/JEditorPane.java	Fri Nov 06 22:33:40 2009 +0300
+++ b/jdk/src/share/classes/javax/swing/JEditorPane.java	Fri Nov 06 22:40:58 2009 +0300
@@ -1330,7 +1330,7 @@
      */
     public Dimension getPreferredSize() {
         Dimension d = super.getPreferredSize();
-        JViewport port = SwingUtilities2.getViewport(this);
+        JViewport port = SwingUtilities.getParentViewport(this);
         if (port != null) {
             TextUI ui = getUI();
             int prefWidth = d.width;
@@ -1452,7 +1452,7 @@
      * match its own, false otherwise
      */
     public boolean getScrollableTracksViewportWidth() {
-        JViewport port = SwingUtilities2.getViewport(this);
+        JViewport port = SwingUtilities.getParentViewport(this);
         if (port != null) {
             TextUI ui = getUI();
             int w = port.getWidth();
@@ -1474,7 +1474,7 @@
      *          false otherwise
      */
     public boolean getScrollableTracksViewportHeight() {
-        JViewport port = SwingUtilities2.getViewport(this);
+        JViewport port = SwingUtilities.getParentViewport(this);
         if (port != null) {
             TextUI ui = getUI();
             int h = port.getHeight();
--- a/jdk/src/share/classes/javax/swing/JList.java	Fri Nov 06 22:33:40 2009 +0300
+++ b/jdk/src/share/classes/javax/swing/JList.java	Fri Nov 06 22:40:58 2009 +0300
@@ -2722,7 +2722,7 @@
                                       getVisibleRowCount() <= 0) {
             return true;
         }
-        JViewport port = SwingUtilities2.getViewport(this);
+        JViewport port = SwingUtilities.getParentViewport(this);
         if (port != null) {
             return port.getWidth() > getPreferredSize().width;
         }
@@ -2748,7 +2748,7 @@
                      getVisibleRowCount() <= 0) {
             return true;
         }
-        JViewport port = SwingUtilities2.getViewport(this);
+        JViewport port = SwingUtilities.getParentViewport(this);
         if (port != null) {
             return port.getHeight() > getPreferredSize().height;
         }
--- a/jdk/src/share/classes/javax/swing/JTable.java	Fri Nov 06 22:33:40 2009 +0300
+++ b/jdk/src/share/classes/javax/swing/JTable.java	Fri Nov 06 22:40:58 2009 +0300
@@ -719,7 +719,7 @@
      * @see #addNotify
      */
     protected void configureEnclosingScrollPane() {
-        JViewport port = SwingUtilities2.getViewport(this);
+        JViewport port = SwingUtilities.getParentViewport(this);
         if (port != null) {
             Container gp = port.getParent();
             if (gp instanceof JScrollPane) {
@@ -728,7 +728,8 @@
                 // example, the rowHeaderView of the scrollPane -
                 // an implementor of fixed columns might do this.
                 JViewport viewport = scrollPane.getViewport();
-                if (viewport == null || viewport.getView() != this) {
+                if (viewport == null ||
+                        SwingUtilities.getUnwrappedView(viewport) != this) {
                     return;
                 }
                 scrollPane.setColumnHeaderView(getTableHeader());
@@ -751,7 +752,7 @@
      * from configureEnclosingScrollPane() and updateUI() in a safe manor.
      */
     private void configureEnclosingScrollPaneUI() {
-        JViewport port = SwingUtilities2.getViewport(this);
+        JViewport port = SwingUtilities.getParentViewport(this);
         if (port != null) {
             Container gp = port.getParent();
             if (gp instanceof JScrollPane) {
@@ -760,7 +761,8 @@
                 // example, the rowHeaderView of the scrollPane -
                 // an implementor of fixed columns might do this.
                 JViewport viewport = scrollPane.getViewport();
-                if (viewport == null || viewport.getView() != this) {
+                if (viewport == null ||
+                        SwingUtilities.getUnwrappedView(viewport) != this) {
                     return;
                 }
                 //  scrollPane.getViewport().setBackingStoreEnabled(true);
@@ -820,7 +822,7 @@
      * @since 1.3
      */
     protected void unconfigureEnclosingScrollPane() {
-        JViewport port = SwingUtilities2.getViewport(this);
+        JViewport port = SwingUtilities.getParentViewport(this);
         if (port != null) {
             Container gp = port.getParent();
             if (gp instanceof JScrollPane) {
@@ -829,7 +831,8 @@
                 // example, the rowHeaderView of the scrollPane -
                 // an implementor of fixed columns might do this.
                 JViewport viewport = scrollPane.getViewport();
-                if (viewport == null || viewport.getView() != this) {
+                if (viewport == null ||
+                        SwingUtilities.getUnwrappedView(viewport) != this) {
                     return;
                 }
                 scrollPane.setColumnHeaderView(null);
@@ -5216,7 +5219,7 @@
      * @see #getFillsViewportHeight
      */
     public boolean getScrollableTracksViewportHeight() {
-        JViewport port = SwingUtilities2.getViewport(this);
+        JViewport port = SwingUtilities.getParentViewport(this);
         return getFillsViewportHeight()
                && port != null
                && port.getHeight() > getPreferredSize().height;
--- a/jdk/src/share/classes/javax/swing/JTextField.java	Fri Nov 06 22:33:40 2009 +0300
+++ b/jdk/src/share/classes/javax/swing/JTextField.java	Fri Nov 06 22:40:58 2009 +0300
@@ -290,7 +290,7 @@
      * @see JComponent#isValidateRoot
      */
     public boolean isValidateRoot() {
-        return SwingUtilities2.getViewport(this) == null;
+        return SwingUtilities.getParentViewport(this) == null;
     }
 
 
--- a/jdk/src/share/classes/javax/swing/JTree.java	Fri Nov 06 22:33:40 2009 +0300
+++ b/jdk/src/share/classes/javax/swing/JTree.java	Fri Nov 06 22:40:58 2009 +0300
@@ -3498,7 +3498,7 @@
      * @see Scrollable#getScrollableTracksViewportWidth
      */
     public boolean getScrollableTracksViewportWidth() {
-        JViewport port = SwingUtilities2.getViewport(this);
+        JViewport port = SwingUtilities.getParentViewport(this);
         if (port != null) {
             return port.getWidth() > getPreferredSize().width;
         }
@@ -3515,7 +3515,7 @@
      * @see Scrollable#getScrollableTracksViewportHeight
      */
     public boolean getScrollableTracksViewportHeight() {
-        JViewport port = SwingUtilities2.getViewport(this);
+        JViewport port = SwingUtilities.getParentViewport(this);
         if (port != null) {
             return port.getHeight() > getPreferredSize().height;
         }
--- a/jdk/src/share/classes/javax/swing/SwingUtilities.java	Fri Nov 06 22:33:40 2009 +0300
+++ b/jdk/src/share/classes/javax/swing/SwingUtilities.java	Fri Nov 06 22:40:58 2009 +0300
@@ -1967,4 +1967,64 @@
             SwingUtilities.updateComponentTreeUI(component);
         }
     }
+
+    /**
+     * Looks for the first ancestor of the {@code component}
+     * which is not an instance of {@link JLayer}.
+     * If this ancestor is an instance of {@code JViewport},
+     * this {@code JViewport} is returned, otherwise returns {@code null}.
+     * The following way of obtaining the parent {@code JViewport}
+     * is not recommended any more:
+     * <pre>
+     * JViewport port = null;
+     * Container parent = component.getParent();
+     * // not recommended any more
+     * if(parent instanceof JViewport) {
+     *     port = (JViewport) parent;
+     * }
+     * </pre>
+     * Here is the way to go:
+     * <pre>
+     * // the correct way:
+     * JViewport port = SwingUtilities.getParentViewport(component);
+     * </pre>
+     * @param component {@code Component} to get the parent {@code JViewport} of.
+     * @return the {@code JViewport} instance for the {@code component}
+     * or {@code null}
+     * @throws NullPointerException if {@code component} is {@code null}
+     *
+     * @since 1.7
+     */
+    public static JViewport getParentViewport(Component component) {
+        do {
+            component = component.getParent();
+            if (component instanceof JViewport) {
+                return (JViewport) component;
+            }
+        } while(component instanceof JLayer);
+        return null;
+    }
+
+    /**
+     * Returns the first {@code JViewport}'s descendant
+     * which is not an instance of {@code JLayer} or {@code null}.
+     *
+     * If the {@code viewport}'s view component is not a {@code JLayer},
+     * this method is equal to {@link JViewport#getView()}
+     * otherwise {@link JLayer#getView()} will be recursively tested
+     *
+     * @return the first {@code JViewport}'s descendant
+     * which is not an instance of {@code JLayer} or {@code null}.
+     *
+     * @throws NullPointerException if {@code viewport} is {@code null}
+     * @see JViewport#getView()
+     * @see JLayer
+     */
+    static Component getUnwrappedView(JViewport viewport) {
+        Component view = viewport.getView();
+        while (view instanceof JLayer) {
+            view = ((JLayer)view).getView();
+        }
+        return view;
+    }
 }
--- a/jdk/src/share/classes/javax/swing/text/JTextComponent.java	Fri Nov 06 22:33:40 2009 +0300
+++ b/jdk/src/share/classes/javax/swing/text/JTextComponent.java	Fri Nov 06 22:40:58 2009 +0300
@@ -2069,7 +2069,7 @@
      *   width to match its own
      */
     public boolean getScrollableTracksViewportWidth() {
-        JViewport port = SwingUtilities2.getViewport(this);
+        JViewport port = SwingUtilities.getParentViewport(this);
         if (port != null) {
             return port.getWidth() > getPreferredSize().width;
         }
@@ -2090,7 +2090,7 @@
      *   to match its own
      */
     public boolean getScrollableTracksViewportHeight() {
-        JViewport port = SwingUtilities2.getViewport(this);
+        JViewport port = SwingUtilities.getParentViewport(this);
         if (port != null) {
             return (port.getHeight() > getPreferredSize().height);
         }
--- a/jdk/src/share/classes/sun/swing/SwingUtilities2.java	Fri Nov 06 22:33:40 2009 +0300
+++ b/jdk/src/share/classes/sun/swing/SwingUtilities2.java	Fri Nov 06 22:40:58 2009 +0300
@@ -1807,22 +1807,4 @@
                                          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;
-    }
 }