~ubuntu-branches/debian/stretch/insubstantial/stretch

« back to all changes in this revision

Viewing changes to substance/src/main/java/org/pushingpixels/substance/internal/ui/SubstanceProgressBarUI.java

  • Committer: Package Import Robot
  • Author(s): Felix Natter
  • Date: 2016-01-18 20:58:45 UTC
  • Revision ID: package-import@ubuntu.com-20160118205845-crbmrkda61qsi5qa
Tags: upstream-7.3+dfsg2
ImportĀ upstreamĀ versionĀ 7.3+dfsg2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2005-2010 Substance Kirill Grouchnikov. All Rights Reserved.
 
3
 *
 
4
 * Redistribution and use in source and binary forms, with or without
 
5
 * modification, are permitted provided that the following conditions are met:
 
6
 *
 
7
 *  o Redistributions of source code must retain the above copyright notice,
 
8
 *    this list of conditions and the following disclaimer.
 
9
 *
 
10
 *  o Redistributions in binary form must reproduce the above copyright notice,
 
11
 *    this list of conditions and the following disclaimer in the documentation
 
12
 *    and/or other materials provided with the distribution.
 
13
 *
 
14
 *  o Neither the name of Substance Kirill Grouchnikov nor the names of
 
15
 *    its contributors may be used to endorse or promote products derived
 
16
 *    from this software without specific prior written permission.
 
17
 *
 
18
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 
19
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 
20
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 
21
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 
22
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 
23
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 
24
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 
25
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 
26
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 
27
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 
28
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
29
 */
 
30
package org.pushingpixels.substance.internal.ui;
 
31
 
 
32
import java.awt.*;
 
33
import java.awt.image.BufferedImage;
 
34
import java.beans.PropertyChangeEvent;
 
35
import java.beans.PropertyChangeListener;
 
36
 
 
37
import javax.swing.*;
 
38
import javax.swing.event.ChangeEvent;
 
39
import javax.swing.event.ChangeListener;
 
40
import javax.swing.plaf.ComponentUI;
 
41
import javax.swing.plaf.basic.BasicProgressBarUI;
 
42
 
 
43
import org.pushingpixels.lafwidget.LafWidgetUtilities;
 
44
import org.pushingpixels.lafwidget.animation.AnimationConfigurationManager;
 
45
import org.pushingpixels.substance.api.*;
 
46
import org.pushingpixels.substance.api.painter.fill.SubstanceFillPainter;
 
47
import org.pushingpixels.substance.internal.utils.*;
 
48
import org.pushingpixels.trident.Timeline;
 
49
import org.pushingpixels.trident.Timeline.RepeatBehavior;
 
50
import org.pushingpixels.trident.Timeline.TimelineState;
 
51
import org.pushingpixels.trident.TimelinePropertyBuilder.PropertySetter;
 
52
import org.pushingpixels.trident.callback.TimelineCallback;
 
53
import org.pushingpixels.trident.ease.Spline;
 
54
 
 
55
/**
 
56
 * UI for progress bars in <b>Substance</b> look and feel.
 
57
 * 
 
58
 * @author Kirill Grouchnikov
 
59
 */
 
