test/jdk/javax/swing/JTextArea/TestTabSize.java
author psadhukhan
Tue, 14 Nov 2017 10:32:31 +0530
changeset 47835 dde53d789c3d
child 48063 50d61f4b5d1a
permissions -rw-r--r--
8187957: Tab Size does not work correctly in JTextArea Reviewed-by: ssadetsky, serb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
47835
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
     1
/*
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
     2
 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
     4
 *
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
     7
 * published by the Free Software Foundation.
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
     8
 *
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    13
 * accompanied this code).
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    14
 *
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    18
 *
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    21
 * questions.
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    22
 */
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    23
/*
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    24
 * @test
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    25
 * @bug 8187957
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    26
 * @summary  Verifies Tab Size works correctly in JTextArea
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    27
 * @run main TestTabSize
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    28
 */
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    29
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    30
import java.awt.geom.Rectangle2D;
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    31
import javax.swing.GroupLayout;
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    32
import javax.swing.JFrame;
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    33
import javax.swing.JScrollPane;
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    34
import javax.swing.JTextArea;
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    35
import javax.swing.SwingUtilities;
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    36
import javax.swing.text.BadLocationException;
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    37
import javax.swing.text.Caret;
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    38
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    39
public class TestTabSize {
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    40
    private static JScrollPane jScrollPane1;
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    41
    private static JTextArea jTextArea1;
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    42
    private static JFrame f;
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    43
    private static Rectangle2D rect;
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    44
    private static Rectangle2D rect1;
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    45
    private static boolean excpnthrown = false;
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    46
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    47
    public static void main(String args[]) throws Exception {
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    48
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    49
        SwingUtilities.invokeAndWait(() -> {
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    50
            try {
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    51
                jScrollPane1 = new javax.swing.JScrollPane();
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    52
                jTextArea1 = new javax.swing.JTextArea();
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    53
                jTextArea1.setTabSize(8);
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    54
                f = new JFrame();
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    55
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    56
                jTextArea1.setFont(new java.awt.Font("Monospaced", 0, 10));
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    57
                String str =
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    58
                        "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!#\n"
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    59
                        + "! Some Text\t\t\t\t\t#";
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    60
                jTextArea1.setText(str);
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    61
                jScrollPane1.setViewportView(jTextArea1);
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    62
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    63
                GroupLayout layout = new javax.swing.GroupLayout(f.getContentPane());
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    64
                f.getContentPane().setLayout(layout);
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    65
                layout.setHorizontalGroup(
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    66
                    layout.createParallelGroup(
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    67
                            javax.swing.GroupLayout.Alignment.LEADING)
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    68
                            .addGroup(layout.createSequentialGroup()
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    69
                                    .addContainerGap()
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    70
                                    .addComponent(jScrollPane1,
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    71
                                            javax.swing.GroupLayout.DEFAULT_SIZE,
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    72
                                            446, Short.MAX_VALUE)
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    73
                                    .addContainerGap())
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    74
                );
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    75
                layout.setVerticalGroup(
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    76
                        layout.createParallelGroup(
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    77
                                javax.swing.GroupLayout.Alignment.LEADING)
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    78
                                .addGroup(layout.createSequentialGroup()
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    79
                                        .addContainerGap()
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    80
                                        .addComponent(jScrollPane1)
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    81
                                        .addContainerGap())
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    82
                );
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    83
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    84
                f.pack();
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    85
                int first = str.indexOf("#");
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    86
                jTextArea1.setCaretPosition(first);
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    87
                Caret caret = jTextArea1.getCaret();
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    88
                rect = jTextArea1.modelToView2D(caret.getDot());
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    89
                System.out.println("caret x position " + rect.getX());
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    90
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    91
                jTextArea1.setCaretPosition(str.indexOf("#", first+1));
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    92
                caret = jTextArea1.getCaret();
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    93
                rect1 = jTextArea1.modelToView2D(caret.getDot());
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    94
                System.out.println("2nd caret x position " + rect1.getX());
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    95
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    96
            } catch (BadLocationException ex) {
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    97
                excpnthrown = true;
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    98
            } finally {
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
    99
                f.dispose();
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
   100
            }
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
   101
        });
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
   102
        if (excpnthrown) {
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
   103
            throw new RuntimeException("BadLocationException thrown");
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
   104
        }
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
   105
        if ((int)rect.getX() != (int)rect1.getX()) {
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
   106
            throw new RuntimeException("Tab width calculation wrong");
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
   107
        }
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
   108
    }
dde53d789c3d 8187957: Tab Size does not work correctly in JTextArea
psadhukhan
parents:
diff changeset
   109
}