16
16
17
17
package rx .subscribers ;
18
18
19
+ import com .sun .javafx .binding .ExpressionHelper ;
20
+ import javafx .beans .InvalidationListener ;
19
21
import javafx .beans .binding .Binding ;
20
- import javafx .beans .value .ObservableValueBase ;
22
+ import javafx .beans .value .ChangeListener ;
23
+ import javafx .beans .value .ObservableValue ;
21
24
import javafx .collections .ObservableList ;
22
- import rx .Observable ;
23
25
import rx .Subscriber ;
26
+ import rx .Subscription ;
24
27
import rx .functions .Action1 ;
25
28
26
- import java .util .Optional ;
27
29
30
+ public final class BindingSubscriber <T > extends Subscriber <T > implements ObservableValue <T >, Binding <T >, Subscription {
28
31
29
- public final class BindingSubscriber <T > extends ObservableValueBase <T > implements Binding <T > {
30
-
31
- private final Subscriber <T > subscriber ;
32
-
32
+ private final Action1 <Throwable > onError ;
33
+ private ExpressionHelper <T > helper ;
33
34
private T value ;
34
35
35
- BindingSubscriber (Observable <T > observable , Optional <T > initialValue , final Action1 <Throwable > onError ) {
36
-
37
- this .subscriber = new Subscriber <T >() {
38
- @ Override
39
- public void onCompleted () {
40
- //do nothing
41
- }
42
-
43
- @ Override
44
- public void onError (Throwable e ) {
45
- onError .call (e );
46
- }
47
-
48
- @ Override
49
- public void onNext (T item ) {
50
- value = item ;
51
- fireValueChangedEvent ();
52
- }
53
- };
36
+ BindingSubscriber (final Action1 <Throwable > onError ) {
37
+ this .onError = onError ;
38
+ }
39
+ @ Override
40
+ public void onCompleted () {
41
+ //do nothing
42
+ }
54
43
55
- observable .subscribe (subscriber );
44
+ @ Override
45
+ public void onError (Throwable e ) {
46
+ onError .call (e );
47
+ }
56
48
49
+ @ Override
50
+ public void onNext (T t ) {
51
+ value = t ;
52
+ fireValueChangedEvent ();
57
53
}
58
54
@ Override
59
55
public T getValue () {
@@ -76,6 +72,49 @@ public ObservableList<?> getDependencies() {
76
72
77
73
@ Override
78
74
public void dispose () {
79
- subscriber .unsubscribe ();
75
+ this .unsubscribe ();
76
+ }
77
+
78
+ /**
79
+ * {@inheritDoc}
80
+ */
81
+ @ Override
82
+ public void addListener (InvalidationListener listener ) {
83
+ helper = ExpressionHelper .addListener (helper , this , listener );
84
+ }
85
+
86
+ /**
87
+ * {@inheritDoc}
88
+ */
89
+ @ Override
90
+ public void addListener (ChangeListener <? super T > listener ) {
91
+ helper = ExpressionHelper .addListener (helper , this , listener );
92
+ }
93
+
94
+ /**
95
+ * {@inheritDoc}
96
+ */
97
+ @ Override
98
+ public void removeListener (InvalidationListener listener ) {
99
+ helper = ExpressionHelper .removeListener (helper , listener );
100
+ }
101
+
102
+ /**
103
+ * {@inheritDoc}
104
+ */
105
+ @ Override
106
+ public void removeListener (ChangeListener <? super T > listener ) {
107
+ helper = ExpressionHelper .removeListener (helper , listener );
108
+ }
109
+
110
+ /**
111
+ * Notify the currently registered observers of a value change.
112
+ *
113
+ * This implementation will ignore all adds and removes of observers that
114
+ * are done while a notification is processed. The changes take effect in
115
+ * the following call to fireValueChangedEvent.
116
+ */
117
+ protected void fireValueChangedEvent () {
118
+ ExpressionHelper .fireValueChangedEvent (helper );
80
119
}
81
120
}
0 commit comments