Path: blob/master/test/jdk/javax/swing/JTextArea/TestTabSize.java
41152 views
/*1* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 818795726* @summary Verifies Tab Size works correctly in JTextArea27* @key headful28* @run main TestTabSize29*/3031import java.awt.geom.Rectangle2D;32import javax.swing.GroupLayout;33import javax.swing.JFrame;34import javax.swing.JScrollPane;35import javax.swing.JTextArea;36import javax.swing.SwingUtilities;37import javax.swing.text.BadLocationException;38import javax.swing.text.Caret;3940public class TestTabSize {41private static JScrollPane jScrollPane1;42private static JTextArea jTextArea1;43private static JFrame f;44private static Rectangle2D rect;45private static Rectangle2D rect1;46private static boolean excpnthrown = false;4748public static void main(String args[]) throws Exception {4950SwingUtilities.invokeAndWait(() -> {51try {52jScrollPane1 = new javax.swing.JScrollPane();53jTextArea1 = new javax.swing.JTextArea();54jTextArea1.setTabSize(8);55f = new JFrame();5657jTextArea1.setFont(new java.awt.Font("Monospaced", 0, 10));58String str =59"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!#\n"60+ "! Some Text\t\t\t\t\t#";61jTextArea1.setText(str);62jScrollPane1.setViewportView(jTextArea1);6364GroupLayout layout = new javax.swing.GroupLayout(f.getContentPane());65f.getContentPane().setLayout(layout);66layout.setHorizontalGroup(67layout.createParallelGroup(68javax.swing.GroupLayout.Alignment.LEADING)69.addGroup(layout.createSequentialGroup()70.addContainerGap()71.addComponent(jScrollPane1,72javax.swing.GroupLayout.DEFAULT_SIZE,73446, Short.MAX_VALUE)74.addContainerGap())75);76layout.setVerticalGroup(77layout.createParallelGroup(78javax.swing.GroupLayout.Alignment.LEADING)79.addGroup(layout.createSequentialGroup()80.addContainerGap()81.addComponent(jScrollPane1)82.addContainerGap())83);8485f.pack();86int first = str.indexOf("#");87jTextArea1.setCaretPosition(first);88Caret caret = jTextArea1.getCaret();89rect = jTextArea1.modelToView2D(caret.getDot());90System.out.println("caret x position " + rect.getX());9192jTextArea1.setCaretPosition(str.indexOf("#", first+1));93caret = jTextArea1.getCaret();94rect1 = jTextArea1.modelToView2D(caret.getDot());95System.out.println("2nd caret x position " + rect1.getX());9697} catch (BadLocationException ex) {98excpnthrown = true;99} finally {100if (f != null) {101f.dispose();102}103}104});105if (excpnthrown) {106throw new RuntimeException("BadLocationException thrown");107}108if ((int)rect.getX() != (int)rect1.getX()) {109throw new RuntimeException("Tab width calculation wrong");110}111}112}113114115