6878399: public SwingUtilities.getParentViewport() is required
Reviewed-by: peterz
This commit is contained in:
parent
f28a61c6d9
commit
eedc1f473a
@ -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();
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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:
|
||||
* <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;
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user