1/*
2 * Copyright (c) 2014, 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/*
25 * @test
26 * @key headful
27 * @bug 8048110
28 * @summary Using tables in JTextPane leads to infinite loop in FlowLayout.layoutRow
29 * @author Dmitry Markov
30 * @run main bug8048110
31 */
32
33import javax.swing.*;
34import javax.swing.text.Element;
35import javax.swing.text.html.HTMLDocument;
36import javax.swing.text.html.HTMLEditorKit;
37import java.awt.*;
38
39public class bug8048110 {
40    private static Robot robot;
41    private static Object lock = new Object();
42    private static boolean isRealSyncPerformed = false;
43    private static final String htmlText = "<table width=\"100%\" cellpadding=\"10\" cellspacing=\"5\" align=\"center\">" +
44            "<tr><th align=\"left\" bgcolor=\"#bec3c6\">Devices</th><th align=\"left\" bgcolor=\"#bec3c6\">State</th></tr>" +
45            "<tr><td align=\"left\" bgcolor=\"#bec3c6\">PC</td><td align=\"left\" bgcolor=\"#46a055\">Ok</td></tr></table>";
46
47    public static void main(String[] args) throws Exception {
48        robot = new Robot();
49        SwingUtilities.invokeAndWait(new Runnable() {
50            @Override
51            public void run() {
52                createAndShowGUI();
53            }
54        });
55
56        Thread thread = new Thread() {
57            @Override
58            public void run() {
59                robot.waitForIdle();
60                synchronized (lock) {
61                    isRealSyncPerformed = true;
62                    lock.notifyAll();
63                }
64            }
65        };
66        thread.start();
67
68        synchronized (lock) {
69            if (!isRealSyncPerformed) {
70                lock.wait(5000);
71            }
72        }
73
74        if (!isRealSyncPerformed) {
75            throw new RuntimeException("Test Failed!");
76        }
77    }
78
79    private static void createAndShowGUI() {
80        try {
81            UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
82        } catch (Exception ex) {
83            throw new RuntimeException(ex);
84        }
85        HTMLEditorKit editorKit = new HTMLEditorKit();
86        JTextPane textPane = new JTextPane();
87        textPane.setContentType("text/html");
88        textPane.setEditorKit(editorKit);
89        textPane.setText("Initial text without table");
90
91        JFrame frame = new JFrame("bug8048110");
92        frame.getContentPane().add(textPane, BorderLayout.CENTER);
93        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
94        frame.setSize(500, 200);
95        frame.setVisible(true);
96
97        textPane.setDocument(textPane.getEditorKit().createDefaultDocument());
98        HTMLDocument htmlDocument = (HTMLDocument) textPane.getDocument();
99        Element firstParagraph = findFirstElement(textPane.getDocument().getDefaultRootElement(), "p");
100
101        try {
102            htmlDocument.setInnerHTML(firstParagraph, htmlText);
103        } catch (Exception ex) {
104            throw new RuntimeException(ex);
105        }
106    }
107
108    private static Element findFirstElement(Element e, String name) {
109        String elementName = e.getName();
110        if (elementName != null && elementName.equalsIgnoreCase(name)) {
111            return e;
112        }
113        for (int i = 0; i < e.getElementCount(); i++) {
114            Element result = findFirstElement(e.getElement(i), name);
115            if (result != null) {
116                return result;
117            }
118        }
119        return null;
120    }
121}
122
123