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