1
1
package org .codefx .libfx .listener .handle ;
2
2
3
+ import javafx .beans .InvalidationListener ;
4
+ import javafx .beans .Observable ;
5
+ import javafx .beans .value .ChangeListener ;
6
+ import javafx .beans .value .ObservableValue ;
7
+
3
8
/**
4
- * Utility class for functionality surrounding {@link ListenerHandle}s.
9
+ * Factory class for functionality surrounding {@link ListenerHandle}s.
5
10
*/
6
11
public class ListenerHandles {
7
12
@@ -26,8 +31,84 @@ private ListenerHandles() {
26
31
* the listener which will be added to the {@code observable}
27
32
* @return a {@link ListenerHandleBuilder} for a {@code ListenerHandle}.
28
33
*/
29
- public static <O , L > ListenerHandleBuilder <O , L > from (O observable , L listener ) {
34
+ public static <O , L > ListenerHandleBuilder <O , L > buildFor (O observable , L listener ) {
30
35
return ListenerHandleBuilder .from (observable , listener );
31
36
}
32
37
38
+ // Observable + InvalidationListener
39
+
40
+ /**
41
+ * Ands the specified listener to the specified observable and returns a handle for the combination.
42
+ *
43
+ * @param observable
44
+ * the {@link Observable} to which the {@code invalidationListener} will be added
45
+ * @param invalidationListener
46
+ * the {@link InvalidationListener} which will be added to the {@code observable}
47
+ * @return a {@link ListenerHandle} for the specified arguments; the listener is initially attached
48
+ */
49
+ public static ListenerHandle create (Observable observable , InvalidationListener invalidationListener ) {
50
+ ListenerHandle handle = createDetached (observable , invalidationListener );
51
+ handle .attach ();
52
+ return handle ;
53
+ }
54
+
55
+ /**
56
+ * Creates a listener handle for the specified observable and listener. The listener is not yet attached!
57
+ *
58
+ * @param observable
59
+ * the {@link Observable} to which the {@code invalidationListener} will be added
60
+ * @param invalidationListener
61
+ * the {@link InvalidationListener} which will be added to the {@code observableValue}
62
+ * @return a {@link ListenerHandle} for the specified arguments; the listener is initially detached
63
+ */
64
+ public static ListenerHandle createDetached (Observable observable , InvalidationListener invalidationListener ) {
65
+ return ListenerHandleBuilder
66
+ .from (observable , invalidationListener )
67
+ .onAttach ((obs , listener ) -> obs .addListener (listener ))
68
+ .onDetach ((obs , listener ) -> obs .removeListener (listener ))
69
+ .build ();
70
+ }
71
+
72
+ // ObservableValue + ChangeListener
73
+
74
+ /**
75
+ * Ands the specified listener to the specified observable and returns a handle for the combination.
76
+ *
77
+ * @param <T>
78
+ * the type of the value wrapped by the observable
79
+ * @param observableValue
80
+ * the {@link ObservableValue} to which the {@code changeListener} will be added
81
+ * @param changeListener
82
+ * the {@link ChangeListener} which will be added to the {@code observableValue}
83
+ * @return a {@link ListenerHandle} for the specified arguments; the listener is initially attached
84
+ */
85
+ public static <T > ListenerHandle create (
86
+ ObservableValue <T > observableValue , ChangeListener <? super T > changeListener ) {
87
+
88
+ ListenerHandle handle = createDetached (observableValue , changeListener );
89
+ handle .attach ();
90
+ return handle ;
91
+ }
92
+
93
+ /**
94
+ * Creates a listener handle for the specified observable and listener. The listener is not yet attached!
95
+ *
96
+ * @param <T>
97
+ * the type of the value wrapped by the observable
98
+ * @param observableValue
99
+ * the {@link ObservableValue} to which the {@code changeListener} will be added
100
+ * @param changeListener
101
+ * the {@link ChangeListener} which will be added to the {@code observableValue}
102
+ * @return a {@link ListenerHandle} for the specified arguments; the listener is initially detached
103
+ */
104
+ public static <T > ListenerHandle createDetached (
105
+ ObservableValue <T > observableValue , ChangeListener <? super T > changeListener ) {
106
+
107
+ return ListenerHandleBuilder
108
+ .from (observableValue , changeListener )
109
+ .onAttach ((observable , listener ) -> observable .addListener (listener ))
110
+ .onDetach ((observable , listener ) -> observable .removeListener (listener ))
111
+ .build ();
112
+ }
113
+
33
114
}
0 commit comments