提交 76121946 编写于 作者: A alexp

6824395: Several Swing core components prevent using them in wrapper classes

Reviewed-by: peterz
上级 8ca994fa
...@@ -24,6 +24,8 @@ ...@@ -24,6 +24,8 @@
*/ */
package javax.swing; package javax.swing;
import sun.swing.SwingUtilities2;
import java.awt.*; import java.awt.*;
import java.awt.event.*; import java.awt.event.*;
import java.lang.reflect.*; import java.lang.reflect.*;
...@@ -1323,8 +1325,8 @@ public class JEditorPane extends JTextComponent { ...@@ -1323,8 +1325,8 @@ public class JEditorPane extends JTextComponent {
*/ */
public Dimension getPreferredSize() { public Dimension getPreferredSize() {
Dimension d = super.getPreferredSize(); Dimension d = super.getPreferredSize();
if (getParent() instanceof JViewport) { JViewport port = SwingUtilities2.getViewport(this);
JViewport port = (JViewport)getParent(); if (port != null) {
TextUI ui = getUI(); TextUI ui = getUI();
int prefWidth = d.width; int prefWidth = d.width;
int prefHeight = d.height; int prefHeight = d.height;
...@@ -1445,8 +1447,8 @@ public class JEditorPane extends JTextComponent { ...@@ -1445,8 +1447,8 @@ public class JEditorPane extends JTextComponent {
* match its own, false otherwise * match its own, false otherwise
*/ */
public boolean getScrollableTracksViewportWidth() { public boolean getScrollableTracksViewportWidth() {
if (getParent() instanceof JViewport) { JViewport port = SwingUtilities2.getViewport(this);
JViewport port = (JViewport)getParent(); if (port != null) {
TextUI ui = getUI(); TextUI ui = getUI();
int w = port.getWidth(); int w = port.getWidth();
Dimension min = ui.getMinimumSize(this); Dimension min = ui.getMinimumSize(this);
...@@ -1467,8 +1469,8 @@ public class JEditorPane extends JTextComponent { ...@@ -1467,8 +1469,8 @@ public class JEditorPane extends JTextComponent {
* false otherwise * false otherwise
*/ */
public boolean getScrollableTracksViewportHeight() { public boolean getScrollableTracksViewportHeight() {
if (getParent() instanceof JViewport) { JViewport port = SwingUtilities2.getViewport(this);
JViewport port = (JViewport)getParent(); if (port != null) {
TextUI ui = getUI(); TextUI ui = getUI();
int h = port.getHeight(); int h = port.getHeight();
Dimension min = ui.getMinimumSize(this); Dimension min = ui.getMinimumSize(this);
......
...@@ -495,9 +495,6 @@ public final class JLayer<V extends Component> ...@@ -495,9 +495,6 @@ public final class JLayer<V extends Component>
if (getUI() != null) { if (getUI() != null) {
return getUI().getScrollableTracksViewportHeight(this); return getUI().getScrollableTracksViewportHeight(this);
} }
if (getParent() instanceof JViewport) {
return ((getParent()).getHeight() > getPreferredSize().height);
}
return false; return false;
} }
...@@ -518,9 +515,6 @@ public final class JLayer<V extends Component> ...@@ -518,9 +515,6 @@ public final class JLayer<V extends Component>
if (getUI() != null) { if (getUI() != null) {
return getUI().getScrollableTracksViewportWidth(this); return getUI().getScrollableTracksViewportWidth(this);
} }
if (getParent() instanceof JViewport) {
return ((getParent()).getWidth() > getPreferredSize().width);
}
return false; return false;
} }
......
...@@ -2722,8 +2722,9 @@ public class JList extends JComponent implements Scrollable, Accessible ...@@ -2722,8 +2722,9 @@ public class JList extends JComponent implements Scrollable, Accessible
getVisibleRowCount() <= 0) { getVisibleRowCount() <= 0) {
return true; return true;
} }
if (getParent() instanceof JViewport) { JViewport port = SwingUtilities2.getViewport(this);
return (getParent().getWidth() > getPreferredSize().width); if (port != null) {
return port.getWidth() > getPreferredSize().width;
} }
return false; return false;
} }
...@@ -2747,8 +2748,9 @@ public class JList extends JComponent implements Scrollable, Accessible ...@@ -2747,8 +2748,9 @@ public class JList extends JComponent implements Scrollable, Accessible
getVisibleRowCount() <= 0) { getVisibleRowCount() <= 0) {
return true; return true;
} }
if (getParent() instanceof JViewport) { JViewport port = SwingUtilities2.getViewport(this);
return (getParent().getHeight() > getPreferredSize().height); if (port != null) {
return port.getHeight() > getPreferredSize().height;
} }
return false; return false;
} }
......
...@@ -718,9 +718,9 @@ public class JTable extends JComponent implements TableModelListener, Scrollable ...@@ -718,9 +718,9 @@ public class JTable extends JComponent implements TableModelListener, Scrollable
* @see #addNotify * @see #addNotify
*/ */
protected void configureEnclosingScrollPane() { protected void configureEnclosingScrollPane() {
Container p = getParent(); JViewport port = SwingUtilities2.getViewport(this);
if (p instanceof JViewport) { if (port != null) {
Container gp = p.getParent(); Container gp = port.getParent();
if (gp instanceof JScrollPane) { if (gp instanceof JScrollPane) {
JScrollPane scrollPane = (JScrollPane)gp; JScrollPane scrollPane = (JScrollPane)gp;
// Make certain we are the viewPort's view and not, for // Make certain we are the viewPort's view and not, for
...@@ -750,9 +750,9 @@ public class JTable extends JComponent implements TableModelListener, Scrollable ...@@ -750,9 +750,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() {
Container p = getParent(); JViewport port = SwingUtilities2.getViewport(this);
if (p instanceof JViewport) { if (port != null) {
Container gp = p.getParent(); Container gp = port.getParent();
if (gp instanceof JScrollPane) { if (gp instanceof JScrollPane) {
JScrollPane scrollPane = (JScrollPane)gp; JScrollPane scrollPane = (JScrollPane)gp;
// Make certain we are the viewPort's view and not, for // Make certain we are the viewPort's view and not, for
...@@ -819,9 +819,9 @@ public class JTable extends JComponent implements TableModelListener, Scrollable ...@@ -819,9 +819,9 @@ public class JTable extends JComponent implements TableModelListener, Scrollable
* @since 1.3 * @since 1.3
*/ */
protected void unconfigureEnclosingScrollPane() { protected void unconfigureEnclosingScrollPane() {
Container p = getParent(); JViewport port = SwingUtilities2.getViewport(this);
if (p instanceof JViewport) { if (port != null) {
Container gp = p.getParent(); Container gp = port.getParent();
if (gp instanceof JScrollPane) { if (gp instanceof JScrollPane) {
JScrollPane scrollPane = (JScrollPane)gp; JScrollPane scrollPane = (JScrollPane)gp;
// Make certain we are the viewPort's view and not, for // Make certain we are the viewPort's view and not, for
...@@ -5215,9 +5215,10 @@ public class JTable extends JComponent implements TableModelListener, Scrollable ...@@ -5215,9 +5215,10 @@ public class JTable extends JComponent implements TableModelListener, Scrollable
* @see #getFillsViewportHeight * @see #getFillsViewportHeight
*/ */
public boolean getScrollableTracksViewportHeight() { public boolean getScrollableTracksViewportHeight() {
JViewport port = SwingUtilities2.getViewport(this);
return getFillsViewportHeight() return getFillsViewportHeight()
&& getParent() instanceof JViewport && port != null
&& (getParent().getHeight() > getPreferredSize().height); && port.getHeight() > getPreferredSize().height;
} }
/** /**
......
...@@ -24,6 +24,8 @@ ...@@ -24,6 +24,8 @@
*/ */
package javax.swing; package javax.swing;
import sun.swing.SwingUtilities2;
import java.awt.*; import java.awt.*;
import java.awt.event.*; import java.awt.event.*;
import java.beans.*; import java.beans.*;
...@@ -288,11 +290,7 @@ public class JTextField extends JTextComponent implements SwingConstants { ...@@ -288,11 +290,7 @@ public class JTextField extends JTextComponent implements SwingConstants {
* @see JComponent#isValidateRoot * @see JComponent#isValidateRoot
*/ */
public boolean isValidateRoot() { public boolean isValidateRoot() {
Component parent = getParent(); return SwingUtilities2.getViewport(this) == null;
if (parent instanceof JViewport) {
return false;
}
return true;
} }
......
...@@ -3498,8 +3498,9 @@ public class JTree extends JComponent implements Scrollable, Accessible ...@@ -3498,8 +3498,9 @@ public class JTree extends JComponent implements Scrollable, Accessible
* @see Scrollable#getScrollableTracksViewportWidth * @see Scrollable#getScrollableTracksViewportWidth
*/ */
public boolean getScrollableTracksViewportWidth() { public boolean getScrollableTracksViewportWidth() {
if (getParent() instanceof JViewport) { JViewport port = SwingUtilities2.getViewport(this);
return getParent().getWidth() > getPreferredSize().width; if (port != null) {
return port.getWidth() > getPreferredSize().width;
} }
return false; return false;
} }
...@@ -3514,8 +3515,9 @@ public class JTree extends JComponent implements Scrollable, Accessible ...@@ -3514,8 +3515,9 @@ public class JTree extends JComponent implements Scrollable, Accessible
* @see Scrollable#getScrollableTracksViewportHeight * @see Scrollable#getScrollableTracksViewportHeight
*/ */
public boolean getScrollableTracksViewportHeight() { public boolean getScrollableTracksViewportHeight() {
if (getParent() instanceof JViewport) { JViewport port = SwingUtilities2.getViewport(this);
return getParent().getHeight() > getPreferredSize().height; if (port != null) {
return port.getHeight() > getPreferredSize().height;
} }
return false; return false;
} }
......
...@@ -303,9 +303,6 @@ public class LayerUI<V extends Component> ...@@ -303,9 +303,6 @@ public class LayerUI<V extends Component>
if (l.getView() instanceof Scrollable) { if (l.getView() instanceof Scrollable) {
return ((Scrollable)l.getView()).getScrollableTracksViewportHeight(); return ((Scrollable)l.getView()).getScrollableTracksViewportHeight();
} }
if (l.getParent() instanceof JViewport) {
return (((JViewport)l.getParent()).getHeight() > l.getPreferredSize().height);
}
return false; return false;
} }
...@@ -322,9 +319,6 @@ public class LayerUI<V extends Component> ...@@ -322,9 +319,6 @@ public class LayerUI<V extends Component>
if (l.getView() instanceof Scrollable) { if (l.getView() instanceof Scrollable) {
return ((Scrollable)l.getView()).getScrollableTracksViewportWidth(); return ((Scrollable)l.getView()).getScrollableTracksViewportWidth();
} }
if (l.getParent() instanceof JViewport) {
return (((JViewport)l.getParent()).getWidth() > l.getPreferredSize().width);
}
return false; return false;
} }
......
...@@ -2069,8 +2069,9 @@ public abstract class JTextComponent extends JComponent implements Scrollable, A ...@@ -2069,8 +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() {
if (getParent() instanceof JViewport) { JViewport port = SwingUtilities2.getViewport(this);
return (getParent().getWidth() > getPreferredSize().width); if (port != null) {
return port.getWidth() > getPreferredSize().width;
} }
return false; return false;
} }
...@@ -2089,8 +2090,9 @@ public abstract class JTextComponent extends JComponent implements Scrollable, A ...@@ -2089,8 +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() {
if (getParent() instanceof JViewport) { JViewport port = SwingUtilities2.getViewport(this);
return (getParent().getHeight() > getPreferredSize().height); if (port != null) {
return (port.getHeight() > getPreferredSize().height);
} }
return false; return false;
} }
......
...@@ -1844,4 +1844,22 @@ public class SwingUtilities2 { ...@@ -1844,4 +1844,22 @@ public class SwingUtilities2 {
boolean three) { boolean three) {
return liesIn(rect, p, false, false, 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;
}
} }
/*
* Copyright 2009 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
/*
* @test
* @summary Checks that JLayer inside JViewport works is correctly laid out
* @author Alexander Potochkin
* @run main bug6824395
*/
import sun.awt.SunToolkit;
import javax.swing.*;
import javax.swing.plaf.LayerUI;
import java.awt.*;
public class bug6824395 {
static JScrollPane scrollPane;
public static void main(String[] args) throws Exception {
SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
JFrame frame = new JFrame("testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JEditorPane editorPane = new JEditorPane();
String str = "hello\n";
for(int i = 0; i<5; i++) {
str += str;
}
editorPane.setText(str);
JLayer<JEditorPane> editorPaneLayer = new JLayer<JEditorPane>(editorPane);
LayerUI<JComponent> layerUI = new LayerUI<JComponent>();
editorPaneLayer.setUI(layerUI);
scrollPane = new JScrollPane(editorPaneLayer);
scrollPane.setPreferredSize(new Dimension(200, 250));
frame.add(scrollPane);
frame.setSize(200, 200);
frame.pack();
frame.setVisible(true);
}
});
toolkit.realSync();
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
if (scrollPane.getViewportBorderBounds().width != scrollPane.getViewport().getView().getWidth()) {
throw new RuntimeException("Wrong component's width!");
}
}
});
}
}
/*
* Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
/* /*
* @test * @test
* @summary Makes sure that JLayer is synchronizable * @summary Makes sure that JLayer is synchronizable
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册