Path: blob/master/test/jdk/javax/swing/JTextArea/TestTabSizeWithLineWrap.java
41149 views
/*1* Copyright (c) 2020, 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 823933426* @summary Verifies Tab Size works correctly in JTextArea with setLineWrap on27* @key headful28* @run main TestTabSizeWithLineWrap29*/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 TestTabSizeWithLineWrap {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 JScrollPane();53jTextArea1 = new JTextArea();54jTextArea1.setTabSize(8);55f = new JFrame();5657jTextArea1.setFont(new java.awt.Font("Monospaced", 0, 10));58jTextArea1.setLineWrap( true );59String str =60"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!#\n"61+ "! Some Text\t\t\t\t\t#";62jTextArea1.setText(str);63jScrollPane1.setViewportView(jTextArea1);6465GroupLayout layout = new javax.swing.GroupLayout(f.getContentPane());66f.getContentPane().setLayout(layout);67layout.setHorizontalGroup(68layout.createParallelGroup(69javax.swing.GroupLayout.Alignment.LEADING)70.addGroup(layout.createSequentialGroup()71.addContainerGap()72.addComponent(jScrollPane1,73javax.swing.GroupLayout.DEFAULT_SIZE,74446, Short.MAX_VALUE)75.addContainerGap())76);77layout.setVerticalGroup(78layout.createParallelGroup(79javax.swing.GroupLayout.Alignment.LEADING)80.addGroup(layout.createSequentialGroup()81.addContainerGap()82.addComponent(jScrollPane1)83.addContainerGap())84);8586f.pack();87int first = str.indexOf("#");88jTextArea1.setCaretPosition(first);89Caret caret = jTextArea1.getCaret();90rect = jTextArea1.modelToView2D(caret.getDot());91System.out.println("caret x position " + rect.getX());9293jTextArea1.setCaretPosition(str.indexOf("#", first+1));94caret = jTextArea1.getCaret();95rect1 = jTextArea1.modelToView2D(caret.getDot());96System.out.println("2nd caret x position " + rect1.getX());9798} catch (BadLocationException ex) {99excpnthrown = true;100} finally {101if (f != null) {102f.dispose();103}104}105});106if (excpnthrown) {107throw new RuntimeException("BadLocationException thrown");108}109if ((int)rect.getX() != (int)rect1.getX()) {110throw new RuntimeException("Tab width calculation wrong");111}112}113}114115116