blob: c046751678936f54ffdd924911a0c1cc0304b48a [file] [log] [blame]
jgodinez5d7c0922010-01-15 09:06:57 -08001/*
ohairbf91ea12011-04-06 22:06:11 -07002 * Copyright (c) 2001, 2011, Oracle and/or its affiliates. All rights reserved.
jgodinez5d7c0922010-01-15 09:06:57 -08003 * 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
ohair2283b9d2010-05-25 15:58:33 -07007 * published by the Free Software Foundation. Oracle designates this
jgodinez5d7c0922010-01-15 09:06:57 -08008 * particular file as subject to the "Classpath" exception as provided
ohair2283b9d2010-05-25 15:58:33 -07009 * by Oracle in the LICENSE file that accompanied this code.
jgodinez5d7c0922010-01-15 09:06:57 -080010 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
ohair2283b9d2010-05-25 15:58:33 -070021 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
jgodinez5d7c0922010-01-15 09:06:57 -080024 */
25
26/**
27 * @test
jgodinezcf160c62013-07-26 15:25:12 -070028 * @bug 4485755 6361370 6448717 5080051 6939417 8016343
jgodinez5d7c0922010-01-15 09:06:57 -080029 * @summary dialog doesn't have way to specify margins
30 * for 6361370, verify exception for offline printer in Windows
31 * for 6448717, faster display of print dialog
32 * for 6500903, verify status of printer if accepting jobs or not
jgodinezcf160c62013-07-26 15:25:12 -070033 * for 8016343, verify printing to non-default printer
jgodinez5d7c0922010-01-15 09:06:57 -080034 * @author prr
35 * @run main/manual DialogMargins
36 */
37
38import java.awt.*;
39import java.awt.event.*;
40import java.awt.print.*;
41import javax.print.*;
42import javax.print.attribute.*;
43import javax.print.attribute.standard.*;
44
45public class DialogMargins extends Frame {
46
47 public DialogMargins() {
48 super("Dialog Margins Test");
49
50 Button printButton = new Button ("Print ...");
51 add("Center", printButton);
52 printButton.addActionListener(new ActionListener() {
53 public void actionPerformed (ActionEvent e) {
54 new MarginsPrinter();
55 }
56 });
57
58 addWindowListener (new WindowAdapter() {
59 public void windowClosing (WindowEvent e) {
60 dispose();
61 }
62
63 });
64
65 pack();
66 setVisible (true);
67 }
68
69class MarginsPrinter implements Printable {
70
71 PrinterJob myPrinterJob;
72 PageFormat myPageFormat;
73
74 public MarginsPrinter() {
75 PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
76 //aset.add(MediaSizeName.ISO_A4);
77 //aset.add(new MediaPrintableArea(0f,0f,210f,297f,MediaPrintableArea.MM));
78 myPrinterJob = PrinterJob.getPrinterJob();
79 myPageFormat = myPrinterJob.pageDialog(aset);
80 myPrinterJob.setPrintable(this, myPageFormat);
81 //myPrinterJob.setPrintable(this);
82 if (myPrinterJob.printDialog(aset)) {
83 try {
84 //PrintRequestAttributeSet newaset =
85 //new HashPrintRequestAttributeSet();
86 myPrinterJob.print(aset);
87
88 } catch (PrinterException pe ) {
89 System.out.println("DialogMargins Exception caught:" + pe);
90 }
91 }
92 }
93
94 public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) {
95
96 if (pageIndex > 0) {
97 return Printable.NO_SUCH_PAGE;
98 }
99
100 Graphics2D g2d = (Graphics2D)graphics;
101 g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
102 g2d.drawString("ORIGIN("+pageFormat.getImageableX()+","+
103 pageFormat.getImageableY()+")", 20, 20);
104 g2d.drawString("X THIS WAY", 200, 50);
105 g2d.drawString("Y THIS WAY", 60 , 200);
106 g2d.drawString("Graphics is " + g2d.getClass().getName(), 100, 100);
107 g2d.drawRect(0,0,(int)pageFormat.getImageableWidth(),
108 (int)pageFormat.getImageableHeight());
109 g2d.setColor(Color.black);
110 g2d.drawRect(1,1,(int)pageFormat.getImageableWidth()-2,
111 (int)pageFormat.getImageableHeight()-2);
112
113 return Printable.PAGE_EXISTS;
114 }
115
116}
117 public static void main( String[] args) {
118
119 String[] instructions =
120 {
121 "You must have a printer available to perform this test",
122 "Specify various pageformats and compare the printed results with the",
123 "request."
124 };
125 Sysout.createDialog( );
126 Sysout.printInstructions( instructions );
127
128 new DialogMargins();
129 }
130}
131
132
133class Sysout {
134 private static TestDialog dialog;
135
136 public static void createDialogWithInstructions( String[] instructions )
137 {
138 dialog = new TestDialog( new Frame(), "Instructions" );
139 dialog.printInstructions( instructions );
140 dialog.show();
141 println( "Any messages for the tester will display here." );
142 }
143
144 public static void createDialog( )
145 {
146 dialog = new TestDialog( new Frame(), "Instructions" );
147 String[] defInstr = { "Instructions will appear here. ", "" } ;
148 dialog.printInstructions( defInstr );
149 dialog.show();
150 println( "Any messages for the tester will display here." );
151 }
152
153
154 public static void printInstructions( String[] instructions )
155 {
156 dialog.printInstructions( instructions );
157 }
158
159
160 public static void println( String messageIn )
161 {
162 dialog.displayMessage( messageIn );
163 }
164
165}// Sysout class
166
167/**
168 This is part of the standard test machinery. It provides a place for the
169 test instructions to be displayed, and a place for interactive messages
170 to the user to be displayed.
171 To have the test instructions displayed, see Sysout.
172 To have a message to the user be displayed, see Sysout.
173 Do not call anything in this dialog directly.
174 */
175class TestDialog extends Dialog {
176
177 TextArea instructionsText;
178 TextArea messageText;
179 int maxStringLength = 80;
180
181 //DO NOT call this directly, go through Sysout
182 public TestDialog( Frame frame, String name )
183 {
184 super( frame, name );
185 int scrollBoth = TextArea.SCROLLBARS_BOTH;
186 instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
187 add( "North", instructionsText );
188
189 messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
190 add("Center", messageText);
191
192 pack();
193
194 show();
195 }// TestDialog()
196
197 //DO NOT call this directly, go through Sysout
198 public void printInstructions( String[] instructions )
199 {
200 //Clear out any current instructions
201 instructionsText.setText( "" );
202
203 //Go down array of instruction strings
204
205 String printStr, remainingStr;
206 for( int i=0; i < instructions.length; i++ )
207 {
208 //chop up each into pieces maxSringLength long
209 remainingStr = instructions[ i ];
210 while( remainingStr.length() > 0 )
211 {
212 //if longer than max then chop off first max chars to print
213 if( remainingStr.length() >= maxStringLength )
214 {
215 //Try to chop on a word boundary
216 int posOfSpace = remainingStr.
217 lastIndexOf( ' ', maxStringLength - 1 );
218
219 if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
220
221 printStr = remainingStr.substring( 0, posOfSpace + 1 );
222 remainingStr = remainingStr.substring( posOfSpace + 1 );
223 }
224 //else just print
225 else
226 {
227 printStr = remainingStr;
228 remainingStr = "";
229 }
230
231 instructionsText.append( printStr + "\n" );
232
233 }// while
234
235 }// for
236
237 }//printInstructions()
238
239 //DO NOT call this directly, go through Sysout
240 public void displayMessage( String messageIn )
241 {
242 messageText.append( messageIn + "\n" );
243 }
244
245 }// TestDialog class