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 4252108 6229507
27 * @summary PrinterJob.validatePage() is unimplemented.
28 * @author prr
29 * @run main/manual ValidatePage
30 */
31
32import java.awt.*;
33import java.awt.event.*;
34import java.awt.print.*;
35
36public class ValidatePage extends Frame implements Printable {
37
38PrinterJob myPrinterJob;
39PageFormat myPageFormat;
40Label pw, ph, pglm, pgrm, pgiw, pgih, pgtm, pgbm;
41TextField tpw, tph, tpglm, tpgtm, tpgiw, tpgih;
42Label myWidthLabel;
43Label myHeightLabel;
44Label myImageableXLabel;
45Label myImageableYLabel;
46Label myImageableRightLabel;
47Label myImageableBottomLabel;
48Label myImageableWidthLabel;
49Label myImageableHeightLabel;
50Label myOrientationLabel;
51
52  protected void displayPageFormatAttributes() {
53    myWidthLabel.setText ("Format Width = " + drnd(myPageFormat.getWidth()));
54    myHeightLabel.setText ("Format Height = " + drnd(myPageFormat.getHeight()));
55    myImageableXLabel.setText
56        ("Format Left Margin = " + drnd(myPageFormat.getImageableX()));
57    myImageableRightLabel.setText
58        ("Format Right Margin = " + drnd(myPageFormat.getWidth() -
59        (myPageFormat.getImageableX() + myPageFormat.getImageableWidth())));
60    myImageableWidthLabel.setText
61        ("Format ImageableWidth = " + drnd(myPageFormat.getImageableWidth()));
62    myImageableYLabel.setText
63        ("Format Top Margin = " + drnd(myPageFormat.getImageableY()));
64    myImageableBottomLabel.setText
65        ("Format Bottom Margin = " + drnd(myPageFormat.getHeight() -
66        (myPageFormat.getImageableY() + myPageFormat.getImageableHeight())));
67    myImageableHeightLabel.setText
68        ("Format ImageableHeight = " + drnd(myPageFormat.getImageableHeight()));
69    int o = myPageFormat.getOrientation();
70    myOrientationLabel.setText
71        ("Format Orientation = " +
72                (o == PageFormat.PORTRAIT ? "PORTRAIT" :
73                 o == PageFormat.LANDSCAPE ? "LANDSCAPE" :
74                 o == PageFormat.REVERSE_LANDSCAPE ? "REVERSE_LANDSCAPE" :
75                 "<invalid>"));
76    Paper p = myPageFormat.getPaper();
77    pw.setText("Paper Width = " + drnd(p.getWidth()));
78    ph.setText("Paper Height = " + drnd(p.getHeight()));
79    pglm.setText("Paper Left Margin = " + drnd(p.getImageableX()));
80    pgiw.setText("Paper Imageable Width = " + drnd(p.getImageableWidth()));
81    pgih.setText("Paper Imageable Height = " + drnd(p.getImageableHeight()));
82
83    pgrm.setText("Paper Right Margin = " +
84         drnd(p.getWidth() - (p.getImageableX()+p.getImageableWidth())));
85    pgtm.setText("Paper Top Margin = " + drnd(p.getImageableY()));
86    pgbm.setText("Paper Bottom Margin = " +
87       drnd(p.getHeight() - (p.getImageableY()+p.getImageableHeight())));
88  }
89
90  static String drnd(double d) {
91      d = d * 10.0 + 0.5;
92      d = Math.floor(d) /10.0;
93      String ds = Double.toString(d);
94      int decimal_pos = ds.indexOf(".");
95      int len = ds.length();
96      if (len > decimal_pos+2) {
97          return ds.substring(0, decimal_pos+2);
98      } else {
99          return ds;
100      }
101  }
102
103  public ValidatePage() {
104    super ("Validate Page Test");
105    myPrinterJob = PrinterJob.getPrinterJob();
106    myPageFormat = new PageFormat();
107    Paper p = new Paper();
108    p.setSize(28*72, 21.5 * 72);
109    myPageFormat.setPaper(p);
110    setLayout(new FlowLayout());
111    Panel pfp = new Panel();
112    pfp.setLayout (new GridLayout (9, 1, 0, 0));
113    pfp.add (myOrientationLabel = new Label());
114    pfp.add (myWidthLabel = new Label());
115    pfp.add (myImageableXLabel = new Label());
116    pfp.add (myImageableRightLabel = new Label());
117    pfp.add (myImageableWidthLabel = new Label());
118    pfp.add (myHeightLabel = new Label());
119    pfp.add (myImageableYLabel = new Label());
120    pfp.add (myImageableBottomLabel = new Label());
121    pfp.add (myImageableHeightLabel = new Label());
122
123    add(pfp);
124
125    Panel pp = new Panel();
126    pp.setLayout (new GridLayout (8, 1, 0, 0));
127    pp.add (pw = new Label());
128    pp.add (pglm = new Label());
129    pp.add (pgtm = new Label());
130    pp.add (ph = new Label());
131    pp.add (pgiw = new Label());
132    pp.add (pgih = new Label());
133    pp.add (pgrm = new Label());
134    pp.add (pgbm = new Label());
135
136    add(pp);
137
138    Panel epp = new Panel();
139    epp.setLayout (new GridLayout (6, 2, 0, 0));
140
141    epp.add(new Label("Page width:"));
142    epp.add (tpw = new TextField());
143    epp.add(new Label("Page height:"));
144    epp.add (tph = new TextField());
145    epp.add(new Label("Left Margin:"));
146    epp.add (tpglm = new TextField());
147    epp.add(new Label("Top margin:"));
148    epp.add (tpgtm = new TextField());
149    epp.add(new Label("Imageable Wid:"));
150    epp.add (tpgiw = new TextField());
151    epp.add(new Label("Imageable Hgt:"));
152    epp.add (tpgih = new TextField());
153
154    add(epp);
155       displayPageFormatAttributes();
156
157    Panel panel = new Panel();
158    Button defButton = new Button ("Default Page");
159    defButton.addActionListener(new ActionListener() {
160                public void actionPerformed (ActionEvent e) {
161                        myPageFormat = myPrinterJob.defaultPage();
162                        displayPageFormatAttributes();
163                }
164    });
165
166    Button pageButton = new Button ("Page Setup..");
167    pageButton.addActionListener(new ActionListener() {
168                public void actionPerformed (ActionEvent e) {
169                        myPageFormat = myPrinterJob.pageDialog (myPageFormat);
170                        displayPageFormatAttributes();
171                }
172    });
173    Button printButton = new Button ("Print");
174    printButton.addActionListener(new ActionListener() {
175                public void actionPerformed (ActionEvent e) {
176                    try {
177                         //if (myPrinterJob.printDialog()) {
178                             myPrinterJob.setPrintable(ValidatePage.this,
179                                                       myPageFormat);
180                             myPrinterJob.print();
181                   // }
182                    } catch (PrinterException pe ) {
183                    }
184                }
185    });
186
187    Button chooseButton = new Button ("Printer..");
188    chooseButton.addActionListener(new ActionListener() {
189                public void actionPerformed (ActionEvent e) {
190                            myPrinterJob.printDialog();
191                }
192    });
193
194    Button validateButton = new Button ("Validate Page");
195    validateButton.addActionListener(new ActionListener() {
196                public void actionPerformed (ActionEvent e) {
197                        myPageFormat = myPrinterJob.validatePage(myPageFormat);
198                        displayPageFormatAttributes();
199                }
200    });
201    Button setButton = new Button ("Set Paper");
202    setButton.addActionListener(new ActionListener() {
203                public void actionPerformed (ActionEvent e) {
204                  try {
205                      Paper p = new Paper();
206                      double pwid = Double.parseDouble(tpw.getText());
207                      double phgt = Double.parseDouble(tph.getText());
208                      double pimx = Double.parseDouble(tpglm.getText());
209                      double pimy = Double.parseDouble(tpgtm.getText());
210                      double pimwid = Double.parseDouble(tpgiw.getText());
211                      double pimhgt = Double.parseDouble(tpgih.getText());
212                      p.setSize(pwid, phgt);
213                      p.setImageableArea(pimx, pimy, pimwid, pimhgt);
214                      myPageFormat.setPaper(p);
215                            displayPageFormatAttributes();
216                  } catch (NumberFormatException nfe) {
217                  }
218                }
219    });
220    panel.add (setButton);
221    panel.add (defButton);
222    panel.add (pageButton);
223    panel.add (chooseButton);
224    panel.add (validateButton);
225    panel.add (printButton);
226    add(panel);
227    TextArea ta = new TextArea(7, 60);
228    String ls = System.getProperty("line.Separator", "\n");
229    ta.setText(
230        "When validating a page, the process is 1st to find the closest matching " + ls +
231        "paper size, next to make sure the requested imageable area fits within " + ls +
232        "the printer's imageable area for that paper size. Finally the top and " + ls +
233        "left margins will be shrunk if they are too great for the adjusted " + ls +
234        "imageable area to fit at that position. They will shrink by the minimum" + ls +
235        "needed to accomodate the imageable area."+ls+ls+
236        "To test 6229507, put the minimum margins (all 0s) in Page Setup dialog."+ls+
237        "Compare Imageable width, height, and margins of portrait against landscape.");
238    ta.setEditable(false);
239    add(ta);
240
241    addWindowListener (new WindowAdapter() {
242         public void windowClosing (WindowEvent e) {
243            dispose();
244            System.exit (0);
245         }
246
247      });
248      setSize (500, 630);
249      setVisible (true);
250  }
251
252  public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) {
253
254     if (pageIndex > 0) {
255        return Printable.NO_SUCH_PAGE;
256     }
257
258     Graphics2D g2d = (Graphics2D)graphics;
259
260    int o = pageFormat.getOrientation();
261
262     System.out.println("Format Orientation = " +
263                (o == PageFormat.PORTRAIT ? "PORTRAIT" :
264                 o == PageFormat.LANDSCAPE ? "LANDSCAPE" :
265                 o == PageFormat.REVERSE_LANDSCAPE ? "REVERSE_LANDSCAPE" :
266                 "<invalid>"));
267     System.out.println(g2d.getTransform());
268     System.out.println("ix="+pageFormat.getImageableX()+
269                       " iy="+pageFormat.getImageableY());
270     g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
271     g2d.drawString("ORIGIN", 20, 20);
272     g2d.drawString("X THIS WAY", 200, 50);
273     g2d.drawString("Y THIS WAY", 60 , 200);
274     g2d.drawRect(0,0,(int)pageFormat.getImageableWidth(),
275                      (int)pageFormat.getImageableHeight());
276     g2d.setColor(Color.blue);
277     g2d.drawRect(1,1,(int)pageFormat.getImageableWidth()-2,
278                      (int)pageFormat.getImageableHeight()-2);
279
280     return  Printable.PAGE_EXISTS;
281  }
282
283  public static void main( String[] args) {
284  String[] instructions =
285        {
286         "You must have a printer available to perform this test",
287         "This test is very flexible and requires much interaction.",
288         "There are several buttons.",
289         "Set Paper: if all fields are valid numbers it sets the Paper object.",
290         "This is used to create arbitrary nonsensical paper sizes to help",
291         "test validatePage.",
292         "Default Page: sets a default page. This should always be valid.",
293         "Page Setup: brings up the page dialog. You must OK this dialog",
294         "for it to have any effect. You can use this to set different size,",
295         "orientation and margins - which of course affect imageable area.",
296         "Printer: Used to set the current printer. Useful because current",
297         "printer affects the choice of paper sizes available.",
298         "You must OK this dialog for it to have any effect.",
299         "Validate Page:",
300         "The most important button in the test. By setting nonsensical",
301         "or valid papers with varying margins etc, this should always find",
302         "the closest",
303         "match within the limits of what is possible on the current printer.",
304         "Print: to the current printer. Not vital for this test.",
305         "request."
306       };
307      Sysout.createDialog( );
308      Sysout.printInstructions( instructions );
309
310     new ValidatePage();
311  }
312
313}
314
315class Sysout {
316   private static TestDialog dialog;
317
318   public static void createDialogWithInstructions( String[] instructions )
319    {
320      dialog = new TestDialog( new Frame(), "Instructions" );
321      dialog.printInstructions( instructions );
322      dialog.show();
323      println( "Any messages for the tester will display here." );
324    }
325
326   public static void createDialog( )
327    {
328      dialog = new TestDialog( new Frame(), "Instructions" );
329      String[] defInstr = { "Instructions will appear here. ", "" } ;
330      dialog.printInstructions( defInstr );
331      dialog.show();
332      println( "Any messages for the tester will display here." );
333    }
334
335
336   public static void printInstructions( String[] instructions )
337    {
338      dialog.printInstructions( instructions );
339    }
340
341
342   public static void println( String messageIn )
343    {
344      dialog.displayMessage( messageIn );
345    }
346
347}// Sysout  class
348
349/**
350  This is part of the standard test machinery.  It provides a place for the
351   test instructions to be displayed, and a place for interactive messages
352   to the user to be displayed.
353  To have the test instructions displayed, see Sysout.
354  To have a message to the user be displayed, see Sysout.
355  Do not call anything in this dialog directly.
356  */
357class TestDialog extends Dialog {
358
359   TextArea instructionsText;
360   TextArea messageText;
361   int maxStringLength = 80;
362
363   //DO NOT call this directly, go through Sysout
364   public TestDialog( Frame frame, String name )
365    {
366      super( frame, name );
367      int scrollBoth = TextArea.SCROLLBARS_BOTH;
368      instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
369      add( "North", instructionsText );
370
371      messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
372      add("Center", messageText);
373
374      pack();
375
376      show();
377    }// TestDialog()
378
379   //DO NOT call this directly, go through Sysout
380   public void printInstructions( String[] instructions )
381    {
382      //Clear out any current instructions
383      instructionsText.setText( "" );
384
385      //Go down array of instruction strings
386
387      String printStr, remainingStr;
388      for( int i=0; i < instructions.length; i++ )
389       {
390         //chop up each into pieces maxSringLength long
391         remainingStr = instructions[ i ];
392         while( remainingStr.length() > 0 )
393          {
394            //if longer than max then chop off first max chars to print
395            if( remainingStr.length() >= maxStringLength )
396             {
397               //Try to chop on a word boundary
398               int posOfSpace = remainingStr.
399                  lastIndexOf( ' ', maxStringLength - 1 );
400
401               if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
402
403               printStr = remainingStr.substring( 0, posOfSpace + 1 );
404               remainingStr = remainingStr.substring( posOfSpace + 1 );
405             }
406            //else just print
407            else
408             {
409               printStr = remainingStr;
410               remainingStr = "";
411             }
412
413            instructionsText.append( printStr + "\n" );
414
415          }// while
416
417       }// for
418
419    }//printInstructions()
420
421   //DO NOT call this directly, go through Sysout
422   public void displayMessage( String messageIn )
423    {
424      messageText.append( messageIn + "\n" );
425    }
426
427 }// TestDialog  class
428