forked from ReactiveX/RxJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSwingObservable.java
More file actions
143 lines (130 loc) · 4.95 KB
/
SwingObservable.java
File metadata and controls
143 lines (130 loc) · 4.95 KB
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
/**
* Copyright 2013 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.observables;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ComponentEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.util.Set;
import javax.swing.AbstractButton;
import rx.Observable;
import rx.swing.sources.AbstractButtonSource;
import rx.swing.sources.ComponentEventSource;
import rx.swing.sources.KeyEventSource;
import rx.swing.sources.MouseEventSource;
import rx.util.functions.Func1;
/**
* Allows creating observables from various sources specific to Swing.
*/
public enum SwingObservable { ; // no instances
/**
* Creates an observable corresponding to a Swing button action.
*
* @param button
* The button to register the observable for.
* @return Observable of action events.
*/
public static Observable<ActionEvent> fromButtonAction(AbstractButton button) {
return AbstractButtonSource.fromActionOf(button);
}
/**
* Creates an observable corresponding to raw key events.
*
* @param component
* The component to register the observable for.
* @return Observable of key events.
*/
public static Observable<KeyEvent> fromKeyEvents(Component component) {
return KeyEventSource.fromKeyEventsOf(component);
}
/**
* Creates an observable corresponding to raw key events, restricted a set of given key codes.
*
* @param component
* The component to register the observable for.
* @return Observable of key events.
*/
public static Observable<KeyEvent> fromKeyEvents(Component component, final Set<Integer> keyCodes) {
return fromKeyEvents(component).filter(new Func1<KeyEvent, Boolean>() {
@Override
public Boolean call(KeyEvent event) {
return keyCodes.contains(event.getKeyCode());
}
});
}
/**
* Creates an observable that emits the set of all currently pressed keys each time
* this set changes.
* @param component
* The component to register the observable for.
* @return Observable of currently pressed keys.
*/
public static Observable<Set<Integer>> fromPressedKeys(Component component) {
return KeyEventSource.currentlyPressedKeysOf(component);
}
/**
* Creates an observable corresponding to raw mouse events (excluding mouse motion events).
*
* @param component
* The component to register the observable for.
* @return Observable of mouse events.
*/
public static Observable<MouseEvent> fromMouseEvents(Component component) {
return MouseEventSource.fromMouseEventsOf(component);
}
/**
* Creates an observable corresponding to raw mouse motion events.
*
* @param component
* The component to register the observable for.
* @return Observable of mouse motion events.
*/
public static Observable<MouseEvent> fromMouseMotionEvents(Component component) {
return MouseEventSource.fromMouseMotionEventsOf(component);
}
/**
* Creates an observable corresponding to relative mouse motion.
* @param component
* The component to register the observable for.
* @return A point whose x and y coordinate represent the relative horizontal and vertical mouse motion.
*/
public static Observable<Point> fromRelativeMouseMotion(Component component) {
return MouseEventSource.fromRelativeMouseMotion(component);
}
/**
* Creates an observable corresponding to raw component events.
*
* @param component
* The component to register the observable for.
* @return Observable of component events.
*/
public static Observable<ComponentEvent> fromComponentEvents(Component component) {
return ComponentEventSource.fromComponentEventsOf(component);
}
/**
* Creates an observable corresponding to component resize events.
*
* @param component
* The component to register the observable for.
* @return Observable emitting the current size of the given component after each resize event.
*/
public static Observable<Dimension> fromResizing(Component component) {
return ComponentEventSource.fromResizing(component);
}
}