1 /*
   2  * Copyright (c) 2015, 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 import java.awt.Button;
  24 import java.awt.Component;
  25 import java.awt.Frame;
  26 import java.awt.GraphicsDevice;
  27 import java.awt.GraphicsEnvironment;
  28 import java.awt.Window;
  29 import java.awt.event.ActionEvent;
  30 import java.awt.event.ActionListener;
  31 import java.awt.event.WindowAdapter;
  32 import java.awt.event.WindowEvent;
  33 import javax.swing.JOptionPane;
  34 import javax.swing.SwingUtilities;
  35 
  36 /**
  37  * @test
  38  * @bug 8138749
  39  * @summary PrinterJob.printDialog() does not support multi-mon,
  40  *           always displayed on primary
  41  * @run main/manual MultiMonPrintDlgTest
  42  */
  43 public class MultiMonPrintDlgTest implements ActionListener {
  44 
  45     Frame primaryFrame = null;
  46     Frame secFrame = null;
  47  
  48     public MultiMonPrintDlgTest() throws Exception {
  49 
  50         GraphicsDevice gd[] = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
  51         if (gd.length <= 1) {
  52             System.out.println("This test should be run only on dual-monitor systems. Aborted!!");
  53             return;
  54         }
  55 
  56         String[] instructions =
  57             {
  58              " This test should be running on a dual-monitor setup.",
  59              "A frame will be created on each of the 2 monitor. ",
  60              "Click the Print button on the frame displayed in the non-default monitor.",
  61              "Please verify that print dialog is displayed in the same screen",
  62              "where the frame is located ie, in the non-default monitor.",
  63             };
  64 
  65         SwingUtilities.invokeAndWait(() -> {
  66             JOptionPane.showMessageDialog(
  67                     (Component) null,
  68                     instructions,
  69                     "information", JOptionPane.INFORMATION_MESSAGE);
  70         });
  71         GraphicsDevice defDev = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
  72         int x = 0;
  73         Frame f = null;
  74         for (x = 0; x < gd.length; x ++) {
  75             if (gd[x] != defDev) {
  76                 secFrame = new Frame("Screen " + x + " - secondary", gd[x].getDefaultConfiguration());
  77                 f = secFrame;
  78             } else {
  79                 primaryFrame = new Frame("Screen " + x + " - primary", gd[x].getDefaultConfiguration());
  80                 f = primaryFrame;
  81             }
  82             Button b = new Button("Print");
  83             b.addActionListener(this);
  84             f.add("South", b);
  85             f.addWindowListener (new WindowAdapter() {
  86                 public void windowClosing(WindowEvent we) {
  87                     ((Window) we.getSource()).dispose();
  88                 }
  89             });
  90             f.setSize(200, 200);
  91             f.setVisible(true);
  92         }
  93 
  94     }
  95 
  96     public void actionPerformed (ActionEvent ae) {
  97         try {
  98             javax.print.attribute.PrintRequestAttributeSet prSet = 
  99                   new javax.print.attribute.HashPrintRequestAttributeSet();
 100             java.awt.print.PrinterJob.getPrinterJob().printDialog(prSet));
 101             int dialogButton = JOptionPane.showConfirmDialog (null, 
 102                             "Did the printDialog shown in non-default monitor?",
 103                             null, JOptionPane.YES_NO_OPTION);
 104             if(dialogButton == JOptionPane.NO_OPTION) {
 105                 throw new RuntimeException("PrintDialog is shown in wrong monitor");
 106             }
 107         } finally {
 108             primaryFrame.dispose();
 109             secFrame.dispose();
 110         }
 111     }
 112     
 113     public static void main (String args[]) throws Exception {
 114         MultiMonPrintDlgTest test = new MultiMonPrintDlgTest();
 115     }
 116 }