|
| 1 | +/* |
| 2 | + * Copyright (c) 2003, 2024, 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 | + * @test |
| 26 | + * @bug 4102292 |
| 27 | + * @summary Tests that location by platform works with other APIs |
| 28 | + * @library /java/awt/regtesthelpers |
| 29 | + * @build PassFailJFrame |
| 30 | + * @run main/manual TestLocationByPlatformWithControls |
| 31 | + */ |
| 32 | + |
| 33 | +import java.awt.BorderLayout; |
| 34 | +import java.awt.Button; |
| 35 | +import java.awt.Checkbox; |
| 36 | +import java.awt.Frame; |
| 37 | +import java.awt.Label; |
| 38 | +import java.awt.Panel; |
| 39 | +import java.awt.event.ActionEvent; |
| 40 | +import java.awt.event.ActionListener; |
| 41 | +import java.awt.event.ItemEvent; |
| 42 | +import java.awt.event.ItemListener; |
| 43 | +import java.util.Vector; |
| 44 | + |
| 45 | +public class TestLocationByPlatformWithControls extends Frame |
| 46 | + implements ActionListener, ItemListener { |
| 47 | + Panel northP; |
| 48 | + Panel centerP; |
| 49 | + Checkbox undecoratedCB; |
| 50 | + Checkbox defaultLocationCB; |
| 51 | + Checkbox visibleCB; |
| 52 | + Checkbox iconifiedCB; |
| 53 | + Checkbox maximizedCB; |
| 54 | + Button createB; |
| 55 | + Button packB; |
| 56 | + Button moveB; |
| 57 | + Button resizeB; |
| 58 | + Button reshapeB; |
| 59 | + Button disposeB; |
| 60 | + Vector frames; |
| 61 | + public static void main(String[] args) throws Exception { |
| 62 | + String INSTRUCTIONS = """ |
| 63 | + This test is to check that LocationByPlatform works with other |
| 64 | + controls API. |
| 65 | + 1) Create New Frame by clicking on "Create" Button in |
| 66 | + "TestLocationByPlatformWithControls" window. |
| 67 | + 2) Initially this Frame will not be visible, Click on checkbox |
| 68 | + "LocationByPlatform" to set default platform location for the frame |
| 69 | + and then click on checkbox "Visible" to see that Frame is displayed |
| 70 | + at default offsets. |
| 71 | + 3) Now you can play with different controls like Iconified, |
| 72 | + Maximized, Pack, Move, Resize and Reshape to verify that these |
| 73 | + controls work properly with the Frame. |
| 74 | + 4) At the end dispose the Frame by clicking on "Dispose" button. |
| 75 | + 5) Also we can do verify this for Undecorated Frame but for that we |
| 76 | + need to follow same steps but in step 2 before we click on checkbox |
| 77 | + "Visible", select "Undecorated" checkbox along with |
| 78 | + "LocationByPlatform". |
| 79 | + 6) If everything works properly test is passed, otherwise failed. |
| 80 | + """; |
| 81 | + |
| 82 | + PassFailJFrame.builder() |
| 83 | + .title("Test Instructions") |
| 84 | + .instructions(INSTRUCTIONS) |
| 85 | + .columns(40) |
| 86 | + .testUI(TestLocationByPlatformWithControls::new) |
| 87 | + .logArea(4) |
| 88 | + .build() |
| 89 | + .awaitAndCheck(); |
| 90 | + } |
| 91 | + |
| 92 | + public TestLocationByPlatformWithControls() { |
| 93 | + northP = new Panel(); |
| 94 | + centerP = new Panel(); |
| 95 | + |
| 96 | + undecoratedCB = new Checkbox("Undecorated"); |
| 97 | + defaultLocationCB = new Checkbox("LocationByPlatform"); |
| 98 | + visibleCB = new Checkbox("Visible"); |
| 99 | + iconifiedCB = new Checkbox("Iconified"); |
| 100 | + maximizedCB = new Checkbox("Maximized"); |
| 101 | + |
| 102 | + createB = new Button("Create"); |
| 103 | + packB = new Button("Pack"); |
| 104 | + moveB = new Button("Move"); |
| 105 | + resizeB = new Button("Resize"); |
| 106 | + reshapeB = new Button("Reshape"); |
| 107 | + disposeB = new Button("Dispose"); |
| 108 | + |
| 109 | + frames = new Vector(10); |
| 110 | + this.setTitle("TestLocationByPlatformWithControls"); |
| 111 | + this.setLayout(new BorderLayout()); |
| 112 | + this.add(northP, BorderLayout.NORTH); |
| 113 | + |
| 114 | + northP.add(new Label("New Frame")); |
| 115 | + |
| 116 | + createB.addActionListener(this); |
| 117 | + northP.add(createB); |
| 118 | + |
| 119 | + centerP.setEnabled(false); |
| 120 | + this.add(centerP, BorderLayout.CENTER); |
| 121 | + |
| 122 | + centerP.add(new Label("Last Frame")); |
| 123 | + |
| 124 | + centerP.add(defaultLocationCB); |
| 125 | + defaultLocationCB.addItemListener(this); |
| 126 | + |
| 127 | + centerP.add(undecoratedCB); |
| 128 | + undecoratedCB.addItemListener(this); |
| 129 | + |
| 130 | + centerP.add(iconifiedCB); |
| 131 | + iconifiedCB.addItemListener(this); |
| 132 | + |
| 133 | + centerP.add(maximizedCB); |
| 134 | + maximizedCB.addItemListener(this); |
| 135 | + |
| 136 | + centerP.add(visibleCB); |
| 137 | + visibleCB.addItemListener(this); |
| 138 | + |
| 139 | + packB.addActionListener(this); |
| 140 | + centerP.add(packB); |
| 141 | + |
| 142 | + moveB.addActionListener(this); |
| 143 | + centerP.add(moveB); |
| 144 | + |
| 145 | + resizeB.addActionListener(this); |
| 146 | + centerP.add(resizeB); |
| 147 | + |
| 148 | + reshapeB.addActionListener(this); |
| 149 | + centerP.add(reshapeB); |
| 150 | + |
| 151 | + disposeB.addActionListener(this); |
| 152 | + centerP.add(disposeB); |
| 153 | + this.pack(); |
| 154 | + } |
| 155 | + |
| 156 | + public void actionPerformed(ActionEvent e) { |
| 157 | + if (e.getSource() == createB) { |
| 158 | + Frame frame = new Frame(); |
| 159 | + frame.setSize(200, 200); |
| 160 | + frames.add(frame); |
| 161 | + updateControls(frame); |
| 162 | + Panel panel = new Panel(); |
| 163 | + frame.add(panel); |
| 164 | + panel.add(new Button ("Test Button")); |
| 165 | + panel.add(new Button ("Test Button 1")); |
| 166 | + panel.add(new Button ("Test Button 2")); |
| 167 | + panel.add(new Button ("Test Button 3")); |
| 168 | + centerP.setEnabled(true); |
| 169 | + return; |
| 170 | + } |
| 171 | + |
| 172 | + if (frames.isEmpty()) { |
| 173 | + return; |
| 174 | + } |
| 175 | + |
| 176 | + Frame last = (Frame)frames.lastElement(); |
| 177 | + |
| 178 | + if (e.getSource() == packB) { |
| 179 | + last.pack(); |
| 180 | + } else |
| 181 | + if (e.getSource() == moveB) { |
| 182 | + int x = (int)(Math.random() * 200); |
| 183 | + int y = (int)(Math.random() * 200); |
| 184 | + last.setLocation(x, y); |
| 185 | + } else |
| 186 | + if (e.getSource() == resizeB) { |
| 187 | + int w = (int)(Math.random() * 200); |
| 188 | + int h = (int)(Math.random() * 200); |
| 189 | + last.setSize(w, h); |
| 190 | + } else |
| 191 | + if (e.getSource() == reshapeB) { |
| 192 | + int x = (int)(Math.random() * 200); |
| 193 | + int y = (int)(Math.random() * 200); |
| 194 | + int w = (int)(Math.random() * 200); |
| 195 | + int h = (int)(Math.random() * 200); |
| 196 | + last.setBounds(x, y, w, h); |
| 197 | + } else |
| 198 | + if (e.getSource() == disposeB) { |
| 199 | + last.dispose(); |
| 200 | + frames.remove(frames.size() - 1); |
| 201 | + if (frames.isEmpty()) { |
| 202 | + updateControls(null); |
| 203 | + centerP.setEnabled(false); |
| 204 | + return; |
| 205 | + } |
| 206 | + last = (Frame)frames.lastElement(); |
| 207 | + } |
| 208 | + updateControls(last); |
| 209 | + } |
| 210 | + |
| 211 | + public void updateControls(Frame f) { |
| 212 | + undecoratedCB.setState(f != null ? |
| 213 | + f.isUndecorated() : false); |
| 214 | + defaultLocationCB.setState(f != null ? |
| 215 | + f.isLocationByPlatform() : false); |
| 216 | + visibleCB.setState(f != null ? |
| 217 | + f.isVisible() : false); |
| 218 | + iconifiedCB.setState(f != null ? |
| 219 | + (f.getExtendedState() & Frame.ICONIFIED) != 0 : false); |
| 220 | + maximizedCB.setState(f != null ? |
| 221 | + (f.getExtendedState() & Frame.MAXIMIZED_BOTH) != 0 : false); |
| 222 | + } |
| 223 | + |
| 224 | + public void itemStateChanged(ItemEvent e) { |
| 225 | + Frame last = (Frame)frames.lastElement(); |
| 226 | + try { |
| 227 | + boolean state = e.getStateChange() == ItemEvent.SELECTED; |
| 228 | + if (e.getSource() == visibleCB) { |
| 229 | + last.setVisible(state); |
| 230 | + } else |
| 231 | + if (e.getSource() == defaultLocationCB) { |
| 232 | + last.setLocationByPlatform(state); |
| 233 | + } else |
| 234 | + if (e.getSource() == undecoratedCB) { |
| 235 | + last.setUndecorated(state); |
| 236 | + } else |
| 237 | + if (e.getSource() == iconifiedCB) { |
| 238 | + if (state) { |
| 239 | + last.setExtendedState(last.getExtendedState() | |
| 240 | + Frame.ICONIFIED); |
| 241 | + } else { |
| 242 | + last.setExtendedState(last.getExtendedState() & |
| 243 | + ~Frame.ICONIFIED); |
| 244 | + } |
| 245 | + } else |
| 246 | + if (e.getSource() == maximizedCB) { |
| 247 | + if (state) { |
| 248 | + last.setExtendedState(last.getExtendedState() | |
| 249 | + Frame.MAXIMIZED_BOTH); |
| 250 | + } else { |
| 251 | + last.setExtendedState(last.getExtendedState() & |
| 252 | + ~Frame.MAXIMIZED_BOTH); |
| 253 | + } |
| 254 | + } |
| 255 | + } catch (Throwable ex) { |
| 256 | + PassFailJFrame.log(ex.getMessage()); |
| 257 | + } finally { |
| 258 | + updateControls(last); |
| 259 | + } |
| 260 | + } |
| 261 | + |
| 262 | + @Override |
| 263 | + public void dispose() { |
| 264 | + while (!frames.isEmpty()) { |
| 265 | + Frame last = (Frame)frames.lastElement(); |
| 266 | + last.dispose(); |
| 267 | + frames.remove(frames.size() - 1); |
| 268 | + } |
| 269 | + } |
| 270 | +} |
0 commit comments