Path: blob/master/test/jdk/java/awt/Modal/PrintDialogsTest/Test.java
41152 views
/*1* Copyright (c) 2007, 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/222324import java.awt.*;25import java.awt.event.ActionEvent;26import java.awt.event.ActionListener;27import java.awt.print.*;282930public class Test {3132class CustomFrame extends Frame {33public CustomFrame() {34super();35setTitle("Frame");36setSize(150, 100);37Button dummy = new Button("Dummy");38add(dummy);39}40}4142class CustomWindow extends Window {4344private void GUI() {45setSize(150, 100);46Button dummy = new Button("Dummy");47add(dummy);48}4950public CustomWindow(Dialog d) {51super(d);52GUI();53}5455public CustomWindow(Frame f) {56super(f);57GUI();58}59}6061private class CustomDialog extends Dialog implements ActionListener {6263private Button open, close;6465private void GUI() {66setTitle("Dialog");67setSize(150, 100);6869Panel p = new Panel();70p.setLayout(new GridLayout(1, 2));71open = new Button("Open");72open.addActionListener(this);73p.add(open);74close = new Button("Finish");75close.addActionListener(this);76p.add(close);77add(p);78}7980public CustomDialog(Dialog d) {81super(d);82GUI();83}8485public CustomDialog(Frame f) {86super(f);87GUI();88}8990@Override91public void actionPerformed(ActionEvent e) {92if (open.equals(e.getSource())) {93if (isPrintDialog) {94PrinterJob.getPrinterJob().printDialog();95} else {96PrinterJob.getPrinterJob().pageDialog(new PageFormat());97}98} else if (close.equals(e.getSource())) {99if (parentDialog != null) { parentDialog.dispose(); }100if ( parentFrame != null) { parentFrame.dispose(); }101if (parent != null) { parent.dispose(); }102if (dialog != null) { dialog.dispose(); }103if ( frame != null) { frame.dispose(); }104if (window != null) { window.dispose(); }105}106}107}108109class ParentDialog extends Dialog {110public ParentDialog() {111super((Frame) null);112setTitle("Dialog");113setSize(150, 100);114Button dummy = new Button("Dummy");115add(dummy);116}117}118119private CustomFrame frame;120private CustomWindow window;121private CustomDialog dialog;122private ParentDialog parent;123124private boolean isPrintDialog;125126private final Dialog.ModalityType modalityType;127private final boolean setModal;128129public enum DialogParent130{NULL_FRAME, HIDDEN_FRAME, NULL_DIALOG, HIDDEN_DIALOG, FRAME, DIALOG};131private final DialogParent dialogParent;132133private Dialog parentDialog;134private Frame parentFrame;135136public Test(boolean isPrintDlg,137Dialog.ModalityType type,138DialogParent p){139isPrintDialog = isPrintDlg;140modalityType = type;141setModal = false;142dialogParent = p;143EventQueue.invokeLater( this::createGUI );144}145146public Test(boolean isPrintDlg,147boolean modal,148DialogParent p) {149isPrintDialog = isPrintDlg;150modalityType = null;151setModal = modal;152dialogParent = p;153EventQueue.invokeLater( this::createGUI );154}155156private void createGUI() {157158Window p;159160if (dialogParent == DialogParent.DIALOG) {161parent = new ParentDialog();162window = new CustomWindow(parent);163p = parent;164} else {165frame = new CustomFrame();166window = new CustomWindow(frame);167p = frame;168}169170int x = 50, y = 50;171p.setLocation(x, y);172173y += (50 + p.getHeight());174window.setLocation(x, y);175176switch (dialogParent) {177case NULL_DIALOG:178dialog = new CustomDialog((Dialog) null);179break;180case NULL_FRAME:181dialog = new CustomDialog((Frame) null);182break;183case HIDDEN_DIALOG:184parentDialog = new Dialog((Frame) null);185dialog = new CustomDialog(parentDialog);186break;187case HIDDEN_FRAME:188parentFrame = new Frame();189dialog = new CustomDialog(parentFrame);190break;191case DIALOG:192dialog = new CustomDialog(parent);193case FRAME:194dialog = new CustomDialog(frame);195break;196}197198y += (50 + dialog.getHeight());199dialog.setLocation(x, y);200201if (modalityType == null) {202dialog.setModal(setModal);203} else {204dialog.setModalityType(modalityType);205}206}207208public void start() {209EventQueue.invokeLater(() -> {210if (parent != null) { parent.setVisible(true); }211else if (frame != null) { frame.setVisible(true); }212});213EventQueue.invokeLater(() -> { window.setVisible(true); });214EventQueue.invokeLater(() -> { dialog.setVisible(true); });215}216}217218219