Skip to content

Commit 70e541b

Browse files
committed
Implement ActionEvents for Node, ContextMenu, and MenuItem
1 parent eca3fd3 commit 70e541b

File tree

3 files changed

+73
-16
lines changed

3 files changed

+73
-16
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package rx.javafx.sources;
2+
3+
import javafx.event.ActionEvent;
4+
import javafx.event.EventHandler;
5+
import javafx.scene.Node;
6+
import javafx.scene.control.ContextMenu;
7+
import javafx.scene.control.MenuItem;
8+
import rx.Observable;
9+
import rx.schedulers.JavaFxScheduler;
10+
import rx.subscriptions.JavaFxSubscriptions;
11+
12+
public final class ActionEventSource {
13+
private ActionEventSource() {}
14+
15+
public static Observable<ActionEvent> fromActionEvents(final Node node) {
16+
return NodeEventSource.fromNodeEvents(node, ActionEvent.ACTION);
17+
}
18+
public static Observable<ActionEvent> fromActionEvents(final ContextMenu source) {
19+
return Observable.create((Observable.OnSubscribe<ActionEvent>) subscriber -> {
20+
final EventHandler<ActionEvent> handler = subscriber::onNext;
21+
22+
source.addEventHandler(ActionEvent.ANY, handler);
23+
24+
subscriber.add(JavaFxSubscriptions.unsubscribeInEventDispatchThread(() -> source.removeEventHandler(ActionEvent.ANY, handler)));
25+
}).subscribeOn(JavaFxScheduler.getInstance());
26+
}
27+
public static Observable<ActionEvent> fromActionEvents(final MenuItem source) {
28+
return Observable.create((Observable.OnSubscribe<ActionEvent>) subscriber -> {
29+
final EventHandler<ActionEvent> handler = subscriber::onNext;
30+
31+
source.addEventHandler(ActionEvent.ANY, handler);
32+
33+
subscriber.add(JavaFxSubscriptions.unsubscribeInEventDispatchThread(() -> source.removeEventHandler(ActionEvent.ANY, handler)));
34+
}).subscribeOn(JavaFxScheduler.getInstance());
35+
}
36+
37+
}

src/main/java/rx/javafx/sources/NodeEventSource.java

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,11 @@ public static <T extends Event> Observable<T> fromNodeEvents(final Node source,
1919
return Observable.create(new Observable.OnSubscribe<T>() {
2020
@Override
2121
public void call(final Subscriber<? super T> subscriber) {
22-
final EventHandler<T> handler = new EventHandler<T>() {
23-
@Override
24-
public void handle(T t) {
25-
subscriber.onNext(t);
26-
}
27-
};
22+
final EventHandler<T> handler = subscriber::onNext;
2823

2924
source.addEventHandler(eventType, handler);
3025

31-
subscriber.add(JavaFxSubscriptions.unsubscribeInEventDispatchThread(new Action0() {
32-
@Override
33-
public void call() {
34-
source.removeEventHandler(eventType, handler);
35-
}
36-
}));
26+
subscriber.add(JavaFxSubscriptions.unsubscribeInEventDispatchThread(() -> source.removeEventHandler(eventType, handler)));
3727
}
3828

3929
}).subscribeOn(JavaFxScheduler.getInstance());

src/main/java/rx/observables/JavaFxObservable.java

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@
1717

1818

1919
import javafx.beans.value.ObservableValue;
20+
import javafx.event.ActionEvent;
2021
import javafx.event.Event;
2122
import javafx.event.EventType;
2223
import javafx.scene.Node;
2324
import javafx.scene.Scene;
25+
import javafx.scene.control.ContextMenu;
26+
import javafx.scene.control.MenuItem;
2427
import rx.Observable;
25-
import rx.javafx.sources.Change;
26-
import rx.javafx.sources.NodeEventSource;
27-
import rx.javafx.sources.ObservableValueSource;
28-
import rx.javafx.sources.SceneEventSource;
28+
import rx.javafx.sources.*;
2929

3030

3131
public enum JavaFxObservable {
@@ -74,4 +74,34 @@ public static <T> Observable<Change<T>> fromObservableValueChanges(final Observa
7474
public static <T extends Event> Observable<T> fromSceneEvents(final Scene scene, final EventType<T> eventType) {
7575
return SceneEventSource.fromSceneEvents(scene,eventType);
7676
}
77+
78+
/**
79+
* Creates an observable corresponding to javafx Node action events.
80+
*
81+
* @param node The target of the ActionEvents
82+
* @return An Observable of UI ActionEvents
83+
*/
84+
public static Observable<ActionEvent> fromActionEvents(final Node node) {
85+
return ActionEventSource.fromActionEvents(node);
86+
}
87+
88+
/**
89+
* Creates an observable corresponding to javafx ContextMenu action events.
90+
*
91+
* @param contextMenu The target of the ActionEvents
92+
* @return An Observable of UI ActionEvents
93+
*/
94+
public static Observable<ActionEvent> fromActionEvents(final ContextMenu contextMenu) {
95+
return ActionEventSource.fromActionEvents(contextMenu);
96+
}
97+
98+
/**
99+
* Creates an observable corresponding to javafx MenuItem action events.
100+
*
101+
* @param menuItem The target of the ActionEvents
102+
* @return An Observable of UI ActionEvents
103+
*/
104+
public static Observable<ActionEvent> fromActionEvents(final MenuItem menuItem) {
105+
return ActionEventSource.fromActionEvents(menuItem);
106+
}
77107
}

0 commit comments

Comments
 (0)