
import java.awt.*;
import java.awt.event.*;

public final class Test {

    final static Object monitor = new Object();
    static boolean flag = false;
    
    private static void test1() throws Exception {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                final Dialog dialog = new Dialog(new Dialog((Frame) null));
                dialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
                final Frame frame = new Frame();
                final Window window = new Window(frame);
                frame.setLocation(0, 450);
                frame.setSize(100, 100);
                dialog.setLocation(200, 450);
                dialog.setSize(100, 100);
                window.setLocation(400, 450);
                window.setSize(100, 100);
                Button frameButton = new Button("Open");
                frameButton.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        window.setVisible(true);
                    }
                });
                Button closeButton = new Button("Close");
                closeButton.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        frame.dispose();
                        dialog.dispose();
                        window.dispose();
                        synchronized (monitor) {
                            flag = true;
                            monitor.notify();
                        }
                    }
                });
                Button dialogButton = new Button("Open");
                dialogButton.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        FileDialog fd = new FileDialog((Frame) null);
                        fd.setVisible(true);
                    }
                });
                Button windowButton = new Button("Open");
                windowButton.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        dialog.setVisible(true);
                    }
                });
                frame.add(frameButton);
                Panel panel = new Panel();
                panel.add(dialogButton);
                panel.add(closeButton);
                dialog.add(panel);
                window.add(windowButton);
                frame.setVisible(true);
            }
        });
    }
    
    private static void test2() throws Exception {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                Frame frame = new Frame();
                final Dialog dialog = new Dialog(frame);
                dialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
                final Window window = new Window(frame);
                frame.setLocation(0, 450);
                frame.setSize(100, 100);
                dialog.setLocation(200, 450);
                dialog.setSize(100, 100);
                window.setLocation(400, 450);
                window.setSize(100, 100);
                Button frameButton = new Button("Open");
                frameButton.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        window.setVisible(true);
                    }
                });
                Button dialogButton = new Button("Open");
                dialogButton.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        FileDialog fd = new FileDialog((Frame) null);
                        fd.setVisible(true);
                    }
                });
                Button windowButton = new Button("Open");
                windowButton.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        dialog.setVisible(true);
                    }
                });
                frame.add(frameButton);
                dialog.add(dialogButton);
                window.add(windowButton);
                frame.setVisible(true);
            }
        });
    }
    
    public static void main(String[] args) throws Exception {
        test1();
        while (!flag) {
            synchronized (monitor) {
                monitor.wait();
            }
        }
        test2();
    }
}