/*
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 *
 */
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import java.awt.Button;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Label;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Robot;
/**
 * NEED_TEST : JDK-2225361 [macosx] Problems with popup menus, tooltips and dialog boxes in dual monitor setup
 *
 * @author andrei-eremeev
 *
 */
public class DialogLocationTest {

    private static Frame frame;
    private static Button button;
    private static Dialog dialog;

    public static void main(String[] args) throws Exception {
        GraphicsDevice[] gds = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
        for (GraphicsDevice gd : gds) {
            try {
                final GraphicsConfiguration gc = gd.getDefaultConfiguration();
                EventQueue.invokeAndWait(new Runnable() {
                    public void run() {
                        frame = new Frame("Test frame", gc);
                        dialog = new Dialog(frame, true);
                        dialog.add(new Label("test"));
                        dialog.setLocationRelativeTo(frame);
                        dialog.pack();
                        button = new Button("Ok");
                        button.addActionListener(new ActionListener() {
                            @Override
                            public void actionPerformed(ActionEvent e) {
                                dialog.setVisible(true);
                            }
                        });
                        button.setPreferredSize(new Dimension(100, 100));
                        frame.add(button);
                        frame.addWindowListener(new WindowAdapter() {
                            public void windowClosing(WindowEvent e) {
                                System.exit(0);
                            }
                        });
                        frame.pack();
                        frame.setVisible(true);
                    }
                });
                delay(500);
                Robot robot = new Robot();
                System.out.println(frame.getLocation());
                Point buttonLocation = button.getLocationOnScreen();
                Dimension buttonSize = button.getSize();
                robot.mouseMove(buttonLocation.x + buttonSize.width / 2, buttonLocation.y + buttonSize.height / 2);
                delay(50);
                robot.mousePress(MouseEvent.BUTTON1_MASK);
                delay(50);
                robot.mouseRelease(MouseEvent.BUTTON1_MASK);
                delay(500);
                Point dialogLocation = dialog.getLocationOnScreen();
                Dimension dialogSize = dialog.getSize();
                Rectangle bounds = gc.getBounds();
                if (bounds.x <= dialogLocation.x && dialogLocation.x + dialogSize.width <= bounds.width &&
                    bounds.y <= dialogLocation.y && dialogLocation.y + dialogSize.height <= bounds.height) {
                    System.out.println("Dialog is located on the same display : " + bounds + ", " + dialogLocation);
                } else {
                    System.out.println("Dialog is not located on the same display : " + bounds + ", " + dialogLocation);
                    System.exit(1);
                }
            } finally {
                EventQueue.invokeAndWait(new Runnable() {
                    public void run() {
                        dialog.dispose();
                        frame.dispose();
                    }
                });
            }
        }
    }

    private static void delay(int time) throws InterruptedException {
        Thread.sleep(time);
    }
}