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 8132136
  26    @summary [PIT] RTL orientation in JEditorPane is broken
  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.StyleConstants;
  34 import java.awt.*;
  35 
  36 public class JTextPaneDocumentAlignment {
  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</b></body></html>");
  54                 SimpleAttributeSet right = new SimpleAttributeSet();
  55                 StyleConstants.setAlignment(right, StyleConstants.ALIGN_RIGHT);
  56                 jTextPane.getStyledDocument()
  57                         .setParagraphAttributes(0, 10, right, true);
  58                 frame.getContentPane().add(jTextPane);
  59                 frame.setVisible(true);
  60            }
  61         });
  62         Robot robot = new Robot();
  63         robot.waitForIdle();
  64         robot.delay(200);
  65         SwingUtilities.invokeAndWait(new Runnable() {
  66             @Override
  67             public void run() {
  68                 try {
  69                     position = jTextPane.modelToView(1).x;
  70                     SimpleAttributeSet center = new SimpleAttributeSet();
  71                     StyleConstants.setAlignment(center,
  72                             StyleConstants.ALIGN_CENTER);
  73                     jTextPane.getStyledDocument()
  74                             .setParagraphAttributes(0, 10, center, true);
  75                 } catch (BadLocationException e) {
  76                     e.printStackTrace();
  77                 }
  78             }
  79         });
  80         if(position < 100) {
  81             throw  new RuntimeException("Text is not right aligned " + position);
  82         }
  83         SwingUtilities.invokeAndWait(new Runnable() {
  84             @Override
  85             public void run() {
  86                 try {
  87                     position = jTextPane.modelToView(1).x;
  88                 } catch (BadLocationException e) {
  89                     e.printStackTrace();
  90                 }
  91                 frame.dispose();
  92             }
  93         });
  94         if(position < 20) {
  95             throw  new RuntimeException("Text is not center aligned " + position);
  96         }
  97         System.out.println("ok");
  98     }
  99 }