Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.desktop/macosx/classes/com/apple/laf/AquaComboBoxButton.java
41154 views
1
/*
2
* Copyright (c) 2011, 2015, 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 com.apple.laf;
27
28
import java.awt.*;
29
30
import javax.swing.*;
31
import javax.swing.plaf.UIResource;
32
33
import apple.laf.JRSUIState;
34
import apple.laf.JRSUIConstants.*;
35
36
@SuppressWarnings("serial") // Superclass is not serializable across versions
37
class AquaComboBoxButton extends JButton {
38
protected final JComboBox<Object> comboBox;
39
protected final JList<?> list;
40
protected final CellRendererPane rendererPane;
41
protected final AquaComboBoxUI ui;
42
43
protected final AquaPainter<JRSUIState> painter = AquaPainter.create(JRSUIState.getInstance());
44
boolean isPopDown;
45
boolean isSquare;
46
47
@SuppressWarnings("serial") // anonymous class
48
protected AquaComboBoxButton(final AquaComboBoxUI ui,
49
final JComboBox<Object> comboBox,
50
final CellRendererPane rendererPane,
51
final JList<?> list) {
52
super("");
53
putClientProperty("JButton.buttonType", "comboboxInternal");
54
55
this.ui = ui;
56
this.comboBox = comboBox;
57
this.rendererPane = rendererPane;
58
this.list = list;
59
60
setModel(new DefaultButtonModel() {
61
@Override
62
public void setArmed(final boolean armed) {
63
super.setArmed(isPressed() ? true : armed);
64
}
65
});
66
67
setEnabled(comboBox.isEnabled());
68
}
69
70
@Override
71
public boolean isEnabled() {
72
return comboBox == null ? true : comboBox.isEnabled();
73
}
74
75
@Override
76
public boolean isFocusable() {
77
return false;
78
}
79
80
protected void setIsPopDown(final boolean isPopDown) {
81
this.isPopDown = isPopDown;
82
repaint();
83
}
84
85
protected void setIsSquare(final boolean isSquare) {
86
this.isSquare = isSquare;
87
repaint();
88
}
89
90
protected State getState(final ButtonModel buttonModel) {
91
if (!comboBox.isEnabled()) return State.DISABLED;
92
if (!AquaFocusHandler.isActive(comboBox)) return State.INACTIVE;
93
if (buttonModel.isArmed()) return State.PRESSED;
94
return State.ACTIVE;
95
}
96
97
@Override
98
public void paintComponent(final Graphics g) {
99
// Don't Paint the button as usual
100
// super.paintComponent( g );
101
final boolean editable = comboBox.isEditable();
102
103
int top = 0;
104
int left = 0;
105
int width = getWidth();
106
int height = getHeight();
107
108
if (comboBox.isOpaque()) {
109
g.setColor(getBackground());
110
g.fillRect(0, 0, width, height);
111
}
112
113
final Size size = AquaUtilControlSize.getUserSizeFrom(comboBox);
114
painter.state.set(size == null ? Size.REGULAR : size);
115
116
final ButtonModel buttonModel = getModel();
117
painter.state.set(getState(buttonModel));
118
119
painter.state.set(AlignmentVertical.CENTER);
120
121
if (AquaComboBoxUI.isTableCellEditor(comboBox)) {
122
painter.state.set(AlignmentHorizontal.RIGHT);
123
painter.state.set(Widget.BUTTON_POP_UP);
124
painter.state.set(ArrowsOnly.YES);
125
painter.paint(g, this, left, top, width, height);
126
doRendererPaint(g, buttonModel, editable, getInsets(), left, top, width, height);
127
return;
128
}
129
130
painter.state.set(AlignmentHorizontal.CENTER);
131
final Insets insets = getInsets();
132
if (!editable) {
133
top += insets.top;
134
left += insets.left;
135
width -= insets.left + insets.right;
136
height -= insets.top + insets.bottom;
137
}
138
139
if (height <= 0 || width <= 0) {
140
return;
141
}
142
143
boolean hasFocus = comboBox.hasFocus();
144
if (editable) {
145
painter.state.set(Widget.BUTTON_COMBO_BOX);
146
painter.state.set(IndicatorOnly.YES);
147
painter.state.set(AlignmentHorizontal.LEFT);
148
hasFocus |= comboBox.getEditor().getEditorComponent().hasFocus();
149
} else {
150
painter.state.set(IndicatorOnly.NO);
151
painter.state.set(AlignmentHorizontal.CENTER);
152
if (isPopDown) {
153
painter.state.set(isSquare ? Widget.BUTTON_POP_DOWN_SQUARE : Widget.BUTTON_POP_DOWN);
154
} else {
155
painter.state.set(isSquare ? Widget.BUTTON_POP_UP_SQUARE : Widget.BUTTON_POP_UP);
156
}
157
}
158
painter.state.set(hasFocus ? Focused.YES : Focused.NO);
159
160
if (isSquare) {
161
painter.paint(g, comboBox, left + 2, top - 1, width - 4, height);
162
} else {
163
painter.paint(g, comboBox, left, top, width, height);
164
}
165
166
// Let the renderer paint
167
if (!editable && comboBox != null) {
168
doRendererPaint(g, buttonModel, editable, insets, left, top, width, height);
169
}
170
}
171
172
protected void doRendererPaint(final Graphics g, final ButtonModel buttonModel, final boolean editable, final Insets insets, int left, int top, int width, int height) {
173
final ListCellRenderer<Object> renderer = comboBox.getRenderer();
174
175
// fake it out! not renderPressed
176
final Component c = renderer.getListCellRendererComponent(list, comboBox.getSelectedItem(), -1, false, false);
177
// System.err.println("Renderer: " + renderer);
178
179
if (!editable && !AquaComboBoxUI.isTableCellEditor(comboBox)) {
180
final int indentLeft = 10;
181
final int buttonWidth = 24;
182
183
// hardcoded for now. We should adjust as necessary.
184
top += 1;
185
height -= 4;
186
left += indentLeft;
187
width -= (indentLeft + buttonWidth);
188
}
189
190
c.setFont(rendererPane.getFont());
191
192
if (buttonModel.isArmed() && buttonModel.isPressed()) {
193
if (isOpaque()) {
194
c.setBackground(UIManager.getColor("Button.select"));
195
}
196
c.setForeground(comboBox.getForeground());
197
} else if (!comboBox.isEnabled()) {
198
if (isOpaque()) {
199
c.setBackground(UIManager.getColor("ComboBox.disabledBackground"));
200
}
201
c.setForeground(UIManager.getColor("ComboBox.disabledForeground"));
202
} else {
203
c.setForeground(comboBox.getForeground());
204
c.setBackground(comboBox.getBackground());
205
}
206
207
// Sun Fix for 4238829: should lay out the JPanel.
208
boolean shouldValidate = false;
209
if (c instanceof JPanel) {
210
shouldValidate = true;
211
}
212
213
final int iconWidth = 0;
214
final int cWidth = width - (insets.right + iconWidth);
215
216
// fix for 3156483 we need to crop images that are too big.
217
// if (height > 18)
218
// always crop.
219
{
220
top = height / 2 - 8;
221
height = 19;
222
}
223
224
// It doesn't need to draw its background, we handled it
225
final Color bg = c.getBackground();
226
final boolean inhibitBackground = bg instanceof UIResource;
227
if (inhibitBackground) c.setBackground(new Color(0, 0, 0, 0));
228
229
rendererPane.paintComponent(g, c, this, left, top, cWidth, height, shouldValidate); // h - (insets.top + insets.bottom) );
230
231
if (inhibitBackground) c.setBackground(bg);
232
233
// Remove component from renderer pane, allowing it to be gc'ed.
234
rendererPane.remove(c);
235
}
236
}
237
238