From e3e6b35814656073338aa16ff02c9ee6968aaf66 Mon Sep 17 00:00:00 2001 From: Salathiel Date: Sun, 18 Aug 2024 16:56:13 +0200 Subject: [PATCH] fix: API & Desktop spring boot autoconf --- jsgenerator-api/pom.xml | 4 + jsgenerator-desktop/pom.xml | 170 ++---------------- .../jsgenerator/desktop/HelloApplication.java | 23 --- .../jsgenerator/desktop/HelloController.java | 14 -- .../autoconfigure/JsGeneratorDesktop.java | 62 +++++++ .../desktop/controller/FxmlNavigator.java | 8 + .../desktop/controller/FxmlResolver.java | 10 ++ .../controller/HelloViewController.java | 34 ++++ .../src/main/java/module-info.java | 23 +-- .../desktop/controller/hello-view.fxml | 18 ++ .../jsgenerator/desktop/hello-view.fxml | 16 -- .../jsgenerator/api/JsGeneratorApi.java | 9 +- pom.xml | 6 + 13 files changed, 178 insertions(+), 219 deletions(-) delete mode 100644 jsgenerator-desktop/src/main/java/com/osscameroon/jsgenerator/desktop/HelloApplication.java delete mode 100644 jsgenerator-desktop/src/main/java/com/osscameroon/jsgenerator/desktop/HelloController.java create mode 100644 jsgenerator-desktop/src/main/java/com/osscameroon/jsgenerator/desktop/autoconfigure/JsGeneratorDesktop.java create mode 100644 jsgenerator-desktop/src/main/java/com/osscameroon/jsgenerator/desktop/controller/FxmlNavigator.java create mode 100644 jsgenerator-desktop/src/main/java/com/osscameroon/jsgenerator/desktop/controller/FxmlResolver.java create mode 100644 jsgenerator-desktop/src/main/java/com/osscameroon/jsgenerator/desktop/controller/HelloViewController.java create mode 100644 jsgenerator-desktop/src/main/resources/com/osscameroon/jsgenerator/desktop/controller/hello-view.fxml delete mode 100644 jsgenerator-desktop/src/main/resources/com/osscameroon/jsgenerator/desktop/hello-view.fxml diff --git a/jsgenerator-api/pom.xml b/jsgenerator-api/pom.xml index b75b2287..22cb263d 100644 --- a/jsgenerator-api/pom.xml +++ b/jsgenerator-api/pom.xml @@ -17,6 +17,10 @@ + + org.graalvm.buildtools + native-maven-plugin + org.springframework.boot spring-boot-maven-plugin diff --git a/jsgenerator-desktop/pom.xml b/jsgenerator-desktop/pom.xml index 0c7d9e6e..b7cb3f75 100644 --- a/jsgenerator-desktop/pom.xml +++ b/jsgenerator-desktop/pom.xml @@ -9,179 +9,45 @@ ${revision} - 4.0.0 - jsgenerator-desktop - jsgenerator-desktop - 17.0.2 - 0.0.8 - com.osscameroon.jsgenerator.desktop.HelloApplication + com.osscameroon.jsgenerator.desktop.autoconfigure.JsGeneratorDesktop - - - - org.springframework.boot - spring-boot-starter - - com.osscameroon jsgenerator-core - - - - org.openjfx - javafx-controls - ${javafx.version} - org.openjfx javafx-fxml - ${javafx.version} - - - org.openjfx - javafx-web - ${javafx.version} - - org.controlsfx - controlsfx - 11.1.1 - - - com.dlsc.formsfx - formsfx-core - 11.5.0 - - - org.openjfx - * - - - - - net.synedra - validatorfx - 0.4.0 - - - org.openjfx - * - - - - - org.kordamp.ikonli - ikonli-javafx - 12.3.1 - - - org.kordamp.bootstrapfx - bootstrapfx-core - 0.4.0 - - - eu.hansolo - tilesfx - 17.1.9 - - - org.openjfx - * - - - - - - - - eu.hansolo.fx - countries - 17.0.22 - - - - - - - - eu.hansolo.fx - heatmap - 17.0.9 - - - - - - - eu.hansolo - toolboxfx - 17.0.28 - - - - - - - - eu.hansolo - toolbox - 17.0.22 - - - - - - - - org.openjfx - javafx-swing - 20-ea+4 - - - - - org.openjfx - javafx-maven-plugin - ${javafx.plugin.version} - - - - default-cli - - ${mainClass} - app - app - app - true - true - true - - - + org.graalvm.buildtools + native-maven-plugin + + + org.springframework.boot + spring-boot-maven-plugin + + false + + + + org.apache.maven.plugins + maven-compiler-plugin + + 22 + 22 + diff --git a/jsgenerator-desktop/src/main/java/com/osscameroon/jsgenerator/desktop/HelloApplication.java b/jsgenerator-desktop/src/main/java/com/osscameroon/jsgenerator/desktop/HelloApplication.java deleted file mode 100644 index 95ea741e..00000000 --- a/jsgenerator-desktop/src/main/java/com/osscameroon/jsgenerator/desktop/HelloApplication.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.osscameroon.jsgenerator.desktop; - -import javafx.application.Application; -import javafx.fxml.FXMLLoader; -import javafx.scene.Scene; -import javafx.stage.Stage; - -import java.io.IOException; - -public class HelloApplication extends Application { - @Override - public void start(Stage stage) throws IOException { - FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml")); - Scene scene = new Scene(fxmlLoader.load(), 320, 240); - stage.setTitle("Hello!"); - stage.setScene(scene); - stage.show(); - } - - public static void main(String[] args) { - launch(); - } -} \ No newline at end of file diff --git a/jsgenerator-desktop/src/main/java/com/osscameroon/jsgenerator/desktop/HelloController.java b/jsgenerator-desktop/src/main/java/com/osscameroon/jsgenerator/desktop/HelloController.java deleted file mode 100644 index 7e3cfb26..00000000 --- a/jsgenerator-desktop/src/main/java/com/osscameroon/jsgenerator/desktop/HelloController.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.osscameroon.jsgenerator.desktop; - -import javafx.fxml.FXML; -import javafx.scene.control.Label; - -public class HelloController { - @FXML - private Label welcomeText; - - @FXML - protected void onHelloButtonClick() { - welcomeText.setText("Welcome to JavaFX Application!"); - } -} \ No newline at end of file diff --git a/jsgenerator-desktop/src/main/java/com/osscameroon/jsgenerator/desktop/autoconfigure/JsGeneratorDesktop.java b/jsgenerator-desktop/src/main/java/com/osscameroon/jsgenerator/desktop/autoconfigure/JsGeneratorDesktop.java new file mode 100644 index 00000000..8af9795d --- /dev/null +++ b/jsgenerator-desktop/src/main/java/com/osscameroon/jsgenerator/desktop/autoconfigure/JsGeneratorDesktop.java @@ -0,0 +1,62 @@ +package com.osscameroon.jsgenerator.desktop.autoconfigure; + +import com.osscameroon.jsgenerator.core.autoconfigure.JsGeneratorCoreAutoconfigure; +import com.osscameroon.jsgenerator.desktop.controller.FxmlNavigator; +import com.osscameroon.jsgenerator.desktop.controller.FxmlResolver; +import com.osscameroon.jsgenerator.desktop.controller.HelloViewController; +import javafx.application.Application; +import javafx.fxml.FXMLLoader; +import javafx.scene.Parent; +import javafx.scene.Scene; +import javafx.stage.Stage; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.ImportAutoConfiguration; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Lazy; +import org.springframework.core.io.ClassPathResource; + +import java.io.IOException; + +@ImportAutoConfiguration(JsGeneratorCoreAutoconfigure.class) +@SpringBootApplication(scanBasePackageClasses = HelloViewController.class) +public class JsGeneratorDesktop extends Application { + private static ApplicationContext context; + private static FxmlResolver fxmlResolver; + private static Scene scene; + + public static void main(String[] args) { + context = SpringApplication.run(JsGeneratorDesktop.class, args); + fxmlResolver = context.getBean(FxmlResolver.class); + launch(JsGeneratorDesktop.class, args); + } + + /** + * Inject this bean to navigate from one view to another, like a router. + * + * @return + */ + @Bean + @Lazy + public FxmlNavigator fxmlNavigator() { + return scene::setRoot; + } + + @Bean + public FxmlResolver fxmlResolver() { + return path -> { + path = "com/osscameroon/jsgenerator/desktop/controller/%s.fxml".formatted(path); + final var loader = new FXMLLoader(new ClassPathResource(path).getURL()); + loader.setControllerFactory(context::getBean); + return (Parent) loader.load(); + }; + } + + @Override + public void start(Stage stage) throws IOException { + final var parent = fxmlResolver.resolve("hello-view"); + stage.setScene(scene = new Scene(parent)); + stage.show(); + } +} diff --git a/jsgenerator-desktop/src/main/java/com/osscameroon/jsgenerator/desktop/controller/FxmlNavigator.java b/jsgenerator-desktop/src/main/java/com/osscameroon/jsgenerator/desktop/controller/FxmlNavigator.java new file mode 100644 index 00000000..67d0487e --- /dev/null +++ b/jsgenerator-desktop/src/main/java/com/osscameroon/jsgenerator/desktop/controller/FxmlNavigator.java @@ -0,0 +1,8 @@ +package com.osscameroon.jsgenerator.desktop.controller; + +import javafx.scene.Parent; + +@FunctionalInterface +public interface FxmlNavigator { + void navigate(Parent parent); +} diff --git a/jsgenerator-desktop/src/main/java/com/osscameroon/jsgenerator/desktop/controller/FxmlResolver.java b/jsgenerator-desktop/src/main/java/com/osscameroon/jsgenerator/desktop/controller/FxmlResolver.java new file mode 100644 index 00000000..b74cf34e --- /dev/null +++ b/jsgenerator-desktop/src/main/java/com/osscameroon/jsgenerator/desktop/controller/FxmlResolver.java @@ -0,0 +1,10 @@ +package com.osscameroon.jsgenerator.desktop.controller; + +import javafx.scene.Parent; + +import java.io.IOException; + +@FunctionalInterface +public interface FxmlResolver { + Parent resolve(String relativePathWithoutExtension) throws IOException; +} diff --git a/jsgenerator-desktop/src/main/java/com/osscameroon/jsgenerator/desktop/controller/HelloViewController.java b/jsgenerator-desktop/src/main/java/com/osscameroon/jsgenerator/desktop/controller/HelloViewController.java new file mode 100644 index 00000000..aa18df1f --- /dev/null +++ b/jsgenerator-desktop/src/main/java/com/osscameroon/jsgenerator/desktop/controller/HelloViewController.java @@ -0,0 +1,34 @@ +package com.osscameroon.jsgenerator.desktop.controller; + +import com.osscameroon.jsgenerator.core.Converter; +import javafx.fxml.FXML; +import javafx.scene.control.Label; +import javafx.scene.control.TextArea; +import org.springframework.stereotype.Component; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.nio.charset.StandardCharsets; + +@Component +public final class HelloViewController { + private final Converter converter; + + @FXML + private TextArea inputArea; + @FXML + private Label outputLabel; + + public HelloViewController(Converter converter) { + this.converter = converter; + } + + @FXML + private void convert() throws IOException { + try (var stream = new ByteArrayOutputStream()) { + converter.convert(new ByteArrayInputStream(inputArea.textProperty().getValue().getBytes()), stream); + outputLabel.setText(stream.toString(StandardCharsets.UTF_8)); + } + } +} diff --git a/jsgenerator-desktop/src/main/java/module-info.java b/jsgenerator-desktop/src/main/java/module-info.java index 8224e317..eeebdaca 100644 --- a/jsgenerator-desktop/src/main/java/module-info.java +++ b/jsgenerator-desktop/src/main/java/module-info.java @@ -1,17 +1,18 @@ module com.osscameroon.jsgenerator.desktop { + exports com.osscameroon.jsgenerator.desktop.autoconfigure; + exports com.osscameroon.jsgenerator.desktop.controller; + + opens com.osscameroon.jsgenerator.desktop.controller to javafx.fxml, spring.beans; + opens com.osscameroon.jsgenerator.desktop.autoconfigure to javafx.fxml, spring.beans; requires com.osscameroon.jsgenerator.core; - requires javafx.controls; - requires javafx.fxml; - requires javafx.web; - requires org.controlsfx.controls; - requires com.dlsc.formsfx; - requires net.synedra.validatorfx; - requires org.kordamp.ikonli.javafx; - requires org.kordamp.bootstrapfx.core; - requires eu.hansolo.tilesfx; + requires spring.boot.autoconfigure; + requires spring.boot; + requires spring.context; - opens com.osscameroon.jsgenerator.desktop to javafx.fxml; - exports com.osscameroon.jsgenerator.desktop; + requires javafx.graphics; + requires javafx.controls; + requires javafx.fxml; + requires spring.core; } diff --git a/jsgenerator-desktop/src/main/resources/com/osscameroon/jsgenerator/desktop/controller/hello-view.fxml b/jsgenerator-desktop/src/main/resources/com/osscameroon/jsgenerator/desktop/controller/hello-view.fxml new file mode 100644 index 00000000..ce6799d1 --- /dev/null +++ b/jsgenerator-desktop/src/main/resources/com/osscameroon/jsgenerator/desktop/controller/hello-view.fxml @@ -0,0 +1,18 @@ + + + + + + + + + +