11/*
2- * Copyright (c) 2009, 2013 , Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 2009, 2024 , Oracle and/or its affiliates. All rights reserved.
33 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44 *
55 * This code is free software; you can redistribute it and/or modify it
2121 * questions.
2222 */
2323
24- /**
24+ import java .awt .Graphics ;
25+ import java .awt .print .PageFormat ;
26+ import java .awt .print .Printable ;
27+ import java .awt .print .PrinterException ;
28+ import java .awt .print .PrinterJob ;
29+ import java .io .InputStream ;
30+ import java .io .Reader ;
31+
32+ import javax .print .Doc ;
33+ import javax .print .DocFlavor ;
34+ import javax .print .DocPrintJob ;
35+ import javax .print .PrintException ;
36+ import javax .print .PrintService ;
37+ import javax .print .PrintServiceLookup ;
38+ import javax .print .attribute .DocAttributeSet ;
39+ import javax .print .attribute .HashPrintRequestAttributeSet ;
40+ import javax .print .attribute .standard .Copies ;
41+ import javax .print .attribute .standard .SheetCollate ;
42+ import javax .swing .BorderFactory ;
43+ import javax .swing .Box ;
44+ import javax .swing .JButton ;
45+ import javax .swing .JComponent ;
46+ import javax .swing .JOptionPane ;
47+
48+ /*
2549 * @test
2650 * @bug 6362683 8012381
2751 * @summary Collation should work.
2852 * @key printer
53+ * @library /java/awt/regtesthelpers
54+ * @build PassFailJFrame
2955 * @run main/manual Collate2DPrintingTest
3056 */
31- import java .awt .*;
32- import java .awt .event .*;
33- import java .awt .print .*;
34- import javax .print .attribute .standard .*;
35- import javax .print .attribute .*;
36- import javax .print .*;
37- import java .io .*;
38-
39- public class Collate2DPrintingTest
40- extends Frame implements Doc , Printable , ActionListener {
41-
42- Button print2D = new Button ("2D Print" );
43- Button printMerlin = new Button ("PrintService" );
44- PrinterJob pj = PrinterJob .getPrinterJob ();
45- PrintService defService = null ;
57+ public class Collate2DPrintingTest implements Doc , Printable {
58+ private static JComponent createTestUI () {
4659 HashPrintRequestAttributeSet prSet = new HashPrintRequestAttributeSet ();
60+ PrintService defService = PrintServiceLookup .lookupDefaultPrintService ();
61+ prSet .add (SheetCollate .COLLATED );
62+ prSet .add (new Copies (2 ));
4763
48- public Collate2DPrintingTest () {
49-
50- Panel butPanel = new Panel ();
51- butPanel .add (print2D );
52- butPanel .add (printMerlin );
53- print2D .addActionListener (this );
54- printMerlin .addActionListener (this );
55- addWindowListener (new WindowAdapter () {
56- public void windowClosing (WindowEvent e ) {
57- dispose ();
64+ JButton print2D = new JButton ("2D Print" );
65+ print2D .addActionListener ((ae ) -> {
66+ try {
67+ PrinterJob pj = PrinterJob .getPrinterJob ();
68+ pj .setPrintable (new Collate2DPrintingTest ());
69+ if (pj .printDialog (prSet )) {
70+ pj .print (prSet );
71+ }
72+ } catch (PrinterException ex ) {
73+ ex .printStackTrace ();
74+ String msg = "PrinterException: " + ex .getMessage ();
75+ JOptionPane .showMessageDialog (print2D , msg , "Error occurred" ,
76+ JOptionPane .ERROR_MESSAGE );
77+ PassFailJFrame .forceFail (msg );
5878 }
5979 });
60- add ("South" , butPanel );
6180
62- defService = PrintServiceLookup .lookupDefaultPrintService ();
63- PrintService [] pservice ;
64- if (defService == null ) {
65- pservice = PrintServiceLookup .lookupPrintServices (null , null );
66- if (pservice .length == 0 ) {
67- throw new RuntimeException ("No printer found. TEST ABORTED" );
81+ JButton printMerlin = new JButton ("PrintService" );
82+ printMerlin .addActionListener ((ae ) -> {
83+ try {
84+ DocPrintJob pj = defService .createPrintJob ();
85+ pj .print (new Collate2DPrintingTest (), prSet );
86+ } catch (PrintException ex ) {
87+ ex .printStackTrace ();
88+ String msg = "PrintException: " + ex .getMessage ();
89+ JOptionPane .showMessageDialog (printMerlin , msg , "Error occurred" ,
90+ JOptionPane .ERROR_MESSAGE );
91+ PassFailJFrame .forceFail (msg );
6892 }
69- defService = pservice [0 ];
70- }
71- prSet .add (SheetCollate .COLLATED );
72- prSet .add (new Copies (2 ));
73- pj .setPrintable (Collate2DPrintingTest .this );
74- setSize (300 , 200 );
75- setVisible (true );
76- }
93+ });
7794
95+ Box main = Box .createVerticalBox ();
96+ main .setBorder (BorderFactory .createEmptyBorder (8 , 8 , 8 , 8 ));
97+ main .add (Box .createVerticalGlue ());
98+ main .add (print2D );
99+ main .add (Box .createVerticalStrut (4 ));
100+ main .add (printMerlin );
101+ main .add (Box .createVerticalGlue ());
102+ return main ;
103+ }
78104
105+ @ Override
79106 public int print (Graphics g , PageFormat pf , int pageIndex )
80- throws PrinterException {
107+ throws PrinterException {
81108 g .drawString ("Page: " + pageIndex , 100 , 100 );
82109 if (pageIndex == 2 ) {
83110 return Printable .NO_SUCH_PAGE ;
@@ -86,168 +113,51 @@ public int print(Graphics g, PageFormat pf, int pageIndex)
86113 }
87114 }
88115
89- public void actionPerformed (ActionEvent ae ) {
90- try {
91- if (ae .getSource () == print2D ) {
92- if (pj .printDialog (prSet )) {
93- pj .print (prSet );
94- }
95- } else {
96- DocPrintJob pj = defService .createPrintJob ();
97- pj .print (this , prSet );
98- }
99- System .out .println ("DONE" );
100- } catch (Exception e ) {
101- e .printStackTrace ();
102- }
103- }
104-
116+ @ Override
105117 public DocAttributeSet getAttributes () {
106118 return null ;
107119 }
108120
121+ @ Override
109122 public DocFlavor getDocFlavor () {
110- DocFlavor flavor = DocFlavor .SERVICE_FORMATTED .PRINTABLE ;
111- return flavor ;
123+ return DocFlavor .SERVICE_FORMATTED .PRINTABLE ;
112124 }
113125
126+ @ Override
114127 public Object getPrintData () {
115128 return this ;
116129 }
117130
131+ @ Override
118132 public Reader getReaderForText () {
119133 return null ;
120134 }
121135
136+ @ Override
122137 public InputStream getStreamForBytes () {
123138 return null ;
124139 }
125140
126- public static void main ( String [] args ) {
127-
128- String [] instructions =
129- {
130- "You must have a printer available to perform this test" ,
131- "The print result should be collated."
132- };
133- Sysout .createDialog ( );
134- Sysout .printInstructions ( instructions );
135-
136- new Collate2DPrintingTest ();
137- }
138- }
139-
140-
141- class Sysout {
142- private static TestDialog dialog ;
143-
144- public static void createDialogWithInstructions ( String [] instructions )
145- {
146- dialog = new TestDialog ( new Frame (), "Instructions" );
147- dialog .printInstructions ( instructions );
148- dialog .setVisible (true );
149- println ( "Any messages for the tester will display here." );
150- }
151-
152- public static void createDialog ( )
153- {
154- dialog = new TestDialog ( new Frame (), "Instructions" );
155- String [] defInstr = { "Instructions will appear here. " , "" } ;
156- dialog .printInstructions ( defInstr );
157- dialog .setVisible (true );
158- println ( "Any messages for the tester will display here." );
159- }
160-
161-
162- public static void printInstructions ( String [] instructions )
163- {
164- dialog .printInstructions ( instructions );
165- }
166-
167-
168- public static void println ( String messageIn )
169- {
170- dialog .displayMessage ( messageIn );
171- }
172-
173- }// Sysout class
174-
175- /**
176- This is part of the standard test machinery. It provides a place for the
177- test instructions to be displayed, and a place for interactive messages
178- to the user to be displayed.
179- To have the test instructions displayed, see Sysout.
180- To have a message to the user be displayed, see Sysout.
181- Do not call anything in this dialog directly.
182- */
183- class TestDialog extends Dialog {
184-
185- TextArea instructionsText ;
186- TextArea messageText ;
187- int maxStringLength = 80 ;
188-
189- //DO NOT call this directly, go through Sysout
190- public TestDialog ( Frame frame , String name )
191- {
192- super ( frame , name );
193- int scrollBoth = TextArea .SCROLLBARS_BOTH ;
194- instructionsText = new TextArea ( "" , 15 , maxStringLength , scrollBoth );
195- add ( "North" , instructionsText );
196-
197- messageText = new TextArea ( "" , 5 , maxStringLength , scrollBoth );
198- add ("Center" , messageText );
199-
200- pack ();
201-
202- setVisible (true );
203- }// TestDialog()
204-
205- //DO NOT call this directly, go through Sysout
206- public void printInstructions ( String [] instructions )
207- {
208- //Clear out any current instructions
209- instructionsText .setText ( "" );
210-
211- //Go down array of instruction strings
212-
213- String printStr , remainingStr ;
214- for ( int i =0 ; i < instructions .length ; i ++ )
215- {
216- //chop up each into pieces maxSringLength long
217- remainingStr = instructions [ i ];
218- while ( remainingStr .length () > 0 )
219- {
220- //if longer than max then chop off first max chars to print
221- if ( remainingStr .length () >= maxStringLength )
222- {
223- //Try to chop on a word boundary
224- int posOfSpace = remainingStr .
225- lastIndexOf ( ' ' , maxStringLength - 1 );
226-
227- if ( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1 ;
228-
229- printStr = remainingStr .substring ( 0 , posOfSpace + 1 );
230- remainingStr = remainingStr .substring ( posOfSpace + 1 );
231- }
232- //else just print
233- else
234- {
235- printStr = remainingStr ;
236- remainingStr = "" ;
237- }
238-
239- instructionsText .append ( printStr + "\n " );
240-
241- }// while
242-
243- }// for
244-
245- }//printInstructions()
141+ private static final String INSTRUCTIONS =
142+ "Click on the '2D Print' button.\n " +
143+ "Choose copies as '2' with 'Collated' checkbox and Print\n " +
144+ "\n " +
145+ "Click on the 'PrintService', should get a print from default printer\n " +
146+ "\n " +
147+ "If you get only one copy or non 'Collated' prints from any of the above cases, " +
148+ "test failed" ;
149+
150+ public static void main (String [] args ) throws Exception {
151+ if (PrinterJob .lookupPrintServices ().length == 0 ) {
152+ throw new RuntimeException ("Printer not configured or available." );
153+ }
246154
247- //DO NOT call this directly, go through Sysout
248- public void displayMessage ( String messageIn )
249- {
250- messageText .append ( messageIn + "\n " );
155+ PassFailJFrame .builder ()
156+ .instructions (INSTRUCTIONS )
157+ .splitUI (Collate2DPrintingTest ::createTestUI )
158+ .rows ((int ) INSTRUCTIONS .lines ().count () + 1 )
159+ .columns (45 )
160+ .build ()
161+ .awaitAndCheck ();
251162 }
252-
253- }// TestDialog class
163+ }
0 commit comments