jdk/test/javax/print/DialogMargins.java
changeset 4595 84bc8a58537f
child 5506 202f599c92aa
equal deleted inserted replaced
4594:4ef018d37f45 4595:84bc8a58537f
       
     1 /*
       
     2  * Copyright 2001-2010 Sun Microsystems, Inc.  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.  Sun designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Sun in the LICENSE file that accompanied this code.
       
    10  *
       
    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  *
       
    21  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    23  * have any questions.
       
    24  */
       
    25 
       
    26 /**
       
    27  * @test
       
    28  * @bug 4485755 6361370 6448717 5080051
       
    29  * @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
       
    33  * @author prr
       
    34  * @run main/manual DialogMargins
       
    35  */
       
    36 
       
    37 import java.awt.*;
       
    38 import java.awt.event.*;
       
    39 import java.awt.print.*;
       
    40 import javax.print.*;
       
    41 import javax.print.attribute.*;
       
    42 import javax.print.attribute.standard.*;
       
    43 
       
    44 public class DialogMargins extends Frame {
       
    45 
       
    46   public DialogMargins() {
       
    47      super("Dialog Margins Test");
       
    48 
       
    49     Button printButton = new Button ("Print ...");
       
    50     add("Center", printButton);
       
    51     printButton.addActionListener(new ActionListener() {
       
    52                 public void actionPerformed (ActionEvent e) {
       
    53                      new MarginsPrinter();
       
    54                 }
       
    55     });
       
    56 
       
    57     addWindowListener (new WindowAdapter() {
       
    58          public void windowClosing (WindowEvent e) {
       
    59             dispose();
       
    60          }
       
    61 
       
    62      });
       
    63 
       
    64      pack();
       
    65      setVisible (true);
       
    66   }
       
    67 
       
    68 class MarginsPrinter implements Printable {
       
    69 
       
    70   PrinterJob myPrinterJob;
       
    71   PageFormat myPageFormat;
       
    72 
       
    73   public MarginsPrinter() {
       
    74       PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
       
    75       //aset.add(MediaSizeName.ISO_A4);
       
    76       //aset.add(new MediaPrintableArea(0f,0f,210f,297f,MediaPrintableArea.MM));
       
    77       myPrinterJob = PrinterJob.getPrinterJob();
       
    78       myPageFormat = myPrinterJob.pageDialog(aset);
       
    79       myPrinterJob.setPrintable(this, myPageFormat);
       
    80       //myPrinterJob.setPrintable(this);
       
    81       if (myPrinterJob.printDialog(aset)) {
       
    82           try {
       
    83              //PrintRequestAttributeSet newaset =
       
    84                    //new HashPrintRequestAttributeSet();
       
    85               myPrinterJob.print(aset);
       
    86 
       
    87           } catch (PrinterException pe ) {
       
    88               System.out.println("DialogMargins Exception caught:" + pe);
       
    89           }
       
    90       }
       
    91    }
       
    92 
       
    93   public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) {
       
    94 
       
    95      if (pageIndex > 0) {
       
    96         return Printable.NO_SUCH_PAGE;
       
    97      }
       
    98 
       
    99      Graphics2D g2d = (Graphics2D)graphics;
       
   100      g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
       
   101      g2d.drawString("ORIGIN("+pageFormat.getImageableX()+","+
       
   102                              pageFormat.getImageableY()+")", 20, 20);
       
   103      g2d.drawString("X THIS WAY", 200, 50);
       
   104      g2d.drawString("Y THIS WAY", 60 , 200);
       
   105      g2d.drawString("Graphics is " + g2d.getClass().getName(), 100, 100);
       
   106      g2d.drawRect(0,0,(int)pageFormat.getImageableWidth(),
       
   107                       (int)pageFormat.getImageableHeight());
       
   108      g2d.setColor(Color.black);
       
   109      g2d.drawRect(1,1,(int)pageFormat.getImageableWidth()-2,
       
   110                       (int)pageFormat.getImageableHeight()-2);
       
   111 
       
   112      return  Printable.PAGE_EXISTS;
       
   113   }
       
   114 
       
   115 }
       
   116   public static void main( String[] args) {
       
   117 
       
   118   String[] instructions =
       
   119         {
       
   120          "You must have a printer available to perform this test",
       
   121          "Specify various pageformats and compare the printed results with the",
       
   122          "request."
       
   123        };
       
   124       Sysout.createDialog( );
       
   125       Sysout.printInstructions( instructions );
       
   126 
       
   127      new DialogMargins();
       
   128   }
       
   129 }
       
   130 
       
   131 
       
   132 class Sysout {
       
   133    private static TestDialog dialog;
       
   134 
       
   135    public static void createDialogWithInstructions( String[] instructions )
       
   136     {
       
   137       dialog = new TestDialog( new Frame(), "Instructions" );
       
   138       dialog.printInstructions( instructions );
       
   139       dialog.show();
       
   140       println( "Any messages for the tester will display here." );
       
   141     }
       
   142 
       
   143    public static void createDialog( )
       
   144     {
       
   145       dialog = new TestDialog( new Frame(), "Instructions" );
       
   146       String[] defInstr = { "Instructions will appear here. ", "" } ;
       
   147       dialog.printInstructions( defInstr );
       
   148       dialog.show();
       
   149       println( "Any messages for the tester will display here." );
       
   150     }
       
   151 
       
   152 
       
   153    public static void printInstructions( String[] instructions )
       
   154     {
       
   155       dialog.printInstructions( instructions );
       
   156     }
       
   157 
       
   158 
       
   159    public static void println( String messageIn )
       
   160     {
       
   161       dialog.displayMessage( messageIn );
       
   162     }
       
   163 
       
   164 }// Sysout  class
       
   165 
       
   166 /**
       
   167   This is part of the standard test machinery.  It provides a place for the
       
   168    test instructions to be displayed, and a place for interactive messages
       
   169    to the user to be displayed.
       
   170   To have the test instructions displayed, see Sysout.
       
   171   To have a message to the user be displayed, see Sysout.
       
   172   Do not call anything in this dialog directly.
       
   173   */
       
   174 class TestDialog extends Dialog {
       
   175 
       
   176    TextArea instructionsText;
       
   177    TextArea messageText;
       
   178    int maxStringLength = 80;
       
   179 
       
   180    //DO NOT call this directly, go through Sysout
       
   181    public TestDialog( Frame frame, String name )
       
   182     {
       
   183       super( frame, name );
       
   184       int scrollBoth = TextArea.SCROLLBARS_BOTH;
       
   185       instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
       
   186       add( "North", instructionsText );
       
   187 
       
   188       messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
       
   189       add("Center", messageText);
       
   190 
       
   191       pack();
       
   192 
       
   193       show();
       
   194     }// TestDialog()
       
   195 
       
   196    //DO NOT call this directly, go through Sysout
       
   197    public void printInstructions( String[] instructions )
       
   198     {
       
   199       //Clear out any current instructions
       
   200       instructionsText.setText( "" );
       
   201 
       
   202       //Go down array of instruction strings
       
   203 
       
   204       String printStr, remainingStr;
       
   205       for( int i=0; i < instructions.length; i++ )
       
   206        {
       
   207          //chop up each into pieces maxSringLength long
       
   208          remainingStr = instructions[ i ];
       
   209          while( remainingStr.length() > 0 )
       
   210           {
       
   211             //if longer than max then chop off first max chars to print
       
   212             if( remainingStr.length() >= maxStringLength )
       
   213              {
       
   214                //Try to chop on a word boundary
       
   215                int posOfSpace = remainingStr.
       
   216                   lastIndexOf( ' ', maxStringLength - 1 );
       
   217 
       
   218                if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
       
   219 
       
   220                printStr = remainingStr.substring( 0, posOfSpace + 1 );
       
   221                remainingStr = remainingStr.substring( posOfSpace + 1 );
       
   222              }
       
   223             //else just print
       
   224             else
       
   225              {
       
   226                printStr = remainingStr;
       
   227                remainingStr = "";
       
   228              }
       
   229 
       
   230             instructionsText.append( printStr + "\n" );
       
   231 
       
   232           }// while
       
   233 
       
   234        }// for
       
   235 
       
   236     }//printInstructions()
       
   237 
       
   238    //DO NOT call this directly, go through Sysout
       
   239    public void displayMessage( String messageIn )
       
   240     {
       
   241       messageText.append( messageIn + "\n" );
       
   242     }
       
   243 
       
   244  }// TestDialog  class