|
| 1 | +/* |
| 2 | + * Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | + |
| 24 | +import java.awt.Robot; |
| 25 | +import java.awt.Shape; |
| 26 | +import javax.swing.JEditorPane; |
| 27 | +import javax.swing.JFrame; |
| 28 | +import javax.swing.JTextPane; |
| 29 | +import javax.swing.SwingUtilities; |
| 30 | +import javax.swing.text.View; |
| 31 | + |
| 32 | +/* |
| 33 | + * @test |
| 34 | + * @bug 4764897 |
| 35 | + * @key headful |
| 36 | + * @summary Tests if text and borders in HTML table doesn't run over right edge |
| 37 | + */ |
| 38 | + |
| 39 | +public class bug4764897 { |
| 40 | + private static JEditorPane pane; |
| 41 | + private static JFrame frame; |
| 42 | + private static volatile boolean passed = false; |
| 43 | + |
| 44 | + public static void main(String[] args) throws Exception { |
| 45 | + try { |
| 46 | + Robot robot = new Robot(); |
| 47 | + |
| 48 | + SwingUtilities.invokeAndWait(bug4764897::createAndShowUI); |
| 49 | + robot.waitForIdle(); |
| 50 | + robot.delay(500); |
| 51 | + |
| 52 | + SwingUtilities.invokeAndWait(bug4764897::testUI); |
| 53 | + |
| 54 | + if (!passed) { |
| 55 | + throw new RuntimeException("Test failed!!" + |
| 56 | + " Text and Borders of HTML table run over the right edge"); |
| 57 | + } |
| 58 | + } finally { |
| 59 | + SwingUtilities.invokeAndWait(() -> { |
| 60 | + if (frame != null) { |
| 61 | + frame.dispose(); |
| 62 | + } |
| 63 | + }); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + public static void createAndShowUI() { |
| 68 | + pane = new JTextPane(); |
| 69 | + pane.setContentType("text/html"); |
| 70 | + pane.setText("<html>" |
| 71 | + + "<html><head><table border>" |
| 72 | + + "<tr><td width=20%>one</td><td width=20%> </td>" |
| 73 | + + "<td width=20%>ThisIsAnExtraWideWord</td><td width=40%>" |
| 74 | + + "This is text that won't get displayed correctly.</td></tr>" |
| 75 | + + "</table></body></html>"); |
| 76 | + |
| 77 | + frame = new JFrame("Table Border & Text Test"); |
| 78 | + frame.getContentPane().add(pane); |
| 79 | + frame.setSize(600, 200); |
| 80 | + frame.setVisible(true); |
| 81 | + } |
| 82 | + |
| 83 | + private static void testUI() { |
| 84 | + Shape r = pane.getBounds(); |
| 85 | + View v = pane.getUI().getRootView(pane); |
| 86 | + int tableWidth = 0; |
| 87 | + int cellsWidth = 0; |
| 88 | + while (!(v instanceof javax.swing.text.html.ParagraphView)) { |
| 89 | + int n = v.getViewCount(); |
| 90 | + Shape sh = v.getChildAllocation(n - 1, r); |
| 91 | + String viewName = v.getClass().getName(); |
| 92 | + if (viewName.endsWith("TableView")) { |
| 93 | + tableWidth = r.getBounds().width; |
| 94 | + } |
| 95 | + |
| 96 | + if (viewName.endsWith("CellView")) { |
| 97 | + cellsWidth = r.getBounds().x + r.getBounds().width; |
| 98 | + } |
| 99 | + |
| 100 | + v = v.getView(n - 1); |
| 101 | + if (sh != null) { |
| 102 | + r = sh; |
| 103 | + } |
| 104 | + } |
| 105 | + passed = cellsWidth <= tableWidth; |
| 106 | + } |
| 107 | +} |
0 commit comments