1 /*
   2  * Copyright (c) 2007, 2014, 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 
  24 
  25 import java.awt.*;
  26 import java.awt.event.ActionEvent;
  27 import java.awt.event.ActionListener;
  28 import java.awt.print.*;
  29 
  30 
  31 public class Test {
  32 
  33     class CustomFrame extends Frame {
  34         public CustomFrame() {
  35             super();
  36             setTitle("Frame");
  37             setSize(150, 100);
  38             Button dummy = new Button("Dummy");
  39             add(dummy);
  40         }
  41     }
  42 
  43     class CustomWindow extends Window {
  44 
  45         private void GUI() {
  46             setSize(150, 100);
  47             Button dummy = new Button("Dummy");
  48             add(dummy);
  49         }
  50 
  51         public CustomWindow(Dialog d) {
  52             super(d);
  53             GUI();
  54         }
  55 
  56         public CustomWindow(Frame f) {
  57             super(f);
  58             GUI();
  59         }
  60     }
  61 
  62     private class CustomDialog extends Dialog implements ActionListener {
  63 
  64         private Button open, close;
  65 
  66         private void GUI() {
  67             setTitle("Dialog");
  68             setSize(150, 100);
  69 
  70             Panel p = new Panel();
  71             p.setLayout(new GridLayout(1, 2));
  72             open = new Button("Open");
  73             open.addActionListener(this);
  74             p.add(open);
  75             close = new Button("Finish");
  76             close.addActionListener(this);
  77             p.add(close);
  78             add(p);
  79         }
  80 
  81         public CustomDialog(Dialog d) {
  82             super(d);
  83             GUI();
  84         }
  85 
  86         public CustomDialog(Frame f) {
  87             super(f);
  88             GUI();
  89         }
  90 
  91         @Override
  92         public void actionPerformed(ActionEvent e) {
  93             if (open.equals(e.getSource())) {
  94                 if (isPrintDialog) {
  95                     PrinterJob.getPrinterJob().printDialog();
  96                 } else {
  97                     PrinterJob.getPrinterJob().pageDialog(new PageFormat());
  98                 }
  99             } else if (close.equals(e.getSource())) {
 100                 if (parentDialog != null) { parentDialog.dispose(); }
 101                 if ( parentFrame != null) {  parentFrame.dispose(); }
 102                 if (parent != null) { parent.dispose(); }
 103                 if (dialog != null) { dialog.dispose(); }
 104                 if ( frame != null) {  frame.dispose(); }
 105                 if (window != null) { window.dispose(); }
 106             }
 107         }
 108     }
 109 
 110     class ParentDialog extends Dialog {
 111         public ParentDialog() {
 112             super((Frame) null);
 113             setTitle("Dialog");
 114             setSize(150, 100);
 115             Button dummy = new Button("Dummy");
 116             add(dummy);
 117         }
 118     }
 119 
 120     private CustomFrame  frame;
 121     private CustomWindow window;
 122     private CustomDialog dialog;
 123     private ParentDialog parent;
 124 
 125     private boolean isPrintDialog;
 126 
 127     private final Dialog.ModalityType modalityType;
 128     private final boolean setModal;
 129 
 130     public enum DialogParent
 131         {NULL_FRAME, HIDDEN_FRAME, NULL_DIALOG, HIDDEN_DIALOG, FRAME, DIALOG};
 132     private final DialogParent dialogParent;
 133 
 134     private Dialog parentDialog;
 135     private Frame  parentFrame;
 136 
 137     public Test(boolean             isPrintDlg,
 138                 Dialog.ModalityType type,
 139                 DialogParent        p){
 140         isPrintDialog = isPrintDlg;
 141         modalityType = type;
 142         setModal = false;
 143         dialogParent = p;
 144         EventQueue.invokeLater( this::createGUI );
 145     }
 146 
 147     public Test(boolean      isPrintDlg,
 148                 boolean      modal,
 149                 DialogParent p) {
 150         isPrintDialog = isPrintDlg;
 151         modalityType = null;
 152         setModal = modal;
 153         dialogParent = p;
 154         EventQueue.invokeLater( this::createGUI );
 155     }
 156 
 157     private void createGUI() {
 158 
 159         Window p;
 160 
 161         if (dialogParent == DialogParent.DIALOG) {
 162             parent = new ParentDialog();
 163             window = new CustomWindow(parent);
 164             p = parent;
 165         } else {
 166             frame = new CustomFrame();
 167             window = new CustomWindow(frame);
 168             p = frame;
 169         }
 170 
 171         int x = 50, y = 50;
 172         p.setLocation(x, y);
 173 
 174         y += (50 + p.getHeight());
 175         window.setLocation(x, y);
 176 
 177         switch (dialogParent) {
 178             case NULL_DIALOG:
 179                 dialog = new CustomDialog((Dialog) null);
 180                 break;
 181             case NULL_FRAME:
 182                 dialog = new CustomDialog((Frame) null);
 183                 break;
 184             case HIDDEN_DIALOG:
 185                 parentDialog = new Dialog((Frame) null);
 186                 dialog = new CustomDialog(parentDialog);
 187                 break;
 188             case HIDDEN_FRAME:
 189                 parentFrame = new Frame();
 190                 dialog = new CustomDialog(parentFrame);
 191                 break;
 192             case DIALOG:
 193                 dialog = new CustomDialog(parent);
 194             case FRAME:
 195                 dialog = new CustomDialog(frame);
 196                 break;
 197         }
 198 
 199         y += (50 + dialog.getHeight());
 200         dialog.setLocation(x, y);
 201 
 202         if (modalityType == null) {
 203             dialog.setModal(setModal);
 204         } else {
 205             dialog.setModalityType(modalityType);
 206         }
 207     }
 208 
 209     public void start() {
 210         EventQueue.invokeLater(() -> {
 211             if (parent != null)     { parent.setVisible(true); }
 212             else if (frame != null) {  frame.setVisible(true); }
 213         });
 214         EventQueue.invokeLater(() -> { window.setVisible(true); });
 215         EventQueue.invokeLater(() -> { dialog.setVisible(true); });
 216     }
 217 }