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