|
| 1 | +/* |
| 2 | + * Copyright (c) 1999, 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 | +import java.awt.Canvas; |
| 25 | +import java.awt.Color; |
| 26 | +import java.awt.Dimension; |
| 27 | +import java.awt.Frame; |
| 28 | +import java.awt.Graphics; |
| 29 | +import java.awt.GridLayout; |
| 30 | +import java.awt.Panel; |
| 31 | +import java.awt.Window; |
| 32 | +import java.awt.event.FocusEvent; |
| 33 | +import java.awt.event.FocusListener; |
| 34 | +import java.awt.event.MouseAdapter; |
| 35 | +import java.awt.event.MouseEvent; |
| 36 | +import java.awt.event.WindowAdapter; |
| 37 | +import java.awt.event.WindowEvent; |
| 38 | + |
| 39 | +/* |
| 40 | + * @test |
| 41 | + * @bug 4140293 |
| 42 | + * @summary Tests that focus is returned to the correct Component when a Frame |
| 43 | + * is reactivated. |
| 44 | + * @library /java/awt/regtesthelpers |
| 45 | + * @build PassFailJFrame |
| 46 | + * @run main/manual FocusTest |
| 47 | + */ |
| 48 | + |
| 49 | +public class FocusTest { |
| 50 | + private static final String INSTRUCTIONS = """ |
| 51 | + Click on the bottom rectangle. Move the mouse slightly. |
| 52 | + A focus rectangle should appear around the bottom rectangle. |
| 53 | +
|
| 54 | + Now, deactivate the window and then reactivate it. |
| 55 | + (You would click on the caption bar of another window, |
| 56 | + and then on the caption bar of the FocusTest Frame.) |
| 57 | +
|
| 58 | + If the focus rectangle appears again, the test passes. |
| 59 | + If it does not appear, or appears around the top rectangle, |
| 60 | + the test fails. |
| 61 | + """; |
| 62 | + |
| 63 | + public static void main(String[] args) throws Exception { |
| 64 | + PassFailJFrame.builder() |
| 65 | + .title("FocusTest Instructions") |
| 66 | + .instructions(INSTRUCTIONS) |
| 67 | + .columns(45) |
| 68 | + .logArea(6) |
| 69 | + .testUI(FocusTest::createAndShowUI) |
| 70 | + .build() |
| 71 | + .awaitAndCheck(); |
| 72 | + } |
| 73 | + |
| 74 | + private static Window createAndShowUI() { |
| 75 | + Frame frame = new Frame("FocusTest"); |
| 76 | + |
| 77 | + frame.add(new FocusTestPanel()); |
| 78 | + frame.setSize(400, 400); |
| 79 | + |
| 80 | + frame.addWindowListener(new WindowAdapter() { |
| 81 | + public void windowClosing(WindowEvent e) { |
| 82 | + frame.dispose(); |
| 83 | + } |
| 84 | + }); |
| 85 | + |
| 86 | + frame.validate(); |
| 87 | + return frame; |
| 88 | + } |
| 89 | + |
| 90 | + private static class FocusTestPanel extends Panel { |
| 91 | + PassiveClient pc1 = new PassiveClient("pc1"); |
| 92 | + PassiveClient pc2 = new PassiveClient("pc2"); |
| 93 | + |
| 94 | + public FocusTestPanel() { |
| 95 | + super(); |
| 96 | + setLayout(new GridLayout(2, 1, 10, 10)); |
| 97 | + add(pc1); |
| 98 | + add(pc2); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + private static class PassiveClient extends Canvas implements FocusListener { |
| 103 | + boolean haveFocus = false; |
| 104 | + final String name; |
| 105 | + |
| 106 | + PassiveClient(String name) { |
| 107 | + super(); |
| 108 | + this.name = name; |
| 109 | + setSize(400, 100); |
| 110 | + setBackground(Color.cyan); |
| 111 | + setVisible(true); |
| 112 | + setEnabled(true); |
| 113 | + addMouseListener(new MouseAdapter() { |
| 114 | + @Override |
| 115 | + public void mouseClicked(MouseEvent e) { |
| 116 | + requestFocus(); |
| 117 | + } |
| 118 | + }); |
| 119 | + addFocusListener(this); |
| 120 | + } |
| 121 | + |
| 122 | + public void paint(Graphics g) { |
| 123 | + g.setColor(getBackground()); |
| 124 | + Dimension size = getSize(); |
| 125 | + g.fillRect(0, 0, size.width, size.height); |
| 126 | + if (haveFocus) { |
| 127 | + g.setColor(Color.black); |
| 128 | + g.drawRect(0, 0, size.width - 1, size.height - 1); |
| 129 | + g.drawRect(1, 1, size.width - 3, size.height - 3); |
| 130 | + } |
| 131 | + g.setColor(getForeground()); |
| 132 | + } |
| 133 | + |
| 134 | + public void focusGained(FocusEvent event) { |
| 135 | + haveFocus = true; |
| 136 | + paint(getGraphics()); |
| 137 | + PassFailJFrame.log("<<<< %s Got focus!! %s>>>>".formatted(this, event)); |
| 138 | + } |
| 139 | + |
| 140 | + public void focusLost(FocusEvent event) { |
| 141 | + haveFocus = false; |
| 142 | + paint(getGraphics()); |
| 143 | + PassFailJFrame.log("<<<< %s Lost focus!! %s>>>>".formatted(this, event)); |
| 144 | + } |
| 145 | + |
| 146 | + @Override |
| 147 | + public String toString() { |
| 148 | + return "PassiveClient " + name; |
| 149 | + } |
| 150 | + } |
| 151 | +} |
0 commit comments