-
Notifications
You must be signed in to change notification settings - Fork 23
/
SwingScheduler.java
154 lines (127 loc) · 4.92 KB
/
SwingScheduler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/**
* Copyright 2014 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rx.schedulers;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.TimeUnit;
import javax.swing.*;
import rx.Scheduler;
import rx.Subscription;
import rx.functions.Action0;
import rx.subscriptions.BooleanSubscription;
import rx.subscriptions.CompositeSubscription;
import rx.subscriptions.Subscriptions;
/**
* Executes work on the Swing UI thread.
* This scheduler should only be used with actions that execute quickly.
*
* If the calling thread is the Swing UI thread, and no delay parameter is
* provided, the action will run immediately. Otherwise, if the calling
* thread is NOT the Swing UI thread, the action will be deferred until
* all pending UI events have been processed.
*/
public final class SwingScheduler extends Scheduler {
private static final SwingScheduler INSTANCE = new SwingScheduler();
public static SwingScheduler getInstance() {
return INSTANCE;
}
/* package for unit test */SwingScheduler() {
}
@Override
public Worker createWorker() {
return new InnerSwingScheduler();
}
private static class InnerSwingScheduler extends Worker {
private final CompositeSubscription innerSubscription = new CompositeSubscription();
@Override
public void unsubscribe() {
innerSubscription.unsubscribe();
}
@Override
public boolean isUnsubscribed() {
return innerSubscription.isUnsubscribed();
}
@Override
public Subscription schedule(final Action0 action, long delayTime, TimeUnit unit) {
long delay = Math.max(0, unit.toMillis(delayTime));
assertThatTheDelayIsValidForTheSwingTimer(delay);
final BooleanSubscription s = BooleanSubscription.create();
class ExecuteOnceAction implements ActionListener {
private Timer timer;
private void setTimer(Timer timer) {
this.timer = timer;
}
@Override
public void actionPerformed(ActionEvent e) {
timer.stop();
if (innerSubscription.isUnsubscribed() || s.isUnsubscribed()) {
return;
}
action.call();
innerSubscription.remove(s);
}
}
ExecuteOnceAction executeOnce = new ExecuteOnceAction();
final Timer timer = new Timer((int) delay, executeOnce);
executeOnce.setTimer(timer);
timer.start();
innerSubscription.add(s);
// wrap for returning so it also removes it from the 'innerSubscription'
return Subscriptions.create(new Action0() {
@Override
public void call() {
timer.stop();
s.unsubscribe();
innerSubscription.remove(s);
}
});
}
@Override
public Subscription schedule(final Action0 action) {
final BooleanSubscription s = BooleanSubscription.create();
final Runnable runnable = new Runnable() {
@Override
public void run() {
if (innerSubscription.isUnsubscribed() || s.isUnsubscribed()) {
return;
}
action.call();
innerSubscription.remove(s);
}
};
if (SwingUtilities.isEventDispatchThread()){
runnable.run();
} else {
EventQueue.invokeLater(runnable);
}
innerSubscription.add(s);
// wrap for returning so it also removes it from the 'innerSubscription'
return Subscriptions.create(new Action0() {
@Override
public void call() {
s.unsubscribe();
innerSubscription.remove(s);
}
});
}
}
private static void assertThatTheDelayIsValidForTheSwingTimer(long delay) {
if (delay < 0 || delay > Integer.MAX_VALUE) {
throw new IllegalArgumentException(String.format("The swing timer only accepts non-negative delays up to %d milliseconds.", Integer.MAX_VALUE));
}
}
}