Skip to content

Commit 1446ea5

Browse files
committed
Implemented set_javafx_event_receiver
1 parent 6f4189e commit 1446ea5

File tree

5 files changed

+48
-3
lines changed

5 files changed

+48
-3
lines changed

java/src/main/java/org/astonbitecode/j4rs/api/invocation/JavaFxInvocation.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,15 @@ public Instance invokeStatic(String methodName, InvocationArg... args) {
6161

6262
@Override
6363
public void invokeAsync(long functionPointerAddress, String methodName, InvocationArg... args) {
64-
Platform.runLater(() -> this.invokeAsync(functionPointerAddress, methodName, args));
64+
Platform.runLater(() -> jsonInvocation.invokeAsync(functionPointerAddress, methodName, args));
6565
}
6666

6767
@Override
6868
public void invokeToChannel(long channelAddress, String methodName, InvocationArg... args) {
69-
Platform.runLater(() -> this.invokeToChannel(channelAddress, methodName, args));
69+
System.out.println("Invoking to channel " + methodName + " with " + args.length + " args");
70+
Platform.runLater(() -> {
71+
jsonInvocation.invokeToChannel(channelAddress, methodName, args);
72+
});
7073
}
7174

7275
@Override
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2020 astonbitecode
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
package org.astonbitecode.j4rs.api.jfx.handlers;
16+
17+
import javafx.event.Event;
18+
import javafx.event.EventHandler;
19+
import org.astonbitecode.j4rs.api.invocation.NativeCallbackToRustChannelSupport;
20+
21+
public class J4rsEventHandler<T extends Event> extends NativeCallbackToRustChannelSupport implements EventHandler<T> {
22+
public J4rsEventHandler() {
23+
System.out.println("------NEW---");
24+
}
25+
26+
@Override
27+
public void handle(T event) {
28+
System.out.println("---------");
29+
doCallback(event);
30+
}
31+
}
Binary file not shown.

rust/src/api.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -923,6 +923,7 @@ impl Jvm {
923923
ChainableInstance::new_with_instance_ref(&instance, &self)
924924
}
925925

926+
/// Initiates a chain of operations on Instances.
926927
pub fn into_chain(&self, instance: Instance) -> ChainableInstance {
927928
ChainableInstance::new(instance, &self)
928929
}

rust/src/jfx.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414
use std::env;
15+
use std::convert::TryFrom;
1516

16-
use crate::{InstanceReceiver, Jvm, MavenArtifact};
17+
use crate::{InstanceReceiver, Jvm, MavenArtifact, Instance, InvocationArg};
1718
use crate::errors;
1819
use crate::errors::J4RsError;
1920

@@ -25,6 +26,8 @@ pub trait JavaFxSupport {
2526
fn start_javafx_app(&self) -> errors::Result<InstanceReceiver>;
2627
/// Deploys the required dependencies to run a JavaFX application in order to be able to be used by j4rs.
2728
fn deploy_javafx_dependencies(&self) -> errors::Result<()>;
29+
30+
fn set_javafx_event_receiver(&self, instance: &Instance, method: &str) -> errors::Result<InstanceReceiver>;
2831
}
2932

3033
impl JavaFxSupport for Jvm {
@@ -41,6 +44,13 @@ impl JavaFxSupport for Jvm {
4144
Ok(cb)
4245
}
4346

47+
fn set_javafx_event_receiver(&self, instance: &Instance, method: &str) -> errors::Result<InstanceReceiver> {
48+
let j4rs_event_handler = self.create_instance("org.astonbitecode.j4rs.api.jfx.handlers.J4rsEventHandler", &[])?;
49+
let btn_action_channel = self.init_callback_channel(&j4rs_event_handler)?;
50+
self.invoke(&instance, method, &[InvocationArg::try_from(j4rs_event_handler)?])?;
51+
Ok(btn_action_channel)
52+
}
53+
4454
/// Deploys the required dependencies to run a JavaFX application in order to be able to be used by j4rs.
4555
fn deploy_javafx_dependencies(&self) -> errors::Result<()> {
4656
let target_os_res = env::var("CARGO_CFG_TARGET_OS");

0 commit comments

Comments
 (0)