60
public class SubstanceProgressBarUI extends BasicProgressBarUI {
 
61
        private static final ComponentState DETERMINATE_SELECTED = new ComponentState(
 
62
                        "determinate enabled", new ComponentStateFacet[] {
 
63
                                        ComponentStateFacet.ENABLE,
 
64
                                        ComponentStateFacet.DETERMINATE,
 
65
                                        ComponentStateFacet.SELECTION }, null);
 
66
 
 
67
        private static final ComponentState DETERMINATE_SELECTED_DISABLED = new ComponentState(
 
68
                        "determinate disabled", new ComponentStateFacet[] {
 
69
                                        ComponentStateFacet.DETERMINATE,
 
70
                                        ComponentStateFacet.SELECTION },
 
71
                        new ComponentStateFacet[] { ComponentStateFacet.ENABLE });
 
72
 
 
73
        private static final ComponentState INDETERMINATE_SELECTED = new ComponentState(
 
74
                        "indeterminate enabled",
 
75
                        new ComponentStateFacet[] { ComponentStateFacet.ENABLE,
 
76
                                        ComponentStateFacet.SELECTION },
 
77
                        new ComponentStateFacet[] { ComponentStateFacet.DETERMINATE });
 
78
 
 
79
        private static final ComponentState INDETERMINATE_SELECTED_DISABLED = new ComponentState(
 
80
                        "indeterminate disabled", null, new ComponentStateFacet[] {
 
81
                                        ComponentStateFacet.DETERMINATE,
 
82
                                        ComponentStateFacet.ENABLE, ComponentStateFacet.SELECTION });
 
83
 
 
84
        private final class SubstanceChangeListener implements ChangeListener {
 
85
                @Override
 
86
        public void stateChanged(ChangeEvent e) {
 
87
                        SubstanceCoreUtilities
 
88
                                        .testComponentStateChangeThreadingViolation(progressBar);
 
89
 
 
90
                        int currValue = progressBar.getValue();
 
91
                        int span = progressBar.getMaximum() - progressBar.getMinimum();
 
92
 
 
93
                        int barRectWidth = progressBar.getWidth() - 2 * margin;
 
94
                        int barRectHeight = progressBar.getHeight() - 2 * margin;
 
95
                        int totalPixels = (progressBar.getOrientation() == JProgressBar.HORIZONTAL) ? barRectWidth
 
96
                                        : barRectHeight;
 
97
                        // fix for defect 223 (min and max on the model are the
 
98
                        // same).
 
99
                        int pixelDelta = (span <= 0) ? 0 : (currValue - displayedValue)
 
100
                                        * totalPixels / span;
 
101
 
 
102
                        if (displayTimeline != null) {
 
103
                                displayTimeline.abort();
 
104
                        }
 
105
                        displayTimeline = new Timeline(progressBar);
 
106
                        displayTimeline.addPropertyToInterpolate(Timeline
 
107
                                        .<Integer> property("displayedValue").from(displayedValue)
 
108
                                        .to(currValue).setWith(new PropertySetter<Integer>() {
 
109
                                                @Override
 
110
                                                public void set(Object obj, String fieldName,
 
111
                                                                Integer value) {
 
112
                                                        displayedValue = value;
 
113
                                                        progressBar.repaint();
 
114
                                                }
 
115
                                        }));
 
116
                        displayTimeline.setEase(new Spline(0.4f));
 
117
                        AnimationConfigurationManager.getInstance().configureTimeline(
 
118
                                        displayTimeline);
 
119
 
 
120
                        // do not animate progress bars used in cell renderers
 
121
                        // since in this case it will most probably be the
 
122
                        // same progress bar used to display different
 
123
                        // values for different cells.
 
124
                        boolean isInCellRenderer = (SwingUtilities.getAncestorOfClass(
 
125
                                        CellRendererPane.class, progressBar) != null);
 
126
                        if (!isInCellRenderer && Math.abs(pixelDelta) > 5) {
 
127
                                displayTimeline.play();
 
128
                        } else {
 
129
                                displayedValue = currValue;
 
130
                                progressBar.repaint();
 
131
                        }
 
132
                }
 
133
        }
 
134
 
 
135
        /**
 
136
         * Hash for computed stripe images.
 
137
         */
 
138
        private static LazyResettableHashMap<BufferedImage> stripeMap = new LazyResettableHashMap<BufferedImage>(
 
139
                        "SubstanceProgressBarUI.stripeMap");
 
140
 
 
141
        /**
 
142
         * Hash for computed background images.
 
143
         */
 
144
        private static LazyResettableHashMap<BufferedImage> backgroundMap = new LazyResettableHashMap<BufferedImage>(
 
145
                        "SubstanceProgressBarUI.backgroundMap");
 
146
 
 
147
        /**
 
148
         * The current position of the indeterminate animation's cycle. 0, the
 
149
         * initial value, means paint the first frame. When the progress bar is
 
150
         * indeterminate and showing, the {@link #indeterminateLoopTimeline} is
 
151
         * updating this value.
 
152
         */
 
153
        private float animationPosition;
 
154
 
 
155
        /**
 
156
         * Value change listener on the associated progress bar.
 
157
         */
 
158
        protected ChangeListener substanceValueChangeListener;
 
159
 
 
160
        /**
 
161
         * Property change listener. Tracks changes to the <code>font</code>
 
162
         * property.
 
163
         */
 
164
        protected PropertyChangeListener substancePropertyChangeListener;
 
165
 
 
166
        /**
 
167
         * Inner margin.
 
168
         */
 
169
        protected int margin;
 
170
 
 
171
        /**
 
172
         * The speed factor for the indeterminate progress bars.
 
173
         */
 
174
        protected float speed;
 
175
 
 
176
        protected int displayedValue;
 
177
 
 
178
        protected Timeline displayTimeline;
 
179
 
 
180
        protected Timeline indeterminateLoopTimeline;
 
181
 
 
182
        /*
 
183
         * (non-Javadoc)
 
184
         * 
 
185
         * @see javax.swing.plaf.ComponentUI#createUI(javax.swing.JComponent)
 
186
         */
 
187
        public static ComponentUI createUI(JComponent comp) {
 
188
                SubstanceCoreUtilities.testComponentCreationThreadingViolation(comp);
 
189
                return new SubstanceProgressBarUI();
 
190
        }
 
191
 
 
192
        @Override
 
193
        protected void installDefaults() {
 
194
                super.installDefaults();
 
195
 
 
196
                this.displayedValue = progressBar.getValue();
 
197
                LookAndFeel.installProperty(progressBar, "opaque", Boolean.FALSE);
 
198
 
 
199
                this.speed = (20.0f * UIManager.getInt("ProgressBar.repaintInterval"))
 
200
                                / UIManager.getInt("ProgressBar.cycleTime");
 
201
 
 
202
                float borderThickness = SubstanceSizeUtils
 
203
                                .getBorderStrokeWidth(SubstanceSizeUtils
 
204
                                                .getComponentFontSize(this.progressBar));
 
205
                this.margin = (int) Math.ceil(1.5 * borderThickness);
 
206
        }
 
207
 
 
208
        @Override
 
209
        protected void installListeners() {
 
210
                super.installListeners();
 
211
 
 
212
                substanceValueChangeListener = new SubstanceChangeListener();
 
213
                this.progressBar.addChangeListener(this.substanceValueChangeListener);
 
214
 
 
215
                this.substancePropertyChangeListener = new PropertyChangeListener() {
 
216
                        @Override
 
217
                        public void propertyChange(PropertyChangeEvent evt) {
 
218
                                if ("font".equals(evt.getPropertyName())) {
 
219
                                        SwingUtilities.invokeLater(new Runnable() {
 
220
                                                @Override
 
221
                        public void run() {
 
222
                                                        if (progressBar != null)
 
223
                                                                progressBar.updateUI();
 
224
                                                }
 
225
                                        });
 
226
                                }
 
227
                        }
 
228
                };
 
229
                this.progressBar
 
230
                                .addPropertyChangeListener(this.substancePropertyChangeListener);
 
231
        }
 
232
 
 
233
        /*
 
234
         * (non-Javadoc)
 
235
         * 
 
236
         * @see javax.swing.plaf.basic.BasicProgressBarUI#uninstallListeners()
 
237
         */
 
238
        @Override
 
239
        protected void uninstallListeners() {
 
240
                this.progressBar
 
241
                                .removeChangeListener(this.substanceValueChangeListener);
 
242
                this.substanceValueChangeListener = null;
 
243
 
 
244
                this.progressBar
 
245
                                .removePropertyChangeListener(this.substancePropertyChangeListener);
 
246
                this.substancePropertyChangeListener = null;
 
247
 
 
248
                super.uninstallListeners();
 
249
        }
 
250
 
 
251
        /**
 
252
         * Retrieves stripe image.
 
253
         * 
 
254
         * @param baseSize
 
255
         *            Stripe base in pixels.
 
256
         * @param isRotated
 
257
         *            if <code>true</code>, the resulting stripe image will be
 
258
         *            rotated.
 
259
         * @param colorScheme
 
260
         *            Color scheme to paint the stripe image.
 
261
         * @return Stripe image.
 
262
         */
 
263
        private static BufferedImage getStripe(int baseSize, boolean isRotated,
 
264
                        SubstanceColorScheme colorScheme) {
 
265
                HashMapKey key = SubstanceCoreUtilities.getHashKey(baseSize, isRotated,
 
266
                                colorScheme.getDisplayName());
 
267
                BufferedImage result = SubstanceProgressBarUI.stripeMap.get(key);
 
268
                if (result == null) {
 
269
                        result = SubstanceImageCreator.getStripe(baseSize, colorScheme
 
270
                                        .getUltraLightColor());
 
271
                        if (isRotated) {
 
272
                                result = SubstanceImageCreator.getRotated(result, 1);
 
273
                        }
 
274
                        SubstanceProgressBarUI.stripeMap.put(key, result);
 
275
                }
 
276
                return result;
 
277
        }
 
278
 
 
279
        /**
 
280
         * Returns the background of a determinate progress bar.
 
281
         * 
 
282
         * @param bar
 
283
         *            Progress bar.
 
284
         * @param width
 
285
         *            Progress bar width.
 
286
         * @param height
 
287
         *            Progress bar height.
 
288
         * @param scheme
 
289
         *            Color scheme for the background.
 
290
         * @param gp
 
291
         *            Gradient painter.
 
292
         * @param orientation
 
293
         *            Progress bar orientation (vertical / horizontal).
 
294
         * @param componentOrientation
 
295
         *            Progress bar LTR / RTL orientation.
 
296
         * @return Background image.
 
297
         */
 
298
        private static BufferedImage getDeterminateBackground(JProgressBar bar,
 
299
                        int width, int height, SubstanceColorScheme scheme,
 
300
                        SubstanceFillPainter gp, int orientation,
 
301
                        ComponentOrientation componentOrientation) {
 
302
                HashMapKey key = SubstanceCoreUtilities.getHashKey(width, height,
 
303
                                scheme.getDisplayName(), gp.getDisplayName(), orientation,
 
304
                                componentOrientation);
 
305
                BufferedImage result = SubstanceProgressBarUI.backgroundMap.get(key);
 
306
                if (result == null) {
 
307
                        result = SubstanceCoreUtilities.getBlankImage(width, height);
 
308
                        Graphics2D g2d = result.createGraphics();
 
309
                        Shape contour = SubstanceOutlineUtilities.getBaseOutline(width,
 
310
                                        height, 0, null);
 
311
                        gp.paintContourBackground(g2d, bar, width, height, contour, false,
 
312
                                        scheme, true);
 
313
                        g2d.dispose();
 
314
 
 
315
                        if (orientation == SwingConstants.VERTICAL) {
 
316
                                if (componentOrientation.isLeftToRight())
 
317
                                        result = SubstanceImageCreator.getRotated(result, 3);
 
318
                                else
 
319
                                        result = SubstanceImageCreator.getRotated(result, 1);
 
320
                        }
 
321
                        SubstanceProgressBarUI.backgroundMap.put(key, result);
 
322
                }
 
323
                return result;
 
324
 
 
325
        }
 
326
 
 
327
        /*
 
328
         * (non-Javadoc)
 
329
         * 
 
330
         * @see
 
331
         * javax.swing.plaf.basic.BasicProgressBarUI#paintDeterminate(java.awt.Graphics
 
332
         * , javax.swing.JComponent)
 
333
         */
 
334
        @Override
 
335
        public void paintDeterminate(Graphics g, JComponent c) {
 
336
                if (!(g instanceof Graphics2D)) {
 
337
                        return;
 
338
                }
 
339
 
 
340
                ComponentState fillState = getFillState();
 
341
                ComponentState progressState = getProgressState();
 
342
 
 
343
                // final Insets insets = this.getInsets();
 
344
                // insets.top /= 2;
 
345
                // insets.left /= 2;
 
346
                // insets.bottom /= 2;
 
347
                // insets.right /= 2;
 
348
                int barRectWidth = progressBar.getWidth() - 2 * margin;
 
349
                int barRectHeight = progressBar.getHeight() - 2 * margin;
 
350
 
 
351
                // amount of progress to draw
 
352
                int amountFull = getAmountFull(new Insets(margin, margin, margin,
 
353
                                margin), barRectWidth, barRectHeight);
 
354
 
 
355
                // System.out.println("@" + progressBar.hashCode() + " - display="
 
356
                // + this.displayedValue + ", amount=" + amountFull);
 
357
 
 
358
                Graphics2D g2d = (Graphics2D) g.create();
 
359
                // install state-aware alpha channel (support for skins
 
360
                // that use translucency on disabled states).
 
361
                float stateAlpha = SubstanceColorSchemeUtilities.getAlpha(progressBar,
 
362
                                fillState);
 
363
                g2d.setComposite(LafWidgetUtilities.getAlphaComposite(progressBar,
 
364
                                stateAlpha, g));
 
365
 
 
366
                SubstanceColorScheme fillScheme = SubstanceColorSchemeUtilities
 
367
                                .getColorScheme(progressBar, fillState);
 
368
 
 
369
                SubstanceFillPainter fillPainter = SubstanceCoreUtilities
 
370
                                .getFillPainter(progressBar);
 
371
                if (progressBar.getOrientation() == SwingConstants.HORIZONTAL) {
 
372
                        BufferedImage back = getDeterminateBackground(progressBar,
 
373
                                        barRectWidth + 1, barRectHeight + 1, fillScheme,
 
374
                                        fillPainter, progressBar.getOrientation(), this.progressBar
 
375
                                                        .getComponentOrientation());
 
376
                        g2d.drawImage(back, margin, margin, null);
 
377
                } else {
 
378
                        BufferedImage back = getDeterminateBackground(progressBar,
 
379
                                        barRectHeight + 1, barRectWidth + 1, fillScheme,
 
380
                                        fillPainter, progressBar.getOrientation(), this.progressBar
 
381
                                                        .getComponentOrientation());
 
382
                        g2d.drawImage(back, margin, margin, null);
 
383
                }
 
384
 
 
385
                if (amountFull > 0) {
 
386
                        int borderDelta = 0;
 
387
 
 
388
                        SubstanceColorScheme fillColorScheme = SubstanceColorSchemeUtilities
 
389
                                        .getColorScheme(progressBar, progressState);
 
390
                        if (progressBar.getOrientation() == SwingConstants.HORIZONTAL) {
 
391
                                int barWidth = amountFull - 2 * borderDelta;
 
392
                                int barHeight = barRectHeight - 2 * borderDelta;
 
393
                                if ((barWidth > 0) && (barHeight > 0)) {
 
394
                                        if (progressBar.getComponentOrientation().isLeftToRight()) {
 
395
                                                SubstanceImageCreator.paintRectangularBackground(
 
396
                                                                progressBar, g, margin + borderDelta, margin
 
397
                                                                                + borderDelta, barWidth, barHeight,
 
398
                                                                fillColorScheme, 0.6f, false);
 
399
 
 
400
                                                // g.setColor(Color.red);
 
401
                                                // g.fillRect(insets.left + borderDelta,
 
402
                                                // insets.top + borderDelta, barWidth, barHeight);
 
403
                                        } else {
 
404
                                                // fix for RTL determinate horizontal progress
 
405
                                                // bar in 2.3
 
406
                                                SubstanceImageCreator.paintRectangularBackground(
 
407
                                                                progressBar, g, margin + barRectWidth
 
408
                                                                                - amountFull - 2 * borderDelta, margin
 
409
                                                                                + borderDelta, barWidth, barHeight,
 
410
                                                                fillColorScheme, 0.6f, false);
 
411
                                        }
 
412
                                }
 
413
                        } else { // VERTICAL
 
414
                                int barWidth = amountFull - 2 * borderDelta;
 
415
                                int barHeight = barRectWidth - 2 * borderDelta;
 
416
                                if ((amountFull > 0) && (barHeight > 0)) {
 
417
                                        // fix for issue 95. Vertical bar is growing from
 
418
                                        // the bottom
 
419
                                        SubstanceImageCreator.paintRectangularBackground(
 
420
                                                        progressBar, g, margin + borderDelta, margin
 
421
                                                                        + barRectHeight - barWidth - borderDelta,
 
422
                                                        barHeight, barWidth, fillColorScheme, 0.6f, true);
 
423
                                }
 
424
                        }
 
425
                }
 
426
 
 
427
                // Deal with possible text painting
 
428
                if (progressBar.isStringPainted()) {
 
429
                        g2d.setComposite(LafWidgetUtilities.getAlphaComposite(progressBar,
 
430
                                        1.0f, g));
 
431
                        this.paintString(g2d, margin, margin, barRectWidth, barRectHeight,
 
432
                                        amountFull, new Insets(margin, margin, margin, margin));
 
433
                }
 
434
                g2d.dispose();
 
435
        }
 
436
 
 
437
        /*
 
438
         * (non-Javadoc)
 
439
         * 
 
440
         * @see javax.swing.plaf.basic.BasicProgressBarUI#getSelectionBackground()
 
441
         */
 
442
        @Override
 
443
        protected Color getSelectionBackground() {
 
444
                ComponentState fillState = getFillState();
 
445
 
 
446
                SubstanceColorScheme scheme = SubstanceColorSchemeUtilities
 
447
                                .getColorScheme(progressBar, fillState);
 
448
                return SubstanceColorUtilities.getForegroundColor(scheme);
 
449
        }
 
450
 
 
451
        /*
 
452
         * (non-Javadoc)
 
453
         * 
 
454
         * @see javax.swing.plaf.basic.BasicProgressBarUI#getSelectionForeground()
 
455
         */
 
456
        @Override
 
457
        protected Color getSelectionForeground() {
 
458
                ComponentState progressState = getProgressState();
 
459
 
 
460
                SubstanceColorScheme scheme = SubstanceColorSchemeUtilities
 
461
                                .getColorScheme(progressBar, progressState);
 
462
                return SubstanceColorUtilities.getForegroundColor(scheme);
 
463
        }
 
464
 
 
465
        /*
 
466
         * (non-Javadoc)
 
467
         * 
 
468
         * @see
 
469
         * javax.swing.plaf.basic.BasicProgressBarUI#paintIndeterminate(java.awt
 
470
         * .Graphics, javax.swing.JComponent)
 
471
         */
 
472
        @Override
 
473
        public void paintIndeterminate(Graphics g, JComponent c) {
 
474
                if (!(g instanceof Graphics2D)) {
 
475
                        return;
 
476
                }
 
477
 
 
478
                ComponentState progressState = getProgressState();
 
479
 
 
480
                // final Insets b = this.getInsets(); // area for border
 
481
                final int barRectWidth = progressBar.getWidth() - 2 * margin;
 
482
                final int barRectHeight = progressBar.getHeight() - 2 * margin;
 
483
 
 
484
                int valComplete = 0;
 
485
                if (progressBar.getOrientation() == SwingConstants.HORIZONTAL) {
 
486
                        valComplete = (int) (this.animationPosition * (2 * barRectHeight + 1));
 
487
                } else {
 
488
                        valComplete = (int) (this.animationPosition * (2 * barRectWidth + 1));
 
489
                }
 
490
 
 
491
                // final int valComplete = (int) animationIndex;
 
492
 
 
493
                Graphics2D g2d = (Graphics2D) g.create();
 
494
                // install state-aware alpha channel (support for skins
 
495
                // that use translucency on disabled states).
 
496
                float stateAlpha = SubstanceColorSchemeUtilities.getAlpha(progressBar,
 
497
                                progressState);
 
498
                g2d.setComposite(LafWidgetUtilities.getAlphaComposite(progressBar,
 
499
                                stateAlpha, g));
 
500
 
 
501
                SubstanceColorScheme scheme = SubstanceColorSchemeUtilities
 
502
                                .getColorScheme(progressBar, progressState);
 
503
                if (progressBar.getOrientation() == SwingConstants.HORIZONTAL) {
 
504
                        SubstanceImageCreator.paintRectangularStripedBackground(
 
505
                                        progressBar, g2d, margin, margin, barRectWidth,
 
506
                                        barRectHeight, scheme, SubstanceProgressBarUI.getStripe(
 
507
                                                        barRectHeight, false, scheme), valComplete, 0.6f,
 
508
                                        false);
 
509
                } else {
 
510
                        // fix for issue 95. Vertical progress bar grows from the
 
511
                        // bottom.
 
512
                        SubstanceImageCreator.paintRectangularStripedBackground(
 
513
                                        progressBar, g2d, margin, margin, barRectWidth,
 
514
                                        barRectHeight, scheme, SubstanceProgressBarUI.getStripe(
 
515
                                                        barRectWidth, true, scheme), 2 * barRectWidth
 
516
                                                        - valComplete, 0.6f, true);
 
517
                }
 
518
 
 
519
                // Deal with possible text painting
 
520
                if (progressBar.isStringPainted()) {
 
521
                        g2d.setComposite(LafWidgetUtilities.getAlphaComposite(progressBar,
 
522
                                        1.0f, g));
 
523
                        this.paintString(g2d, margin, margin, barRectWidth, barRectHeight,
 
524
                                        barRectWidth, new Insets(margin, margin, margin, margin));
 
525
                }
 
526
                g2d.dispose();
 
527
        }
 
528
 
 
529
        private ComponentState getFillState() {
 
530
                return progressBar.isEnabled() ? ComponentState.ENABLED
 
531
                                : ComponentState.DISABLED_UNSELECTED;
 
532
        }
 
533
 
 
534
        private ComponentState getProgressState() {
 
535
                if (progressBar.isIndeterminate()) {
 
536
                        return progressBar.isEnabled() ? INDETERMINATE_SELECTED
 
537
                                        : INDETERMINATE_SELECTED_DISABLED;
 
538
                } else {
 
539
                        return progressBar.isEnabled() ? DETERMINATE_SELECTED
 
540
                                        : DETERMINATE_SELECTED_DISABLED;
 
541
                }
 
542
        }
 
543
 
 
544
        /*
 
545
         * (non-Javadoc)
 
546
         * 
 
547
         * @see javax.swing.plaf.basic.BasicProgressBarUI#getBox(java.awt.Rectangle)
 
548
         */
 
549
        @Override
 
550
        protected Rectangle getBox(Rectangle r) {
 
551
                // Insets b = this.getInsets(); // area for border
 
552
                int barRectWidth = progressBar.getWidth() - 2 * margin;
 
553
                int barRectHeight = progressBar.getHeight() - 2 * margin;
 
554
                return new Rectangle(margin, margin, barRectWidth, barRectHeight);
 
555
        }
 
556
 
 
557
        @Override
 
558
        protected void startAnimationTimer() {
 
559
                this.indeterminateLoopTimeline = new Timeline(this);
 
560
                Integer cycleDuration = UIManager.getInt("ProgressBar.cycleTime");
 
561
                if (cycleDuration == null)
 
562
                        cycleDuration = 1000;
 
563
                this.indeterminateLoopTimeline.setDuration(cycleDuration);
 
564
                this.indeterminateLoopTimeline.addCallback(new TimelineCallback() {
 
565
                        @Override
 
566
                        public void onTimelineStateChanged(TimelineState oldState,
 
567
                                        TimelineState newState, float durationFraction,
 
568
                                        float timelinePosition) {
 
569
                                if ((progressBar != null) && progressBar.isVisible())
 
570
                                        progressBar.repaint();
 
571
                        }
 
572
 
 
573
                        @Override
 
574
                        public void onTimelinePulse(float durationFraction,
 
575
                                        float timelinePosition) {
 
576
                                if ((progressBar != null) && progressBar.isVisible())
 
577
                                        progressBar.repaint();
 
578
                        }
 
579
                });
 
580
                this.indeterminateLoopTimeline.addPropertyToInterpolate(Timeline
 
581
                                .<Float> property("animationPosition").from(0.0f).to(1.0f)
 
582
                                .setWith(new PropertySetter<Float>() {
 
583
                                        @Override
 
584
                                        public void set(Object obj, String fieldName, Float value) {
 
585
                                                animationPosition = value;
 
586
                                        }
 
587
                                }));
 
588
                this.indeterminateLoopTimeline.playLoop(RepeatBehavior.LOOP);
 
589
        }
 
590
 
 
591
        @Override
 
592
        protected void stopAnimationTimer() {
 
593
                this.indeterminateLoopTimeline.abort();
 
594
        }
 
595
 
 
596
        /**
 
597
         * Returns the memory usage string.
 
598
         * 
 
599
         * @return The memory usage string.
 
600
         */
 
601
        public static String getMemoryUsage() {
 
602
                StringBuffer sb = new StringBuffer();
 
603
                sb.append("SubstanceProgressBarUI: \n");
 
604
                sb.append("\t" + SubstanceProgressBarUI.stripeMap.size() + " stripes");
 
605
                return sb.toString();
 
606
        }
 
607
 
 
608
        @Override
 
609
        protected int getAmountFull(Insets b, int width, int height) {
 
610
                int amountFull = 0;
 
611
                BoundedRangeModel model = progressBar.getModel();
 
612
 
 
613
                long span = model.getMaximum() - model.getMinimum();
 
614
                double percentComplete = (double) (this.displayedValue - model
 
615
                                .getMinimum())
 
616
                                / (double) span;
 
617
 
 
618
                if ((model.getMaximum() - model.getMinimum()) != 0) {
 
619
                        if (this.progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
 
620
                                amountFull = (int) Math.round(width * percentComplete);
 
621
                        } else {
 
622
                                amountFull = (int) Math.round(height * percentComplete);
 
623
                        }
 
624
                }
 
625
                return amountFull;
 
626
        }
 
627
 
 
628
        /*
 
629
         * (non-Javadoc)
 
630
         * 
 
631
         * @see
 
632
         * javax.swing.plaf.basic.BasicProgressBarUI#getPreferredInnerHorizontal()
 
633
         */
 
634
        @Override
 
635
        protected Dimension getPreferredInnerHorizontal() {
 
636
                int size = SubstanceSizeUtils.getComponentFontSize(this.progressBar);
 
637
                size += 2 * SubstanceSizeUtils.getAdjustedSize(size, 1, 4, 1, false);
 
638
                return new Dimension(146 + SubstanceSizeUtils.getAdjustedSize(size, 0,
 
639
                                1, 10, false), size);
 
640
        }
 
641
 
 
642
        /*
 
643
         * (non-Javadoc)
 
644
         * 
 
645
         * @see
 
646
         * javax.swing.plaf.basic.BasicProgressBarUI#getPreferredInnerVertical()
 
647
         */
 
648
        @Override
 
649
        protected Dimension getPreferredInnerVertical() {
 
650
                int size = SubstanceSizeUtils.getComponentFontSize(this.progressBar);
 
651
                size += 2 * SubstanceSizeUtils.getAdjustedSize(size, 1, 4, 1, false);
 
652
                return new Dimension(size, 146 + SubstanceSizeUtils.getAdjustedSize(
 
653
                                size, 0, 1, 10, false));
 
654
        }
 
655
 
 
656
        @Override
 
657
        protected void paintString(Graphics g, int x, int y, int width, int height,
 
658
                        int amountFull, Insets b) {
 
659
                if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
 
660
                        if (progressBar.getComponentOrientation().isLeftToRight()) {
 
661
                                if (progressBar.isIndeterminate()) {
 
662
                                        boxRect = getBox(boxRect);
 
663
                                        paintString(g, x, y, width, height, boxRect.x,
 
664
                                                        boxRect.width, b);
 
665
                                } else {
 
666
                                        paintString(g, x, y, width, height, x, amountFull, b);
 
667
                                }
 
668
                        } else {
 
669
                                paintString(g, x, y, width, height, x + width - amountFull,
 
670
                                                amountFull, b);
 
671
                        }
 
672
                } else {
 
673
                        if (progressBar.isIndeterminate()) {
 
674
                                boxRect = getBox(boxRect);
 
675
                                paintString(g, x, y, width, height, boxRect.y, boxRect.height,
 
676
                                                b);
 
677
                        } else {
 
678
                                paintString(g, x, y, width, height, y + height - amountFull,
 
679
                                                amountFull, b);
 
680
                        }
 
681
                }
 
682
        }
 
