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 4197377 27 * @bug 4299145 28 * @bug 6358747 29 * @bug 6574633 30 * @summary Page setup dialog settings 31 * @author prr 32 * @run main/manual PageSetupDialog 33 */ 34 35import java.awt.*; 36import java.awt.event.*; 37import java.awt.print.*; 38 39public class PageSetupDialog extends Frame implements Printable { 40 41 PrinterJob myPrinterJob; 42 PageFormat myPageFormat; 43 Label pw, ph, pglm, pgiw, pgrm, pgtm, pgih, pgbm; 44 Label myWidthLabel; 45 Label myHeightLabel; 46 Label myImageableXLabel; 47 Label myImageableYLabel; 48 Label myImageableRightLabel; 49 Label myImageableBottomLabel; 50 Label myImageableWidthLabel; 51 Label myImageableHeightLabel; 52 Label myOrientationLabel; 53 Checkbox reverseCB; 54 boolean alpha = false; 55 boolean reverse = false; 56 57 protected void displayPageFormatAttributes() { 58 59 myWidthLabel.setText("Format Width = " + (float)myPageFormat.getWidth()); 60 myHeightLabel.setText("Format Height = " + (float)myPageFormat.getHeight()); 61 myImageableXLabel.setText 62 ("Format Left Margin = " + (float)myPageFormat.getImageableX()); 63 myImageableRightLabel.setText 64 ("Format Right Margin = " + (float)(myPageFormat.getWidth() - 65 (myPageFormat.getImageableX() + myPageFormat.getImageableWidth()))); 66 myImageableWidthLabel.setText 67 ("Format ImageableWidth = " + (float)myPageFormat.getImageableWidth()); 68 myImageableYLabel.setText 69 ("Format Top Margin = " + (float)myPageFormat.getImageableY()); 70 myImageableBottomLabel.setText 71 ("Format Bottom Margin = " + (float)(myPageFormat.getHeight() - 72 (myPageFormat.getImageableY() + myPageFormat.getImageableHeight()))); 73 myImageableHeightLabel.setText 74 ("Format ImageableHeight = " + (float)myPageFormat.getImageableHeight()); 75 int o = myPageFormat.getOrientation(); 76 if (o == PageFormat.LANDSCAPE && reverse) { 77 o = PageFormat.REVERSE_LANDSCAPE; 78 myPageFormat.setOrientation(PageFormat.REVERSE_LANDSCAPE); 79 } else if (o == PageFormat.REVERSE_LANDSCAPE && !reverse) { 80 o = PageFormat.LANDSCAPE; 81 myPageFormat.setOrientation(PageFormat.LANDSCAPE); 82 } 83 myOrientationLabel.setText 84 ("Format Orientation = " + 85 (o == PageFormat.PORTRAIT ? "PORTRAIT" : 86 o == PageFormat.LANDSCAPE ? "LANDSCAPE" : 87 o == PageFormat.REVERSE_LANDSCAPE ? "REVERSE_LANDSCAPE" : 88 "<invalid>")); 89 Paper p = myPageFormat.getPaper(); 90 pw.setText("Paper Width = " + (float)p.getWidth()); 91 ph.setText("Paper Height = " + (float)p.getHeight()); 92 pglm.setText("Paper Left Margin = " + (float)p.getImageableX()); 93 pgiw.setText("Paper Imageable Width = " + (float)p.getImageableWidth()); 94 pgrm.setText("Paper Right Margin = " + 95 (float)(p.getWidth() - (p.getImageableX()+p.getImageableWidth()))); 96 pgtm.setText("Paper Top Margin = " + (float)p.getImageableY()); 97 pgih.setText("Paper Imageable Height = " + (float)p.getImageableHeight()); 98 pgbm.setText("Paper Bottom Margin = " + 99 (float)(p.getHeight() - (p.getImageableY()+p.getImageableHeight()))); 100 } 101 102 public PageSetupDialog() { 103 super ("Page Dialog Test"); 104 myPrinterJob = PrinterJob.getPrinterJob(); 105 myPageFormat = new PageFormat(); 106 Paper p = new Paper(); 107 double margin = 1.5*72; 108 p.setImageableArea(margin, margin, 109 p.getWidth()-2*margin, p.getHeight()-2*margin); 110 myPageFormat.setPaper(p); 111 Panel c = new Panel(); 112 c.setLayout (new GridLayout (9, 2, 0, 0)); 113 c.add (reverseCB = new Checkbox("reverse if landscape")); 114 c.add (myOrientationLabel = new Label()); 115 c.add (myWidthLabel = new Label()); 116 c.add (pw = new Label()); 117 c.add (myImageableXLabel = new Label()); 118 c.add (pglm = new Label()); 119 c.add (myImageableRightLabel = new Label()); 120 c.add (pgrm = new Label()); 121 c.add (myImageableWidthLabel = new Label()); 122 c.add (pgiw = new Label()); 123 c.add (myHeightLabel = new Label()); 124 c.add (ph = new Label()); 125 c.add (myImageableYLabel = new Label()); 126 c.add (pgtm = new Label()); 127 c.add (myImageableHeightLabel = new Label()); 128 c.add (pgih = new Label()); 129 c.add (myImageableBottomLabel = new Label()); 130 c.add (pgbm = new Label()); 131 132 reverseCB.addItemListener(new ItemListener() { 133 public void itemStateChanged(ItemEvent e) { 134 reverse = e.getStateChange() == ItemEvent.SELECTED; 135 int o = myPageFormat.getOrientation(); 136 if (o == PageFormat.LANDSCAPE || 137 o == PageFormat.REVERSE_LANDSCAPE) { 138 displayPageFormatAttributes(); 139 } 140 } 141 }); 142 143 add("Center", c); 144 displayPageFormatAttributes(); 145 Panel panel = new Panel(); 146 Button pageButton = new Button ("Page Setup..."); 147 pageButton.addActionListener(new ActionListener() { 148 public void actionPerformed (ActionEvent e) { 149 myPageFormat = myPrinterJob.pageDialog (myPageFormat); 150 displayPageFormatAttributes(); 151 } 152 }); 153 Button printButton = new Button ("Print ..."); 154 printButton.addActionListener(new ActionListener() { 155 public void actionPerformed (ActionEvent e) { 156 try { 157 if (myPrinterJob.printDialog()) { 158 myPrinterJob.setPrintable(PageSetupDialog.this, 159 myPageFormat); 160 alpha = false; 161 myPrinterJob.print(); 162 } 163 } catch (PrinterException pe ) { 164 } 165 } 166 }); 167 Button printAlphaButton = new Button ("Print w/Alpha..."); 168 printAlphaButton.addActionListener(new ActionListener() { 169 public void actionPerformed (ActionEvent e) { 170 try { 171 if (myPrinterJob.printDialog()) { 172 myPrinterJob.setPrintable(PageSetupDialog.this, 173 myPageFormat); 174 alpha = true; 175 myPrinterJob.print(); 176 } 177 } catch (PrinterException pe ) { 178 } 179 } 180 }); 181 panel.add (pageButton); 182 panel.add (printButton); 183 panel.add (printAlphaButton); 184 add("South", panel); 185 addWindowListener (new WindowAdapter() { 186 public void windowClosing (WindowEvent e) { 187 dispose(); 188 System.exit (0); 189 } 190 191 }); 192 //setSize (280, 550); 193 pack(); 194 setVisible (true); 195 } 196 197 public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) { 198 199 if (pageIndex > 0) { 200 return Printable.NO_SUCH_PAGE; 201 } 202 203 Graphics2D g2d = (Graphics2D)graphics; 204 g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY()); 205 g2d.drawString("ORIGIN("+pageFormat.getImageableX()+","+ 206 pageFormat.getImageableY()+")", 20, 20); 207 g2d.drawString("X THIS WAY", 200, 50); 208 g2d.drawString("Y THIS WAY", 60 , 200); 209 g2d.drawString("Graphics is " + g2d.getClass().getName(), 100, 100); 210 g2d.drawRect(0,0,(int)pageFormat.getImageableWidth(), 211 (int)pageFormat.getImageableHeight()); 212 if (alpha) { 213 g2d.setColor(new Color(0,0,255,192)); 214 } else { 215 g2d.setColor(Color.blue); 216 } 217 g2d.drawRect(1,1,(int)pageFormat.getImageableWidth()-2, 218 (int)pageFormat.getImageableHeight()-2); 219 220 return Printable.PAGE_EXISTS; 221 } 222 223 public static void main( String[] args) { 224 225 String[] instructions = 226 { 227 "You must have a printer available to perform this test", 228 "This test is very flexible and requires much interaction.", 229 "If the platform print dialog supports it, adjust orientation", 230 "and margins and print pages and compare the results with the", 231 "request." 232 }; 233 Sysout.createDialog( ); 234 Sysout.printInstructions( instructions ); 235 236 new PageSetupDialog(); 237 } 238 239} 240 241class Sysout { 242 private static TestDialog dialog; 243 244 public static void createDialogWithInstructions( String[] instructions ) 245 { 246 dialog = new TestDialog( new Frame(), "Instructions" ); 247 dialog.printInstructions( instructions ); 248 dialog.show(); 249 println( "Any messages for the tester will display here." ); 250 } 251 252 public static void createDialog( ) 253 { 254 dialog = new TestDialog( new Frame(), "Instructions" ); 255 String[] defInstr = { "Instructions will appear here. ", "" } ; 256 dialog.printInstructions( defInstr ); 257 dialog.show(); 258 println( "Any messages for the tester will display here." ); 259 } 260 261 262 public static void printInstructions( String[] instructions ) 263 { 264 dialog.printInstructions( instructions ); 265 } 266 267 268 public static void println( String messageIn ) 269 { 270 dialog.displayMessage( messageIn ); 271 } 272 273}// Sysout class 274 275/** 276 This is part of the standard test machinery. It provides a place for the 277 test instructions to be displayed, and a place for interactive messages 278 to the user to be displayed. 279 To have the test instructions displayed, see Sysout. 280 To have a message to the user be displayed, see Sysout. 281 Do not call anything in this dialog directly. 282 */ 283class TestDialog extends Dialog { 284 285 TextArea instructionsText; 286 TextArea messageText; 287 int maxStringLength = 80; 288 289 //DO NOT call this directly, go through Sysout 290 public TestDialog( Frame frame, String name ) 291 { 292 super( frame, name ); 293 int scrollBoth = TextArea.SCROLLBARS_BOTH; 294 instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth ); 295 add( "North", instructionsText ); 296 297 messageText = new TextArea( "", 5, maxStringLength, scrollBoth ); 298 add("Center", messageText); 299 300 pack(); 301 302 show(); 303 }// TestDialog() 304 305 //DO NOT call this directly, go through Sysout 306 public void printInstructions( String[] instructions ) 307 { 308 //Clear out any current instructions 309 instructionsText.setText( "" ); 310 311 //Go down array of instruction strings 312 313 String printStr, remainingStr; 314 for( int i=0; i < instructions.length; i++ ) 315 { 316 //chop up each into pieces maxSringLength long 317 remainingStr = instructions[ i ]; 318 while( remainingStr.length() > 0 ) 319 { 320 //if longer than max then chop off first max chars to print 321 if( remainingStr.length() >= maxStringLength ) 322 { 323 //Try to chop on a word boundary 324 int posOfSpace = remainingStr. 325 lastIndexOf( ' ', maxStringLength - 1 ); 326 327 if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1; 328 329 printStr = remainingStr.substring( 0, posOfSpace + 1 ); 330 remainingStr = remainingStr.substring( posOfSpace + 1 ); 331 } 332 //else just print 333 else 334 { 335 printStr = remainingStr; 336 remainingStr = ""; 337 } 338 339 instructionsText.append( printStr + "\n" ); 340 341 }// while 342 343 }// for 344 345 }//printInstructions() 346 347 //DO NOT call this directly, go through Sysout 348 public void displayMessage( String messageIn ) 349 { 350 messageText.append( messageIn + "\n" ); 351 } 352 353 }// TestDialog class 354