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/AquaProgressBarUI.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.awt.geom.AffineTransform;
31
import java.beans.*;
32
33
import javax.swing.*;
34
import javax.swing.event.*;
35
import javax.swing.plaf.*;
36
37
import sun.swing.SwingUtilities2;
38
39
import apple.laf.JRSUIStateFactory;
40
import apple.laf.JRSUIConstants.*;
41
import apple.laf.JRSUIState.ValueState;
42
43
import com.apple.laf.AquaUtilControlSize.*;
44
import com.apple.laf.AquaUtils.RecyclableSingleton;
45
46
public class AquaProgressBarUI extends ProgressBarUI implements ChangeListener, PropertyChangeListener, AncestorListener, Sizeable {
47
private static final boolean ADJUSTTIMER = true;
48
49
protected static final RecyclableSingleton<SizeDescriptor> sizeDescriptor = new RecyclableSingleton<SizeDescriptor>() {
50
@Override
51
protected SizeDescriptor getInstance() {
52
return new SizeDescriptor(new SizeVariant(146, 20)) {
53
public SizeVariant deriveSmall(final SizeVariant v) { v.alterMinSize(0, -6); return super.deriveSmall(v); }
54
};
55
}
56
};
57
static SizeDescriptor getSizeDescriptor() {
58
return sizeDescriptor.get();
59
}
60
61
protected Size sizeVariant = Size.REGULAR;
62
63
protected Color selectionForeground;
64
65
private Animator animator;
66
protected boolean isAnimating;
67
protected boolean isCircular;
68
69
protected final AquaPainter<ValueState> painter = AquaPainter.create(JRSUIStateFactory.getProgressBar());
70
71
protected JProgressBar progressBar;
72
73
public static ComponentUI createUI(final JComponent x) {
74
return new AquaProgressBarUI();
75
}
76
77
protected AquaProgressBarUI() { }
78
79
public void installUI(final JComponent c) {
80
progressBar = (JProgressBar)c;
81
installDefaults();
82
installListeners();
83
}
84
85
public void uninstallUI(final JComponent c) {
86
uninstallDefaults();
87
uninstallListeners();
88
stopAnimationTimer();
89
progressBar = null;
90
}
91
92
protected void installDefaults() {
93
progressBar.setOpaque(false);
94
LookAndFeel.installBorder(progressBar, "ProgressBar.border");
95
LookAndFeel.installColorsAndFont(progressBar, "ProgressBar.background", "ProgressBar.foreground", "ProgressBar.font");
96
selectionForeground = UIManager.getColor("ProgressBar.selectionForeground");
97
}
98
99
protected void uninstallDefaults() {
100
LookAndFeel.uninstallBorder(progressBar);
101
}
102
103
protected void installListeners() {
104
progressBar.addChangeListener(this); // Listen for changes in the progress bar's data
105
progressBar.addPropertyChangeListener(this); // Listen for changes between determinate and indeterminate state
106
progressBar.addAncestorListener(this);
107
AquaUtilControlSize.addSizePropertyListener(progressBar);
108
}
109
110
protected void uninstallListeners() {
111
AquaUtilControlSize.removeSizePropertyListener(progressBar);
112
progressBar.removeAncestorListener(this);
113
progressBar.removePropertyChangeListener(this);
114
progressBar.removeChangeListener(this);
115
}
116
117
public void stateChanged(final ChangeEvent e) {
118
progressBar.repaint();
119
}
120
121
public void propertyChange(final PropertyChangeEvent e) {
122
final String prop = e.getPropertyName();
123
if ("indeterminate".equals(prop)) {
124
if (!progressBar.isIndeterminate()) return;
125
stopAnimationTimer();
126
// start the animation thread
127
if (progressBar.isDisplayable()) {
128
startAnimationTimer();
129
}
130
}
131
132
if ("JProgressBar.style".equals(prop)) {
133
isCircular = "circular".equalsIgnoreCase(e.getNewValue() + "");
134
progressBar.repaint();
135
}
136
}
137
138
// listen for Ancestor events to stop our timer when we are no longer visible
139
// <rdar://problem/5405035> JProgressBar: UI in Aqua look and feel causes memory leaks
140
public void ancestorRemoved(final AncestorEvent e) {
141
stopAnimationTimer();
142
}
143
144
public void ancestorAdded(final AncestorEvent e) {
145
if (!progressBar.isIndeterminate()) return;
146
if (progressBar.isDisplayable()) {
147
startAnimationTimer();
148
}
149
}
150
151
public void ancestorMoved(final AncestorEvent e) { }
152
153
public void paint(final Graphics g, final JComponent c) {
154
revalidateAnimationTimers(); // revalidate to turn on/off timers when values change
155
156
painter.state.set(getState(c));
157
painter.state.set(isHorizontal() ? Orientation.HORIZONTAL : Orientation.VERTICAL);
158
painter.state.set(isAnimating ? Animating.YES : Animating.NO);
159
160
if (progressBar.isIndeterminate()) {
161
if (isCircular) {
162
painter.state.set(Widget.PROGRESS_SPINNER);
163
painter.paint(g, c, 2, 2, 16, 16);
164
return;
165
}
166
167
painter.state.set(Widget.PROGRESS_INDETERMINATE_BAR);
168
paint(g);
169
return;
170
}
171
172
painter.state.set(Widget.PROGRESS_BAR);
173
painter.state.setValue(checkValue(progressBar.getPercentComplete()));
174
paint(g);
175
}
176
177
static double checkValue(final double value) {
178
return Double.isNaN(value) ? 0 : value;
179
}
180
181
protected void paint(final Graphics g) {
182
// this is questionable. We may want the insets to mean something different.
183
final Insets i = progressBar.getInsets();
184
final int width = progressBar.getWidth() - (i.right + i.left);
185
final int height = progressBar.getHeight() - (i.bottom + i.top);
186
187
painter.paint(g, progressBar, i.left, i.top, width, height);
188
189
if (progressBar.isStringPainted() && !progressBar.isIndeterminate()) {
190
paintString(g, i.left, i.top, width, height);
191
}
192
}
193
194
protected State getState(final JComponent c) {
195
if (!c.isEnabled()) return State.INACTIVE;
196
if (!AquaFocusHandler.isActive(c)) return State.INACTIVE;
197
return State.ACTIVE;
198
}
199
200
protected void paintString(final Graphics g, final int x, final int y, final int width, final int height) {
201
if (!(g instanceof Graphics2D)) return;
202
203
final Graphics2D g2 = (Graphics2D)g;
204
final String progressString = progressBar.getString();
205
g2.setFont(progressBar.getFont());
206
final Point renderLocation = getStringPlacement(g2, progressString, x, y, width, height);
207
final Rectangle oldClip = g2.getClipBounds();
208
209
if (isHorizontal()) {
210
g2.setColor(selectionForeground);
211
SwingUtilities2.drawString(progressBar, g2, progressString, renderLocation.x, renderLocation.y);
212
} else { // VERTICAL
213
// We rotate it -90 degrees, then translate it down since we are going to be bottom up.
214
final AffineTransform savedAT = g2.getTransform();
215
g2.transform(AffineTransform.getRotateInstance(0.0f - (Math.PI / 2.0f), 0, 0));
216
g2.translate(-progressBar.getHeight(), 0);
217
218
// 0,0 is now the bottom left of the viewable area, so we just draw our image at
219
// the render location since that calculation knows about rotation.
220
g2.setColor(selectionForeground);
221
SwingUtilities2.drawString(progressBar, g2, progressString, renderLocation.x, renderLocation.y);
222
223
g2.setTransform(savedAT);
224
}
225
226
g2.setClip(oldClip);
227
}
228
229
/**
230
* Designate the place where the progress string will be painted. This implementation places it at the center of the
231
* progress bar (in both x and y). Override this if you want to right, left, top, or bottom align the progress
232
* string or if you need to nudge it around for any reason.
233
*/
234
protected Point getStringPlacement(final Graphics g, final String progressString, int x, int y, int width, int height) {
235
final FontMetrics fontSizer = progressBar.getFontMetrics(progressBar.getFont());
236
final int stringWidth = fontSizer.stringWidth(progressString);
237
238
if (!isHorizontal()) {
239
// Calculate the location for the rotated text in real component coordinates.
240
// swapping x & y and width & height
241
final int oldH = height;
242
height = width;
243
width = oldH;
244
245
final int oldX = x;
246
x = y;
247
y = oldX;
248
}
249
250
return new Point(x + Math.round(width / 2 - stringWidth / 2), y + ((height + fontSizer.getAscent() - fontSizer.getLeading() - fontSizer.getDescent()) / 2) - 1);
251
}
252
253
static Dimension getCircularPreferredSize() {
254
return new Dimension(20, 20);
255
}
256
257
public Dimension getPreferredSize(final JComponent c) {
258
if (isCircular) {
259
return getCircularPreferredSize();
260
}
261
262
final FontMetrics metrics = progressBar.getFontMetrics(progressBar.getFont());
263
264
final Dimension size = isHorizontal() ? getPreferredHorizontalSize(metrics) : getPreferredVerticalSize(metrics);
265
final Insets insets = progressBar.getInsets();
266
267
size.width += insets.left + insets.right;
268
size.height += insets.top + insets.bottom;
269
return size;
270
}
271
272
protected Dimension getPreferredHorizontalSize(final FontMetrics metrics) {
273
final SizeVariant variant = getSizeDescriptor().get(sizeVariant);
274
final Dimension size = new Dimension(variant.w, variant.h);
275
if (!progressBar.isStringPainted()) return size;
276
277
// Ensure that the progress string will fit
278
final String progString = progressBar.getString();
279
final int stringWidth = metrics.stringWidth(progString);
280
if (stringWidth > size.width) {
281
size.width = stringWidth;
282
}
283
284
// This uses both Height and Descent to be sure that
285
// there is more than enough room in the progress bar
286
// for everything.
287
// This does have a strange dependency on
288
// getStringPlacememnt() in a funny way.
289
final int stringHeight = metrics.getHeight() + metrics.getDescent();
290
if (stringHeight > size.height) {
291
size.height = stringHeight;
292
}
293
return size;
294
}
295
296
protected Dimension getPreferredVerticalSize(final FontMetrics metrics) {
297
final SizeVariant variant = getSizeDescriptor().get(sizeVariant);
298
final Dimension size = new Dimension(variant.h, variant.w);
299
if (!progressBar.isStringPainted()) return size;
300
301
// Ensure that the progress string will fit.
302
final String progString = progressBar.getString();
303
final int stringHeight = metrics.getHeight() + metrics.getDescent();
304
if (stringHeight > size.width) {
305
size.width = stringHeight;
306
}
307
308
// This is also for completeness.
309
final int stringWidth = metrics.stringWidth(progString);
310
if (stringWidth > size.height) {
311
size.height = stringWidth;
312
}
313
return size;
314
}
315
316
public Dimension getMinimumSize(final JComponent c) {
317
if (isCircular) {
318
return getCircularPreferredSize();
319
}
320
321
final Dimension pref = getPreferredSize(progressBar);
322
323
// The Minimum size for this component is 10.
324
// The rationale here is that there should be at least one pixel per 10 percent.
325
if (isHorizontal()) {
326
pref.width = 10;
327
} else {
328
pref.height = 10;
329
}
330
331
return pref;
332
}
333
334
public Dimension getMaximumSize(final JComponent c) {
335
if (isCircular) {
336
return getCircularPreferredSize();
337
}
338
339
final Dimension pref = getPreferredSize(progressBar);
340
341
if (isHorizontal()) {
342
pref.width = Short.MAX_VALUE;
343
} else {
344
pref.height = Short.MAX_VALUE;
345
}
346
347
return pref;
348
}
349
350
public void applySizeFor(final JComponent c, final Size size) {
351
painter.state.set(sizeVariant = size == Size.MINI ? Size.SMALL : sizeVariant); // CUI doesn't support mini progress bars right now
352
}
353
354
protected void startAnimationTimer() {
355
if (animator == null) animator = new Animator();
356
animator.start();
357
isAnimating = true;
358
}
359
360
protected void stopAnimationTimer() {
361
if (animator != null) animator.stop();
362
isAnimating = false;
363
}
364
365
private final Rectangle fUpdateArea = new Rectangle(0, 0, 0, 0);
366
private final Dimension fLastSize = new Dimension(0, 0);
367
protected Rectangle getRepaintRect() {
368
int height = progressBar.getHeight();
369
int width = progressBar.getWidth();
370
371
if (isCircular) {
372
return new Rectangle(20, 20);
373
}
374
375
if (fLastSize.height == height && fLastSize.width == width) {
376
return fUpdateArea;
377
}
378
379
int x = 0;
380
int y = 0;
381
fLastSize.height = height;
382
fLastSize.width = width;
383
384
final int maxHeight = getMaxProgressBarHeight();
385
386
if (isHorizontal()) {
387
final int excessHeight = height - maxHeight;
388
y += excessHeight / 2;
389
height = maxHeight;
390
} else {
391
final int excessHeight = width - maxHeight;
392
x += excessHeight / 2;
393
width = maxHeight;
394
}
395
396
fUpdateArea.setBounds(x, y, width, height);
397
398
return fUpdateArea;
399
}
400
401
protected int getMaxProgressBarHeight() {
402
return getSizeDescriptor().get(sizeVariant).h;
403
}
404
405
protected boolean isHorizontal() {
406
return progressBar.getOrientation() == SwingConstants.HORIZONTAL;
407
}
408
409
protected void revalidateAnimationTimers() {
410
if (progressBar.isIndeterminate()) return;
411
412
if (!isAnimating) {
413
startAnimationTimer(); // only starts if supposed to!
414
return;
415
}
416
417
final BoundedRangeModel model = progressBar.getModel();
418
final double currentValue = model.getValue();
419
if ((currentValue == model.getMaximum()) || (currentValue == model.getMinimum())) {
420
stopAnimationTimer();
421
}
422
}
423
424
protected void repaint() {
425
final Rectangle repaintRect = getRepaintRect();
426
if (repaintRect == null) {
427
progressBar.repaint();
428
return;
429
}
430
431
progressBar.repaint(repaintRect);
432
}
433
434
protected class Animator implements ActionListener {
435
private static final int MINIMUM_DELAY = 5;
436
private Timer timer;
437
private long previousDelay; // used to tune the repaint interval
438
private long lastCall; // the last time actionPerformed was called
439
private int repaintInterval;
440
441
public Animator() {
442
repaintInterval = UIManager.getInt("ProgressBar.repaintInterval");
443
444
// Make sure repaintInterval is reasonable.
445
if (repaintInterval <= 0) repaintInterval = 100;
446
}
447
448
protected void start() {
449
previousDelay = repaintInterval;
450
lastCall = 0;
451
452
if (timer == null) {
453
timer = new Timer(repaintInterval, this);
454
} else {
455
timer.setDelay(repaintInterval);
456
}
457
458
if (ADJUSTTIMER) {
459
timer.setRepeats(false);
460
timer.setCoalesce(false);
461
}
462
463
timer.start();
464
}
465
466
protected void stop() {
467
timer.stop();
468
}
469
470
public void actionPerformed(final ActionEvent e) {
471
if (!ADJUSTTIMER) {
472
repaint();
473
return;
474
}
475
476
final long time = System.currentTimeMillis();
477
478
if (lastCall > 0) {
479
// adjust nextDelay
480
int nextDelay = (int)(previousDelay - time + lastCall + repaintInterval);
481
if (nextDelay < MINIMUM_DELAY) {
482
nextDelay = MINIMUM_DELAY;
483
}
484
485
timer.setInitialDelay(nextDelay);
486
previousDelay = nextDelay;
487
}
488
489
timer.start();
490
lastCall = time;
491
492
repaint();
493
}
494
}
495
}
496
497