changeset 57745:39f133168348

8226513: JEditorPane is shown with incorrect size Reviewed-by: prr, psadhukhan Contributed-by: semyon.sadetsky@oracle.com
author psadhukhan
date Mon, 19 Aug 2019 12:13:47 +0530
parents 020f8fab32e2
children e00a2d8a1016
files src/java.desktop/share/classes/javax/swing/plaf/basic/BasicTextUI.java test/jdk/javax/swing/JEditorPane/8226513/JEditorPaneLayoutTest.java
diffstat 2 files changed, 145 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicTextUI.java	Sun Aug 18 21:36:01 2019 +0100
+++ b/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicTextUI.java	Mon Aug 19 12:13:47 2019 +0530
@@ -702,6 +702,7 @@
         Document doc = editor.getDocument();
         Element elem = doc.getDefaultRootElement();
         setView(f.create(elem));
+        rootViewNeedsLayout = false;
     }
 
     /**
@@ -947,11 +948,10 @@
             if ((d.width > (i.left + i.right + caretMargin)) && (d.height > (i.top + i.bottom))) {
                 rootView.setSize(d.width - i.left - i.right -
                         caretMargin, d.height - i.top - i.bottom);
-            }
-            else if (!rootViewInitialized && (d.width <= 0 || d.height <= 0)) {
+            } if (!rootViewNeedsLayout) {
                 // Probably haven't been layed out yet, force some sort of
                 // initial sizing.
-                rootViewInitialized = true;
+                rootViewNeedsLayout = true;
                 rootView.setSize(Integer.MAX_VALUE, Integer.MAX_VALUE);
             }
             d.width = (int) Math.min((long) rootView.getPreferredSpan(View.X_AXIS) +
@@ -1403,7 +1403,7 @@
     private static final Position.Bias[] discardBias = new Position.Bias[1];
     private DefaultCaret dropCaret;
     private int caretMargin;
-    private boolean rootViewInitialized;
+    private boolean rootViewNeedsLayout;
 
     /**
      * Root view that acts as a gateway between the component
@@ -1966,6 +1966,7 @@
             // normal insert update
             Rectangle alloc = (painted) ? getVisibleEditorRect() : null;
             rootView.insertUpdate(e, alloc, rootView.getViewFactory());
+            rootViewNeedsLayout = false;
         }
 
         /**
@@ -1981,6 +1982,7 @@
         public final void removeUpdate(DocumentEvent e) {
             Rectangle alloc = (painted) ? getVisibleEditorRect() : null;
             rootView.removeUpdate(e, alloc, rootView.getViewFactory());
+            rootViewNeedsLayout = false;
         }
 
         /**
@@ -1996,6 +1998,7 @@
         public final void changedUpdate(DocumentEvent e) {
             Rectangle alloc = (painted) ? getVisibleEditorRect() : null;
             rootView.changedUpdate(e, alloc, rootView.getViewFactory());
+            rootViewNeedsLayout = false;
         }
 
         // --- LayoutManager2 methods --------------------------------
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/javax/swing/JEditorPane/8226513/JEditorPaneLayoutTest.java	Mon Aug 19 12:13:47 2019 +0530
@@ -0,0 +1,138 @@
+/*
+ * Copyright (c) 2019, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/**
+ * @test
+ * @key headful
+ * @bug 8226513
+ * @summary JEditorPane is shown with incorrect size
+ * @run main/othervm -Dsun.java2d.uiScale=1.0 JEditorPaneLayoutTest
+ */
+
+import javax.swing.JFrame;
+import javax.swing.JEditorPane;
+import javax.swing.SwingUtilities;
+import javax.swing.text.BadLocationException;
+import javax.swing.text.Document;
+import javax.swing.text.html.HTMLEditorKit;
+import java.awt.Dimension;
+import java.awt.Robot;
+
+public class JEditorPaneLayoutTest {
+
+    public static final String TEXT =
+                                "some text some text some text <br> some text";
+    static JFrame frame;
+    static JEditorPane editorPane;
+    static Dimension size1;
+    static Dimension size2;
+    static Dimension size3;
+    static Dimension size4;
+
+    public static void main(String[] args) throws Exception {
+        Robot robot = new Robot();
+
+        SwingUtilities.invokeAndWait(() -> {
+            frame = new JFrame();
+            editorPane = new JEditorPane("text/html", TEXT);
+            size1 = editorPane.getPreferredSize();
+            editorPane.setText(TEXT);
+            frame.add(editorPane);
+            frame.pack();
+            frame.setLocationRelativeTo(null);
+            frame.setVisible(true);
+        });
+
+        robot.waitForIdle();
+        robot.delay(300);
+
+        SwingUtilities.invokeAndWait(() -> {
+            size2 = editorPane.getSize();
+            frame.dispose();
+
+            frame = new JFrame();
+            editorPane = new JEditorPane("text/html", TEXT);
+            editorPane.getPreferredSize();
+            editorPane.setText(TEXT);
+            frame.add(editorPane);
+            frame.setLocationRelativeTo(null);
+            frame.setVisible(true);
+        });
+
+        robot.waitForIdle();
+        robot.delay(300);
+
+        if (!size1.equals(size2)) {
+            SwingUtilities.invokeLater(frame::dispose);
+            throw new RuntimeException("Wrong size " + size2 +
+                    " expected " + size1);
+        }
+
+        SwingUtilities.invokeAndWait(() -> {
+            editorPane.setText(TEXT);
+            frame.pack();
+            size3 = editorPane.getSize();
+            frame.dispose();
+
+            frame = new JFrame();
+            editorPane = new JEditorPane("text/html", TEXT);
+            editorPane.getPreferredSize();
+            editorPane.setSize(1, 1);
+            Document doc = new HTMLEditorKit().createDefaultDocument();
+            try {
+                doc.insertString(0, TEXT, null);
+            } catch (BadLocationException e) {
+                e.printStackTrace();
+            }
+            editorPane.setDocument(doc);
+            editorPane.setText(TEXT);
+            frame.add(editorPane);
+            frame.pack();
+            frame.setLocationRelativeTo(null);
+            frame.setVisible(true);
+        });
+
+        robot.waitForIdle();
+        robot.delay(300);
+
+        if (!size1.equals(size3)) {
+            SwingUtilities.invokeLater(frame::dispose);
+            throw new RuntimeException("Wrong size " + size3 +
+                    " expected " + size1);
+        }
+
+        SwingUtilities.invokeAndWait(() -> {
+            size4 = editorPane.getSize();
+            frame.dispose();
+        });
+
+        robot.waitForIdle();
+        robot.delay(300);
+
+        if (!size1.equals(size4)) {
+            SwingUtilities.invokeLater(frame::dispose);
+            throw new RuntimeException("Wrong size " + size4 +
+                    " expected " + size1);
+        }
+    }
+}