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