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 4414987
27 * @author Jennifer Godinez
28 * @summary Displays width & height of validated custom paper size
29 * @run main/manual ValidateCustom
30 */
31import java.awt.*;
32import java.awt.print.*;
33import java.awt.geom.*;
34import javax.swing.*;
35
36public class ValidateCustom implements Pageable, Printable{
37
38  private static double PIXELS_PER_INCH = 72.0;
39  private static double WIDTH = 17.0; //width of paper in inches
40  private static double LENGTH = 24.0; //length of paper in inches
41  private static boolean VALIDATE = true;
42
43  private PrinterJob printerJob;
44  private PageFormat pageFormat;
45
46  ValidateCustom(){
47    printerJob = PrinterJob.getPrinterJob();
48    createPageFormat();
49  }
50
51  private void createPageFormat(){
52    pageFormat = new PageFormat();
53    Paper p = new Paper();
54    double width   = WIDTH*PIXELS_PER_INCH;
55    double height  = LENGTH*PIXELS_PER_INCH;
56    double ix      = PIXELS_PER_INCH;
57    double iy      = PIXELS_PER_INCH;
58    double iwidth  = width  - 2.0*PIXELS_PER_INCH;
59    double iheight = height - 2.0*PIXELS_PER_INCH;
60    p.setSize(width, height);
61    p.setImageableArea(ix, iy, iwidth, iheight);
62    pageFormat.setPaper(p);
63  }
64
65  public Printable getPrintable(int index){
66    return this;
67  }
68
69  public PageFormat getPageFormat(int index){
70    return pageFormat;
71  }
72
73  public int getNumberOfPages(){
74    return 1;
75  }
76
77  private void printPaperSize(PageFormat pf){
78    Paper p = pf.getPaper();
79    System.out.println("paper size = ("+p.getWidth()+", "+p.getHeight()+")");
80  }
81
82  public void print(){
83    //if(printerJob.printDialog())
84    {
85      try{
86        //printPaperSize(pageFormat);
87        if(VALIDATE){
88            this.pageFormat = printerJob.validatePage(this.pageFormat);
89        }
90        printPaperSize(pageFormat);
91        //printerJob.setPageable(this);
92        //printerJob.print();
93      }catch(Exception e){e.printStackTrace();}
94    }
95  }
96
97  public int print(Graphics g, PageFormat pf, int pageIndex){
98    if(pageIndex == 0){
99      Graphics2D g2 = (Graphics2D)g;
100      Rectangle2D r = new Rectangle2D.Double(PIXELS_PER_INCH, PIXELS_PER_INCH, PIXELS_PER_INCH, PIXELS_PER_INCH);
101      g2.setStroke(new BasicStroke(1.0f));
102      g2.draw(r);
103      return PAGE_EXISTS;
104    }else{
105      return NO_SUCH_PAGE;
106    }
107  }
108
109  public static void main(String[] args){
110        System.out.println("-----------------instructions--------------------");
111        System.out.println("You must have a printer installed in your system \nthat supports custom paper sizes in order to run this test.");
112        System.out.println("Passing test will display the correct width & height\nof custom paper in 1/72nds of an inch.\n");
113        System.out.println("-------------------------------------------------");
114    ValidateCustom pt = new ValidateCustom();
115    pt.print();
116    try{
117      System.in.read();
118    }catch(Exception e){}
119  }
120
121}
122