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 8058120
  26  * @summary Rendering / caret errors with HTMLDocument
  27  * @author Dmitry Markov
  28  * @run main bug8058120
  29  */
  30 
  31 import sun.awt.SunToolkit;
  32 
  33 import javax.swing.*;
  34 import javax.swing.text.Element;
  35 import javax.swing.text.html.HTML;
  36 import javax.swing.text.html.HTMLDocument;
  37 import javax.swing.text.html.HTMLEditorKit;
  38 import java.awt.*;
  39 
  40 public class bug8058120 {
  41     private static SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
  42     private static HTMLDocument document = null;
  43     private static final String text = "<p id = 'ab'>ab</p>";
  44     private static final String textToInsert = "c";
  45 
  46     public static void main(String[] args) {
  47         SwingUtilities.invokeLater(new Runnable() {
  48             @Override
  49             public void run() {
  50                 createAndShowGUI();
  51             }
  52         });
  53 
  54         toolkit.realSync();
  55 
  56         SwingUtilities.invokeLater(new Runnable() {
  57             @Override
  58             public void run() {
  59                 try {
  60                     document.insertAfterEnd(document.getElement("ab"), textToInsert);
  61                 } catch (Exception ex) {
  62                     throw new RuntimeException(ex);
  63                 }
  64             }
  65         });
  66 
  67         toolkit.realSync();
  68 
  69         SwingUtilities.invokeLater(new Runnable() {
  70             @Override
  71             public void run() {
  72                 Element parent = document.getElement("ab").getParentElement();
  73                 int count = parent.getElementCount();
  74                 if (count != 2) {
  75                     throw new RuntimeException("Test Failed! Unexpected Element count = "+count);
  76                 }
  77                 Element insertedElement = parent.getElement(count - 1);
  78                 if (!HTML.Tag.IMPLIED.toString().equals(insertedElement.getName())) {
  79                     throw new RuntimeException("Test Failed! Inserted text is not wrapped by " + HTML.Tag.IMPLIED + " tag");
  80                 }
  81             }
  82         });
  83     }
  84 
  85     private static void createAndShowGUI() {
  86         try {
  87             UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
  88         } catch (Exception ex) {
  89             throw new RuntimeException(ex);
  90         }
  91 
  92         JFrame frame = new JFrame("bug8058120");
  93         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  94 
  95         JEditorPane editorPane = new JEditorPane();
  96         editorPane.setContentType("text/html");
  97         editorPane.setEditorKit(new HTMLEditorKit());
  98 
  99         document = (HTMLDocument) editorPane.getDocument();
 100 
 101         editorPane.setText(text);
 102 
 103         frame.add(editorPane);
 104         frame.setSize(200, 200);
 105         frame.setVisible(true);
 106     }
 107 }
 108 
 109