1 /* 2 * Copyright (c) 2007, 2014, 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 import javax.print.PrintService; 25 import javax.print.attribute.PrintServiceAttributeSet; 26 import java.awt.*; 27 import java.awt.geom.Line2D; 28 import java.awt.geom.Rectangle2D; 29 import java.awt.geom.RoundRectangle2D; 30 import java.awt.print.*; 31 32 /* 33 * @test 34 * @summary Check that PrinterJob constructor and methods do not throw unexpected 35 * exceptions in headless mode 36 * @run main/othervm -Djava.awt.headless=true HeadlessPrinterJob 37 */ 38 39 public class HeadlessPrinterJob { 40 41 class testPrintable implements Printable { 42 print(Graphics graphics, PageFormat pageFormat, int pageIndex)43 public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) { 44 Graphics2D g2 = (Graphics2D) graphics; 45 46 if (pageIndex >= 10) { 47 return Printable.NO_SUCH_PAGE; 48 } 49 50 int gridWidth = 400 / 6; 51 int gridHeight = 300 / 2; 52 53 int rowspacing = 5; 54 int columnspacing = 7; 55 int rectWidth = gridWidth - columnspacing; 56 int rectHeight = gridHeight - rowspacing; 57 58 Color fg3D = Color.lightGray; 59 60 g2.setPaint(fg3D); 61 g2.drawRect(80, 80, 400 - 1, 310); 62 g2.setPaint(Color.black); 63 64 int x = 85; 65 int y = 87; 66 67 68 // draw Line2D.Double 69 g2.draw(new Line2D.Double(x, y + rectHeight - 1, x + rectWidth, y)); 70 x += gridWidth; 71 72 // draw Rectangle2D.Double 73 //g2.setStroke(stroke); 74 g2.draw(new Rectangle2D.Double(x, y, rectWidth, rectHeight)); 75 x += gridWidth; 76 77 // draw RoundRectangle2D.Double 78 //g2.setStroke(dashed); 79 g2.draw(new RoundRectangle2D.Double(x, y, rectWidth, 80 rectHeight, 10, 10)); 81 return Printable.PAGE_EXISTS; 82 } 83 } 84 85 class testPageable implements Pageable { 86 getNumberOfPages()87 public int getNumberOfPages() { 88 return 10; 89 } 90 getPageFormat(int pageIndex)91 public PageFormat getPageFormat(int pageIndex) throws IndexOutOfBoundsException { 92 PageFormat pf = null; 93 if (pageIndex >= 10) { 94 throw new IndexOutOfBoundsException("Wrong page#"); 95 } 96 switch (pageIndex) { 97 case 0: 98 case 2: 99 case 4: 100 case 6: 101 case 8: 102 pf = new PageFormat(); 103 pf.setOrientation(PageFormat.REVERSE_LANDSCAPE); 104 break; 105 case 1: 106 case 3: 107 case 5: 108 case 7: 109 case 9: 110 pf = new PageFormat(); 111 pf.setOrientation(PageFormat.LANDSCAPE); 112 break; 113 } 114 return pf; 115 } 116 getPrintable(int pageIndex)117 public Printable getPrintable(int pageIndex) throws IndexOutOfBoundsException { 118 if (pageIndex >= 10) { 119 throw new IndexOutOfBoundsException("Wrong page#"); 120 } 121 return new testPrintable(); 122 } 123 } 124 main(String args[])125 public static void main(String args[]) throws Exception { 126 new HeadlessPrinterJob().doTest(); 127 } 128 doTest()129 void doTest() throws Exception { 130 PrinterJob pj = PrinterJob.getPrinterJob(); 131 for (PrintService psl : pj.lookupPrintServices()) { 132 PrintServiceAttributeSet psas = psl.getAttributes(); 133 pj.setPrintService(psl); 134 } 135 PrintService ps = pj.getPrintService(); 136 pj.setPrintable(new testPrintable()); 137 138 pj = PrinterJob.getPrinterJob(); 139 PageFormat pf = new PageFormat(); 140 pf.setOrientation(PageFormat.REVERSE_LANDSCAPE); 141 pj.setPrintable(new testPrintable(), pf); 142 pj.setPageable(new testPageable()); 143 144 boolean exceptions = false; 145 try { 146 pj.printDialog(); 147 } catch (HeadlessException e) { 148 exceptions = true; 149 } 150 if (!exceptions) 151 throw new RuntimeException("HeadlessException did not occur when expected"); 152 153 exceptions = false; 154 try { 155 pj = PrinterJob.getPrinterJob(); 156 pf = new PageFormat(); 157 pf.setOrientation(PageFormat.REVERSE_LANDSCAPE); 158 pf = pj.pageDialog(pf); 159 } catch (HeadlessException e) { 160 exceptions = true; 161 } 162 if (!exceptions) 163 throw new RuntimeException("HeadlessException did not occur when expected"); 164 165 pf = new PageFormat(); 166 pf.setOrientation(PageFormat.REVERSE_LANDSCAPE); 167 pf = pj.defaultPage(pf); 168 pf = pj.defaultPage(); 169 170 pf = new PageFormat(); 171 pf.setOrientation(PageFormat.REVERSE_LANDSCAPE); 172 pf = pj.validatePage(pf); 173 pj.setCopies(10); 174 pj.getCopies(); 175 pj.getUserName(); 176 pj.setJobName("no-job-name"); 177 pj.getJobName(); 178 } 179 } 180