|
| 1 | +/* |
| 2 | + * Copyright (c) 1998, 2023, 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 | +import java.awt.Frame; |
| 25 | +import java.awt.FlowLayout; |
| 26 | +import java.awt.Window; |
| 27 | +import java.awt.event.ItemEvent; |
| 28 | +import java.awt.event.WindowEvent; |
| 29 | +import javax.swing.JButton; |
| 30 | +import javax.swing.JComboBox; |
| 31 | +import javax.swing.JDialog; |
| 32 | +import javax.swing.JFrame; |
| 33 | +import javax.swing.JLabel; |
| 34 | +import javax.swing.JPanel; |
| 35 | +import javax.swing.SwingUtilities; |
| 36 | +import javax.swing.WindowConstants; |
| 37 | + |
| 38 | +/* |
| 39 | + * @test |
| 40 | + * @summary test for defaultCloseOperation property for Swing JFrame and JDialog |
| 41 | + * @library /java/awt/regtesthelpers |
| 42 | + * @build PassFailJFrame |
| 43 | + * @run main/manual DefaultCloseOperation |
| 44 | + */ |
| 45 | + |
| 46 | +public class DefaultCloseOperation extends JPanel { |
| 47 | + |
| 48 | + private static final String INSTRUCTIONS = "Do the following steps:\n\n" + |
| 49 | + "- Click the \"Open Frame\" button (a TestFrame will appear)\n" + |
| 50 | + "- On the TestFrame, select \"Close\" from the system menu (the window should go away)\n" + |
| 51 | + "- Select \"Do Nothing\" from the \"JFrame Default Close Operation\" ComboBox\n" + |
| 52 | + "- Click the \"Open Frame\" button\n" + |
| 53 | + "- On the TestFrame, select \"Close\" from the system menu (the window should remain open)\n" + |
| 54 | + "- Select \"Dispose\" from the \"JFrame Default Close Operation\" ComboBox\n" + |
| 55 | + "- On the TestFrame, select \"Close\" from the system menu (the window should go away)\n\n\n" + |
| 56 | + "- Click the \"Open Frame\" button\n" + |
| 57 | + "- Click the \"Open Dialog\" button (a TestDialog will appear)\n" + |
| 58 | + "- On the TestDialog, select \"Close\" from the system menu (the window should go away)\n" + |
| 59 | + "- Select \"Do Nothing\" from the \"JDialog Default Close Operation\" ComboBox\n" + |
| 60 | + "- Click the \"Open Dialog\" button\n" + |
| 61 | + "- On the TestDialog, select \"Close\" from the system menu (the window should remain open)\n" + |
| 62 | + "- Select \"Dispose\" from the \"JDialog Default Close Operation\" ComboBox\n" + |
| 63 | + "- On the TestDialog, select \"Close\" from the system menu (the window should go away)"; |
| 64 | + |
| 65 | + JComboBox<String> frameCloseOp; |
| 66 | + |
| 67 | + CloseOpDialog testDialog; |
| 68 | + JComboBox<String> dialogCloseOp; |
| 69 | + |
| 70 | + public static void main(String[] args) throws Exception { |
| 71 | + |
| 72 | + PassFailJFrame passFailJFrame = new PassFailJFrame.Builder() |
| 73 | + .title("DefaultCloseOperation Manual Test") |
| 74 | + .instructions(INSTRUCTIONS) |
| 75 | + .testTimeOut(5) |
| 76 | + .rows(20) |
| 77 | + .columns(70) |
| 78 | + .build(); |
| 79 | + |
| 80 | + SwingUtilities.invokeAndWait(() -> { |
| 81 | + DefaultCloseOperation dco = new DefaultCloseOperation(); |
| 82 | + dco.init(); |
| 83 | + |
| 84 | + JFrame frame = new JFrame("DefaultCloseOperation"); |
| 85 | + frame.add(dco); |
| 86 | + frame.setSize(500,200); |
| 87 | + |
| 88 | + PassFailJFrame.addTestWindow(frame); |
| 89 | + PassFailJFrame |
| 90 | + .positionTestWindow(frame, PassFailJFrame.Position.HORIZONTAL); |
| 91 | + |
| 92 | + frame.setVisible(true); |
| 93 | + }); |
| 94 | + |
| 95 | + passFailJFrame.awaitAndCheck(); |
| 96 | + } |
| 97 | + |
| 98 | + public void init() { |
| 99 | + setLayout(new FlowLayout()); |
| 100 | + |
| 101 | + CloseOpFrame testFrame = new CloseOpFrame(); |
| 102 | + testFrame.setLocationRelativeTo(null); |
| 103 | + PassFailJFrame.addTestWindow(testFrame); |
| 104 | + |
| 105 | + add(new JLabel("JFrame Default Close Operation:")); |
| 106 | + frameCloseOp = new JComboBox<>(); |
| 107 | + frameCloseOp.addItem("Hide"); |
| 108 | + frameCloseOp.addItem("Do Nothing"); |
| 109 | + frameCloseOp.addItem("Dispose"); |
| 110 | + frameCloseOp.addItemListener(e -> { |
| 111 | + if (e.getStateChange() == ItemEvent.SELECTED) { |
| 112 | + String item = (String)e.getItem(); |
| 113 | + switch (item) { |
| 114 | + case "Do Nothing": |
| 115 | + testFrame |
| 116 | + .setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); |
| 117 | + break; |
| 118 | + case "Hide": |
| 119 | + testFrame |
| 120 | + .setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE); |
| 121 | + break; |
| 122 | + case "Dispose": |
| 123 | + testFrame |
| 124 | + .setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); |
| 125 | + break; |
| 126 | + } |
| 127 | + } |
| 128 | + }); |
| 129 | + add(frameCloseOp); |
| 130 | + |
| 131 | + JButton b = new JButton("Open Frame..."); |
| 132 | + b.addActionListener(e -> testFrame.setVisible(true)); |
| 133 | + add(b); |
| 134 | + |
| 135 | + testDialog = new CloseOpDialog(testFrame); |
| 136 | + testDialog.setLocationRelativeTo(null); |
| 137 | + PassFailJFrame.addTestWindow(testDialog); |
| 138 | + |
| 139 | + add(new JLabel("JDialog Default Close Operation:")); |
| 140 | + dialogCloseOp = new JComboBox<>(); |
| 141 | + dialogCloseOp.addItem("Hide"); |
| 142 | + dialogCloseOp.addItem("Do Nothing"); |
| 143 | + dialogCloseOp.addItem("Dispose"); |
| 144 | + dialogCloseOp.addItemListener(e -> { |
| 145 | + if (e.getStateChange() == ItemEvent.SELECTED) { |
| 146 | + String item = (String)e.getItem(); |
| 147 | + switch (item) { |
| 148 | + case "Do Nothing": |
| 149 | + testDialog |
| 150 | + .setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); |
| 151 | + break; |
| 152 | + case "Hide": |
| 153 | + testDialog |
| 154 | + .setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE); |
| 155 | + break; |
| 156 | + case "Dispose": |
| 157 | + testDialog |
| 158 | + .setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); |
| 159 | + break; |
| 160 | + } |
| 161 | + } |
| 162 | + }); |
| 163 | + add(dialogCloseOp); |
| 164 | + |
| 165 | + b = new JButton("Open Dialog..."); |
| 166 | + b.addActionListener(e -> testDialog.setVisible(true)); |
| 167 | + add(b); |
| 168 | + } |
| 169 | + |
| 170 | + public static void verifyCloseOperation(Window window, int op) { |
| 171 | + switch (op) { |
| 172 | + case WindowConstants.DO_NOTHING_ON_CLOSE: |
| 173 | + if (!window.isVisible()) { |
| 174 | + PassFailJFrame |
| 175 | + .forceFail("defaultCloseOperation=DoNothing failed"); |
| 176 | + } |
| 177 | + break; |
| 178 | + case WindowConstants.HIDE_ON_CLOSE: |
| 179 | + if (window.isVisible()) { |
| 180 | + PassFailJFrame |
| 181 | + .forceFail("defaultCloseOperation=Hide failed"); |
| 182 | + } |
| 183 | + break; |
| 184 | + case WindowConstants.DISPOSE_ON_CLOSE: |
| 185 | + if (window.isVisible() || window.isDisplayable()) { |
| 186 | + PassFailJFrame |
| 187 | + .forceFail("defaultCloseOperation=Dispose failed"); |
| 188 | + } |
| 189 | + break; |
| 190 | + } |
| 191 | + } |
| 192 | +} |
| 193 | + |
| 194 | +class CloseOpFrame extends JFrame { |
| 195 | + |
| 196 | + public CloseOpFrame() { |
| 197 | + super("DefaultCloseOperation Test"); |
| 198 | + getContentPane().add("Center", new JLabel("Test Frame")); |
| 199 | + pack(); |
| 200 | + } |
| 201 | + |
| 202 | + protected void processWindowEvent(WindowEvent e) { |
| 203 | + super.processWindowEvent(e); |
| 204 | + |
| 205 | + if (e.getID() == WindowEvent.WINDOW_CLOSING) { |
| 206 | + DefaultCloseOperation |
| 207 | + .verifyCloseOperation(this, getDefaultCloseOperation()); |
| 208 | + } |
| 209 | + } |
| 210 | +} |
| 211 | + |
| 212 | +class CloseOpDialog extends JDialog { |
| 213 | + |
| 214 | + public CloseOpDialog(Frame owner) { |
| 215 | + super(owner, "DefaultCloseOperation Test Dialog"); |
| 216 | + getContentPane().add("Center", new JLabel("Test Dialog")); |
| 217 | + pack(); |
| 218 | + } |
| 219 | + |
| 220 | + protected void processWindowEvent(WindowEvent e) { |
| 221 | + super.processWindowEvent(e); |
| 222 | + |
| 223 | + if (e.getID() == WindowEvent.WINDOW_CLOSING) { |
| 224 | + DefaultCloseOperation |
| 225 | + .verifyCloseOperation(this, getDefaultCloseOperation()); |
| 226 | + } |
| 227 | + } |
| 228 | +} |
0 commit comments