1 /*
   2  * Copyright (c) 2015, 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 8133108
  26    @summary [PIT] Container size is wrong in JEditorPane
  27    @author Semyon Sadetsky
  28   */
  29 
  30 import javax.swing.*;
  31 import javax.swing.text.BadLocationException;
  32 import javax.swing.text.SimpleAttributeSet;
  33 import javax.swing.text.html.CSS;
  34 import java.awt.*;
  35 
  36 public class JTextPaneDocumentWrapping {
  37 
  38     private static JFrame frame;
  39     private static JTextPane jTextPane;
  40     private static int position;
  41 
  42     public static void main(String[] args) throws Exception{
  43         SwingUtilities.invokeAndWait(new Runnable() {
  44             @Override
  45             public void run() {
  46                 frame = new JFrame();
  47                 frame.setUndecorated(true);
  48                 frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  49                 frame.setSize(200, 200);
  50                 jTextPane = new JTextPane();
  51                 jTextPane.setContentType("text/html");
  52                 jTextPane.setText(
  53                         "<html><body><b id='test'>Test Test Test Test Test Test " +
  54                                 "Test Test Test Test Test Test Test Test Test Test " +
  55                                 "Test Test Test Test Test Test Test Test Test Test" +
  56                                 "</b></body></html>");
  57                 frame.getContentPane().add(jTextPane);
  58                 frame.setVisible(true);
  59             }
  60         });
  61         Robot robot = new Robot();
  62         robot.waitForIdle();
  63         robot.delay(200);
  64 
  65         SwingUtilities.invokeAndWait(new Runnable() {
  66             @Override
  67             public void run() {
  68                 try {
  69                     position = jTextPane.modelToView(100).y;
  70                     SimpleAttributeSet wrap = new SimpleAttributeSet();
  71                     wrap.addAttribute(CSS.Attribute.WHITE_SPACE, "nowrap");
  72                     jTextPane.getStyledDocument()
  73                             .setParagraphAttributes(0, 10, wrap, true);
  74                 } catch (BadLocationException e) {
  75                     e.printStackTrace();
  76                 }
  77             }
  78         });
  79         if(position < 40) {
  80             throw  new RuntimeException("Text is not wrapped " + position);
  81         }
  82         robot.waitForIdle();
  83         robot.delay(200);
  84         SwingUtilities.invokeAndWait(new Runnable() {
  85             @Override
  86             public void run() {
  87                 try {
  88                     position = jTextPane.modelToView(100).y;
  89                 } catch (BadLocationException e) {
  90                     e.printStackTrace();
  91                 }
  92                 frame.dispose();
  93             }
  94         });
  95         if(position > 20) {
  96             throw  new RuntimeException("Text is wrapped " + position);
  97         }
  98         System.out.println("ok");
  99 
 100     }
 101 }