提交 76121946 编写于 作者: A alexp

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

Reviewed-by: peterz
上级 8ca994fa
......@@ -24,6 +24,8 @@
*/
package javax.swing;
import sun.swing.SwingUtilities2;
import java.awt.*;
import java.awt.event.*;
import java.lang.reflect.*;
......@@ -1323,8 +1325,8 @@ public class JEditorPane extends JTextComponent {
*/
public Dimension getPreferredSize() {
Dimension d = super.getPreferredSize();
if (getParent() instanceof JViewport) {
JViewport port = (JViewport)getParent();
JViewport port = SwingUtilities2.getViewport(this);
if (port != null) {
TextUI ui = getUI();
int prefWidth = d.width;
int prefHeight = d.height;
......@@ -1445,8 +1447,8 @@ public class JEditorPane extends JTextComponent {
* match its own, false otherwise
*/
public boolean getScrollableTracksViewportWidth() {
if (getParent() instanceof JViewport) {
JViewport port = (JViewport)getParent();
JViewport port = SwingUtilities2.getViewport(this);
if (port != null) {
TextUI ui = getUI();
int w = port.getWidth();
Dimension min = ui.getMinimumSize(this);
......@@ -1467,8 +1469,8 @@ public class JEditorPane extends JTextComponent {
* false otherwise
*/
public boolean getScrollableTracksViewportHeight() {
if (getParent() instanceof JViewport) {
JViewport port = (JViewport)getParent();
JViewport port = SwingUtilities2.getViewport(this);
if (port != null) {
TextUI ui = getUI();
int h = port.getHeight();
Dimension min = ui.getMinimumSize(this);
......
......@@ -495,9 +495,6 @@ public final class JLayer<V extends Component>
if (getUI() != null) {
return getUI().getScrollableTracksViewportHeight(this);
}
if (getParent() instanceof JViewport) {
return ((getParent()).getHeight() > getPreferredSize().height);
}
return false;
}
......@@ -518,9 +515,6 @@ public final class JLayer<V extends Component>
if (getUI() != null) {
return getUI().getScrollableTracksViewportWidth(this);
}
if (getParent() instanceof JViewport) {
return ((getParent()).getWidth() > getPreferredSize().width);
}
return false;
}
......
......@@ -2722,8 +2722,9 @@ public class JList extends JComponent implements Scrollable, Accessible
getVisibleRowCount() <= 0) {
return true;
}
if (getParent() instanceof JViewport) {
return (getParent().getWidth() > getPreferredSize().width);
JViewport port = SwingUtilities2.getViewport(this);
if (port != null) {
return port.getWidth() > getPreferredSize().width;
}
return false;
}
......@@ -2747,8 +2748,9 @@ public class JList extends JComponent implements Scrollable, Accessible
getVisibleRowCount() <= 0) {
return true;
}
if (getParent() instanceof JViewport) {
return (getParent().getHeight() > getPreferredSize().height);
JViewport port = SwingUtilities2.getViewport(this);
if (port != null) {
return port.getHeight() > getPreferredSize().height;
}
return false;
}
......
......@@ -718,9 +718,9 @@ public class JTable extends JComponent implements TableModelListener, Scrollable
* @see #addNotify
*/
protected void configureEnclosingScrollPane() {
Container p = getParent();
if (p instanceof JViewport) {
Container gp = p.getParent();
JViewport port = SwingUtilities2.getViewport(this);
if (port != null) {
Container gp = port.getParent();
if (gp instanceof JScrollPane) {
JScrollPane scrollPane = (JScrollPane)gp;
// Make certain we are the viewPort's view and not, for
......@@ -750,9 +750,9 @@ public class JTable extends JComponent implements TableModelListener, Scrollable
* from configureEnclosingScrollPane() and updateUI() in a safe manor.
*/
private void configureEnclosingScrollPaneUI() {
Container p = getParent();
if (p instanceof JViewport) {
Container gp = p.getParent();
JViewport port = SwingUtilities2.getViewport(this);
if (port != null) {
Container gp = port.getParent();
if (gp instanceof JScrollPane) {
JScrollPane scrollPane = (JScrollPane)gp;
// Make certain we are the viewPort's view and not, for
......@@ -819,9 +819,9 @@ public class JTable extends JComponent implements TableModelListener, Scrollable
* @since 1.3
*/
protected void unconfigureEnclosingScrollPane() {
Container p = getParent();
if (p instanceof JViewport) {
Container gp = p.getParent();
JViewport port = SwingUtilities2.getViewport(this);
if (port != null) {
Container gp = port.getParent();
if (gp instanceof JScrollPane) {
JScrollPane scrollPane = (JScrollPane)gp;
// Make certain we are the viewPort's view and not, for
......@@ -5215,9 +5215,10 @@ public class JTable extends JComponent implements TableModelListener, Scrollable
* @see #getFillsViewportHeight
*/
public boolean getScrollableTracksViewportHeight() {
JViewport port = SwingUtilities2.getViewport(this);
return getFillsViewportHeight()
&& getParent() instanceof JViewport
&& (getParent().getHeight() > getPreferredSize().height);
&& port != null
&& port.getHeight() > getPreferredSize().height;
}
/**
......
......@@ -24,6 +24,8 @@
*/
package javax.swing;
import sun.swing.SwingUtilities2;
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
......@@ -288,11 +290,7 @@ public class JTextField extends JTextComponent implements SwingConstants {
* @see JComponent#isValidateRoot
*/
public boolean isValidateRoot() {
Component parent = getParent();
if (parent instanceof JViewport) {
return false;
}
return true;
return SwingUtilities2.getViewport(this) == null;
}
......
......@@ -3498,8 +3498,9 @@ public class JTree extends JComponent implements Scrollable, Accessible
* @see Scrollable#getScrollableTracksViewportWidth
*/
public boolean getScrollableTracksViewportWidth() {
if (getParent() instanceof JViewport) {
return getParent().getWidth() > getPreferredSize().width;
JViewport port = SwingUtilities2.getViewport(this);
if (port != null) {
return port.getWidth() > getPreferredSize().width;
}
return false;
}
......@@ -3514,8 +3515,9 @@ public class JTree extends JComponent implements Scrollable, Accessible
* @see Scrollable#getScrollableTracksViewportHeight
*/
public boolean getScrollableTracksViewportHeight() {
if (getParent() instanceof JViewport) {
return getParent().getHeight() > getPreferredSize().height;
JViewport port = SwingUtilities2.getViewport(this);
if (port != null) {
return port.getHeight() > getPreferredSize().height;
}
return false;
}
......
......@@ -303,9 +303,6 @@ public class LayerUI<V extends Component>
if (l.getView() instanceof Scrollable) {
return ((Scrollable)l.getView()).getScrollableTracksViewportHeight();
}
if (l.getParent() instanceof JViewport) {
return (((JViewport)l.getParent()).getHeight() > l.getPreferredSize().height);
}
return false;
}
......@@ -322,9 +319,6 @@ public class LayerUI<V extends Component>
if (l.getView() instanceof Scrollable) {
return ((Scrollable)l.getView()).getScrollableTracksViewportWidth();
}
if (l.getParent() instanceof JViewport) {
return (((JViewport)l.getParent()).getWidth() > l.getPreferredSize().width);
}
return false;
}
......
......@@ -2069,8 +2069,9 @@ public abstract class JTextComponent extends JComponent implements Scrollable, A
* width to match its own
*/
public boolean getScrollableTracksViewportWidth() {
if (getParent() instanceof JViewport) {
return (getParent().getWidth() > getPreferredSize().width);
JViewport port = SwingUtilities2.getViewport(this);
if (port != null) {
return port.getWidth() > getPreferredSize().width;
}
return false;
}
......@@ -2089,8 +2090,9 @@ public abstract class JTextComponent extends JComponent implements Scrollable, A
* to match its own
*/
public boolean getScrollableTracksViewportHeight() {
if (getParent() instanceof JViewport) {
return (getParent().getHeight() > getPreferredSize().height);
JViewport port = SwingUtilities2.getViewport(this);
if (port != null) {
return (port.getHeight() > getPreferredSize().height);
}
return false;
}
......
......@@ -1844,4 +1844,22 @@ 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;
}
}
/*
* 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
* @summary Makes sure that JLayer is synchronizable
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册