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/AquaButtonUI.java
41154 views
1
/*
2
* Copyright (c) 2011, 2020, 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
import java.awt.event.*;
30
import java.beans.PropertyChangeEvent;
31
32
import javax.swing.*;
33
import javax.swing.border.Border;
34
import javax.swing.event.*;
35
import javax.swing.plaf.*;
36
import javax.swing.plaf.basic.*;
37
import javax.swing.text.View;
38
39
import sun.swing.SwingUtilities2;
40
41
import apple.laf.JRSUIConstants.Size;
42
43
import com.apple.laf.AquaButtonExtendedTypes.TypeSpecifier;
44
import com.apple.laf.AquaUtilControlSize.Sizeable;
45
import com.apple.laf.AquaUtils.*;
46
47
public class AquaButtonUI extends BasicButtonUI implements Sizeable {
48
private static final String BUTTON_TYPE = "JButton.buttonType";
49
private static final String SEGMENTED_BUTTON_POSITION = "JButton.segmentPosition";
50
51
private static final RecyclableSingleton<AquaButtonUI> buttonUI = new RecyclableSingletonFromDefaultConstructor<AquaButtonUI>(AquaButtonUI.class);
52
public static ComponentUI createUI(final JComponent c) {
53
return buttonUI.get();
54
}
55
56
// Has the shared instance defaults been initialized?
57
private boolean defaults_initialized = false;
58
private Color defaultDisabledTextColor = null;
59
60
protected void installDefaults(final AbstractButton b) {
61
// load shared instance defaults
62
final String pp = getPropertyPrefix();
63
64
if (!defaults_initialized) {
65
defaultDisabledTextColor = UIManager.getColor(pp + "disabledText");
66
defaults_initialized = true;
67
}
68
69
setButtonMarginIfNeeded(b, UIManager.getInsets(pp + "margin"));
70
71
LookAndFeel.installColorsAndFont(b, pp + "background", pp + "foreground", pp + "font");
72
LookAndFeel.installProperty(b, "opaque", UIManager.getBoolean(pp + "opaque"));
73
74
final Object borderProp = b.getClientProperty(BUTTON_TYPE);
75
boolean hasBorder = false;
76
77
if (borderProp != null) {
78
hasBorder = setButtonType(b, borderProp);
79
}
80
if (!hasBorder) setThemeBorder(b);
81
82
final Object segmentProp = b.getClientProperty(SEGMENTED_BUTTON_POSITION);
83
if (segmentProp != null) {
84
final Border border = b.getBorder();
85
if (!(border instanceof AquaBorder)) return;
86
87
b.setBorder(AquaButtonExtendedTypes.getBorderForPosition(b, b.getClientProperty(BUTTON_TYPE), segmentProp));
88
}
89
}
90
91
public void applySizeFor(final JComponent c, final Size size) {
92
// this space intentionally left blank
93
// (subclasses need to do work here)
94
}
95
96
protected void setThemeBorder(final AbstractButton b) {
97
// Set the correct border
98
final ButtonUI genericUI = b.getUI();
99
if (!(genericUI instanceof AquaButtonUI)) return;
100
final AquaButtonUI ui = (AquaButtonUI)genericUI;
101
102
Border border = b.getBorder();
103
if (!ui.isBorderFromProperty(b) && (border == null || border instanceof UIResource || border instanceof AquaButtonBorder)) {
104
// See BasicGraphicsUtils.getPreferredButtonSize - it returns null for preferred size,
105
// causing it to use the subcomponent's size, which doesn't allow space for Aqua pushbuttons
106
boolean iconFont = true;
107
if (isOnToolbar(b)) {
108
if (b instanceof JToggleButton) {
109
border = AquaButtonBorder.getToolBarButtonBorder();
110
} else {
111
border = AquaButtonBorder.getBevelButtonBorder();
112
}
113
} else if (b.getIcon() != null || b.getComponentCount() > 0) {
114
// radar 3308129 && (b.getText() == null || b.getText().equals("")))
115
// we used to only do this for buttons that had images and no text
116
// now we do it for all buttons that have any images - they cannot
117
// be a default button.
118
border = AquaButtonBorder.getToggleButtonBorder();
119
} else {
120
border = UIManager.getBorder(getPropertyPrefix() + "border");
121
iconFont = false;
122
}
123
124
b.setBorder(border);
125
126
final Font currentFont = b.getFont();
127
if (iconFont && (currentFont == null || currentFont instanceof UIResource)) {
128
b.setFont(UIManager.getFont("IconButton.font"));
129
}
130
}
131
}
132
133
protected static boolean isOnToolbar(final AbstractButton b) {
134
Component parent = b.getParent();
135
while (parent != null) {
136
if (parent instanceof JToolBar) return true;
137
parent = parent.getParent();
138
}
139
return false;
140
}
141
142
// A state that affects border has changed. Make sure we have the right one
143
protected static void updateBorder(final AbstractButton b) {
144
// See if the button has overridden the automatic button type
145
final Object prop = b.getClientProperty(BUTTON_TYPE);
146
if (prop != null) return;
147
148
final ButtonUI ui = b.getUI();
149
if (!(ui instanceof AquaButtonUI)) return;
150
if (b.getBorder() != null) ((AquaButtonUI)ui).setThemeBorder(b);
151
}
152
153
protected void setButtonMarginIfNeeded(final AbstractButton b, final Insets insets) {
154
final Insets margin = b.getMargin();
155
if (margin == null || (margin instanceof UIResource)) {
156
b.setMargin(insets);
157
}
158
}
159
160
public boolean isBorderFromProperty(final AbstractButton button) {
161
return button.getClientProperty(BUTTON_TYPE) != null;
162
}
163
164
protected boolean setButtonType(final AbstractButton b, final Object prop) {
165
if (!(prop instanceof String)) {
166
b.putClientProperty(BUTTON_TYPE, null); // so we know to use the automatic button type
167
return false;
168
}
169
170
final String buttonType = (String)prop;
171
boolean iconFont = true;
172
173
final TypeSpecifier specifier = AquaButtonExtendedTypes.getSpecifierByName(buttonType);
174
if (specifier != null) {
175
b.setBorder(specifier.getBorder());
176
iconFont = specifier.setIconFont;
177
}
178
179
final Font currentFont = b.getFont();
180
if (currentFont == null || currentFont instanceof UIResource) {
181
b.setFont(UIManager.getFont(iconFont ? "IconButton.font" : "Button.font"));
182
}
183
184
return true;
185
}
186
187
protected void installListeners(final AbstractButton b) {
188
super.installListeners(b);
189
AquaButtonListener listener = getAquaButtonListener(b);
190
if (listener != null) {
191
// put the listener in the button's client properties so that
192
// we can get at it later
193
b.putClientProperty(this, listener);
194
195
b.addAncestorListener(listener);
196
}
197
installHierListener(b);
198
AquaUtilControlSize.addSizePropertyListener(b);
199
}
200
201
protected void installKeyboardActions(final AbstractButton b) {
202
final BasicButtonListener listener = (BasicButtonListener)b.getClientProperty(this);
203
if (listener != null) listener.installKeyboardActions(b);
204
}
205
206
// Uninstall PLAF
207
public void uninstallUI(final JComponent c) {
208
uninstallKeyboardActions((AbstractButton)c);
209
uninstallListeners((AbstractButton)c);
210
uninstallDefaults((AbstractButton)c);
211
//BasicHTML.updateRenderer(c, "");
212
}
213
214
protected void uninstallKeyboardActions(final AbstractButton b) {
215
final BasicButtonListener listener = (BasicButtonListener)b.getClientProperty(this);
216
if (listener != null) listener.uninstallKeyboardActions(b);
217
}
218
219
protected void uninstallListeners(final AbstractButton b) {
220
super.uninstallListeners(b);
221
final AquaButtonListener listener = (AquaButtonListener)b.getClientProperty(this);
222
b.putClientProperty(this, null);
223
if (listener != null) {
224
b.removeAncestorListener(listener);
225
}
226
uninstallHierListener(b);
227
AquaUtilControlSize.removeSizePropertyListener(b);
228
}
229
230
protected void uninstallDefaults(final AbstractButton b) {
231
LookAndFeel.uninstallBorder(b);
232
defaults_initialized = false;
233
}
234
235
// Create Listeners
236
protected AquaButtonListener createButtonListener(final AbstractButton b) {
237
return new AquaButtonListener(b);
238
}
239
240
/**
241
* Returns the AquaButtonListener for the passed in Button, or null if one
242
* could not be found.
243
*/
244
private AquaButtonListener getAquaButtonListener(AbstractButton b) {
245
MouseMotionListener[] listeners = b.getMouseMotionListeners();
246
247
if (listeners != null) {
248
for (MouseMotionListener listener : listeners) {
249
if (listener instanceof AquaButtonListener) {
250
return (AquaButtonListener) listener;
251
}
252
}
253
}
254
return null;
255
}
256
257
// Paint Methods
258
public void paint(final Graphics g, final JComponent c) {
259
final AbstractButton b = (AbstractButton)c;
260
final ButtonModel model = b.getModel();
261
262
final Insets i = c.getInsets();
263
264
Rectangle viewRect = new Rectangle(b.getWidth(), b.getHeight());
265
Rectangle iconRect = new Rectangle();
266
Rectangle textRect = new Rectangle();
267
268
// we are overdrawing here with translucent colors so we get
269
// a darkening effect. How can we avoid it. Try clear rect?
270
if (b.isOpaque()) {
271
g.setColor(c.getBackground());
272
g.fillRect(viewRect.x, viewRect.y, viewRect.width, viewRect.height);
273
}
274
275
AquaButtonBorder aquaBorder = null;
276
if (((AbstractButton)c).isBorderPainted()) {
277
final Border border = c.getBorder();
278
279
if (border instanceof AquaButtonBorder) {
280
// only do this if borders are on!
281
// this also takes care of focus painting.
282
aquaBorder = (AquaButtonBorder)border;
283
aquaBorder.paintButton(c, g, viewRect.x, viewRect.y, viewRect.width, viewRect.height);
284
}
285
} else {
286
if (b.isOpaque()) {
287
viewRect.x = i.left - 2;
288
viewRect.y = i.top - 2;
289
viewRect.width = b.getWidth() - (i.right + viewRect.x) + 4;
290
viewRect.height = b.getHeight() - (i.bottom + viewRect.y) + 4;
291
if (b.isContentAreaFilled() || model.isSelected()) {
292
if (model.isSelected()) // Toggle buttons
293
g.setColor(c.getBackground().darker());
294
else g.setColor(c.getBackground());
295
g.fillRect(viewRect.x, viewRect.y, viewRect.width, viewRect.height);
296
}
297
}
298
299
// needs focus to be painted
300
// for now we don't know exactly what to do...we'll see!
301
if (b.isFocusPainted() && b.hasFocus()) {
302
// paint UI specific focus
303
paintFocus(g, b, viewRect, textRect, iconRect);
304
}
305
}
306
307
// performs icon and text rect calculations
308
final String text = layoutAndGetText(g, b, aquaBorder, i, viewRect, iconRect, textRect);
309
310
// Paint the Icon
311
if (b.getIcon() != null) {
312
paintIcon(g, b, iconRect);
313
}
314
315
if (textRect.width == 0) {
316
textRect.width = 50;
317
}
318
319
if (text != null && !text.isEmpty()) {
320
final View v = (View)c.getClientProperty(BasicHTML.propertyKey);
321
if (v != null) {
322
v.paint(g, textRect);
323
} else {
324
paintText(g, b, textRect, text);
325
}
326
}
327
}
328
329
protected String layoutAndGetText(final Graphics g, final AbstractButton b, final AquaButtonBorder aquaBorder, final Insets i, Rectangle viewRect, Rectangle iconRect, Rectangle textRect) {
330
// re-initialize the view rect to the selected insets
331
viewRect.x = i.left;
332
viewRect.y = i.top;
333
viewRect.width = b.getWidth() - (i.right + viewRect.x);
334
viewRect.height = b.getHeight() - (i.bottom + viewRect.y);
335
336
// reset the text and icon rects
337
textRect.x = textRect.y = textRect.width = textRect.height = 0;
338
iconRect.x = iconRect.y = iconRect.width = iconRect.height = 0;
339
340
// setup the font
341
g.setFont(b.getFont());
342
final FontMetrics fm = g.getFontMetrics();
343
344
// layout the text and icon
345
final String originalText = b.getText();
346
final String text = SwingUtilities.layoutCompoundLabel(b, fm, originalText, b.getIcon(), b.getVerticalAlignment(), b.getHorizontalAlignment(), b.getVerticalTextPosition(), b.getHorizontalTextPosition(), viewRect, iconRect, textRect, originalText == null ? 0 : b.getIconTextGap());
347
if (text == originalText || aquaBorder == null) return text; // everything fits
348
349
// if the text didn't fit - check if the aqua border has alternate Insets that are more adhering
350
final Insets alternateContentInsets = aquaBorder.getContentInsets(b, b.getWidth(), b.getHeight());
351
if (alternateContentInsets != null) {
352
// recursively call and don't pass AquaBorder
353
return layoutAndGetText(g, b, null, alternateContentInsets, viewRect, iconRect, textRect);
354
}
355
356
// there is no Aqua border, go with what we've got
357
return text;
358
}
359
360
protected void paintIcon(final Graphics g, final AbstractButton b, final Rectangle localIconRect) {
361
final ButtonModel model = b.getModel();
362
Icon icon = b.getIcon();
363
Icon tmpIcon = null;
364
365
if (icon == null) return;
366
367
if (!model.isEnabled()) {
368
if (model.isSelected()) {
369
tmpIcon = b.getDisabledSelectedIcon();
370
} else {
371
tmpIcon = b.getDisabledIcon();
372
}
373
} else if (model.isPressed() && model.isArmed()) {
374
tmpIcon = b.getPressedIcon();
375
if (tmpIcon == null) {
376
if (icon instanceof ImageIcon) {
377
tmpIcon = new ImageIcon(AquaUtils.generateSelectedDarkImage(((ImageIcon)icon).getImage()));
378
}
379
}
380
} else if (b.isRolloverEnabled() && model.isRollover()) {
381
if (model.isSelected()) {
382
tmpIcon = b.getRolloverSelectedIcon();
383
} else {
384
tmpIcon = b.getRolloverIcon();
385
}
386
} else if (model.isSelected()) {
387
tmpIcon = b.getSelectedIcon();
388
}
389
390
if (model.isEnabled() && b.isFocusOwner() && b.getBorder() instanceof AquaButtonBorder.Toolbar) {
391
if (tmpIcon == null) tmpIcon = icon;
392
if (tmpIcon instanceof ImageIcon) {
393
tmpIcon = AquaFocus.createFocusedIcon(tmpIcon, b, 3);
394
tmpIcon.paintIcon(b, g, localIconRect.x - 3, localIconRect.y - 3);
395
return;
396
}
397
}
398
399
if (tmpIcon != null) {
400
icon = tmpIcon;
401
}
402
403
icon.paintIcon(b, g, localIconRect.x, localIconRect.y);
404
}
405
406
/**
407
* As of Java 2 platform v 1.4 this method should not be used or overriden.
408
* Use the paintText method which takes the AbstractButton argument.
409
*/
410
protected void paintText(final Graphics g, final JComponent c, final Rectangle localTextRect, final String text) {
411
final Graphics2D g2d = g instanceof Graphics2D ? (Graphics2D)g : null;
412
413
final AbstractButton b = (AbstractButton)c;
414
final ButtonModel model = b.getModel();
415
final FontMetrics fm = g.getFontMetrics();
416
final int mnemonicIndex = AquaMnemonicHandler.isMnemonicHidden() ? -1 : b.getDisplayedMnemonicIndex();
417
418
/* Draw the Text */
419
if (model.isEnabled()) {
420
/*** paint the text normally */
421
g.setColor(b.getForeground());
422
} else {
423
/*** paint the text disabled ***/
424
g.setColor(defaultDisabledTextColor);
425
}
426
SwingUtilities2.drawStringUnderlineCharAt(c, g, text, mnemonicIndex, localTextRect.x, localTextRect.y + fm.getAscent());
427
}
428
429
protected void paintText(final Graphics g, final AbstractButton b, final Rectangle localTextRect, final String text) {
430
paintText(g, (JComponent)b, localTextRect, text);
431
}
432
433
protected void paintButtonPressed(final Graphics g, final AbstractButton b) {
434
paint(g, b);
435
}
436
437
// Layout Methods
438
public Dimension getMinimumSize(final JComponent c) {
439
final Dimension d = getPreferredSize(c);
440
final View v = (View)c.getClientProperty(BasicHTML.propertyKey);
441
if (v != null) {
442
d.width -= v.getPreferredSpan(View.X_AXIS) - v.getMinimumSpan(View.X_AXIS);
443
}
444
return d;
445
}
446
447
public Dimension getPreferredSize(final JComponent c) {
448
final AbstractButton b = (AbstractButton)c;
449
450
// fix for Radar #3134273
451
final Dimension d = BasicGraphicsUtils.getPreferredButtonSize(b, b.getIconTextGap());
452
if (d == null) return null;
453
454
final Border border = b.getBorder();
455
if (border instanceof AquaButtonBorder) {
456
((AquaButtonBorder)border).alterPreferredSize(d);
457
}
458
459
return d;
460
}
461
462
public Dimension getMaximumSize(final JComponent c) {
463
final Dimension d = getPreferredSize(c);
464
465
final View v = (View)c.getClientProperty(BasicHTML.propertyKey);
466
if (v != null) {
467
d.width += v.getMaximumSpan(View.X_AXIS) - v.getPreferredSpan(View.X_AXIS);
468
}
469
470
return d;
471
}
472
473
private static final RecyclableSingleton<AquaHierarchyButtonListener> fHierListener = new RecyclableSingletonFromDefaultConstructor<AquaHierarchyButtonListener>(AquaHierarchyButtonListener.class);
474
static AquaHierarchyButtonListener getAquaHierarchyButtonListener() {
475
return fHierListener.get();
476
}
477
478
// We need to know when ordinary JButtons are put on JToolbars, but not JComboBoxButtons
479
// JToggleButtons always have the same border
480
481
private boolean shouldInstallHierListener(final AbstractButton b) {
482
return (b instanceof JButton || b instanceof JToggleButton && !(b instanceof AquaComboBoxButton) && !(b instanceof JCheckBox) && !(b instanceof JRadioButton));
483
}
484
485
protected void installHierListener(final AbstractButton b) {
486
if (shouldInstallHierListener(b)) {
487
// super put the listener in the button's client properties
488
b.addHierarchyListener(getAquaHierarchyButtonListener());
489
}
490
}
491
492
protected void uninstallHierListener(final AbstractButton b) {
493
if (shouldInstallHierListener(b)) {
494
b.removeHierarchyListener(getAquaHierarchyButtonListener());
495
}
496
}
497
498
static class AquaHierarchyButtonListener implements HierarchyListener {
499
// Everytime a hierarchy is change we need to check if the button if moved on or from
500
// a toolbar. If that is the case, we need to re-set the border of the button.
501
public void hierarchyChanged(final HierarchyEvent e) {
502
if ((e.getChangeFlags() & HierarchyEvent.PARENT_CHANGED) == 0) return;
503
504
final Object o = e.getSource();
505
if (!(o instanceof AbstractButton)) return;
506
507
final AbstractButton b = (AbstractButton)o;
508
final ButtonUI ui = b.getUI();
509
if (!(ui instanceof AquaButtonUI)) return;
510
511
if (!(b.getBorder() instanceof UIResource)) return; // if the border is not one of ours, or null
512
((AquaButtonUI)ui).setThemeBorder(b);
513
}
514
}
515
516
class AquaButtonListener extends BasicButtonListener implements AncestorListener {
517
protected final AbstractButton b;
518
519
public AquaButtonListener(final AbstractButton b) {
520
super(b);
521
this.b = b;
522
}
523
524
public void focusGained(final FocusEvent e) {
525
((Component)e.getSource()).repaint();
526
}
527
528
public void focusLost(final FocusEvent e) {
529
// 10-06-03 VL: [Radar 3187049]
530
// If focusLost arrives while the button has been left-clicked this would disarm the button,
531
// causing actionPerformed not to fire on mouse release!
532
//b.getModel().setArmed(false);
533
b.getModel().setPressed(false);
534
((Component)e.getSource()).repaint();
535
}
536
537
public void propertyChange(final PropertyChangeEvent e) {
538
super.propertyChange(e);
539
540
final String propertyName = e.getPropertyName();
541
542
// Repaint the button, since its border needs to handle the new state.
543
if (AquaFocusHandler.FRAME_ACTIVE_PROPERTY.equals(propertyName)) {
544
b.repaint();
545
return;
546
}
547
548
if ("icon".equals(propertyName) || "text".equals(propertyName)) {
549
setThemeBorder(b);
550
return;
551
}
552
553
if (BUTTON_TYPE.equals(propertyName)) {
554
// Forced border types
555
final String value = (String)e.getNewValue();
556
557
final Border border = AquaButtonExtendedTypes.getBorderForPosition(b, value, b.getClientProperty(SEGMENTED_BUTTON_POSITION));
558
if (border != null) {
559
b.setBorder(border);
560
}
561
562
return;
563
}
564
565
if (SEGMENTED_BUTTON_POSITION.equals(propertyName)) {
566
final Border border = b.getBorder();
567
if (!(border instanceof AquaBorder)) return;
568
569
b.setBorder(AquaButtonExtendedTypes.getBorderForPosition(b, b.getClientProperty(BUTTON_TYPE), e.getNewValue()));
570
}
571
572
if ("componentOrientation".equals(propertyName)) {
573
final Border border = b.getBorder();
574
if (!(border instanceof AquaBorder)) return;
575
576
Object buttonType = b.getClientProperty(BUTTON_TYPE);
577
Object buttonPosition = b.getClientProperty(SEGMENTED_BUTTON_POSITION);
578
if (buttonType != null && buttonPosition != null) {
579
b.setBorder(AquaButtonExtendedTypes.getBorderForPosition(b, buttonType, buttonPosition));
580
}
581
}
582
}
583
584
public void ancestorMoved(final AncestorEvent e) {}
585
586
public void ancestorAdded(final AncestorEvent e) {
587
updateDefaultButton();
588
}
589
590
public void ancestorRemoved(final AncestorEvent e) {
591
updateDefaultButton();
592
}
593
594
protected void updateDefaultButton() {
595
if (!(b instanceof JButton)) return;
596
if (!((JButton)b).isDefaultButton()) return;
597
598
final JRootPane rootPane = b.getRootPane();
599
if (rootPane == null) return;
600
601
final RootPaneUI ui = rootPane.getUI();
602
if (!(ui instanceof AquaRootPaneUI)) return;
603
((AquaRootPaneUI)ui).updateDefaultButton(rootPane);
604
}
605
}
606
}
607
608