|
| 1 | +/* |
| 2 | + * Copyright (c) 2007, 2024, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | + |
| 24 | +import java.awt.Color; |
| 25 | +import java.awt.Graphics2D; |
| 26 | +import java.awt.event.ActionEvent; |
| 27 | +import java.awt.print.PageFormat; |
| 28 | +import java.awt.print.Paper; |
| 29 | +import java.awt.print.Printable; |
| 30 | +import java.awt.print.PrinterJob; |
| 31 | + |
| 32 | +import javax.swing.JButton; |
| 33 | + |
| 34 | +/* |
| 35 | + * @test |
| 36 | + * @bug 5024549 |
| 37 | + * @key printer |
| 38 | + * @library /java/awt/regtesthelpers |
| 39 | + * @build PassFailJFrame |
| 40 | + * @summary Pass if dialogs are modal. |
| 41 | + * @run main/manual PrintModalDialog |
| 42 | + */ |
| 43 | + |
| 44 | +public class PrintModalDialog { |
| 45 | + private static JButton jButton1; |
| 46 | + private static final String INSTRUCTIONS = |
| 47 | + """ |
| 48 | + Click the "PRINT" button in the test window. A new dialog |
| 49 | + should appear to print the page. Test if this print new dialog |
| 50 | + is actually modal. |
| 51 | +
|
| 52 | + Modal in this case means that it blocks the user from interacting |
| 53 | + with other windows in the same application. You may still be able |
| 54 | + to interact with unrelated applications on the desktop. |
| 55 | + One sure way to test this is to first show the print dialog and |
| 56 | + then press "Fail", because if you can click on "Fail" and have it |
| 57 | + respond, then the print dialog was not modal. If clicking on it |
| 58 | + does nothing then cancel the print dialog and do the same for the |
| 59 | + other print dialog. If all is well, then press Pass. |
| 60 | + """; |
| 61 | + |
| 62 | + public static void main(String[] args) throws Exception { |
| 63 | + PassFailJFrame |
| 64 | + .builder() |
| 65 | + .title("PrintModalDialog Instructions") |
| 66 | + .instructions(INSTRUCTIONS) |
| 67 | + .rows((int) INSTRUCTIONS.lines().count() + 1) |
| 68 | + .columns(40) |
| 69 | + .splitUIBottom(PrintModalDialog::createAndShowGUI) |
| 70 | + .build() |
| 71 | + .awaitAndCheck(); |
| 72 | + } |
| 73 | + |
| 74 | + public static JButton createAndShowGUI() { |
| 75 | + jButton1 = new JButton("PRINT"); |
| 76 | + jButton1.addActionListener(e -> jButton1_actionPerformed(e)); |
| 77 | + return jButton1; |
| 78 | + } |
| 79 | + |
| 80 | + static void jButton1_actionPerformed(ActionEvent e) { |
| 81 | + PrinterJob printJob = null; |
| 82 | + PageFormat pageFormat = null; |
| 83 | + Paper prtPaper = null; |
| 84 | + boolean bPrintFlg = true; |
| 85 | + |
| 86 | + try { |
| 87 | + printJob = PrinterJob.getPrinterJob(); |
| 88 | + } |
| 89 | + catch (SecurityException se) { |
| 90 | + bPrintFlg = false; |
| 91 | + } |
| 92 | + |
| 93 | + if (bPrintFlg) { |
| 94 | + pageFormat = printJob.pageDialog(printJob.defaultPage()); |
| 95 | + System.out.println("PrintModalDialog: pageFormat = " + |
| 96 | + pageFormat.getWidth() / 72.0 + " x " + |
| 97 | + pageFormat.getHeight() / 72.0); |
| 98 | + if (pageFormat != null) { |
| 99 | + prtPaper = pageFormat.getPaper(); |
| 100 | + pageFormat.setPaper(prtPaper); |
| 101 | + printJob.setPrintable((g, pf, page) -> { |
| 102 | + System.out.println("Calling print"); |
| 103 | + if (page == 0) { |
| 104 | + Graphics2D g2 = (Graphics2D)g; |
| 105 | + g2.translate(pf.getImageableX(), pf.getImageableY()); |
| 106 | + g2.setColor(Color.black); |
| 107 | + g2.drawString("Hello World", 20, 100); |
| 108 | + |
| 109 | + return Printable.PAGE_EXISTS; |
| 110 | + } |
| 111 | + return Printable.NO_SUCH_PAGE; |
| 112 | + }, pageFormat); |
| 113 | + } |
| 114 | + |
| 115 | + if (printJob.printDialog()) { |
| 116 | + try { |
| 117 | + printJob.print(); |
| 118 | + } |
| 119 | + catch (java.awt.print.PrinterException ex) { |
| 120 | + ex.printStackTrace(); |
| 121 | + String msg = "PrinterException: " + ex.getMessage(); |
| 122 | + PassFailJFrame.forceFail(msg); |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments