提交 d44afd52 编写于 作者: A alexp

6899413: Fix for CR #6878399 should be refactored

Reviewed-by: peterz
上级 2f28f2e5
...@@ -1330,8 +1330,9 @@ public class JEditorPane extends JTextComponent { ...@@ -1330,8 +1330,9 @@ public class JEditorPane extends JTextComponent {
*/ */
public Dimension getPreferredSize() { public Dimension getPreferredSize() {
Dimension d = super.getPreferredSize(); Dimension d = super.getPreferredSize();
JViewport port = SwingUtilities.getParentViewport(this); Container parent = SwingUtilities.getUnwrappedParent(this);
if (port != null) { if (parent instanceof JViewport) {
JViewport port = (JViewport) parent;
TextUI ui = getUI(); TextUI ui = getUI();
int prefWidth = d.width; int prefWidth = d.width;
int prefHeight = d.height; int prefHeight = d.height;
...@@ -1452,8 +1453,9 @@ public class JEditorPane extends JTextComponent { ...@@ -1452,8 +1453,9 @@ public class JEditorPane extends JTextComponent {
* match its own, false otherwise * match its own, false otherwise
*/ */
public boolean getScrollableTracksViewportWidth() { public boolean getScrollableTracksViewportWidth() {
JViewport port = SwingUtilities.getParentViewport(this); Container parent = SwingUtilities.getUnwrappedParent(this);
if (port != null) { if (parent instanceof JViewport) {
JViewport port = (JViewport) parent;
TextUI ui = getUI(); TextUI ui = getUI();
int w = port.getWidth(); int w = port.getWidth();
Dimension min = ui.getMinimumSize(this); Dimension min = ui.getMinimumSize(this);
...@@ -1474,8 +1476,9 @@ public class JEditorPane extends JTextComponent { ...@@ -1474,8 +1476,9 @@ public class JEditorPane extends JTextComponent {
* false otherwise * false otherwise
*/ */
public boolean getScrollableTracksViewportHeight() { public boolean getScrollableTracksViewportHeight() {
JViewport port = SwingUtilities.getParentViewport(this); Container parent = SwingUtilities.getUnwrappedParent(this);
if (port != null) { if (parent instanceof JViewport) {
JViewport port = (JViewport) parent;
TextUI ui = getUI(); TextUI ui = getUI();
int h = port.getHeight(); int h = port.getHeight();
Dimension min = ui.getMinimumSize(this); Dimension min = ui.getMinimumSize(this);
......
...@@ -25,17 +25,7 @@ ...@@ -25,17 +25,7 @@
package javax.swing; package javax.swing;
import java.awt.Color; import java.awt.*;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.awt.Insets;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.*; import java.awt.event.*;
import java.util.Vector; import java.util.Vector;
...@@ -2779,9 +2769,9 @@ public class JList<E> extends JComponent implements Scrollable, Accessible ...@@ -2779,9 +2769,9 @@ public class JList<E> extends JComponent implements Scrollable, Accessible
getVisibleRowCount() <= 0) { getVisibleRowCount() <= 0) {
return true; return true;
} }
JViewport port = SwingUtilities.getParentViewport(this); Container parent = SwingUtilities.getUnwrappedParent(this);
if (port != null) { if (parent instanceof JViewport) {
return port.getWidth() > getPreferredSize().width; return parent.getWidth() > getPreferredSize().width;
} }
return false; return false;
} }
...@@ -2805,9 +2795,9 @@ public class JList<E> extends JComponent implements Scrollable, Accessible ...@@ -2805,9 +2795,9 @@ public class JList<E> extends JComponent implements Scrollable, Accessible
getVisibleRowCount() <= 0) { getVisibleRowCount() <= 0) {
return true; return true;
} }
JViewport port = SwingUtilities.getParentViewport(this); Container parent = SwingUtilities.getUnwrappedParent(this);
if (port != null) { if (parent instanceof JViewport) {
return port.getHeight() > getPreferredSize().height; return parent.getHeight() > getPreferredSize().height;
} }
return false; return false;
} }
......
...@@ -719,8 +719,9 @@ public class JTable extends JComponent implements TableModelListener, Scrollable ...@@ -719,8 +719,9 @@ public class JTable extends JComponent implements TableModelListener, Scrollable
* @see #addNotify * @see #addNotify
*/ */
protected void configureEnclosingScrollPane() { protected void configureEnclosingScrollPane() {
JViewport port = SwingUtilities.getParentViewport(this); Container parent = SwingUtilities.getUnwrappedParent(this);
if (port != null) { if (parent instanceof JViewport) {
JViewport port = (JViewport) parent;
Container gp = port.getParent(); Container gp = port.getParent();
if (gp instanceof JScrollPane) { if (gp instanceof JScrollPane) {
JScrollPane scrollPane = (JScrollPane)gp; JScrollPane scrollPane = (JScrollPane)gp;
...@@ -752,8 +753,9 @@ public class JTable extends JComponent implements TableModelListener, Scrollable ...@@ -752,8 +753,9 @@ public class JTable extends JComponent implements TableModelListener, Scrollable
* from configureEnclosingScrollPane() and updateUI() in a safe manor. * from configureEnclosingScrollPane() and updateUI() in a safe manor.
*/ */
private void configureEnclosingScrollPaneUI() { private void configureEnclosingScrollPaneUI() {
JViewport port = SwingUtilities.getParentViewport(this); Container parent = SwingUtilities.getUnwrappedParent(this);
if (port != null) { if (parent instanceof JViewport) {
JViewport port = (JViewport) parent;
Container gp = port.getParent(); Container gp = port.getParent();
if (gp instanceof JScrollPane) { if (gp instanceof JScrollPane) {
JScrollPane scrollPane = (JScrollPane)gp; JScrollPane scrollPane = (JScrollPane)gp;
...@@ -822,8 +824,9 @@ public class JTable extends JComponent implements TableModelListener, Scrollable ...@@ -822,8 +824,9 @@ public class JTable extends JComponent implements TableModelListener, Scrollable
* @since 1.3 * @since 1.3
*/ */
protected void unconfigureEnclosingScrollPane() { protected void unconfigureEnclosingScrollPane() {
JViewport port = SwingUtilities.getParentViewport(this); Container parent = SwingUtilities.getUnwrappedParent(this);
if (port != null) { if (parent instanceof JViewport) {
JViewport port = (JViewport) parent;
Container gp = port.getParent(); Container gp = port.getParent();
if (gp instanceof JScrollPane) { if (gp instanceof JScrollPane) {
JScrollPane scrollPane = (JScrollPane)gp; JScrollPane scrollPane = (JScrollPane)gp;
...@@ -5217,10 +5220,10 @@ public class JTable extends JComponent implements TableModelListener, Scrollable ...@@ -5217,10 +5220,10 @@ public class JTable extends JComponent implements TableModelListener, Scrollable
* @see #getFillsViewportHeight * @see #getFillsViewportHeight
*/ */
public boolean getScrollableTracksViewportHeight() { public boolean getScrollableTracksViewportHeight() {
JViewport port = SwingUtilities.getParentViewport(this); Container parent = SwingUtilities.getUnwrappedParent(this);
return getFillsViewportHeight() return getFillsViewportHeight()
&& port != null && parent instanceof JViewport
&& port.getHeight() > getPreferredSize().height; && parent.getHeight() > getPreferredSize().height;
} }
/** /**
......
...@@ -292,7 +292,7 @@ public class JTextField extends JTextComponent implements SwingConstants { ...@@ -292,7 +292,7 @@ public class JTextField extends JTextComponent implements SwingConstants {
*/ */
@Override @Override
public boolean isValidateRoot() { public boolean isValidateRoot() {
return SwingUtilities.getParentViewport(this) == null; return !(SwingUtilities.getUnwrappedParent(this) instanceof JViewport);
} }
......
...@@ -3498,9 +3498,9 @@ public class JTree extends JComponent implements Scrollable, Accessible ...@@ -3498,9 +3498,9 @@ public class JTree extends JComponent implements Scrollable, Accessible
* @see Scrollable#getScrollableTracksViewportWidth * @see Scrollable#getScrollableTracksViewportWidth
*/ */
public boolean getScrollableTracksViewportWidth() { public boolean getScrollableTracksViewportWidth() {
JViewport port = SwingUtilities.getParentViewport(this); Container parent = SwingUtilities.getUnwrappedParent(this);
if (port != null) { if (parent instanceof JViewport) {
return port.getWidth() > getPreferredSize().width; return parent.getWidth() > getPreferredSize().width;
} }
return false; return false;
} }
...@@ -3515,9 +3515,9 @@ public class JTree extends JComponent implements Scrollable, Accessible ...@@ -3515,9 +3515,9 @@ public class JTree extends JComponent implements Scrollable, Accessible
* @see Scrollable#getScrollableTracksViewportHeight * @see Scrollable#getScrollableTracksViewportHeight
*/ */
public boolean getScrollableTracksViewportHeight() { public boolean getScrollableTracksViewportHeight() {
JViewport port = SwingUtilities.getParentViewport(this); Container parent = SwingUtilities.getUnwrappedParent(this);
if (port != null) { if (parent instanceof JViewport) {
return port.getHeight() > getPreferredSize().height; return parent.getHeight() > getPreferredSize().height;
} }
return false; return false;
} }
......
...@@ -1969,58 +1969,53 @@ public class SwingUtilities implements SwingConstants ...@@ -1969,58 +1969,53 @@ public class SwingUtilities implements SwingConstants
} }
/** /**
* Looks for the first ancestor of the {@code component} * Returns the first ancestor of the {@code component}
* which is not an instance of {@link JLayer}. * 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}. * @param component {@code Component} to get
* The following way of obtaining the parent {@code JViewport} * the first ancestor of, which is not a {@link JLayer} instance.
* is not recommended any more: *
* <pre> * @return the first ancestor of the {@code component}
* JViewport port = null; * which is not an instance of {@link JLayer}.
* Container parent = component.getParent(); * If such an ancestor can not be found, {@code null} is returned.
* // 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} * @throws NullPointerException if {@code component} is {@code null}
* @see JLayer
* *
* @since 1.7 * @since 1.7
*/ */
public static JViewport getParentViewport(Component component) { public static Container getUnwrappedParent(Component component) {
do { Container parent = component.getParent();
component = component.getParent(); while(parent instanceof JLayer) {
if (component instanceof JViewport) { parent = parent.getParent();
return (JViewport) component;
} }
} while(component instanceof JLayer); return parent;
return null;
} }
/** /**
* Returns the first {@code JViewport}'s descendant * Returns the first {@code JViewport}'s descendant
* which is not an instance of {@code JLayer} or {@code null}. * which is not an instance of {@code JLayer}.
* If such a descendant can not be found, {@code null} is returned.
* *
* If the {@code viewport}'s view component is not a {@code JLayer}, * If the {@code viewport}'s view component is not a {@code JLayer},
* this method is equal to {@link JViewport#getView()} * this method is equivalent to {@link JViewport#getView()}
* otherwise {@link JLayer#getView()} will be recursively tested * otherwise {@link JLayer#getView()} will be recursively
* called on all descending {@code JLayer}s.
*
* @param viewport {@code JViewport} to get the first descendant of,
* which in not a {@code JLayer} instance.
* *
* @return the first {@code JViewport}'s descendant * @return the first {@code JViewport}'s descendant
* which is not an instance of {@code JLayer} or {@code null}. * which is not an instance of {@code JLayer}.
* If such a descendant can not be found, {@code null} is returned.
* *
* @throws NullPointerException if {@code viewport} is {@code null} * @throws NullPointerException if {@code viewport} is {@code null}
* @see JViewport#getView() * @see JViewport#getView()
* @see JLayer * @see JLayer
*
* @since 1.7
*/ */
static Component getUnwrappedView(JViewport viewport) { public static Component getUnwrappedView(JViewport viewport) {
Component view = viewport.getView(); Component view = viewport.getView();
while (view instanceof JLayer) { while (view instanceof JLayer) {
view = ((JLayer)view).getView(); view = ((JLayer)view).getView();
......
...@@ -2069,9 +2069,9 @@ public abstract class JTextComponent extends JComponent implements Scrollable, A ...@@ -2069,9 +2069,9 @@ public abstract class JTextComponent extends JComponent implements Scrollable, A
* width to match its own * width to match its own
*/ */
public boolean getScrollableTracksViewportWidth() { public boolean getScrollableTracksViewportWidth() {
JViewport port = SwingUtilities.getParentViewport(this); Container parent = SwingUtilities.getUnwrappedParent(this);
if (port != null) { if (parent instanceof JViewport) {
return port.getWidth() > getPreferredSize().width; return parent.getWidth() > getPreferredSize().width;
} }
return false; return false;
} }
...@@ -2090,9 +2090,9 @@ public abstract class JTextComponent extends JComponent implements Scrollable, A ...@@ -2090,9 +2090,9 @@ public abstract class JTextComponent extends JComponent implements Scrollable, A
* to match its own * to match its own
*/ */
public boolean getScrollableTracksViewportHeight() { public boolean getScrollableTracksViewportHeight() {
JViewport port = SwingUtilities.getParentViewport(this); Container parent = SwingUtilities.getUnwrappedParent(this);
if (port != null) { if (parent instanceof JViewport) {
return (port.getHeight() > getPreferredSize().height); return parent.getHeight() > getPreferredSize().height;
} }
return false; return false;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册