1 /*
2  * Copyright (c) 2007, 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 /*
25   @test
26   @bug 4748055
27   @summary   PASS if the values are same in both cases (2 and 3) below.
28   @run main/manual CompareImageable
29 */
30 
31 /********************************************************************
32 Testcase for comparing the imageable width and height of the paper
33 with and without using print dialog.
34 
35 How to run:
36 
37 1. Launch the app. You'll find a checkbox and a print button.
38 2. Click on the print button with the checkbox unselected. Note the
39 imageable width and height displayed on the console
40 3. Click on the print button with the checkbox selected. This popus up
41 the print dialog. Click ok on the dialog. Note the imageable width and
42 height displayed on the console.
43 
44 Result:  It's a PASS if the values are same in both cases (2 and 3),
45         otherwise not.
46 
47 *********************************************************************/
48 
49 import java.awt.*;
50 import java.awt.event.*;
51 import javax.swing.*;
52 import java.awt.print.*;
53 
54 
55 public class CompareImageable implements Printable {
56 
57 
print(Graphics g, PageFormat pgFmt, int pgIndex)58     public int print(Graphics g, PageFormat pgFmt, int pgIndex) {
59 
60        //get the printable width and height of the paper.
61         int pageHeight = (int)pgFmt.getImageableHeight();
62         int pageWidth = (int)pgFmt.getImageableWidth();
63 
64         System.out.println("imageable width = " + pageWidth + " height = " + pageHeight);
65         return Printable.NO_SUCH_PAGE;
66     }
67 
68 
main(String [] args)69     public static void main(String [] args) {
70 
71         final JFrame frame = new JFrame("Print Test");
72         final JButton printBtn = new JButton("Print");
73         final JCheckBox dialogBtn = new JCheckBox("Native dialog");
74 
75         JPanel panel = new JPanel(new FlowLayout());
76         panel.add(dialogBtn);
77         panel.add(printBtn);
78         frame.getContentPane().add(panel);
79 
80         printBtn.addActionListener(new ActionListener() {
81                 public void actionPerformed(ActionEvent e) {
82 
83                         CompareImageable test = new CompareImageable();
84                         PrinterJob pj = PrinterJob.getPrinterJob();
85 
86                         if (dialogBtn.isSelected() && !pj.printDialog()) {
87                                 //user clicked 'Cancel' button in the print dialog. No printing.
88                                 return;
89                         }
90 
91                         if (dialogBtn.isSelected()) {
92                                 System.out.println("With print dialog...");
93                         } else {
94                                 System.out.println("Without print dialog...");
95                         }
96 
97                         if (pj == null) {
98                                 System.out.println("No printer job found...");
99                                 return;
100                         }
101                         pj.setPrintable(test);
102 
103                         try {
104                                 pj.print();
105 
106                         } catch (Exception ex) {
107                                 ex.printStackTrace();
108                         }
109                 }
110         });
111 
112 
113         frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
114         frame.setSize(400, 400);
115         frame.setVisible(true);
116 
117     }
118 }
119