Skip to content

Commit

Permalink
fix(framework): Generate reflective clearChildren if component does n…
Browse files Browse the repository at this point in the history
…ot extend Pane

Closes #113
  • Loading branch information
Clashsoft committed Jul 1, 2024
1 parent 0b9a964 commit 5aa799b
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ public class FxClassGenerator {
* The `javafx.scene.Parent` type.
*/
private final TypeMirror parent;
/**
* The `javafx.scene.layout.Pane` type.
*/
private final TypeMirror pane;

public FxClassGenerator(ProcessingHelper helper, ProcessingEnvironment processingEnv) {
this.helper = helper;
Expand All @@ -61,6 +65,7 @@ public FxClassGenerator(ProcessingHelper helper, ProcessingEnvironment processin
.orElseThrow();

parent = processingEnv.getElementUtils().getTypeElement("javafx.scene.Parent").asType();
pane = processingEnv.getElementUtils().getTypeElement("javafx.scene.layout.Pane").asType();
}

public void generateSidecar(TypeElement componentClass) {
Expand Down Expand Up @@ -303,9 +308,7 @@ private void generateRenderResult(PrintWriter out, TypeElement componentClass) {
if (view.isEmpty()) {
out.println(" final Node result = instance;");
} else {
if (processingEnv.getTypeUtils().isAssignable(componentClass.asType(), parent)) {
out.println(" instance.getChildren().clear();");
}
generateClearChildren(out, componentClass);
out.printf(" final Node result = this.controllerManager.loadFXML(%s, instance, true);%n", helper.stringLiteral(view));
}
} else if (controller != null) {
Expand All @@ -319,6 +322,14 @@ private void generateRenderResult(PrintWriter out, TypeElement componentClass) {
}
}

private void generateClearChildren(PrintWriter out, TypeElement componentClass) {
if (processingEnv.getTypeUtils().isAssignable(componentClass.asType(), pane)) {
out.println(" instance.getChildren().clear();");
} else if (processingEnv.getTypeUtils().isAssignable(componentClass.asType(), parent)) {
out.println(" org.fulib.fx.util.ReflectionUtil.getChildrenList(instance.getClass(), instance).clear();");
}
}

private void generateCallRenderMethods(PrintWriter out, TypeElement componentClass) {
helper.streamAllMethods(componentClass, OnRender.class)
.sorted(Comparator.comparingInt(a -> a.getAnnotation(OnRender.class).value()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import javafx.scene.Parent;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.Pane;
import org.fulib.fx.FulibFxApp;
import org.fulib.fx.annotation.controller.*;
import org.fulib.fx.annotation.event.OnDestroy;
Expand Down Expand Up @@ -370,10 +371,7 @@ private Node renderNode(T instance, boolean component) {
node = (Node) instance;
} else {
Node root = (Node) instance;
// Due to the way JavaFX works, we have to clear the children list of the old root before loading its fxml file again
if (root instanceof Parent parent) {
ReflectionUtil.getChildrenList(instance.getClass(), parent).clear();
}
clearChildren(root);
node = controllerManager.loadFXML(view, instance, true);
}
}
Expand Down Expand Up @@ -406,6 +404,15 @@ else if (view.startsWith("#")) {
return node;
}

private void clearChildren(Node node) {
// Due to the way JavaFX works, we have to clear the children list of the old root before loading its fxml file again
if (node instanceof Pane pane) {
pane.getChildren().clear();
} else if (node instanceof Parent parent) {
ReflectionUtil.getChildrenList(node.getClass(), parent).clear();
}
}

/**
* Registers all key events for the given controller instance.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.fulib.fx.app.controller.types;

import javafx.scene.control.ListView;
import org.fulib.fx.annotation.controller.Component;

// https://github.com/fujaba/fulibFx/issues/113
@Component(view = "ListView.fxml")
public class ListViewComponent<T> extends ListView<T> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>


<?import javafx.scene.control.ListView?>
<fx:root type="ListView" prefHeight="200.0" prefWidth="200.0" xmlns="http://javafx.com/javafx/22"
xmlns:fx="http://javafx.com/fxml/1"/>

0 comments on commit 5aa799b

Please sign in to comment.