683
 
 
684
        /**
 
685
         * Paints the progress string.
 
686
         * 
 
687
         * @param g
 
688
         *            Graphics used for drawing.
 
689
         * @param x
 
690
         *            x location of bounding box
 
691
         * @param y
 
692
         *            y location of bounding box
 
693
         * @param width
 
694
         *            width of bounding box
 
695
         * @param height
 
696
         *            height of bounding box
 
697
         * @param fillStart
 
698
         *            start location, in x or y depending on orientation, of the
 
699
         *            filled portion of the progress bar.
 
700
         * @param amountFull
 
701
         *            size of the fill region, either width or height depending upon
 
702
         *            orientation.
 
703
         * @param b
 
704
         *            Insets of the progress bar.
 
705
         */
 
706
        private void paintString(Graphics g, int x, int y, int width, int height,
 
707
                        int fillStart, int amountFull, Insets b) {
 
708
                String progressString = progressBar.getString();
 
709
                Rectangle renderRectangle = getStringRectangle(progressString, x, y,
 
710
                                width, height);
 
711
 
 
712
                if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
 
713
                        SubstanceTextUtilities.paintText(g, this.progressBar,
 
714
                                        renderRectangle, progressString, -1, progressBar.getFont(),
 
715
                                        getSelectionBackground(), new Rectangle(amountFull, y,
 
716
                                                        progressBar.getWidth() - amountFull, height));
 
717
                        SubstanceTextUtilities.paintText(g, this.progressBar,
 
718
                                        renderRectangle, progressString, -1, progressBar.getFont(),
 
719
                                        getSelectionForeground(), new Rectangle(fillStart, y,
 
720
                                                        amountFull, height));
 
721
                } else { // VERTICAL
 
722
                        SubstanceTextUtilities.paintVerticalText(g, this.progressBar,
 
723
                                        renderRectangle, progressString, -1, progressBar.getFont(),
 
724
                                        getSelectionBackground(), new Rectangle(x, y, width,
 
725
                                                        progressBar.getHeight() - amountFull), progressBar
 
726
                                                        .getComponentOrientation().isLeftToRight());
 
727
                        SubstanceTextUtilities.paintVerticalText(g, this.progressBar,
 
728
                                        renderRectangle, progressString, -1, progressBar.getFont(),
 
729
                                        getSelectionForeground(), new Rectangle(x, fillStart,
 
730
                                                        width, amountFull), progressBar
 
731
                                                        .getComponentOrientation().isLeftToRight());
 
732
                }
 
