1/* 2 * Copyright (c) 2009, 2013, 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 6362683 8012381 27 * @summary Collation should work. 28 * @run main/manual Collate2DPrintingTest 29 */ 30import java.awt.*; 31import java.awt.event.*; 32import java.awt.print.*; 33import javax.print.attribute.standard.*; 34import javax.print.attribute.*; 35import javax.print.*; 36import java.io.*; 37 38public class Collate2DPrintingTest 39 extends Frame implements Doc, Printable, ActionListener { 40 41 Button print2D = new Button("2D Print"); 42 Button printMerlin = new Button("PrintService"); 43 PrinterJob pj = PrinterJob.getPrinterJob(); 44 PrintService defService = null; 45 HashPrintRequestAttributeSet prSet = new HashPrintRequestAttributeSet(); 46 47 public Collate2DPrintingTest() { 48 49 Panel butPanel = new Panel(); 50 butPanel.add(print2D); 51 butPanel.add(printMerlin); 52 print2D.addActionListener(this); 53 printMerlin.addActionListener(this); 54 addWindowListener (new WindowAdapter() { 55 public void windowClosing (WindowEvent e) { 56 dispose(); 57 } 58 }); 59 add("South", butPanel); 60 61 defService = PrintServiceLookup.lookupDefaultPrintService(); 62 PrintService[] pservice; 63 if (defService == null) { 64 pservice = PrintServiceLookup.lookupPrintServices(null, null); 65 if (pservice.length == 0) { 66 throw new RuntimeException("No printer found. TEST ABORTED"); 67 } 68 defService = pservice[0]; 69 } 70 prSet.add(SheetCollate.COLLATED); 71 prSet.add(new Copies(2)); 72 pj.setPrintable(Collate2DPrintingTest.this); 73 setSize(300, 200); 74 setVisible(true); 75 } 76 77 78 public int print(Graphics g, PageFormat pf, int pageIndex) 79 throws PrinterException { 80 g.drawString("Page: " + pageIndex, 100, 100); 81 if (pageIndex == 2) { 82 return Printable.NO_SUCH_PAGE; 83 } else { 84 return Printable.PAGE_EXISTS; 85 } 86 } 87 88 public void actionPerformed (ActionEvent ae) { 89 try { 90 if (ae.getSource() == print2D) { 91 if (pj.printDialog(prSet)) { 92 pj.print(prSet); 93 } 94 } else { 95 DocPrintJob pj = defService.createPrintJob(); 96 pj.print(this, prSet); 97 } 98 System.out.println ("DONE"); 99 } catch (Exception e) { 100 e.printStackTrace(); 101 } 102 } 103 104 public DocAttributeSet getAttributes() { 105 return null; 106 } 107 108 public DocFlavor getDocFlavor() { 109 DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE; 110 return flavor; 111 } 112 113 public Object getPrintData() { 114 return this; 115 } 116 117 public Reader getReaderForText() { 118 return null; 119 } 120 121 public InputStream getStreamForBytes() { 122 return null; 123 } 124 125 public static void main( String[] args) { 126 127 String[] instructions = 128 { 129 "You must have a printer available to perform this test", 130 "The print result should be collated." 131 }; 132 Sysout.createDialog( ); 133 Sysout.printInstructions( instructions ); 134 135 new Collate2DPrintingTest(); 136 } 137} 138 139 140class Sysout { 141 private static TestDialog dialog; 142 143 public static void createDialogWithInstructions( String[] instructions ) 144 { 145 dialog = new TestDialog( new Frame(), "Instructions" ); 146 dialog.printInstructions( instructions ); 147 dialog.setVisible(true); 148 println( "Any messages for the tester will display here." ); 149 } 150 151 public static void createDialog( ) 152 { 153 dialog = new TestDialog( new Frame(), "Instructions" ); 154 String[] defInstr = { "Instructions will appear here. ", "" } ; 155 dialog.printInstructions( defInstr ); 156 dialog.setVisible(true); 157 println( "Any messages for the tester will display here." ); 158 } 159 160 161 public static void printInstructions( String[] instructions ) 162 { 163 dialog.printInstructions( instructions ); 164 } 165 166 167 public static void println( String messageIn ) 168 { 169 dialog.displayMessage( messageIn ); 170 } 171 172}// Sysout class 173 174/** 175 This is part of the standard test machinery. It provides a place for the 176 test instructions to be displayed, and a place for interactive messages 177 to the user to be displayed. 178 To have the test instructions displayed, see Sysout. 179 To have a message to the user be displayed, see Sysout. 180 Do not call anything in this dialog directly. 181 */ 182class TestDialog extends Dialog { 183 184 TextArea instructionsText; 185 TextArea messageText; 186 int maxStringLength = 80; 187 188 //DO NOT call this directly, go through Sysout 189 public TestDialog( Frame frame, String name ) 190 { 191 super( frame, name ); 192 int scrollBoth = TextArea.SCROLLBARS_BOTH; 193 instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth ); 194 add( "North", instructionsText ); 195 196 messageText = new TextArea( "", 5, maxStringLength, scrollBoth ); 197 add("Center", messageText); 198 199 pack(); 200 201 setVisible(true); 202 }// TestDialog() 203 204 //DO NOT call this directly, go through Sysout 205 public void printInstructions( String[] instructions ) 206 { 207 //Clear out any current instructions 208 instructionsText.setText( "" ); 209 210 //Go down array of instruction strings 211 212 String printStr, remainingStr; 213 for( int i=0; i < instructions.length; i++ ) 214 { 215 //chop up each into pieces maxSringLength long 216 remainingStr = instructions[ i ]; 217 while( remainingStr.length() > 0 ) 218 { 219 //if longer than max then chop off first max chars to print 220 if( remainingStr.length() >= maxStringLength ) 221 { 222 //Try to chop on a word boundary 223 int posOfSpace = remainingStr. 224 lastIndexOf( ' ', maxStringLength - 1 ); 225 226 if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1; 227 228 printStr = remainingStr.substring( 0, posOfSpace + 1 ); 229 remainingStr = remainingStr.substring( posOfSpace + 1 ); 230 } 231 //else just print 232 else 233 { 234 printStr = remainingStr; 235 remainingStr = ""; 236 } 237 238 instructionsText.append( printStr + "\n" ); 239 240 }// while 241 242 }// for 243 244 }//printInstructions() 245 246 //DO NOT call this directly, go through Sysout 247 public void displayMessage( String messageIn ) 248 { 249 messageText.append( messageIn + "\n" ); 250 } 251 252 }// TestDialog class 253