Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/macosx/classes/com/apple/laf/AquaButtonUI.java
38831 views
1
/*
2
* Copyright (c) 2011, 2012, 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
protected 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
final AquaButtonListener listener = createButtonListener(b);
189
if (listener != null) {
190
// put the listener in the button's client properties so that
191
// we can get at it later
192
b.putClientProperty(this, listener);
193
194
b.addMouseListener(listener);
195
b.addMouseMotionListener(listener);
196
b.addFocusListener(listener);
197
b.addPropertyChangeListener(listener);
198
b.addChangeListener(listener);
199
b.addAncestorListener(listener);
200
}
201
installHierListener(b);
202
AquaUtilControlSize.addSizePropertyListener(b);
203
}
204
205
protected void installKeyboardActions(final AbstractButton b) {
206
final BasicButtonListener listener = (BasicButtonListener)b.getClientProperty(this);
207
if (listener != null) listener.installKeyboardActions(b);
208
}
209
210
// Uninstall PLAF
211
public void uninstallUI(final JComponent c) {
212
uninstallKeyboardActions((AbstractButton)c);
213
uninstallListeners((AbstractButton)c);
214
uninstallDefaults((AbstractButton)c);
215
//BasicHTML.updateRenderer(c, "");
216
}
217
218
protected void uninstallKeyboardActions(final AbstractButton b) {
219
final BasicButtonListener listener = (BasicButtonListener)b.getClientProperty(this);
220
if (listener != null) listener.uninstallKeyboardActions(b);
221
}
222
223
protected void uninstallListeners(final AbstractButton b) {
224
final AquaButtonListener listener = (AquaButtonListener)b.getClientProperty(this);
225
b.putClientProperty(this, null);
226
if (listener != null) {
227
b.removeMouseListener(listener);
228
b.removeMouseListener(listener);
229
b.removeMouseMotionListener(listener);
230
b.removeFocusListener(listener);
231
b.removeChangeListener(listener);
232
b.removePropertyChangeListener(listener);
233
b.removeAncestorListener(listener);
234
}
235
uninstallHierListener(b);
236
AquaUtilControlSize.addSizePropertyListener(b);
237
}
238
239
protected void uninstallDefaults(final AbstractButton b) {
240
LookAndFeel.uninstallBorder(b);
241
defaults_initialized = false;
242
}
243
244
// Create Listeners
245
protected AquaButtonListener createButtonListener(final AbstractButton b) {
246
return new AquaButtonListener(b);
247
}
248
249
// Paint Methods
250
public void paint(final Graphics g, final JComponent c) {
251
final AbstractButton b = (AbstractButton)c;
252
final ButtonModel model = b.getModel();
253
254
final Insets i = c.getInsets();
255
256
Rectangle viewRect = new Rectangle(b.getWidth(), b.getHeight());
257
Rectangle iconRect = new Rectangle();
258
Rectangle textRect = new Rectangle();
259
260
// we are overdrawing here with translucent colors so we get
261
// a darkening effect. How can we avoid it. Try clear rect?
262
if (b.isOpaque()) {
263
g.setColor(c.getBackground());
264
g.fillRect(viewRect.x, viewRect.y, viewRect.width, viewRect.height);
265
}
266
267
AquaButtonBorder aquaBorder = null;
268
if (((AbstractButton)c).isBorderPainted()) {
269
final Border border = c.getBorder();
270
271
if (border instanceof AquaButtonBorder) {
272
// only do this if borders are on!
273
// this also takes care of focus painting.
274
aquaBorder = (AquaButtonBorder)border;
275
aquaBorder.paintButton(c, g, viewRect.x, viewRect.y, viewRect.width, viewRect.height);
276
}
277
} else {
278
if (b.isOpaque()) {
279
viewRect.x = i.left - 2;
280
viewRect.y = i.top - 2;
281
viewRect.width = b.getWidth() - (i.right + viewRect.x) + 4;
282
viewRect.height = b.getHeight() - (i.bottom + viewRect.y) + 4;
283
if (b.isContentAreaFilled() || model.isSelected()) {
284
if (model.isSelected()) // Toggle buttons
285
g.setColor(c.getBackground().darker());
286
else g.setColor(c.getBackground());
287
g.fillRect(viewRect.x, viewRect.y, viewRect.width, viewRect.height);
288
}
289
}
290
291
// needs focus to be painted
292
// for now we don't know exactly what to do...we'll see!
293
if (b.isFocusPainted() && b.hasFocus()) {
294
// paint UI specific focus
295
paintFocus(g, b, viewRect, textRect, iconRect);
296
}
297
}
298
299
// performs icon and text rect calculations
300
final String text = layoutAndGetText(g, b, aquaBorder, i, viewRect, iconRect, textRect);
301
302
// Paint the Icon
303
if (b.getIcon() != null) {
304
paintIcon(g, b, iconRect);
305
}
306
307
if (textRect.width == 0) {
308
textRect.width = 50;
309
}
310
311
if (text != null && !text.equals("")) {
312
final View v = (View)c.getClientProperty(BasicHTML.propertyKey);
313
if (v != null) {
314
v.paint(g, textRect);
315
} else {
316
paintText(g, b, textRect, text);
317
}
318
}
319
}
320
321
protected String layoutAndGetText(final Graphics g, final AbstractButton b, final AquaButtonBorder aquaBorder, final Insets i, Rectangle viewRect, Rectangle iconRect, Rectangle textRect) {
322
// re-initialize the view rect to the selected insets
323
viewRect.x = i.left;
324
viewRect.y = i.top;
325
viewRect.width = b.getWidth() - (i.right + viewRect.x);
326
viewRect.height = b.getHeight() - (i.bottom + viewRect.y);
327
328
// reset the text and icon rects
329
textRect.x = textRect.y = textRect.width = textRect.height = 0;
330
iconRect.x = iconRect.y = iconRect.width = iconRect.height = 0;
331
332
// setup the font
333
g.setFont(b.getFont());
334
final FontMetrics fm = g.getFontMetrics();
335
336
// layout the text and icon
337
final String originalText = b.getText();
338
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());
339
if (text == originalText || aquaBorder == null) return text; // everything fits
340
341
// if the text didn't fit - check if the aqua border has alternate Insets that are more adhering
342
final Insets alternateContentInsets = aquaBorder.getContentInsets(b, b.getWidth(), b.getHeight());
343
if (alternateContentInsets != null) {
344
// recursively call and don't pass AquaBorder
345
return layoutAndGetText(g, b, null, alternateContentInsets, viewRect, iconRect, textRect);
346
}
347
348
// there is no Aqua border, go with what we've got
349
return text;
350
}
351
352
protected void paintIcon(final Graphics g, final AbstractButton b, final Rectangle localIconRect) {
353
final ButtonModel model = b.getModel();
354
Icon icon = b.getIcon();
355
Icon tmpIcon = null;
356
357
if (icon == null) return;
358
359
if (!model.isEnabled()) {
360
if (model.isSelected()) {
361
tmpIcon = b.getDisabledSelectedIcon();
362
} else {
363
tmpIcon = b.getDisabledIcon();
364
}
365
} else if (model.isPressed() && model.isArmed()) {
366
tmpIcon = b.getPressedIcon();
367
if (tmpIcon == null) {
368
if (icon instanceof ImageIcon) {
369
tmpIcon = new ImageIcon(AquaUtils.generateSelectedDarkImage(((ImageIcon)icon).getImage()));
370
}
371
}
372
} else if (b.isRolloverEnabled() && model.isRollover()) {
373
if (model.isSelected()) {
374
tmpIcon = b.getRolloverSelectedIcon();
375
} else {
376
tmpIcon = b.getRolloverIcon();
377
}
378
} else if (model.isSelected()) {
379
tmpIcon = b.getSelectedIcon();
380
}
381
382
if (model.isEnabled() && b.isFocusOwner() && b.getBorder() instanceof AquaButtonBorder.Toolbar) {
383
if (tmpIcon == null) tmpIcon = icon;
384
if (tmpIcon instanceof ImageIcon) {
385
tmpIcon = AquaFocus.createFocusedIcon(tmpIcon, b, 3);
386
tmpIcon.paintIcon(b, g, localIconRect.x - 3, localIconRect.y - 3);
387
return;
388
}
389
}
390
391
if (tmpIcon != null) {
392
icon = tmpIcon;
393
}
394
395
icon.paintIcon(b, g, localIconRect.x, localIconRect.y);
396
}
397
398
/**
399
* As of Java 2 platform v 1.4 this method should not be used or overriden.
400
* Use the paintText method which takes the AbstractButton argument.
401
*/
402
protected void paintText(final Graphics g, final JComponent c, final Rectangle localTextRect, final String text) {
403
final Graphics2D g2d = g instanceof Graphics2D ? (Graphics2D)g : null;
404
405
final AbstractButton b = (AbstractButton)c;
406
final ButtonModel model = b.getModel();
407
final FontMetrics fm = g.getFontMetrics();
408
final int mnemonicIndex = AquaMnemonicHandler.isMnemonicHidden() ? -1 : b.getDisplayedMnemonicIndex();
409
410
/* Draw the Text */
411
if (model.isEnabled()) {
412
/*** paint the text normally */
413
g.setColor(b.getForeground());
414
} else {
415
/*** paint the text disabled ***/
416
g.setColor(defaultDisabledTextColor);
417
}
418
SwingUtilities2.drawStringUnderlineCharAt(c, g, text, mnemonicIndex, localTextRect.x, localTextRect.y + fm.getAscent());
419
}
420
421
protected void paintText(final Graphics g, final AbstractButton b, final Rectangle localTextRect, final String text) {
422
paintText(g, (JComponent)b, localTextRect, text);
423
}
424
425
protected void paintButtonPressed(final Graphics g, final AbstractButton b) {
426
paint(g, b);
427
}
428
429
// Layout Methods
430
public Dimension getMinimumSize(final JComponent c) {
431
final Dimension d = getPreferredSize(c);
432
final View v = (View)c.getClientProperty(BasicHTML.propertyKey);
433
if (v != null) {
434
d.width -= v.getPreferredSpan(View.X_AXIS) - v.getMinimumSpan(View.X_AXIS);
435
}
436
return d;
437
}
438
439
public Dimension getPreferredSize(final JComponent c) {
440
final AbstractButton b = (AbstractButton)c;
441
442
// fix for Radar #3134273
443
final Dimension d = BasicGraphicsUtils.getPreferredButtonSize(b, b.getIconTextGap());
444
if (d == null) return null;
445
446
final Border border = b.getBorder();
447
if (border instanceof AquaButtonBorder) {
448
((AquaButtonBorder)border).alterPreferredSize(d);
449
}
450
451
return d;
452
}
453
454
public Dimension getMaximumSize(final JComponent c) {
455
final Dimension d = getPreferredSize(c);
456
457
final View v = (View)c.getClientProperty(BasicHTML.propertyKey);
458
if (v != null) {
459
d.width += v.getMaximumSpan(View.X_AXIS) - v.getPreferredSpan(View.X_AXIS);
460
}
461
462
return d;
463
}
464
465
final static RecyclableSingleton<AquaHierarchyButtonListener> fHierListener = new RecyclableSingletonFromDefaultConstructor<AquaHierarchyButtonListener>(AquaHierarchyButtonListener.class);
466
static AquaHierarchyButtonListener getAquaHierarchyButtonListener() {
467
return fHierListener.get();
468
}
469
470
// We need to know when ordinary JButtons are put on JToolbars, but not JComboBoxButtons
471
// JToggleButtons always have the same border
472
473
private boolean shouldInstallHierListener(final AbstractButton b) {
474
return (b instanceof JButton || b instanceof JToggleButton && !(b instanceof AquaComboBoxButton) && !(b instanceof JCheckBox) && !(b instanceof JRadioButton));
475
}
476
477
protected void installHierListener(final AbstractButton b) {
478
if (shouldInstallHierListener(b)) {
479
// super put the listener in the button's client properties
480
b.addHierarchyListener(getAquaHierarchyButtonListener());
481
}
482
}
483
484
protected void uninstallHierListener(final AbstractButton b) {
485
if (shouldInstallHierListener(b)) {
486
b.removeHierarchyListener(getAquaHierarchyButtonListener());
487
}
488
}
489
490
static class AquaHierarchyButtonListener implements HierarchyListener {
491
// Everytime a hierarchy is change we need to check if the button if moved on or from
492
// a toolbar. If that is the case, we need to re-set the border of the button.
493
public void hierarchyChanged(final HierarchyEvent e) {
494
if ((e.getChangeFlags() & HierarchyEvent.PARENT_CHANGED) == 0) return;
495
496
final Object o = e.getSource();
497
if (!(o instanceof AbstractButton)) return;
498
499
final AbstractButton b = (AbstractButton)o;
500
final ButtonUI ui = b.getUI();
501
if (!(ui instanceof AquaButtonUI)) return;
502
503
if (!(b.getBorder() instanceof UIResource)) return; // if the border is not one of ours, or null
504
((AquaButtonUI)ui).setThemeBorder(b);
505
}
506
}
507
508
class AquaButtonListener extends BasicButtonListener implements AncestorListener {
509
protected final AbstractButton b;
510
511
public AquaButtonListener(final AbstractButton b) {
512
super(b);
513
this.b = b;
514
}
515
516
public void focusGained(final FocusEvent e) {
517
((Component)e.getSource()).repaint();
518
}
519
520
public void focusLost(final FocusEvent e) {
521
// 10-06-03 VL: [Radar 3187049]
522
// If focusLost arrives while the button has been left-clicked this would disarm the button,
523
// causing actionPerformed not to fire on mouse release!
524
//b.getModel().setArmed(false);
525
((Component)e.getSource()).repaint();
526
}
527
528
public void propertyChange(final PropertyChangeEvent e) {
529
super.propertyChange(e);
530
531
final String propertyName = e.getPropertyName();
532
533
// Repaint the button, since its border needs to handle the new state.
534
if (AquaFocusHandler.FRAME_ACTIVE_PROPERTY.equals(propertyName)) {
535
b.repaint();
536
return;
537
}
538
539
if ("icon".equals(propertyName) || "text".equals(propertyName)) {
540
setThemeBorder(b);
541
return;
542
}
543
544
if (BUTTON_TYPE.equals(propertyName)) {
545
// Forced border types
546
final String value = (String)e.getNewValue();
547
548
final Border border = AquaButtonExtendedTypes.getBorderForPosition(b, value, b.getClientProperty(SEGMENTED_BUTTON_POSITION));
549
if (border != null) {
550
b.setBorder(border);
551
}
552
553
return;
554
}
555
556
if (SEGMENTED_BUTTON_POSITION.equals(propertyName)) {
557
final Border border = b.getBorder();
558
if (!(border instanceof AquaBorder)) return;
559
560
b.setBorder(AquaButtonExtendedTypes.getBorderForPosition(b, b.getClientProperty(BUTTON_TYPE), e.getNewValue()));
561
}
562
563
if ("componentOrientation".equals(propertyName)) {
564
final Border border = b.getBorder();
565
if (!(border instanceof AquaBorder)) return;
566
567
Object buttonType = b.getClientProperty(BUTTON_TYPE);
568
Object buttonPosition = b.getClientProperty(SEGMENTED_BUTTON_POSITION);
569
if (buttonType != null && buttonPosition != null) {
570
b.setBorder(AquaButtonExtendedTypes.getBorderForPosition(b, buttonType, buttonPosition));
571
}
572
}
573
}
574
575
public void ancestorMoved(final AncestorEvent e) {}
576
577
public void ancestorAdded(final AncestorEvent e) {
578
updateDefaultButton();
579
}
580
581
public void ancestorRemoved(final AncestorEvent e) {
582
updateDefaultButton();
583
}
584
585
protected void updateDefaultButton() {
586
if (!(b instanceof JButton)) return;
587
if (!((JButton)b).isDefaultButton()) return;
588
589
final JRootPane rootPane = b.getRootPane();
590
if (rootPane == null) return;
591
592
final RootPaneUI ui = rootPane.getUI();
593
if (!(ui instanceof AquaRootPaneUI)) return;
594
((AquaRootPaneUI)ui).updateDefaultButton(rootPane);
595
}
596
}
597
}
598
599