author | psadhukhan |
Thu, 04 Oct 2018 15:03:48 +0530 | |
changeset 52246 | f49c340e3f39 |
permissions | -rw-r--r-- |
52246
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
1 |
/* |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
2 |
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
4 |
* |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
8 |
* |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
13 |
* accompanied this code). |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
14 |
* |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
18 |
* |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
21 |
* questions. |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
22 |
*/ |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
23 |
|
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
24 |
/* @test |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
25 |
@bug 8211055 |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
26 |
@summary Verifies "print to file" works even when there is no printer |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
27 |
@requires (os.family == "mac") |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
28 |
@run main/manual TestSaveFileWithoutPrinter |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
29 |
*/ |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
30 |
|
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
31 |
import java.awt.BorderLayout; |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
32 |
import java.awt.FlowLayout; |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
33 |
import java.awt.Graphics; |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
34 |
import java.awt.event.WindowAdapter; |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
35 |
import java.awt.event.WindowEvent; |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
36 |
import java.awt.print.Printable; |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
37 |
import java.awt.print.PageFormat; |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
38 |
import java.awt.print.PrinterException; |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
39 |
import java.awt.print.PrinterJob; |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
40 |
import java.util.concurrent.CountDownLatch; |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
41 |
import java.util.concurrent.TimeUnit; |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
42 |
import javax.swing.JButton; |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
43 |
import javax.swing.JDialog; |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
44 |
import javax.swing.JLabel; |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
45 |
import javax.swing.JPanel; |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
46 |
import javax.swing.JTextArea; |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
47 |
import javax.swing.SwingUtilities; |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
48 |
import javax.swing.Timer; |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
49 |
import javax.swing.WindowConstants; |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
50 |
|
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
51 |
public class TestSaveFileWithoutPrinter implements Printable { |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
52 |
private static final CountDownLatch testEndedSignal = new CountDownLatch(1); |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
53 |
private static final int testTimeout = 300000; |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
54 |
private static volatile String testFailureMsg; |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
55 |
private static volatile boolean testPassed; |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
56 |
private static volatile boolean testFinished; |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
57 |
private static PrinterJob job; |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
58 |
|
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
59 |
public static void main(String[] args) { |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
60 |
job = PrinterJob.getPrinterJob(); |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
61 |
if (job.getPrintService() != null) { |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
62 |
System.out.println("This test requires no printers to be installed. Exiting."); |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
63 |
return; |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
64 |
} |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
65 |
SwingUtilities.invokeLater(() -> createAndShowTestDialog()); |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
66 |
|
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
67 |
try { |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
68 |
if (!testEndedSignal.await(testTimeout, TimeUnit.MILLISECONDS)) { |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
69 |
throw new RuntimeException(String.format( |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
70 |
"Test timeout '%d ms' elapsed.", testTimeout)); |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
71 |
} |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
72 |
if (!testPassed) { |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
73 |
String failureMsg = testFailureMsg; |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
74 |
if ((failureMsg != null) && (!failureMsg.trim().isEmpty())) { |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
75 |
throw new RuntimeException(failureMsg); |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
76 |
} else { |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
77 |
throw new RuntimeException("Test failed."); |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
78 |
} |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
79 |
} |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
80 |
} catch (InterruptedException ie) { |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
81 |
throw new RuntimeException(ie); |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
82 |
} finally { |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
83 |
testFinished = true; |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
84 |
} |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
85 |
} |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
86 |
|
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
87 |
private static void doTest() { |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
88 |
job.setPrintable(new TestSaveFileWithoutPrinter()); |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
89 |
if (job.printDialog()) { |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
90 |
try { |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
91 |
job.print(); |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
92 |
} catch (PrinterException pe) { |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
93 |
throw new RuntimeException(pe); |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
94 |
} |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
95 |
} |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
96 |
} |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
97 |
|
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
98 |
private static void pass() { |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
99 |
testPassed = true; |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
100 |
testEndedSignal.countDown(); |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
101 |
} |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
102 |
|
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
103 |
private static void fail(String failureMsg) { |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
104 |
testFailureMsg = failureMsg; |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
105 |
testPassed = false; |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
106 |
testEndedSignal.countDown(); |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
107 |
} |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
108 |
|
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
109 |
private static String convertMillisToTimeStr(int millis) { |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
110 |
if (millis < 0) { |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
111 |
return "00:00:00"; |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
112 |
} |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
113 |
int hours = millis / 3600000; |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
114 |
int minutes = (millis - hours * 3600000) / 60000; |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
115 |
int seconds = (millis - hours * 3600000 - minutes * 60000) / 1000; |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
116 |
return String.format("%02d:%02d:%02d", hours, minutes, seconds); |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
117 |
} |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
118 |
|
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
119 |
private static void createAndShowTestDialog() { |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
120 |
String description = |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
121 |
" To run this test it is required to delete any installed printers.\r\n" + |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
122 |
"\r\n" + |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
123 |
" 1. Verify that saving file via \"Save as PDF\" results in saving file\r\n" + |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
124 |
" even if there is no installed printer.\r\n" + |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
125 |
" 2. Click on \"Start Test\" button.\r\n" + |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
126 |
" 3. In the shown print dialog select \"Save as PDF\" in PDF drop-down list\r\n" + |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
127 |
" 4. Another dialog opens prompting for filename, enter any filename and press \"Save\".\r\n" + |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
128 |
"\r\n" + |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
129 |
" If the file is saved without any PrinterException, click on \"PASS\"\r\n" + |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
130 |
" button, otherwise click on \"FAIL\" button."; |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
131 |
|
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
132 |
final JDialog dialog = new JDialog(); |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
133 |
dialog.setTitle("SaveFileWithoutPrinter"); |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
134 |
dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
135 |
dialog.addWindowListener(new WindowAdapter() { |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
136 |
@Override |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
137 |
public void windowClosing(WindowEvent e) { |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
138 |
dialog.dispose(); |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
139 |
fail("Main dialog was closed."); |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
140 |
} |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
141 |
}); |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
142 |
|
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
143 |
final JLabel testTimeoutLabel = new JLabel(String.format( |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
144 |
"Test timeout: %s", convertMillisToTimeStr(testTimeout))); |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
145 |
final long startTime = System.currentTimeMillis(); |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
146 |
final Timer timer = new Timer(0, null); |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
147 |
timer.setDelay(1000); |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
148 |
timer.addActionListener((e) -> { |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
149 |
int leftTime = testTimeout - (int) (System.currentTimeMillis() - startTime); |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
150 |
if ((leftTime < 0) || testFinished) { |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
151 |
timer.stop(); |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
152 |
dialog.dispose(); |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
153 |
} |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
154 |
testTimeoutLabel.setText(String.format( |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
155 |
"Test timeout: %s", convertMillisToTimeStr(leftTime))); |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
156 |
}); |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
157 |
timer.start(); |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
158 |
|
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
159 |
JTextArea textArea = new JTextArea(description); |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
160 |
textArea.setEditable(false); |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
161 |
|
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
162 |
final JButton testButton = new JButton("Start Test"); |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
163 |
final JButton passButton = new JButton("PASS"); |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
164 |
final JButton failButton = new JButton("FAIL"); |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
165 |
testButton.addActionListener((e) -> { |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
166 |
testButton.setEnabled(false); |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
167 |
new Thread(() -> { |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
168 |
try { |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
169 |
doTest(); |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
170 |
|
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
171 |
SwingUtilities.invokeLater(() -> { |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
172 |
passButton.setEnabled(true); |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
173 |
failButton.setEnabled(true); |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
174 |
}); |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
175 |
} catch (Throwable t) { |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
176 |
t.printStackTrace(); |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
177 |
dialog.dispose(); |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
178 |
fail("Exception occurred in a thread executing the test."); |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
179 |
} |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
180 |
}).start(); |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
181 |
}); |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
182 |
passButton.setEnabled(false); |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
183 |
passButton.addActionListener((e) -> { |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
184 |
dialog.dispose(); |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
185 |
pass(); |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
186 |
}); |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
187 |
failButton.setEnabled(false); |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
188 |
failButton.addActionListener((e) -> { |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
189 |
dialog.dispose(); |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
190 |
fail("PrinterException thrown."); |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
191 |
}); |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
192 |
|
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
193 |
JPanel mainPanel = new JPanel(new BorderLayout()); |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
194 |
JPanel labelPanel = new JPanel(new FlowLayout()); |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
195 |
labelPanel.add(testTimeoutLabel); |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
196 |
mainPanel.add(labelPanel, BorderLayout.NORTH); |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
197 |
mainPanel.add(textArea, BorderLayout.CENTER); |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
198 |
JPanel buttonPanel = new JPanel(new FlowLayout()); |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
199 |
buttonPanel.add(testButton); |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
200 |
buttonPanel.add(passButton); |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
201 |
buttonPanel.add(failButton); |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
202 |
mainPanel.add(buttonPanel, BorderLayout.SOUTH); |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
203 |
dialog.add(mainPanel); |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
204 |
|
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
205 |
dialog.pack(); |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
206 |
dialog.setVisible(true); |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
207 |
} |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
208 |
|
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
209 |
@Override |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
210 |
public int print(Graphics g, PageFormat pf, int pageIndex) |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
211 |
throws PrinterException { |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
212 |
if (pageIndex == 0) { |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
213 |
return Printable.PAGE_EXISTS; |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
214 |
} else { |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
215 |
return Printable.NO_SUCH_PAGE; |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
216 |
} |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
217 |
} |
f49c340e3f39
8211055: Provide print to a file (PDF) feature even when printer was not connected
psadhukhan
parents:
diff
changeset
|
218 |
} |