/* * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ /* * @test * @bug 8187957 * @summary Verifies Tab Size works correctly in JTextArea * @run main/manual TestTabSize */ import java.awt.BorderLayout; import java.awt.FlowLayout; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import javax.swing.GroupLayout; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.SwingUtilities; public class TestTabSize { public static void main(String[] args) throws Exception { final CountDownLatch latch = new CountDownLatch(1); TabTest test = new TabTest(latch); Thread T1 = new Thread(test); T1.start(); // wait for latch to complete boolean ret = false; try { ret = latch.await(30, TimeUnit.SECONDS); } catch (InterruptedException ie) { throw ie; } if (!ret) { test.dispose(); throw new RuntimeException(" User has not executed the test"); } if (test.testResult == false) { throw new RuntimeException("TabSize not correct, alignemt issue"); } } } class TabTest implements Runnable { static JFrame f; static JDialog dialog; public boolean testResult = false; private final CountDownLatch latch; private JScrollPane jScrollPane1; private JTextArea jTextArea1; public TabTest(CountDownLatch latch) throws Exception { this.latch = latch; } @Override public void run() { try { createUI(); tabTest(); } catch (Exception ex) { dispose(); latch.countDown(); throw new RuntimeException("createUI Failed: " + ex.getMessage()); } } public void dispose() { if (dialog != null) { dialog.dispose(); } if (f != null) { f.dispose(); } } private void tabTest() throws Exception { f = new JFrame(""); jScrollPane1 = new JScrollPane(); jTextArea1 = new JTextArea(); f.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jTextArea1.setFont(new java.awt.Font("Monospaced", 0, 10)); jTextArea1.setText("#################################################\n#" + " Some Text\t\t\t\t\t#\n# Some Other Much Longer Text\t\t\t#\n" + "#################################################"); jScrollPane1.setViewportView(jTextArea1); GroupLayout layout = new GroupLayout(f.getContentPane()); f.getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(jScrollPane1, GroupLayout.DEFAULT_SIZE, 446, Short.MAX_VALUE) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(jScrollPane1) .addContainerGap()) ); f.pack(); jTextArea1.setTabSize(8); f.setVisible(true); } private void createUI() throws Exception { SwingUtilities.invokeAndWait(() -> { String description = " INSTRUCTIONS:\n" + " 3 lines of string ending with # will be shown.\n " + " If there is no alignment issue of # character at " + " end of string\n" + " Press Pass else press Fail."; dialog = new JDialog(); dialog.setTitle("textselectionTest"); JTextArea textArea = new JTextArea(description); textArea.setEditable(false); final JButton passButton = new JButton("PASS"); passButton.addActionListener((e) -> { testResult = true; dispose(); latch.countDown(); }); final JButton failButton = new JButton("FAIL"); failButton.addActionListener((e) -> { testResult = false; dispose(); latch.countDown(); }); JPanel mainPanel = new JPanel(new BorderLayout()); mainPanel.add(textArea, BorderLayout.CENTER); JPanel buttonPanel = new JPanel(new FlowLayout()); buttonPanel.add(passButton); buttonPanel.add(failButton); mainPanel.add(buttonPanel, BorderLayout.SOUTH); dialog.add(mainPanel); dialog.pack(); dialog.setVisible(true); }); } }