Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/swing/JPopupMenu/8147521/PopupMenuTest.java
41153 views
1
/*
2
* Copyright (c) 2016, 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 8147521 8158358
28
* @summary [macosx] Internal API Usage: setPopupType used to force creation of
29
* heavyweight popup
30
* @run main PopupMenuTest
31
*/
32
import java.awt.Component;
33
import java.awt.Point;
34
import java.awt.Rectangle;
35
import java.awt.Robot;
36
import java.awt.event.InputEvent;
37
import java.awt.event.MouseAdapter;
38
import java.awt.event.MouseEvent;
39
import javax.swing.JFrame;
40
import javax.swing.JMenuItem;
41
import javax.swing.JPanel;
42
import javax.swing.JPopupMenu;
43
import javax.swing.Popup;
44
import javax.swing.PopupFactory;
45
import javax.swing.SwingUtilities;
46
import javax.swing.event.PopupMenuEvent;
47
import javax.swing.event.PopupMenuListener;
48
import javax.swing.plaf.basic.BasicPopupMenuUI;
49
50
public class PopupMenuTest {
51
52
private JPopupMenu jpopup;
53
private static volatile boolean isLightWeight;
54
private static JFrame frame;
55
private static Robot robot;
56
private static JPanel panel;
57
58
public static void main(String s[]) throws Exception {
59
PopupMenuTest obj = new PopupMenuTest();
60
obj.createUI();
61
robot = new Robot();
62
robot.waitForIdle();
63
robot.delay(1000);
64
obj.exectuteTest();
65
obj.dispose();
66
if (isLightWeight) {
67
throw new RuntimeException("Test Failed");
68
}
69
}
70
71
private void createUI() throws Exception {
72
SwingUtilities.invokeAndWait(() -> {
73
frame = new JFrame("Popup Menu");
74
jpopup = new JPopupMenu();
75
jpopup.setUI(new PopMenuUIExt());
76
JMenuItem item = new JMenuItem("Menu Item1");
77
jpopup.add(item);
78
item = new JMenuItem("Menu Item2");
79
jpopup.setLabel("Justification");
80
jpopup.add(item);
81
jpopup.setLabel("Justification");
82
jpopup.addPopupMenuListener(new PopupListener());
83
panel = new JPanel();
84
panel.addMouseListener(new MousePopupListener());
85
frame.setContentPane(panel);
86
frame.setSize(300, 300);
87
frame.setLocationRelativeTo(null);
88
frame.setVisible(true);
89
});
90
91
}
92
93
private void dispose() throws Exception {
94
SwingUtilities.invokeAndWait(() -> {
95
Popup popup = PopMenuUIExt.getPopup();
96
if (popup != null) {
97
popup.hide();
98
}
99
frame.dispose();
100
});
101
}
102
103
private void exectuteTest() {
104
Point p = frame.getLocationOnScreen();
105
Rectangle rect = frame.getBounds();
106
robot.mouseMove(p.x + rect.width / 2, p.y + rect.height / 2);
107
robot.mousePress(InputEvent.BUTTON3_DOWN_MASK);
108
robot.mouseRelease(InputEvent.BUTTON3_DOWN_MASK);
109
robot.delay(1000);
110
robot.mouseMove(p.x + rect.width / 2 - 10, p.y + rect.height / 2 - 10);
111
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
112
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
113
robot.delay(1000);
114
}
115
116
class MousePopupListener extends MouseAdapter {
117
118
@Override
119
public void mousePressed(MouseEvent e) {
120
showPopup(e);
121
}
122
123
@Override
124
public void mouseClicked(MouseEvent e) {
125
showPopup(e);
126
}
127
128
@Override
129
public void mouseReleased(MouseEvent e) {
130
showPopup(e);
131
}
132
133
private void showPopup(MouseEvent e) {
134
jpopup.show(panel, e.getX(), e.getY());
135
}
136
}
137
138
class PopupListener implements PopupMenuListener {
139
140
public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
141
}
142
143
public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
144
Popup popup = ((PopMenuUIExt) jpopup.getUI()).getPopup();
145
if (popup != null) {
146
isLightWeight = !popup.getClass().toString().
147
contains("HeavyWeightPopup");
148
}
149
}
150
151
public void popupMenuCanceled(PopupMenuEvent e) {
152
}
153
}
154
}
155
156
class PopMenuUIExt extends BasicPopupMenuUI {
157
158
private static Popup popUp;
159
160
@Override
161
public Popup getPopup(JPopupMenu popup, int x, int y) {
162
PopupFactory.setSharedInstance(new PopupFactory() {
163
164
@Override
165
public Popup getPopup(Component owner, Component contents,
166
int x, int y) {
167
return super.getPopup(owner, contents, x, y, true);
168
}
169
});
170
PopupFactory factory = PopupFactory.getSharedInstance();
171
popUp = factory.getPopup(popup.getInvoker(), popup, x, y);
172
return popUp;
173
}
174
175
public static Popup getPopup() {
176
return popUp;
177
}
178
}
179
180
181