733
        }
 
734
 
 
735
        /**
 
736
         * Returns the rectangle for the progress bar string.
 
737
         * 
 
738
         * @param progressString
 
739
         *            Progress bar string.
 
740
         * @param x
 
741
         *            x location of bounding box
 
742
         * @param y
 
743
         *            y location of bounding box
 
744
         * @param width
 
745
         *            width of bounding box
 
746
         * @param height
 
747
         *            height of bounding box
 
748
         * @return The rectangle for the progress bar string.
 
749
         */
 
750
        protected Rectangle getStringRectangle(String progressString, int x, int y,
 
751
                        int width, int height) {
 
752
                FontMetrics fontSizer = progressBar.getFontMetrics(progressBar
 
753
                                .getFont());
 
754
 
 
755
                int stringWidth = fontSizer.stringWidth(progressString);
 
756
 
 
757
                if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
 
758
                        return new Rectangle(x + Math.round(width / 2 - stringWidth / 2), y
 
759
                                        + (height - fontSizer.getHeight()) / 2, stringWidth,
 
760
                                        fontSizer.getHeight());
 
761
                } else {
 
762
                        return new Rectangle(x + (width - fontSizer.getHeight()) / 2, y
 
763
                                        + Math.round(height / 2 - stringWidth / 2), fontSizer
 
764
                                        .getHeight(), stringWidth);
 
765
                }
 
766
        }
 
767
}