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