1 /*
2  * Copyright (c) 2017, 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  * @bug 8187957
27  * @summary  Verifies Tab Size works correctly in JTextArea
28  * @key headful
29  * @run main TestTabSize
30  */
31 
32 import java.awt.geom.Rectangle2D;
33 import javax.swing.GroupLayout;
34 import javax.swing.JFrame;
35 import javax.swing.JScrollPane;
36 import javax.swing.JTextArea;
37 import javax.swing.SwingUtilities;
38 import javax.swing.text.BadLocationException;
39 import javax.swing.text.Caret;
40 
41 public class TestTabSize {
42     private static JScrollPane jScrollPane1;
43     private static JTextArea jTextArea1;
44     private static JFrame f;
45     private static Rectangle2D rect;
46     private static Rectangle2D rect1;
47     private static boolean excpnthrown = false;
48 
main(String args[])49     public static void main(String args[]) throws Exception {
50 
51         SwingUtilities.invokeAndWait(() -> {
52             try {
53                 jScrollPane1 = new javax.swing.JScrollPane();
54                 jTextArea1 = new javax.swing.JTextArea();
55                 jTextArea1.setTabSize(8);
56                 f = new JFrame();
57 
58                 jTextArea1.setFont(new java.awt.Font("Monospaced", 0, 10));
59                 String str =
60                         "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!#\n"
61                         + "! Some Text\t\t\t\t\t#";
62                 jTextArea1.setText(str);
63                 jScrollPane1.setViewportView(jTextArea1);
64 
65                 GroupLayout layout = new javax.swing.GroupLayout(f.getContentPane());
66                 f.getContentPane().setLayout(layout);
67                 layout.setHorizontalGroup(
68                     layout.createParallelGroup(
69                             javax.swing.GroupLayout.Alignment.LEADING)
70                             .addGroup(layout.createSequentialGroup()
71                                     .addContainerGap()
72                                     .addComponent(jScrollPane1,
73                                             javax.swing.GroupLayout.DEFAULT_SIZE,
74                                             446, Short.MAX_VALUE)
75                                     .addContainerGap())
76                 );
77                 layout.setVerticalGroup(
78                         layout.createParallelGroup(
79                                 javax.swing.GroupLayout.Alignment.LEADING)
80                                 .addGroup(layout.createSequentialGroup()
81                                         .addContainerGap()
82                                         .addComponent(jScrollPane1)
83                                         .addContainerGap())
84                 );
85 
86                 f.pack();
87                 int first = str.indexOf("#");
88                 jTextArea1.setCaretPosition(first);
89                 Caret caret = jTextArea1.getCaret();
90                 rect = jTextArea1.modelToView2D(caret.getDot());
91                 System.out.println("caret x position " + rect.getX());
92 
93                 jTextArea1.setCaretPosition(str.indexOf("#", first+1));
94                 caret = jTextArea1.getCaret();
95                 rect1 = jTextArea1.modelToView2D(caret.getDot());
96                 System.out.println("2nd caret x position " + rect1.getX());
97 
98             } catch (BadLocationException ex) {
99                 excpnthrown = true;
100             } finally {
101                 if (f != null) {
102                     f.dispose();
103                 }
104             }
105         });
106         if (excpnthrown) {
107             throw new RuntimeException("BadLocationException thrown");
108         }
109         if ((int)rect.getX() != (int)rect1.getX()) {
110             throw new RuntimeException("Tab width calculation wrong");
111         }
112     }
113 }
114