Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate reflective clearChildren if component does not extend Pane #114

Merged
merged 1 commit into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"/>