Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.desktop/macosx/classes/sun/lwawt/LWChoicePeer.java
41153 views
1
/*
2
* Copyright (c) 2011, 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. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package sun.lwawt;
27
28
import java.awt.Point;
29
import java.awt.Choice;
30
31
import java.awt.event.ItemEvent;
32
import java.awt.event.ItemListener;
33
import java.awt.peer.ChoicePeer;
34
35
import javax.accessibility.Accessible;
36
import javax.swing.JComboBox;
37
import javax.swing.SwingUtilities;
38
import javax.swing.JPopupMenu;
39
40
/**
41
* Lightweight implementation of {@link ChoicePeer}. Delegates most of the work
42
* to the {@link JComboBox}.
43
*/
44
final class LWChoicePeer extends LWComponentPeer<Choice, JComboBox<String>>
45
implements ChoicePeer, ItemListener {
46
47
/**
48
* According to Choice specification item events are sent in response to
49
* user input, but not in response to calls to select(). But JComboBox are
50
* sent item events in both cases. Should be used under delegateLock.
51
*/
52
private boolean skipPostMessage;
53
54
LWChoicePeer(final Choice target,
55
final PlatformComponent platformComponent) {
56
super(target, platformComponent);
57
}
58
59
@Override
60
JComboBox<String> createDelegate() {
61
return new JComboBoxDelegate();
62
}
63
64
@Override
65
void initializeImpl() {
66
super.initializeImpl();
67
final Choice choice = getTarget();
68
final JComboBox<String> combo = getDelegate();
69
synchronized (getDelegateLock()) {
70
final int count = choice.getItemCount();
71
for (int i = 0; i < count; ++i) {
72
combo.addItem(choice.getItem(i));
73
}
74
select(choice.getSelectedIndex());
75
76
// NOTE: the listener must be added at the very end, otherwise it
77
// fires events upon initialization of the combo box.
78
combo.addItemListener(this);
79
}
80
}
81
82
@Override
83
public void itemStateChanged(final ItemEvent e) {
84
// AWT Choice sends SELECTED event only whereas JComboBox
85
// sends both SELECTED and DESELECTED.
86
if (e.getStateChange() == ItemEvent.SELECTED) {
87
synchronized (getDelegateLock()) {
88
if (skipPostMessage) {
89
return;
90
}
91
getTarget().select(getDelegate().getSelectedIndex());
92
}
93
postEvent(new ItemEvent(getTarget(), ItemEvent.ITEM_STATE_CHANGED,
94
e.getItem(), ItemEvent.SELECTED));
95
}
96
}
97
98
@Override
99
public void add(final String item, final int index) {
100
synchronized (getDelegateLock()) {
101
getDelegate().insertItemAt(item, index);
102
}
103
}
104
105
@Override
106
public void remove(final int index) {
107
synchronized (getDelegateLock()) {
108
// We shouldn't post event, if selected item was removed.
109
skipPostMessage = true;
110
getDelegate().removeItemAt(index);
111
skipPostMessage = false;
112
}
113
}
114
115
@Override
116
public void removeAll() {
117
synchronized (getDelegateLock()) {
118
getDelegate().removeAllItems();
119
}
120
}
121
122
@Override
123
public void select(final int index) {
124
synchronized (getDelegateLock()) {
125
if (index != getDelegate().getSelectedIndex()) {
126
skipPostMessage = true;
127
getDelegate().setSelectedIndex(index);
128
skipPostMessage = false;
129
}
130
}
131
}
132
133
@Override
134
public boolean isFocusable() {
135
return true;
136
}
137
138
@SuppressWarnings("serial")// Safe: outer class is non-serializable.
139
private final class JComboBoxDelegate extends JComboBox<String> {
140
141
@Override
142
public boolean hasFocus() {
143
return getTarget().hasFocus();
144
}
145
146
//Needed for proper popup menu location
147
@Override
148
public Point getLocationOnScreen() {
149
return LWChoicePeer.this.getLocationOnScreen();
150
}
151
152
@Override
153
public void firePopupMenuWillBecomeVisible() {
154
super.firePopupMenuWillBecomeVisible();
155
SwingUtilities.invokeLater(() -> {
156
JPopupMenu popupMenu = getPopupMenu();
157
// Need to override the invoker for proper grab handling
158
if (popupMenu != null
159
&& popupMenu.isShowing()
160
&& popupMenu.getInvoker() != getTarget()) {
161
// The popup is now visible with correct location
162
// Save it and restore after toggling visibility and changing invoker
163
Point loc = popupMenu.getLocationOnScreen();
164
SwingUtilities.convertPointFromScreen(loc, this);
165
popupMenu.setVisible(false);
166
popupMenu.show(getTarget(), loc.x, loc.y);
167
}
168
});
169
}
170
171
private JPopupMenu getPopupMenu() {
172
for (int i = 0; i < getAccessibleContext().getAccessibleChildrenCount(); i++) {
173
Accessible child = getAccessibleContext().getAccessibleChild(i);
174
if (child instanceof JPopupMenu) {
175
return (JPopupMenu) child;
176
}
177
}
178
return null;
179
}
180
}
181
}
182
183