Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
Duration |
|
| 0.0;0 | ||||
Duration$1Task |
|
| 0.0;0 | ||||
Duration$1Task$1 |
|
| 0.0;0 |
1 | /* | |
2 | * Copyright 2007 Sun Microsystems, Inc. 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. Sun designates this | |
8 | * particular file as subject to the "Classpath" exception as provided | |
9 | * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, | |
22 | * CA 95054 USA or visit www.sun.com if you need additional information or | |
23 | * have any questions. | |
24 | */ | |
25 | ||
26 | package com.sun.javafx.runtime.async; | |
27 | ||
28 | import javax.swing.*; | |
29 | import java.util.concurrent.ScheduledExecutorService; | |
30 | import java.util.concurrent.ScheduledFuture; | |
31 | import java.util.concurrent.TimeUnit; | |
32 | ||
33 | /** | |
34 | * Duration | |
35 | * | |
36 | * @author Brian Goetz | |
37 | */ | |
38 | 0 | public class Duration { |
39 | private final DurationListener listener; | |
40 | private final int min, max, interval, increment; | |
41 | private ScheduledFuture<?> future; | |
42 | public static final int MIN_INTERVAL = 100; | |
43 | ||
44 | 0 | public Duration(DurationListener listener, int min, int max, int duration) { |
45 | 0 | this.listener = listener; |
46 | 0 | this.min = min; |
47 | 0 | this.max = max; |
48 | 0 | int interval = duration / (max - min); |
49 | 0 | if (interval < MIN_INTERVAL) { |
50 | 0 | increment = MIN_INTERVAL / interval; |
51 | 0 | this.interval = interval * increment; |
52 | } | |
53 | else { | |
54 | 0 | this.interval = interval; |
55 | 0 | this.increment = 1; |
56 | } | |
57 | 0 | } |
58 | ||
59 | public void start() { | |
60 | 0 | class Task implements Runnable { |
61 | 0 | private int next = min; |
62 | ||
63 | public void run() { | |
64 | 0 | final int nextNumber = Math.min(next, max); |
65 | 0 | SwingUtilities.invokeLater(new Runnable() { |
66 | public void run() { | |
67 | 0 | listener.onTick(nextNumber); |
68 | 0 | } |
69 | }); | |
70 | 0 | next += increment; |
71 | 0 | if (next > max) |
72 | 0 | future.cancel(false); |
73 | 0 | } |
74 | } | |
75 | ||
76 | 0 | future = BackgroundExecutor.getTimer().scheduleWithFixedDelay(new Task(), 0, interval, TimeUnit.MILLISECONDS); |
77 | 0 | } |
78 | ||
79 | public void cancel() { | |
80 | 0 | if (future != null) |
81 | 0 | future.cancel(false); |
82 | 0 | } |
83 | } |