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