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 4245280
27  * @summary PrinterJob not cancelled when PrinterJob.cancel() is used
28  * @author prr
29  * @run main/manual PrinterJobCancel
30  */
31 
32 import java.awt.* ;
33 import java.awt.print.* ;
34 
35 public class PrinterJobCancel extends Thread implements Printable {
36 
37   PrinterJob pj ;
38   boolean okayed;
39 
main( String args[] )40   public static void main ( String args[] ) {
41 
42      String[] instructions =
43         {
44          "Test that print job cancellation works.",
45          "You must have a printer available to perform this test.",
46          "This test silently starts a print job and while the job is",
47          "still being printed, cancels the print job",
48          "You should see a message on System.out that the job",
49          "was properly cancelled.",
50          "You will need to kill the application manually since regression",
51          "tests apparently aren't supposed to call System.exit()"
52        };
53 
54       Sysout.createDialog( );
55       Sysout.printInstructions( instructions );
56 
57       PrinterJobCancel pjc = new PrinterJobCancel() ;
58 
59       if (pjc.okayed) {
60           pjc.start();
61           try {
62                Thread.sleep(5000);
63                pjc.pj.cancel();
64           } catch ( InterruptedException e ) {
65           }
66       }
67   }
68 
PrinterJobCancel()69   public PrinterJobCancel() {
70 
71     pj = PrinterJob.getPrinterJob() ;
72     pj.setPrintable(this);
73     okayed = pj.printDialog();
74   }
75 
run()76   public void run() {
77     boolean cancelWorked = false;
78     try {
79         pj.print() ;
80     }
81     catch ( PrinterAbortException paex ) {
82       cancelWorked = true;
83       System.out.println("Job was properly cancelled and we");
84       System.out.println("got the expected PrintAbortException");
85     }
86     catch ( PrinterException prex ) {
87       System.out.println("This is wrong .. we shouldn't be here");
88       System.out.println("Looks like a test failure");
89       prex.printStackTrace() ;
90       //throw prex;
91     }
92     finally {
93        System.out.println("DONE PRINTING");
94        if (!cancelWorked) {
95            System.out.println("Looks like the test failed - we didn't get");
96            System.out.println("the expected PrintAbortException ");
97        }
98     }
99     //System.exit(0);
100   }
101 
print(Graphics g, PageFormat pagef, int pidx)102   public int print(Graphics g, PageFormat pagef, int pidx) {
103 
104      if (pidx > 5) {
105         return( Printable.NO_SUCH_PAGE ) ;
106      }
107 
108      Graphics2D g2d = (Graphics2D)g;
109      g2d.translate(pagef.getImageableX(), pagef.getImageableY());
110      g2d.setColor(Color.black);
111 
112      g2d.drawString(("This is page"+(pidx+1)), 60 , 80);
113      // Need to slow things down a bit .. important not to try this
114      // on the event dispathching thread of course.
115      try {
116           Thread.sleep(2000);
117      } catch (InterruptedException e) {
118      }
119 
120      return ( Printable.PAGE_EXISTS );
121   }
122 
123 }
124 
125 
126 class Sysout {
127    private static TestDialog dialog;
128 
createDialogWithInstructions( String[] instructions )129    public static void createDialogWithInstructions( String[] instructions )
130     {
131       dialog = new TestDialog( new Frame(), "Instructions" );
132       dialog.printInstructions( instructions );
133       dialog.show();
134       println( "Any messages for the tester will display here." );
135     }
136 
createDialog( )137    public static void createDialog( )
138     {
139       dialog = new TestDialog( new Frame(), "Instructions" );
140       String[] defInstr = { "Instructions will appear here. ", "" } ;
141       dialog.printInstructions( defInstr );
142       dialog.show();
143       println( "Any messages for the tester will display here." );
144     }
145 
146 
printInstructions( String[] instructions )147    public static void printInstructions( String[] instructions )
148     {
149       dialog.printInstructions( instructions );
150     }
151 
152 
println( String messageIn )153    public static void println( String messageIn )
154     {
155       dialog.displayMessage( messageIn );
156     }
157 
158 }// Sysout  class
159 
160 /**
161   This is part of the standard test machinery.  It provides a place for the
162    test instructions to be displayed, and a place for interactive messages
163    to the user to be displayed.
164   To have the test instructions displayed, see Sysout.
165   To have a message to the user be displayed, see Sysout.
166   Do not call anything in this dialog directly.
167   */
168 class TestDialog extends Dialog {
169 
170    TextArea instructionsText;
171    TextArea messageText;
172    int maxStringLength = 80;
173 
174    //DO NOT call this directly, go through Sysout
TestDialog( Frame frame, String name )175    public TestDialog( Frame frame, String name )
176     {
177       super( frame, name );
178       int scrollBoth = TextArea.SCROLLBARS_BOTH;
179       instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
180       add( "North", instructionsText );
181 
182       messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
183       add("Center", messageText);
184 
185       pack();
186 
187       show();
188     }// TestDialog()
189 
190    //DO NOT call this directly, go through Sysout
printInstructions( String[] instructions )191    public void printInstructions( String[] instructions )
192     {
193       //Clear out any current instructions
194       instructionsText.setText( "" );
195 
196       //Go down array of instruction strings
197 
198       String printStr, remainingStr;
199       for( int i=0; i < instructions.length; i++ )
200        {
201          //chop up each into pieces maxSringLength long
202          remainingStr = instructions[ i ];
203          while( remainingStr.length() > 0 )
204           {
205             //if longer than max then chop off first max chars to print
206             if( remainingStr.length() >= maxStringLength )
207              {
208                //Try to chop on a word boundary
209                int posOfSpace = remainingStr.
210                   lastIndexOf( ' ', maxStringLength - 1 );
211 
212                if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
213 
214                printStr = remainingStr.substring( 0, posOfSpace + 1 );
215                remainingStr = remainingStr.substring( posOfSpace + 1 );
216              }
217             //else just print
218             else
219              {
220                printStr = remainingStr;
221                remainingStr = "";
222              }
223 
224             instructionsText.append( printStr + "\n" );
225 
226           }// while
227 
228        }// for
229 
230     }//printInstructions()
231 
232    //DO NOT call this directly, go through Sysout
displayMessage( String messageIn )233    public void displayMessage( String messageIn )
234     {
235       messageText.append( messageIn + "\n" );
236     }
237 
238  }// TestDialog  class
239