Skip to content

Commit

Permalink
Add support for messaging within stage
Browse files Browse the repository at this point in the history
  • Loading branch information
kamil-sita committed Jun 17, 2024
1 parent 2e75698 commit 349e304
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public Node setupForController(Object bean, String resource, FxSetupContext cont

@Override
public VirtualTreeGroup virtualTreeGroup() {
return null;
return virtualTreeGroup;
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,66 @@

import place.sita.modulefx.FxSceneBuilderProcessor;
import place.sita.modulefx.FxSetupContext;
import place.sita.modulefx.annotations.FxChild;
import place.sita.modulefx.annotations.FxMessageListener;
import place.sita.modulefx.annotations.ModuleFx;
import place.sita.modulefx.messagebus.MessageSender;
import place.sita.modulefx.vtg.MessageListener;
import place.sita.modulefx.vtg.VirtualTreeGroupElement;

import java.util.Arrays;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;

public class MessageBusSupportProcessor implements FxSceneBuilderProcessor {
@Override
public void process(FxSetupContext context) {
VirtualTreeGroupElement element = new VirtualTreeGroupElement();
boolean addedToAnyVirtualTreeGroup = false;
AtomicBoolean addedToAnyVirtualTreeGroup = new AtomicBoolean(false);


Object controller = context.controller();

Class<?> controllerClass = controller.getClass();
Arrays.stream(controllerClass.getDeclaredFields())
.filter(field -> field.isAnnotationPresent(FxChild.class))
.filter(field -> field.isAnnotationPresent(ModuleFx.class))
// instance of MessageSender?
.filter(field -> field.getType().isAssignableFrom(MessageSender.class))
.forEach(field -> {
injectChild(controller, field, controllerClass, context);
addedToAnyVirtualTreeGroup.set(true);
UUID elementId = element.getId();
MessageSender messageSender = new MessageSender() {
@Override
public void send(Object message) {
context.virtualTreeGroup().message(elementId, message);
}
};
field.setAccessible(true);
try {
field.set(controller, messageSender);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
});

Arrays.stream(controllerClass.getMethods())
.filter(method -> method.isAnnotationPresent(FxMessageListener.class))
.forEach(method -> {
addedToAnyVirtualTreeGroup.set(true);
element.addListener(new MessageListener() {
@Override
public void receive(Object message) {
try {
method.invoke(controller, message);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
});
});


if (addedToAnyVirtualTreeGroup.get()) {
context.virtualTreeGroup().addElement(element);
}
}
}

0 comments on commit 349e304

Please sign in to comment.