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