Path: blob/master/test/jdk/javax/print/DialogMargins.java
41144 views
/*1* Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 4485755 6361370 6448717 5080051 6939417 801634326* @summary dialog doesn't have way to specify margins27* for 6361370, verify exception for offline printer in Windows28* for 6448717, faster display of print dialog29* for 6500903, verify status of printer if accepting jobs or not30* for 8016343, verify printing to non-default printer31* @author prr32* @run main/manual DialogMargins33*/3435import java.awt.*;36import java.awt.event.*;37import java.awt.print.*;38import javax.print.*;39import javax.print.attribute.*;40import javax.print.attribute.standard.*;4142public class DialogMargins extends Frame {4344public DialogMargins() {45super("Dialog Margins Test");4647Button printButton = new Button ("Print ...");48add("Center", printButton);49printButton.addActionListener(new ActionListener() {50public void actionPerformed (ActionEvent e) {51new MarginsPrinter();52}53});5455addWindowListener (new WindowAdapter() {56public void windowClosing (WindowEvent e) {57dispose();58}5960});6162pack();63setVisible (true);64}6566class MarginsPrinter implements Printable {6768PrinterJob myPrinterJob;69PageFormat myPageFormat;7071public MarginsPrinter() {72PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();73//aset.add(MediaSizeName.ISO_A4);74//aset.add(new MediaPrintableArea(0f,0f,210f,297f,MediaPrintableArea.MM));75myPrinterJob = PrinterJob.getPrinterJob();76myPageFormat = myPrinterJob.pageDialog(aset);77myPrinterJob.setPrintable(this, myPageFormat);78//myPrinterJob.setPrintable(this);79if (myPrinterJob.printDialog(aset)) {80try {81//PrintRequestAttributeSet newaset =82//new HashPrintRequestAttributeSet();83myPrinterJob.print(aset);8485} catch (PrinterException pe ) {86System.out.println("DialogMargins Exception caught:" + pe);87}88}89}9091public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) {9293if (pageIndex > 0) {94return Printable.NO_SUCH_PAGE;95}9697Graphics2D g2d = (Graphics2D)graphics;98g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());99g2d.drawString("ORIGIN("+pageFormat.getImageableX()+","+100pageFormat.getImageableY()+")", 20, 20);101g2d.drawString("X THIS WAY", 200, 50);102g2d.drawString("Y THIS WAY", 60 , 200);103g2d.drawString("Graphics is " + g2d.getClass().getName(), 100, 100);104g2d.drawRect(0,0,(int)pageFormat.getImageableWidth(),105(int)pageFormat.getImageableHeight());106g2d.setColor(Color.black);107g2d.drawRect(1,1,(int)pageFormat.getImageableWidth()-2,108(int)pageFormat.getImageableHeight()-2);109110return Printable.PAGE_EXISTS;111}112113}114public static void main( String[] args) {115116String[] instructions =117{118"You must have a printer available to perform this test",119"Specify various pageformats and compare the printed results with the",120"request."121};122Sysout.createDialog( );123Sysout.printInstructions( instructions );124125new DialogMargins();126}127}128129130class Sysout {131private static TestDialog dialog;132133public static void createDialogWithInstructions( String[] instructions )134{135dialog = new TestDialog( new Frame(), "Instructions" );136dialog.printInstructions( instructions );137dialog.show();138println( "Any messages for the tester will display here." );139}140141public static void createDialog( )142{143dialog = new TestDialog( new Frame(), "Instructions" );144String[] defInstr = { "Instructions will appear here. ", "" } ;145dialog.printInstructions( defInstr );146dialog.show();147println( "Any messages for the tester will display here." );148}149150151public static void printInstructions( String[] instructions )152{153dialog.printInstructions( instructions );154}155156157public static void println( String messageIn )158{159dialog.displayMessage( messageIn );160}161162}// Sysout class163164/**165This is part of the standard test machinery. It provides a place for the166test instructions to be displayed, and a place for interactive messages167to the user to be displayed.168To have the test instructions displayed, see Sysout.169To have a message to the user be displayed, see Sysout.170Do not call anything in this dialog directly.171*/172class TestDialog extends Dialog {173174TextArea instructionsText;175TextArea messageText;176int maxStringLength = 80;177178//DO NOT call this directly, go through Sysout179public TestDialog( Frame frame, String name )180{181super( frame, name );182int scrollBoth = TextArea.SCROLLBARS_BOTH;183instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );184add( "North", instructionsText );185186messageText = new TextArea( "", 5, maxStringLength, scrollBoth );187add("Center", messageText);188189pack();190191show();192}// TestDialog()193194//DO NOT call this directly, go through Sysout195public void printInstructions( String[] instructions )196{197//Clear out any current instructions198instructionsText.setText( "" );199200//Go down array of instruction strings201202String printStr, remainingStr;203for( int i=0; i < instructions.length; i++ )204{205//chop up each into pieces maxSringLength long206remainingStr = instructions[ i ];207while( remainingStr.length() > 0 )208{209//if longer than max then chop off first max chars to print210if( remainingStr.length() >= maxStringLength )211{212//Try to chop on a word boundary213int posOfSpace = remainingStr.214lastIndexOf( ' ', maxStringLength - 1 );215216if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;217218printStr = remainingStr.substring( 0, posOfSpace + 1 );219remainingStr = remainingStr.substring( posOfSpace + 1 );220}221//else just print222else223{224printStr = remainingStr;225remainingStr = "";226}227228instructionsText.append( printStr + "\n" );229230}// while231232}// for233234}//printInstructions()235236//DO NOT call this directly, go through Sysout237public void displayMessage( String messageIn )238{239messageText.append( messageIn + "\n" );240}241242}// TestDialog class243244245