From e297478ca06f8feac7e2c4d3904b5c9547751e35 Mon Sep 17 00:00:00 2001 From: alexp Date: Fri, 6 Nov 2009 22:40:58 +0300 Subject: [PATCH] 6878399: public SwingUtilities.getParentViewport() is required Reviewed-by: peterz --- .../classes/javax/swing/JEditorPane.java | 6 +- src/share/classes/javax/swing/JList.java | 4 +- src/share/classes/javax/swing/JTable.java | 17 +++--- src/share/classes/javax/swing/JTextField.java | 2 +- src/share/classes/javax/swing/JTree.java | 4 +- .../classes/javax/swing/SwingUtilities.java | 60 +++++++++++++++++++ .../javax/swing/text/JTextComponent.java | 4 +- .../classes/sun/swing/SwingUtilities2.java | 18 ------ 8 files changed, 80 insertions(+), 35 deletions(-) diff --git a/src/share/classes/javax/swing/JEditorPane.java b/src/share/classes/javax/swing/JEditorPane.java index 4dfbf9248..eaf61f1e6 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 796ac11ba..6c06c5ea6 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 28b38ce0c..5489c380f 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 de408914d..65b80ac26 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 1b200f785..55815b81d 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 d7a26a0dd..0e8a32445 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 371afda48..25ddaf9e3 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 60681d56f..5b835bfec 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; - } } -- GitLab