diff --git a/src/share/classes/javax/swing/JEditorPane.java b/src/share/classes/javax/swing/JEditorPane.java index 4dfbf92488ea19183bbf62437b4d7899ee382e56..eaf61f1e60e33d38d1d648efe86c6d77d466ebdd 100644 --- a/src/share/classes/javax/swing/JEditorPane.java +++ b/src/share/classes/javax/swing/JEditorPane.java @@ -1330,7 +1330,7 @@ public class JEditorPane extends JTextComponent { */ 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 @@ public class JEditorPane extends JTextComponent { * 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 @@ public class JEditorPane extends JTextComponent { * 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(); diff --git a/src/share/classes/javax/swing/JList.java b/src/share/classes/javax/swing/JList.java index 796ac11ba6c91060e7f016179cae6bbef3cc9f00..6c06c5ea6827a245dddd804e8d8fc4accc3fae3c 100644 --- a/src/share/classes/javax/swing/JList.java +++ b/src/share/classes/javax/swing/JList.java @@ -2722,7 +2722,7 @@ public class JList extends JComponent implements Scrollable, Accessible 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 @@ public class JList extends JComponent implements Scrollable, Accessible getVisibleRowCount() <= 0) { return true; } - JViewport port = SwingUtilities2.getViewport(this); + JViewport port = SwingUtilities.getParentViewport(this); if (port != null) { return port.getHeight() > getPreferredSize().height; } diff --git a/src/share/classes/javax/swing/JTable.java b/src/share/classes/javax/swing/JTable.java index 28b38ce0c6a6b6fce8f7fee0821ce5f9c5b410fe..5489c380f1a143124855ca094604fc0db38e4467 100644 --- a/src/share/classes/javax/swing/JTable.java +++ b/src/share/classes/javax/swing/JTable.java @@ -719,7 +719,7 @@ public class JTable extends JComponent implements TableModelListener, Scrollable * @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 @@ public class JTable extends JComponent implements TableModelListener, Scrollable // 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 @@ public class JTable extends JComponent implements TableModelListener, Scrollable * 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 @@ public class JTable extends JComponent implements TableModelListener, Scrollable // 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 @@ public class JTable extends JComponent implements TableModelListener, Scrollable * @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 @@ public class JTable extends JComponent implements TableModelListener, Scrollable // 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 @@ public class JTable extends JComponent implements TableModelListener, Scrollable * @see #getFillsViewportHeight */ public boolean getScrollableTracksViewportHeight() { - JViewport port = SwingUtilities2.getViewport(this); + JViewport port = SwingUtilities.getParentViewport(this); return getFillsViewportHeight() && port != null && port.getHeight() > getPreferredSize().height; diff --git a/src/share/classes/javax/swing/JTextField.java b/src/share/classes/javax/swing/JTextField.java index de408914d2222c156ba4530bda43237b00b3f4af..65b80ac2674e915e66b3ee28e2a83aa21563d29c 100644 --- a/src/share/classes/javax/swing/JTextField.java +++ b/src/share/classes/javax/swing/JTextField.java @@ -290,7 +290,7 @@ public class JTextField extends JTextComponent implements SwingConstants { * @see JComponent#isValidateRoot */ public boolean isValidateRoot() { - return SwingUtilities2.getViewport(this) == null; + return SwingUtilities.getParentViewport(this) == null; } diff --git a/src/share/classes/javax/swing/JTree.java b/src/share/classes/javax/swing/JTree.java index 1b200f78591a476b0e10f1555af558fe08ae57b7..55815b81dc26ae851921d8278ff202558d97f0d7 100644 --- a/src/share/classes/javax/swing/JTree.java +++ b/src/share/classes/javax/swing/JTree.java @@ -3498,7 +3498,7 @@ public class JTree extends JComponent implements Scrollable, Accessible * @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 @@ public class JTree extends JComponent implements Scrollable, Accessible * @see Scrollable#getScrollableTracksViewportHeight */ public boolean getScrollableTracksViewportHeight() { - JViewport port = SwingUtilities2.getViewport(this); + JViewport port = SwingUtilities.getParentViewport(this); if (port != null) { return port.getHeight() > getPreferredSize().height; } diff --git a/src/share/classes/javax/swing/SwingUtilities.java b/src/share/classes/javax/swing/SwingUtilities.java index d7a26a0dd729555011a93a6ead80acf5c54f6b87..0e8a324454ec59fa974c4c46b08cd8e96c1998d6 100644 --- a/src/share/classes/javax/swing/SwingUtilities.java +++ b/src/share/classes/javax/swing/SwingUtilities.java @@ -1967,4 +1967,64 @@ public class SwingUtilities implements SwingConstants 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: + *
+     * JViewport port = null;
+     * Container parent = component.getParent();
+     * // not recommended any more
+     * if(parent instanceof JViewport) {
+     *     port = (JViewport) parent;
+     * }
+     * 
+ * Here is the way to go: + *
+     * // the correct way:
+     * JViewport port = SwingUtilities.getParentViewport(component);
+     * 
+ * @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; + } } diff --git a/src/share/classes/javax/swing/text/JTextComponent.java b/src/share/classes/javax/swing/text/JTextComponent.java index 371afda48b049020ac985b62b64e7417acf7abd9..25ddaf9e3cc4dd4e992266dc8406a04678d9fdd2 100644 --- a/src/share/classes/javax/swing/text/JTextComponent.java +++ b/src/share/classes/javax/swing/text/JTextComponent.java @@ -2069,7 +2069,7 @@ public abstract class JTextComponent extends JComponent implements Scrollable, A * 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 @@ public abstract class JTextComponent extends JComponent implements Scrollable, A * 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); } diff --git a/src/share/classes/sun/swing/SwingUtilities2.java b/src/share/classes/sun/swing/SwingUtilities2.java index 60681d56fbc951f2aa1db1105d0acdca58606f41..5b835bfec54def2784100183d5aaba3bba6d856b 100644 --- a/src/share/classes/sun/swing/SwingUtilities2.java +++ b/src/share/classes/sun/swing/SwingUtilities2.java @@ -1807,22 +1807,4 @@ public class SwingUtilities2 { 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; - } }