Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/jdk/javax/swing/JPopupMenu/4634626/bug4634626.java
66646 views
1
/*
2
* Copyright (c) 2003, 2021, 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
* @key headful
27
* @bug 4634626
28
* @summary Implement context popup menus for components
29
* @library /lib/client
30
* @build ExtendedRobot
31
* @run main bug4634626
32
*/
33
34
import javax.swing.JButton;
35
import javax.swing.JComponent;
36
import javax.swing.JFrame;
37
import javax.swing.JPanel;
38
import javax.swing.JPopupMenu;
39
import javax.swing.JTextArea;
40
import javax.swing.SwingUtilities;
41
import java.awt.BorderLayout;
42
import java.awt.Component;
43
import java.awt.event.KeyEvent;
44
import java.awt.event.MouseAdapter;
45
import java.awt.event.MouseEvent;
46
import java.awt.event.WindowAdapter;
47
import java.awt.event.WindowEvent;
48
import java.util.concurrent.atomic.AtomicBoolean;
49
50
public class bug4634626 {
51
52
public volatile boolean passed = true;
53
public volatile boolean done = false;
54
55
public static JFrame mainFrame;
56
public JPanel contentPane;
57
public JButton nopButton;
58
public JTextArea someText;
59
public JButton popButton;
60
61
public JPopupMenu btnPopup;
62
public JPopupMenu commonPopup;
63
static public Error toBeThrown = null;
64
static int popTrig = MouseEvent.BUTTON3_MASK;
65
static boolean popt = false;
66
67
public static class MouseWatcher extends MouseAdapter {
68
public void mousePressed(MouseEvent e) {
69
if(e.isPopupTrigger()) popt = true;
70
if(e.getComponent() != null &&
71
e.getComponent() instanceof JComponent &&
72
e.isPopupTrigger() &&
73
((JComponent)e.getComponent()).getComponentPopupMenu() != null) {
74
toBeThrown =
75
new Error("The event got through the component with popup: "
76
+ e);
77
}
78
}
79
80
public void mouseReleased(MouseEvent e) {
81
if(e.isPopupTrigger()) popt = true;
82
if(e.getComponent() != null &&
83
e.getComponent() instanceof JComponent &&
84
e.isPopupTrigger() &&
85
((JComponent)e.getComponent()).getComponentPopupMenu() != null) {
86
toBeThrown =
87
new Error("The event got through the component with popup: "
88
+ e);
89
}
90
if(toBeThrown != null) {
91
throw(toBeThrown);
92
}
93
}
94
}
95
96
public static MouseWatcher mouser = new MouseWatcher();
97
98
public static void main(final String[] args) throws Exception {
99
try {
100
bug4634626 app = new bug4634626();
101
app.init();
102
app.destroy();
103
} finally {
104
if (mainFrame != null) SwingUtilities.invokeAndWait(() -> mainFrame.dispose());
105
}
106
}
107
108
public void init() throws Exception {
109
SwingUtilities.invokeLater(() -> {
110
mainFrame = new JFrame("Bug4634626");
111
contentPane = new JPanel();
112
nopButton = new JButton("No popup button");
113
someText = new JTextArea("Some text here", 20, 10);
114
popButton = new JButton("Button with the popup");
115
116
btnPopup = new JPopupMenu();
117
commonPopup = new JPopupMenu();
118
119
try {
120
popButton.setComponentPopupMenu(null);
121
popButton.setComponentPopupMenu(null);
122
popButton.setComponentPopupMenu(btnPopup);
123
popButton.setComponentPopupMenu(null);
124
} catch (Exception ex) {
125
System.err.println("Unexpected exception was thrown by " +
126
"setComponentPopupMenu() method: " + ex);
127
}
128
btnPopup.add("Button 1");
129
btnPopup.add("Button 2");
130
btnPopup.add("Button 3");
131
popButton.setComponentPopupMenu(btnPopup);
132
popButton.addMouseListener(mouser);
133
commonPopup.add("One");
134
commonPopup.add("Two");
135
commonPopup.add("Three");
136
137
contentPane.setLayout(new BorderLayout());
138
contentPane.setComponentPopupMenu(commonPopup);
139
contentPane.addMouseListener(mouser);
140
contentPane.add(nopButton, BorderLayout.NORTH);
141
nopButton.addMouseListener(mouser);
142
contentPane.add(popButton, BorderLayout.SOUTH);
143
someText.addMouseListener(mouser);
144
contentPane.add(someText, BorderLayout.CENTER);
145
mainFrame.setContentPane(contentPane);
146
147
mainFrame.pack();
148
mainFrame.setLocation(50, 50);
149
mainFrame.addWindowListener(new TestStateListener());
150
mainFrame.setLocationRelativeTo(null);
151
mainFrame.setVisible(true);
152
});
153
154
while(!done) Thread.yield();
155
156
if(!passed) {
157
throw new RuntimeException("Test failed");
158
}
159
}
160
161
public class TestStateListener extends WindowAdapter {
162
public void windowOpened(WindowEvent ev) {
163
try {
164
ev.getWindow().toFront();
165
ev.getWindow().requestFocus();
166
new Thread(new RobotThread()).start();
167
} catch (Exception ex) {
168
throw new RuntimeException("Thread Exception");
169
}
170
}
171
}
172
173
class RobotThread implements Runnable {
174
public void run() {
175
ExtendedRobot robo;
176
try {
177
robo = new ExtendedRobot();
178
}catch(Exception ex) {
179
ex.printStackTrace();
180
throw new RuntimeException("Cannot create Robot");
181
}
182
robo.setAutoDelay(100);
183
robo.waitForIdle();
184
185
// Determine working popup trigger event
186
clickMouseOn(robo, nopButton, popTrig);
187
robo.waitForIdle();
188
robo.delay(1000);
189
if(!popt) popTrig = MouseEvent.BUTTON2_MASK;
190
191
// Inheritance is OFF by default. Popup should not appear.
192
clickMouseOn(robo, someText, popTrig);
193
194
// Set inheritance ON watch for popup.
195
try {
196
SwingUtilities.invokeAndWait(() -> someText.setInheritsPopupMenu(true));
197
} catch (Exception ex) {
198
throw new RuntimeException("Can not assign popup to button", ex);
199
}
200
clickMouseOn(robo, someText, popTrig);
201
robo.waitForIdle();
202
robo.delay(1000);
203
AtomicBoolean popupVisible = new AtomicBoolean(false);
204
try {
205
SwingUtilities.invokeAndWait(() ->
206
popupVisible.set(commonPopup.isVisible()));
207
} catch (Exception ex) {
208
throw new RuntimeException("Can not get commonPopup status");
209
}
210
if(!popupVisible.get()) {
211
toBeThrown = new Error("Popup should be visible");
212
passed = false;
213
}
214
// Dispose popup.
215
robo.type(KeyEvent.VK_ESCAPE);
216
robo.waitForIdle();
217
try {
218
SwingUtilities.invokeAndWait(() -> someText.setInheritsPopupMenu(false));
219
} catch (Exception ex) {
220
throw new RuntimeException("Can not unassign popup on button", ex);
221
}
222
223
// Button with popup assigned. Wathch for popup.
224
clickMouseOn(robo, popButton, popTrig);
225
robo.waitForIdle();
226
robo.delay(1000);
227
try {
228
SwingUtilities.invokeAndWait(() ->
229
popupVisible.set(btnPopup.isVisible()));
230
} catch (Exception ex) {
231
throw new RuntimeException("Can not get btnPopup status");
232
}
233
if(!popupVisible.get()) {
234
toBeThrown = new Error("Popup should be visible");
235
passed = false;
236
}
237
// Dispose popup.
238
robo.type(KeyEvent.VK_ESCAPE);
239
// Test finished.
240
done = true;
241
}
242
}
243
244
public void destroy() {
245
if(!passed) {
246
throw(toBeThrown);
247
}
248
}
249
250
private void clickMouseOn(ExtendedRobot robot, Component c, int button) {
251
java.awt.Point p = c.getLocationOnScreen();
252
java.awt.Dimension size = c.getSize();
253
p.x += size.width / 2;
254
p.y += size.height / 2;
255
robot.mouseMove(p.x, p.y);
256
robot.delay(100);
257
robot.click(button);
258
}
259
}
260
261