1 /* 2 * Copyright (c) 2001, 2011, 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 4485755 6361370 6448717 5080051 6939417 8016343 27 * @summary dialog doesn't have way to specify margins 28 * for 6361370, verify exception for offline printer in Windows 29 * for 6448717, faster display of print dialog 30 * for 6500903, verify status of printer if accepting jobs or not 31 * for 8016343, verify printing to non-default printer 32 * @author prr 33 * @run main/manual DialogMargins 34 */ 35 36 import java.awt.*; 37 import java.awt.event.*; 38 import java.awt.print.*; 39 import javax.print.*; 40 import javax.print.attribute.*; 41 import javax.print.attribute.standard.*; 42 43 public class DialogMargins extends Frame { 44 45 public DialogMargins() { 46 super("Dialog Margins Test"); 47 48 Button printButton = new Button ("Print ..."); 49 add("Center", printButton); 50 printButton.addActionListener(new ActionListener() { 51 public void actionPerformed (ActionEvent e) { 52 new MarginsPrinter(); 53 } 54 }); 55 56 addWindowListener (new WindowAdapter() { 57 public void windowClosing (WindowEvent e) { 58 dispose(); 59 } 60 61 }); 62 63 pack(); 64 setVisible (true); 65 } 66 67 class MarginsPrinter implements Printable { 68 69 PrinterJob myPrinterJob; 70 PageFormat myPageFormat; 71 72 public MarginsPrinter() { 73 PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); 74 //aset.add(MediaSizeName.ISO_A4); 75 //aset.add(new MediaPrintableArea(0f,0f,210f,297f,MediaPrintableArea.MM)); 76 myPrinterJob = PrinterJob.getPrinterJob(); 77 myPageFormat = myPrinterJob.pageDialog(aset); 78 myPrinterJob.setPrintable(this, myPageFormat); 79 //myPrinterJob.setPrintable(this); 80 if (myPrinterJob.printDialog(aset)) { 81 try { 82 //PrintRequestAttributeSet newaset = 83 //new HashPrintRequestAttributeSet(); 84 myPrinterJob.print(aset); 85 86 } catch (PrinterException pe ) { 87 System.out.println("DialogMargins Exception caught:" + pe); 88 } 89 } 90 } 91 92 public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) { 93 94 if (pageIndex > 0) { 95 return Printable.NO_SUCH_PAGE; 96 } 97 98 Graphics2D g2d = (Graphics2D)graphics; 99 g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY()); 100 g2d.drawString("ORIGIN("+pageFormat.getImageableX()+","+ 101 pageFormat.getImageableY()+")", 20, 20); 102 g2d.drawString("X THIS WAY", 200, 50); 103 g2d.drawString("Y THIS WAY", 60 , 200); 104 g2d.drawString("Graphics is " + g2d.getClass().getName(), 100, 100); 105 g2d.drawRect(0,0,(int)pageFormat.getImageableWidth(), 106 (int)pageFormat.getImageableHeight()); 107 g2d.setColor(Color.black); 108 g2d.drawRect(1,1,(int)pageFormat.getImageableWidth()-2, 109 (int)pageFormat.getImageableHeight()-2); 110 111 return Printable.PAGE_EXISTS; 112 } 113 114 } 115 public static void main( String[] args) { 116 117 String[] instructions = 118 { 119 "You must have a printer available to perform this test", 120 "Specify various pageformats and compare the printed results with the", 121 "request." 122 }; 123 Sysout.createDialog( ); 124 Sysout.printInstructions( instructions ); 125 126 new DialogMargins(); 127 } 128 } 129 130 131 class Sysout { 132 private static TestDialog dialog; 133 134 public static void createDialogWithInstructions( String[] instructions ) 135 { 136 dialog = new TestDialog( new Frame(), "Instructions" ); 137 dialog.printInstructions( instructions ); 138 dialog.show(); 139 println( "Any messages for the tester will display here." ); 140 } 141 142 public static void createDialog( ) 143 { 144 dialog = new TestDialog( new Frame(), "Instructions" ); 145 String[] defInstr = { "Instructions will appear here. ", "" } ; 146 dialog.printInstructions( defInstr ); 147 dialog.show(); 148 println( "Any messages for the tester will display here." ); 149 } 150 151 152 public static void printInstructions( String[] instructions ) 153 { 154 dialog.printInstructions( instructions ); 155 } 156 157 158 public static void println( String messageIn ) 159 { 160 dialog.displayMessage( messageIn ); 161 } 162 163 }// Sysout class 164 165 /** 166 This is part of the standard test machinery. It provides a place for the 167 test instructions to be displayed, and a place for interactive messages 168 to the user to be displayed. 169 To have the test instructions displayed, see Sysout. 170 To have a message to the user be displayed, see Sysout. 171 Do not call anything in this dialog directly. 172 */ 173 class TestDialog extends Dialog { 174 175 TextArea instructionsText; 176 TextArea messageText; 177 int maxStringLength = 80; 178 179 //DO NOT call this directly, go through Sysout 180 public TestDialog( Frame frame, String name ) 181 { 182 super( frame, name ); 183 int scrollBoth = TextArea.SCROLLBARS_BOTH; 184 instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth ); 185 add( "North", instructionsText ); 186 187 messageText = new TextArea( "", 5, maxStringLength, scrollBoth ); 188 add("Center", messageText); 189 190 pack(); 191 192 show(); 193 }// TestDialog() 194 195 //DO NOT call this directly, go through Sysout 196 public void printInstructions( String[] instructions ) 197 { 198 //Clear out any current instructions 199 instructionsText.setText( "" ); 200 201 //Go down array of instruction strings 202 203 String printStr, remainingStr; 204 for( int i=0; i < instructions.length; i++ ) 205 { 206 //chop up each into pieces maxSringLength long 207 remainingStr = instructions[ i ]; 208 while( remainingStr.length() > 0 ) 209 { 210 //if longer than max then chop off first max chars to print 211 if( remainingStr.length() >= maxStringLength ) 212 { 213 //Try to chop on a word boundary 214 int posOfSpace = remainingStr. 215 lastIndexOf( ' ', maxStringLength - 1 ); 216 217 if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1; 218 219 printStr = remainingStr.substring( 0, posOfSpace + 1 ); 220 remainingStr = remainingStr.substring( posOfSpace + 1 ); 221 } 222 //else just print 223 else 224 { 225 printStr = remainingStr; 226 remainingStr = ""; 227 } 228 229 instructionsText.append( printStr + "\n" ); 230 231 }// while 232 233 }// for 234 235 }//printInstructions() 236 237 //DO NOT call this directly, go through Sysout 238 public void displayMessage( String messageIn ) 239 { 240 messageText.append( messageIn + "\n" ); 241 } 242 243 }// TestDialog class