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 4956397 27 * @run main/manual PageDlgPrnButton 28 */ 29 30 import java.awt.print.PrinterJob; 31 import java.awt.print.PageFormat; 32 import java.awt.print.Printable; 33 import java.awt.print.PrinterException; 34 35 import java.awt.Graphics; 36 import java.awt.Graphics2D; 37 import java.awt.Rectangle; 38 import java.awt.* ; 39 40 public class PageDlgPrnButton implements Printable 41 { main( String args[] )42 public static void main ( String args[] ) { 43 44 String[] instructions = 45 {"For non-windows OS, this test PASSes.", 46 "You must have at least 2 printers available to perform this test.", 47 "This test brings up a native Windows page dialog.", 48 "Click on the Printer... button and change the selected printer. ", 49 "Test passes if the printout comes from the new selected printer.", 50 }; 51 52 Sysout.createDialog( ); 53 Sysout.printInstructions( instructions ); 54 55 PageDlgPrnButton pdpb = new PageDlgPrnButton() ; 56 } 57 PageDlgPrnButton()58 public PageDlgPrnButton() 59 { 60 try 61 { 62 pageDialogExample(); 63 } 64 catch(Exception e) 65 {e.printStackTrace(System.err);} 66 } 67 68 69 // This example just displays the page dialog - you cannot change 70 // the printer (press the "Printer..." button and choose one if you like). pageDialogExample()71 public void pageDialogExample() throws PrinterException 72 { 73 PrinterJob job = PrinterJob.getPrinterJob(); 74 PageFormat originalPageFormat = job.defaultPage(); 75 PageFormat pageFormat = job.pageDialog(originalPageFormat); 76 77 if(originalPageFormat == pageFormat) return; 78 79 job.setPrintable(this,pageFormat); 80 job.print(); 81 } 82 83 84 print(Graphics g, PageFormat pageFormat, int pageIndex)85 public int print(Graphics g, PageFormat pageFormat, int pageIndex) 86 { 87 final int boxWidth = 100; 88 final int boxHeight = 100; 89 final Rectangle rect = new Rectangle(0,0,boxWidth,boxHeight); 90 final double pageH = pageFormat.getImageableHeight(); 91 final double pageW = pageFormat.getImageableWidth(); 92 93 if (pageIndex > 0) return (NO_SUCH_PAGE); 94 95 final Graphics2D g2d = (Graphics2D)g; 96 97 // Move the (x,y) origin to account for the left-hand and top margins 98 g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY()); 99 100 // Draw the page bounding box 101 g2d.drawRect(0,0,(int)pageW,(int)pageH); 102 103 // Select the smaller scaling factor so that the figure 104 // fits on the page in both dimensions 105 final double scale = Math.min( (pageW/boxWidth), (pageH/boxHeight) ); 106 107 if(scale < 1.0) g2d.scale(scale, scale); 108 109 // Paint the scaled component on the printer 110 g2d.fillRect(rect.x, rect.y, rect.width, rect.height); 111 112 return(PAGE_EXISTS); 113 } 114 } 115 116 class Sysout { 117 private static TestDialog dialog; 118 createDialogWithInstructions( String[] instructions )119 public static void createDialogWithInstructions( String[] instructions ) 120 { 121 dialog = new TestDialog( new Frame(), "Instructions" ); 122 dialog.printInstructions( instructions ); 123 dialog.show(); 124 println( "Any messages for the tester will display here." ); 125 } 126 createDialog( )127 public static void createDialog( ) 128 { 129 dialog = new TestDialog( new Frame(), "Instructions" ); 130 String[] defInstr = { "Instructions will appear here. ", "" } ; 131 dialog.printInstructions( defInstr ); 132 dialog.show(); 133 println( "Any messages for the tester will display here." ); 134 } 135 136 printInstructions( String[] instructions )137 public static void printInstructions( String[] instructions ) 138 { 139 dialog.printInstructions( instructions ); 140 } 141 142 println( String messageIn )143 public static void println( String messageIn ) 144 { 145 dialog.displayMessage( messageIn ); 146 } 147 148 }// Sysout class 149 150 /** 151 This is part of the standard test machinery. It provides a place for the 152 test instructions to be displayed, and a place for interactive messages 153 to the user to be displayed. 154 To have the test instructions displayed, see Sysout. 155 To have a message to the user be displayed, see Sysout. 156 Do not call anything in this dialog directly. 157 */ 158 class TestDialog extends Dialog { 159 160 TextArea instructionsText; 161 TextArea messageText; 162 int maxStringLength = 80; 163 164 //DO NOT call this directly, go through Sysout TestDialog( Frame frame, String name )165 public TestDialog( Frame frame, String name ) 166 { 167 super( frame, name ); 168 int scrollBoth = TextArea.SCROLLBARS_BOTH; 169 instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth ); 170 add( "North", instructionsText ); 171 172 messageText = new TextArea( "", 5, maxStringLength, scrollBoth ); 173 add("Center", messageText); 174 175 pack(); 176 177 show(); 178 }// TestDialog() 179 180 //DO NOT call this directly, go through Sysout printInstructions( String[] instructions )181 public void printInstructions( String[] instructions ) 182 { 183 //Clear out any current instructions 184 instructionsText.setText( "" ); 185 186 //Go down array of instruction strings 187 188 String printStr, remainingStr; 189 for( int i=0; i < instructions.length; i++ ) 190 { 191 //chop up each into pieces maxSringLength long 192 remainingStr = instructions[ i ]; 193 while( remainingStr.length() > 0 ) 194 { 195 //if longer than max then chop off first max chars to print 196 if( remainingStr.length() >= maxStringLength ) 197 { 198 //Try to chop on a word boundary 199 int posOfSpace = remainingStr. 200 lastIndexOf( ' ', maxStringLength - 1 ); 201 202 if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1; 203 204 printStr = remainingStr.substring( 0, posOfSpace + 1 ); 205 remainingStr = remainingStr.substring( posOfSpace + 1 ); 206 } 207 //else just print 208 else 209 { 210 printStr = remainingStr; 211 remainingStr = ""; 212 } 213 214 instructionsText.append( printStr + "\n" ); 215 216 }// while 217 218 }// for 219 220 }//printInstructions() 221 222 //DO NOT call this directly, go through Sysout displayMessage( String messageIn )223 public void displayMessage( String messageIn ) 224 { 225 messageText.append( messageIn + "\n" ); 226 } 227 228 }// TestDialog class 229