Skip to content

Commit

Permalink
Connect VirtualTreeGroup to FxSceneBuilderProcessors
Browse files Browse the repository at this point in the history
  • Loading branch information
kamil-sita committed Jun 14, 2024
1 parent 0bdf4af commit 2e75698
Show file tree
Hide file tree
Showing 13 changed files with 113 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import place.sita.modulefx.FxSceneBuilderProcessors;
import place.sita.modulefx.threading.ThreadingSupportSupplier;
import place.sita.labelle.gui.local.menu.Menu;
import place.sita.modulefx.vtg.VirtualTreeGroup;

import java.util.*;

Expand Down Expand Up @@ -54,10 +55,13 @@ public UnstableSceneReporter configureStage(Stage stage, StageType stageType) {
FxSceneBuilderProcessors processors = new FxSceneBuilderProcessors(childrenFactory, unstableSceneReporter);
UUID loadId = UUID.randomUUID();
unstableSceneReporter.markUnstable(loadId, "Loading new stage");

VirtualTreeGroup virtualTreeGroup = new VirtualTreeGroup();

Menu menu = applicationContext.getBean(Menu.class);
Node node;
try {
node = FxControllerLoader.setupForController(menu, "/fx/mainmenu.fxml", processors);
node = FxControllerLoader.setupForController(menu, "/fx/mainmenu.fxml", processors, virtualTreeGroup);
} finally {
unstableSceneReporter.markStable(loadId);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package place.sita.modulefx;

import javafx.scene.Node;
import place.sita.modulefx.vtg.VirtualTreeGroup;

public class FxControllerLoader {

public static Node setupForController(Object controller, String resource, FxSceneBuilderProcessors processors) {
return internalSetupForController(controller, null, null, resource, processors);
public static Node setupForController(Object controller, String resource, FxSceneBuilderProcessors processors, VirtualTreeGroup virtualTreeGroup) {
return internalSetupForController(controller, null, null, resource, processors, virtualTreeGroup);
}

private static Node internalSetupForController(Object controller, Object parent, Node parentNode, String resource, FxSceneBuilderProcessors fxSceneBuilderProcessors) {

private static Node internalSetupForController(Object controller, Object parent, Node parentNode, String resource, FxSceneBuilderProcessors fxSceneBuilderProcessors, VirtualTreeGroup virtualTreeGroup) {
Node results = FxSceneBuilder.loadNodeForController(controller, resource);

fxSceneBuilderProcessors.runAll(new FxSetupContext() {
Expand Down Expand Up @@ -38,13 +40,18 @@ public FxSceneBuilderProcessors processors() {
}

@Override
public Node setupForController(Object bean, String resource, FxSetupContext context) {
return internalSetupForController(bean, controller, results, resource, context.processors());
public Node setupForController(Object bean, String resource, FxSetupContext context, VirtualTreeGroup virtualTreeGroup) {
return internalSetupForController(bean, controller, results, resource, context.processors(), virtualTreeGroup);
}

@Override
public Node setupForController(Object bean, String resource, FxSetupContext context, Object parent, Node parentNode, VirtualTreeGroup virtualTreeGroup) {
return internalSetupForController(bean, parent, parentNode, resource, context.processors(), virtualTreeGroup);
}

@Override
public Node setupForController(Object bean, String resource, FxSetupContext context, Object parent, Node parentNode) {
return internalSetupForController(bean, parent, parentNode, resource, context.processors());
public VirtualTreeGroup virtualTreeGroup() {
return null;
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public FxSceneBuilderProcessors(ChildrenFactory childrenFactory, UnstableSceneRe
processors.add(new InjectChildrenProcessor(childrenFactory));
processors.add(new InjectTabsProcessor(childrenFactory, unstableSceneReporter));
processors.add(new PostFxInjectProcessor());
processors.add(new MessageBusSupportProcessor());
}

public void add(FxSceneBuilderProcessor processor) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package place.sita.modulefx;

import javafx.scene.Node;
import place.sita.modulefx.vtg.VirtualTreeGroup;

public interface FxSetupContext {

Expand All @@ -15,7 +16,9 @@ public interface FxSetupContext {
FxSceneBuilderProcessors processors();

@Deprecated
Node setupForController(Object bean, String resource, FxSetupContext context);
Node setupForController(Object bean, String resource, FxSetupContext context, VirtualTreeGroup virtualTreeGroup);

Node setupForController(Object bean, String resource, FxSetupContext context, Object parent, Node parentNode);
Node setupForController(Object bean, String resource, FxSetupContext context, Object parent, Node parentNode, VirtualTreeGroup virtualTreeGroup);

VirtualTreeGroup virtualTreeGroup();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package place.sita.modulefx.annotations;

import java.lang.annotation.*;

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface FxMessageListener {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package place.sita.modulefx.annotations;

import java.lang.annotation.*;

/**
* Generic annotation to autowire a ModuleFx utility.
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface ModuleFx {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package place.sita.modulefx.messagebus;

/**
* See {@link place.sita.modulefx.annotations.ModuleFx}.
*/
public interface MessageSender {

void send(Object message);

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import place.sita.modulefx.FxSetupContext;
import place.sita.modulefx.annotations.FxChild;
import place.sita.modulefx.annotations.FxNode;
import place.sita.modulefx.vtg.VirtualTreeGroup;

import java.lang.reflect.Field;
import java.util.Arrays;
Expand Down Expand Up @@ -41,7 +42,9 @@ private void injectChild(Object parentController, Field fieldWithAnnotation, Cla

String resource = classType.getAnnotation(FxNode.class).resourceFile();
Object bean = childrenFactory.create(classType);
Node node = context.setupForController(bean, resource, context, parentController, parentNode);
VirtualTreeGroup virtualTreeGroup = new VirtualTreeGroup();
context.virtualTreeGroup().addChild(virtualTreeGroup);
Node node = context.setupForController(bean, resource, context, parentController, parentNode, virtualTreeGroup);
fieldWithAnnotation.setAccessible(true);
fieldWithAnnotation.set(parentController, bean);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package place.sita.modulefx.processors;

import place.sita.modulefx.FxSceneBuilderProcessor;
import place.sita.modulefx.FxSetupContext;
import place.sita.modulefx.annotations.FxChild;
import place.sita.modulefx.vtg.VirtualTreeGroupElement;

import java.util.Arrays;

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


Object controller = context.controller();

Class<?> controllerClass = controller.getClass();
Arrays.stream(controllerClass.getDeclaredFields())
.filter(field -> field.isAnnotationPresent(FxChild.class))
.forEach(field -> {
injectChild(controller, field, controllerClass, context);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

import javafx.scene.control.Tab;
import place.sita.modulefx.UnstableSceneReporter;
import place.sita.modulefx.vtg.VirtualTreeGroup;

public interface FxSmartTab {

Tab tab();

void load(UnstableSceneReporter unstableSceneReporter);
VirtualTreeGroup load(UnstableSceneReporter unstableSceneReporter);

void unload();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
import javafx.scene.control.Tab;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Region;
import place.sita.modulefx.BadApiUsageException;
import place.sita.modulefx.ChildrenFactory;
import place.sita.modulefx.FxSetupContext;
import place.sita.modulefx.UnstableSceneReporter;
import place.sita.modulefx.annotations.FxTab;
import place.sita.modulefx.vtg.VirtualTreeGroup;

import java.util.UUID;

Expand Down Expand Up @@ -44,16 +46,18 @@ public Tab tab() {
}

@Override
public void load(UnstableSceneReporter unstableSceneReporter) {
public VirtualTreeGroup load(UnstableSceneReporter unstableSceneReporter) {
if (loaded) {
return;
throw new BadApiUsageException("Cannot load a tab that is already loaded");
}

VirtualTreeGroup virtualTreeGroup = new VirtualTreeGroup();

UUID loadId = UUID.randomUUID();
unstableSceneReporter.markUnstable(loadId, "Loading tab: " + tabClass.getName());

Object bean = factory.create(tabClass);
Node component = setupContext.setupForController(bean, fxTab.resourceFile(), setupContext);
Node component = setupContext.setupForController(bean, fxTab.resourceFile(), setupContext, virtualTreeGroup);

AnchorPane anchorPane = new AnchorPane();
if (component instanceof Region region) {
Expand All @@ -70,6 +74,7 @@ public void load(UnstableSceneReporter unstableSceneReporter) {
unstableSceneReporter.markStable(loadId);
});
loaded = true;
return virtualTreeGroup;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,22 @@
import javafx.scene.control.TabPane;
import place.sita.modulefx.UnstableSceneReporter;
import place.sita.modulefx.threading.Threading;
import place.sita.modulefx.vtg.VirtualTreeGroup;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

public class FxSmartTabManager {

private final Map<String, FxSmartTab> tabs = new LinkedHashMap<>();
private final Map<String, VirtualTreeGroup> virtualTreeGroups = new HashMap<>();

private final VirtualTreeGroup parentVtg;
private final UnstableSceneReporter unstableSceneReporter;

public FxSmartTabManager(UnstableSceneReporter unstableSceneReporter) {
public FxSmartTabManager(VirtualTreeGroup parentVtg, UnstableSceneReporter unstableSceneReporter) {
this.parentVtg = parentVtg;
this.unstableSceneReporter = unstableSceneReporter;
}

Expand All @@ -35,16 +40,23 @@ public void register(TabPane tabPane) {
if (oldValue != null) {
FxSmartTab oldTab = tabs.get(oldValue.getId());
oldTab.unload();
VirtualTreeGroup group = virtualTreeGroups.get(oldValue.getId());
parentVtg.removeChild(group.id());
}
if (newValue != null) {
FxSmartTab newTab = tabs.get(newValue.getId());
newTab.load(unstableSceneReporter);
VirtualTreeGroup group = newTab.load(unstableSceneReporter);
virtualTreeGroups.put(newValue.getId(), group);
parentVtg.addChild(group);
}
});
});

Tab firstTab = tabPane.getTabs().get(0);
FxSmartTab firstFxTab = tabs.get(firstTab.getId());
firstFxTab.load(unstableSceneReporter);
String firstTabId = firstTab.getId();
FxSmartTab firstFxTab = tabs.get(firstTabId);
VirtualTreeGroup group = firstFxTab.load(unstableSceneReporter);
virtualTreeGroups.put(firstTabId, group);
parentVtg.addChild(group);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ private void injectTabs(Object controller, Field field, FxSetupContext context)
List<TabInfo> tabInfos = fetchTabInfoOrder(classes);

TabPane tabPane = getTabPane(controller, field);
FxSmartTabManager manager = new FxSmartTabManager(unstableSceneReporter);
FxSmartTabManager manager = new FxSmartTabManager(context.virtualTreeGroup(), unstableSceneReporter);

for (TabInfo tabInfo : tabInfos) {
FxTab fxTab = tabInfo.clazz.getAnnotation(FxTab.class);
Expand Down

0 comments on commit 2e75698

Please sign in to comment.