test/jdk/javax/swing/SwingUtilities/TestTextPosInPrint.java
author psadhukhan
Tue, 21 May 2019 14:41:43 +0530
changeset 55177 eba6a83dc23b
permissions -rw-r--r--
8214702: Wrong text position for whitespaced string in printing Swing text Reviewed-by: prr, serb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
55177
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
     1
/*
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
     4
 *
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
     7
 * published by the Free Software Foundation.
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
     8
 *
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    13
 * accompanied this code).
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    14
 *
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    18
 *
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    21
 * questions.
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    22
 */
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    23
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    24
/* @test
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    25
   @bug 8214702
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    26
   @summary Verifies text position for whitespaced string in printing Swing text
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    27
   @run main/manual TestTextPosInPrint
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    28
 */
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    29
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    30
import java.awt.BorderLayout;
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    31
import java.awt.FlowLayout;
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    32
import java.awt.Graphics;
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    33
import java.awt.Graphics2D;
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    34
import java.awt.Font;
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    35
import java.awt.event.WindowAdapter;
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    36
import java.awt.event.WindowEvent;
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    37
import java.awt.print.Printable;
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    38
import java.awt.print.PageFormat;
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    39
import java.awt.print.PrinterException;
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    40
import java.awt.print.PrinterJob;
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    41
import java.util.concurrent.CountDownLatch;
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    42
import java.util.concurrent.TimeUnit;
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    43
import javax.swing.JFrame;
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    44
import javax.swing.JButton;
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    45
import javax.swing.JDialog;
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    46
import javax.swing.JLabel;
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    47
import javax.swing.JPanel;
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    48
import javax.swing.JTextArea;
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    49
import javax.swing.SwingUtilities;
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    50
import javax.swing.Timer;
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    51
import javax.swing.WindowConstants;
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    52
import javax.swing.SwingConstants;
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    53
import javax.swing.UIManager;
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    54
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    55
public class TestTextPosInPrint implements Printable {
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    56
    private static final CountDownLatch testEndedSignal = new CountDownLatch(1);
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    57
    private static final int testTimeout = 300000;
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    58
    private static volatile String testFailureMsg;
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    59
    private static volatile boolean testPassed;
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    60
    private static volatile boolean testFinished;
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    61
    private static PrinterJob job;
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    62
    private static JPanel panel;
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    63
    private static JFrame f;
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    64
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    65
    public static void main(String[] args) throws Exception {
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    66
        job = PrinterJob.getPrinterJob();
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    67
        if (job.getPrintService() == null) {
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    68
            System.out.println("This test requires printers to be installed. Exiting.");
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    69
            return;
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    70
        }
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    71
        SwingUtilities.invokeLater(() -> createAndShowTestDialog());
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    72
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    73
        try {
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    74
            if (!testEndedSignal.await(testTimeout, TimeUnit.MILLISECONDS)) {
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    75
                throw new RuntimeException(String.format(
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    76
                    "Test timeout '%d ms' elapsed.", testTimeout));
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    77
            }
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    78
            if (!testPassed) {
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    79
                String failureMsg = testFailureMsg;
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    80
                if ((failureMsg != null) && (!failureMsg.trim().isEmpty())) {
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    81
                    throw new RuntimeException(failureMsg);
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    82
                } else {
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    83
                    throw new RuntimeException("Test failed.");
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    84
                }
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    85
            }
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    86
        } catch (InterruptedException ie) {
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    87
            throw new RuntimeException(ie);
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    88
        } finally {
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    89
            testFinished = true;
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    90
            SwingUtilities.invokeAndWait(() -> f.dispose());
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    91
        }
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    92
    }
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    93
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    94
    private static void doTest() throws Exception {
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    95
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    96
        f = new JFrame();
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    97
        f.setLocationRelativeTo(null);
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    98
        panel = new JPanel();
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
    99
        panel.setLayout(new BorderLayout());
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   100
        Font font = new Font("Serif", Font.PLAIN, 12);
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   101
        JLabel l1 = new JLabel("      1. ABCDE");
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   102
        l1.setHorizontalAlignment(SwingConstants.LEFT);
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   103
        JLabel l2 = new JLabel("      2. ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ");
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   104
        l2.setHorizontalAlignment(SwingConstants.LEFT);
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   105
        //JLabel l3 = new JLabel("      3. ABCDE          ");
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   106
        JLabel l3 = new JLabel("      3. ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ");
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   107
        l3.setHorizontalAlignment(SwingConstants.LEFT);
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   108
        panel.add(BorderLayout.NORTH, l1);
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   109
        panel.add(BorderLayout.CENTER, l2);
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   110
        panel.add(BorderLayout.SOUTH, l3);
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   111
        f.getContentPane().add(BorderLayout.NORTH, panel);
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   112
        f.setSize(400, 300);
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   113
        f.setVisible(true);
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   114
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   115
        job.setPrintable(new TestTextPosInPrint());
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   116
        if (job.printDialog()) {
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   117
            try {
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   118
                job.print();
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   119
            } catch (PrinterException pe) {
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   120
                throw new RuntimeException(pe);
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   121
            }
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   122
        }
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   123
    }
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   124
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   125
    private static void pass() {
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   126
        testPassed = true;
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   127
        testEndedSignal.countDown();
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   128
    }
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   129
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   130
    private static void fail(String failureMsg) {
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   131
        testFailureMsg = failureMsg;
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   132
        testPassed = false;
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   133
        testEndedSignal.countDown();
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   134
    }
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   135
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   136
    private static String convertMillisToTimeStr(int millis) {
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   137
        if (millis < 0) {
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   138
            return "00:00:00";
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   139
        }
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   140
        int hours = millis / 3600000;
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   141
        int minutes = (millis - hours * 3600000) / 60000;
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   142
        int seconds = (millis - hours * 3600000 - minutes * 60000) / 1000;
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   143
        return String.format("%02d:%02d:%02d", hours, minutes, seconds);
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   144
    }
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   145
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   146
    private static void createAndShowTestDialog() {
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   147
        String description =
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   148
            " 1. Click on \"Start Test\" button.\r\n" +
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   149
            " 2. Multiple strings will be displayed on console.\r\n" +
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   150
            " 3. A print dialog will be shown. Select any printer to print. " +
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   151
            "\r\n" +
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   152
            " If the printed output of the strings are same without any alignment issue, click on \"PASS\"\r\n" +
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   153
            " button, otherwise click on \"FAIL\" button.";
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   154
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   155
        final JDialog dialog = new JDialog();
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   156
        dialog.setTitle("SaveFileWithoutPrinter");
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   157
        dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   158
        dialog.addWindowListener(new WindowAdapter() {
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   159
            @Override
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   160
            public void windowClosing(WindowEvent e) {
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   161
                dialog.dispose();
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   162
                fail("Main dialog was closed.");
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   163
            }
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   164
        });
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   165
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   166
        final JLabel testTimeoutLabel = new JLabel(String.format(
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   167
            "Test timeout: %s", convertMillisToTimeStr(testTimeout)));
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   168
        final long startTime = System.currentTimeMillis();
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   169
        final Timer timer = new Timer(0, null);
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   170
        timer.setDelay(1000);
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   171
        timer.addActionListener((e) -> {
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   172
            int leftTime = testTimeout - (int) (System.currentTimeMillis() - startTime);
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   173
            if ((leftTime < 0) || testFinished) {
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   174
                timer.stop();
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   175
                dialog.dispose();
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   176
            }
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   177
            testTimeoutLabel.setText(String.format(
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   178
                "Test timeout: %s", convertMillisToTimeStr(leftTime)));
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   179
        });
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   180
        timer.start();
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   181
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   182
        JTextArea textArea = new JTextArea(description);
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   183
        textArea.setEditable(false);
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   184
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   185
        final JButton testButton = new JButton("Start Test");
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   186
        final JButton passButton = new JButton("PASS");
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   187
        final JButton failButton = new JButton("FAIL");
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   188
        testButton.addActionListener((e) -> {
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   189
            testButton.setEnabled(false);
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   190
            new Thread(() -> {
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   191
                try {
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   192
                    doTest();
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   193
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   194
                    SwingUtilities.invokeLater(() -> {
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   195
                        passButton.setEnabled(true);
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   196
                        failButton.setEnabled(true);
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   197
                    });
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   198
                } catch (Throwable t) {
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   199
                    t.printStackTrace();
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   200
                    dialog.dispose();
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   201
                    fail("Exception occurred in a thread executing the test.");
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   202
                }
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   203
            }).start();
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   204
        });
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   205
        passButton.setEnabled(false);
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   206
        passButton.addActionListener((e) -> {
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   207
            dialog.dispose();
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   208
            pass();
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   209
        });
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   210
        failButton.setEnabled(false);
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   211
        failButton.addActionListener((e) -> {
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   212
            dialog.dispose();
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   213
            fail("Printed texts are not aligned as shown in console");
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   214
        });
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   215
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   216
        JPanel mainPanel = new JPanel(new BorderLayout());
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   217
        JPanel labelPanel = new JPanel(new FlowLayout());
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   218
        labelPanel.add(testTimeoutLabel);
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   219
        mainPanel.add(labelPanel, BorderLayout.NORTH);
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   220
        mainPanel.add(textArea, BorderLayout.CENTER);
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   221
        JPanel buttonPanel = new JPanel(new FlowLayout());
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   222
        buttonPanel.add(testButton);
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   223
        buttonPanel.add(passButton);
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   224
        buttonPanel.add(failButton);
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   225
        mainPanel.add(buttonPanel, BorderLayout.SOUTH);
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   226
        dialog.add(mainPanel);
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   227
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   228
        dialog.pack();
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   229
        dialog.setVisible(true);
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   230
    }
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   231
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   232
    @Override
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   233
    public int print(Graphics pg, PageFormat pf, int pageNum)
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   234
        throws PrinterException {
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   235
        if (pageNum > 0){
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   236
            return Printable.NO_SUCH_PAGE;
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   237
        }
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   238
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   239
        Graphics2D g2 = (Graphics2D) pg;
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   240
        g2.translate(pf.getImageableX(), pf.getImageableY());
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   241
        panel.paint(g2);
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   242
        return Printable.PAGE_EXISTS;
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   243
    }
eba6a83dc23b 8214702: Wrong text position for whitespaced string in printing Swing text
psadhukhan
parents:
diff changeset
   244
}