6917744: JScrollPane Page Up/Down keys do not handle correctly html tables with different cells contents
authorrupashka
Wed, 03 Mar 2010 20:53:35 +0300
changeset 4968 517b279d7f2b
parent 4967 da853cd502c8
child 4969 5a6e31d5ab6d
6917744: JScrollPane Page Up/Down keys do not handle correctly html tables with different cells contents Reviewed-by: peterz, alexp
jdk/src/share/classes/javax/swing/text/DefaultEditorKit.java
jdk/test/javax/swing/JEditorPane/6917744/bug6917744.java
jdk/test/javax/swing/JEditorPane/6917744/test.html
--- a/jdk/src/share/classes/javax/swing/text/DefaultEditorKit.java	Wed Mar 03 20:08:55 2010 +0300
+++ b/jdk/src/share/classes/javax/swing/text/DefaultEditorKit.java	Wed Mar 03 20:53:35 2010 +0300
@@ -1461,13 +1461,17 @@
                             // Make sure the new visible location contains
                             // the location of dot, otherwise Caret will
                             // cause an additional scroll.
-                            adjustScrollIfNecessary(target, newVis, initialY,
-                                                    newIndex);
-                            if (select) {
-                                target.moveCaretPosition(newIndex);
-                            }
-                            else {
-                                target.setCaretPosition(newIndex);
+                            int newY = getAdjustedY(target, newVis, newIndex);
+
+                            if (direction == -1 && newY <= initialY || direction == 1 && newY >= initialY) {
+                                // Change index and correct newVis.y only if won't cause scrolling upward
+                                newVis.y = newY;
+
+                                if (select) {
+                                    target.moveCaretPosition(newIndex);
+                                } else {
+                                    target.setCaretPosition(newIndex);
+                                }
                             }
                         }
                     } catch (BadLocationException ble) { }
@@ -1513,34 +1517,27 @@
         }
 
         /**
-         * Adjusts the rectangle that indicates the location to scroll to
+         * Returns adjustsed {@code y} position that indicates the location to scroll to
          * after selecting <code>index</code>.
          */
-        private void adjustScrollIfNecessary(JTextComponent text,
-                                             Rectangle visible, int initialY,
-                                             int index) {
+        private int getAdjustedY(JTextComponent text, Rectangle visible, int index) {
+            int result = visible.y;
+
             try {
                 Rectangle dotBounds = text.modelToView(index);
 
-                if (dotBounds.y < visible.y ||
-                       (dotBounds.y > (visible.y + visible.height)) ||
-                       (dotBounds.y + dotBounds.height) >
-                       (visible.y + visible.height)) {
-                    int y;
-
-                    if (dotBounds.y < visible.y) {
-                        y = dotBounds.y;
-                    }
-                    else {
-                        y = dotBounds.y + dotBounds.height - visible.height;
-                    }
-                    if ((direction == -1 && y < initialY) ||
-                                        (direction == 1 && y > initialY)) {
-                        // Only adjust if won't cause scrolling upward.
-                        visible.y = y;
+                if (dotBounds.y < visible.y) {
+                    result = dotBounds.y;
+                } else {
+                    if ((dotBounds.y > visible.y + visible.height) ||
+                            (dotBounds.y + dotBounds.height > visible.y + visible.height)) {
+                        result = dotBounds.y + dotBounds.height - visible.height;
                     }
                 }
-            } catch (BadLocationException ble) {}
+            } catch (BadLocationException ble) {
+            }
+
+            return result;
         }
 
         /**
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/javax/swing/JEditorPane/6917744/bug6917744.java	Wed Mar 03 20:53:35 2010 +0300
@@ -0,0 +1,113 @@
+/*
+ * Copyright 2010 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
+ * @bug 6917744
+ * @summary JScrollPane Page Up/Down keys do not handle correctly html tables with different cells contents
+ * @author Pavel Porvatov
+ * @run main bug6917744
+ */
+
+import java.awt.*;
+import java.awt.event.KeyEvent;
+import java.io.IOException;
+import javax.swing.*;
+
+import sun.awt.SunToolkit;
+
+public class bug6917744 {
+    private static JFrame frame;
+
+    private static JEditorPane editorPane;
+
+    private static JScrollPane scrollPane;
+
+    private static Robot robot;
+
+    public static void main(String[] args) throws Exception {
+        SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
+
+        robot = new Robot();
+        robot.setAutoDelay(100);
+
+        SwingUtilities.invokeAndWait(new Runnable() {
+            public void run() {
+                frame = new JFrame();
+
+                editorPane = new JEditorPane();
+
+                try {
+                    editorPane.setPage(bug6917744.class.getResource("/test.html"));
+                } catch (IOException e) {
+                    throw new RuntimeException("HTML resource not found", e);
+                }
+
+                scrollPane = new JScrollPane(editorPane);
+
+                frame.getContentPane().add(scrollPane);
+                frame.setSize(400, 300);
+                frame.setVisible(true);
+            }
+        });
+
+        toolkit.realSync();
+
+        for (int i = 0; i < 50; i++) {
+            robot.keyPress(KeyEvent.VK_PAGE_DOWN);
+        }
+
+        toolkit.realSync();
+
+        // Check that we at the end of document
+        SwingUtilities.invokeAndWait(new Runnable() {
+            public void run() {
+                BoundedRangeModel model = scrollPane.getVerticalScrollBar().getModel();
+
+                if (model.getValue() + model.getExtent() != model.getMaximum()) {
+                    throw new RuntimeException("Invalid HTML position");
+                }
+            }
+        });
+
+        toolkit.realSync();
+
+        for (int i = 0; i < 50; i++) {
+            robot.keyPress(KeyEvent.VK_PAGE_UP);
+        }
+
+        toolkit.realSync();
+
+        // Check that we at the begin of document
+        SwingUtilities.invokeAndWait(new Runnable() {
+            public void run() {
+                BoundedRangeModel model = scrollPane.getVerticalScrollBar().getModel();
+
+                if (model.getValue() != model.getMinimum()) {
+                    throw new RuntimeException("Invalid HTML position");
+                }
+
+                frame.dispose();
+            }
+        });
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/javax/swing/JEditorPane/6917744/test.html	Wed Mar 03 20:53:35 2010 +0300
@@ -0,0 +1,494 @@
+<html>
+<body bgcolor="#ffffff">
+<table border="0">
+<tr>
+<td>&nbsp;</td>
+<td valign="top">
+
+<font size="+1" face="Arial, Helvetica, sans-serif">
+    <b>TEST FOR JScrollPane BUG</b> <br>
+</font>
+<font face="Arial, Helvetica, sans-serif" size="-1">
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+This is a test html file.
+<br>
+<font size="+1" face="Arial, Helvetica, sans-serif">
+    <b>END OF TEST FOR JScrollPane BUG</b> <br>
+</font>
+</font>
+</td>
+<td>&nbsp;</td>
+</tr>
+</table>
+</body>
+</html>