diff --git a/javafx-demo1/build.gradle b/javafx-demo1/build.gradle index a53f342..288fa14 100644 --- a/javafx-demo1/build.gradle +++ b/javafx-demo1/build.gradle @@ -30,6 +30,7 @@ dependencies { implementation "org.openjfx:javafx-controls:15.0.1:${platform}" implementation "org.openjfx:javafx-graphics:15.0.1:${platform}" implementation "org.openjfx:javafx-fxml:15.0.1:${platform}" + implementation "com.google.guava:guava:30.1-jre" testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.0' testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine' } diff --git a/javafx-demo1/src/main/java/ch/tpolgrabia/javashowcase/javafxdemo/JavaFxApplication.java b/javafx-demo1/src/main/java/ch/tpolgrabia/javashowcase/javafxdemo/JavaFxApplication.java index 3e67046..c08faa8 100644 --- a/javafx-demo1/src/main/java/ch/tpolgrabia/javashowcase/javafxdemo/JavaFxApplication.java +++ b/javafx-demo1/src/main/java/ch/tpolgrabia/javashowcase/javafxdemo/JavaFxApplication.java @@ -1,19 +1,20 @@ package ch.tpolgrabia.javashowcase.javafxdemo; -import ch.tpolgrabia.javashowcase.javafxdemo.controllers.LoginController; +import ch.tpolgrabia.javashowcase.javafxdemo.models.SwitchToHomeEvent; +import ch.tpolgrabia.javashowcase.javafxdemo.models.SwitchToLoginEvent; +import ch.tpolgrabia.javashowcase.javafxdemo.services.SceneFactory; +import ch.tpolgrabia.javashowcase.javafxdemo.services.SceneFactoryImpl; +import com.google.common.eventbus.EventBus; +import com.google.common.eventbus.Subscribe; import javafx.application.Application; -import javafx.fxml.FXMLLoader; -import javafx.scene.Parent; -import javafx.scene.Scene; import javafx.stage.Stage; -import java.io.IOException; -import java.io.InputStream; - // TODO add event bus like system to switch between views and add messaging +// TODO add guice to provide better factory style di-injection. public class JavaFxApplication extends Application { private Stage stage; - private FXMLLoader loader; + private EventBus eventBus; + private SceneFactory factory; public static void main(String[] args) { launch(args); @@ -22,43 +23,28 @@ public class JavaFxApplication extends Application { @Override public void start(Stage stage) { this.stage = stage; - this.loader = new FXMLLoader(); - - switchToLogin(); + this.eventBus = new EventBus(); + this.eventBus.register(this); + this.factory = new SceneFactoryImpl(eventBus); + eventBus.post(new SwitchToLoginEvent()); } @Override public void stop() { this.stage = null; - this.loader = null; } - private void switchToLogin() { - try (InputStream is = getClass().getClassLoader().getResourceAsStream("login.fxml")) { - var loader = new FXMLLoader(); - Parent root = loader.load(is); - LoginController loginController = loader.getController(); - loginController.setSwitchToHome(this::switchToHome); - var scene = new Scene(root, 640, 480); - stage.setScene(scene); - stage.show(); + @Subscribe + public void switchToLogin(SwitchToLoginEvent event) { + stage.setScene(factory.loadLoginScene()); + stage.show(); - } catch (IOException e) { - e.printStackTrace(); - System.exit(1); - } } - public void switchToHome() { - try (InputStream is = getClass().getClassLoader().getResourceAsStream("home.fxml")) { - Parent root = loader.load(is); - var scene = new Scene(root, 640, 480); - stage.setScene(scene); - stage.show(); + @Subscribe + public void switchToHome(SwitchToHomeEvent event) { + stage.setScene(factory.loadHomeScene()); + stage.show(); - } catch (IOException e) { - e.printStackTrace(); - System.exit(1); - } } } diff --git a/javafx-demo1/src/main/java/ch/tpolgrabia/javashowcase/javafxdemo/controllers/LoginController.java b/javafx-demo1/src/main/java/ch/tpolgrabia/javashowcase/javafxdemo/controllers/LoginController.java index 934efea..ffb89f0 100644 --- a/javafx-demo1/src/main/java/ch/tpolgrabia/javashowcase/javafxdemo/controllers/LoginController.java +++ b/javafx-demo1/src/main/java/ch/tpolgrabia/javashowcase/javafxdemo/controllers/LoginController.java @@ -1,5 +1,7 @@ package ch.tpolgrabia.javashowcase.javafxdemo.controllers; +import ch.tpolgrabia.javashowcase.javafxdemo.models.SwitchToHomeEvent; +import com.google.common.eventbus.EventBus; import javafx.fxml.FXML; import javafx.scene.control.PasswordField; import javafx.scene.control.TextField; @@ -10,16 +12,16 @@ public class LoginController { @FXML private PasswordField password; - private Runnable switchToHome; - - public void setSwitchToHome(Runnable switchToHome) { - this.switchToHome = switchToHome; - } + private EventBus eventBus; @FXML public void handleLogin() { System.out.printf("Got data login: %s, password: %s\n", login.getText(), password.getText()); - switchToHome.run(); + eventBus.post(new SwitchToHomeEvent()); + } + + public void setEventBus(EventBus eventBus) { + this.eventBus = eventBus; } } diff --git a/javafx-demo1/src/main/java/ch/tpolgrabia/javashowcase/javafxdemo/models/SwitchToHomeEvent.java b/javafx-demo1/src/main/java/ch/tpolgrabia/javashowcase/javafxdemo/models/SwitchToHomeEvent.java new file mode 100644 index 0000000..e74206f --- /dev/null +++ b/javafx-demo1/src/main/java/ch/tpolgrabia/javashowcase/javafxdemo/models/SwitchToHomeEvent.java @@ -0,0 +1,4 @@ +package ch.tpolgrabia.javashowcase.javafxdemo.models; + +public class SwitchToHomeEvent { +} diff --git a/javafx-demo1/src/main/java/ch/tpolgrabia/javashowcase/javafxdemo/models/SwitchToLoginEvent.java b/javafx-demo1/src/main/java/ch/tpolgrabia/javashowcase/javafxdemo/models/SwitchToLoginEvent.java new file mode 100644 index 0000000..388a79c --- /dev/null +++ b/javafx-demo1/src/main/java/ch/tpolgrabia/javashowcase/javafxdemo/models/SwitchToLoginEvent.java @@ -0,0 +1,4 @@ +package ch.tpolgrabia.javashowcase.javafxdemo.models; + +public class SwitchToLoginEvent { +} diff --git a/javafx-demo1/src/main/java/ch/tpolgrabia/javashowcase/javafxdemo/services/SceneFactory.java b/javafx-demo1/src/main/java/ch/tpolgrabia/javashowcase/javafxdemo/services/SceneFactory.java new file mode 100644 index 0000000..1f0b59e --- /dev/null +++ b/javafx-demo1/src/main/java/ch/tpolgrabia/javashowcase/javafxdemo/services/SceneFactory.java @@ -0,0 +1,9 @@ +package ch.tpolgrabia.javashowcase.javafxdemo.services; + +import javafx.scene.Scene; + +public interface SceneFactory { + Scene loadHomeScene(); + + Scene loadLoginScene(); +} diff --git a/javafx-demo1/src/main/java/ch/tpolgrabia/javashowcase/javafxdemo/services/SceneFactoryImpl.java b/javafx-demo1/src/main/java/ch/tpolgrabia/javashowcase/javafxdemo/services/SceneFactoryImpl.java new file mode 100644 index 0000000..5c71017 --- /dev/null +++ b/javafx-demo1/src/main/java/ch/tpolgrabia/javashowcase/javafxdemo/services/SceneFactoryImpl.java @@ -0,0 +1,62 @@ +package ch.tpolgrabia.javashowcase.javafxdemo.services; + +import ch.tpolgrabia.javashowcase.javafxdemo.controllers.LoginController; +import com.google.common.eventbus.EventBus; +import javafx.fxml.FXMLLoader; +import javafx.scene.Parent; +import javafx.scene.Scene; + +import java.io.IOException; +import java.io.InputStream; + +public class SceneFactoryImpl implements SceneFactory { + + private Scene homeScene; + private EventBus eventBus; + + public SceneFactoryImpl(EventBus eventBus) { + this.eventBus = eventBus; + } + + @Override + public Scene loadHomeScene() { + if (homeScene != null) { + return homeScene; + } + + try (InputStream is = getClass().getClassLoader().getResourceAsStream("home.fxml")) { + Parent root = new FXMLLoader().load(is); + + if (root == null) { + System.out.println("Couldn't find parent..."); + return null; + } + + homeScene = new Scene(root, 640, 480); + return homeScene; + } catch (IOException e) { + e.printStackTrace(); + return null; + } + } + + @Override + public Scene loadLoginScene() { + FXMLLoader loader = new FXMLLoader(); + try (InputStream is = getClass().getClassLoader().getResourceAsStream("login.fxml")) { + Parent parent = loader.load(is); + + if (parent == null) { + System.out.println("Couldn't load login component..."); + return null; + } + LoginController loginController = loader.getController(); + loginController.setEventBus(eventBus); + Scene scene = new Scene(parent, 640, 480); + return scene; + } catch (IOException e) { + e.printStackTrace(); + return null; + } + } +} diff --git a/javafx-demo1/src/main/java/module-info.java b/javafx-demo1/src/main/java/module-info.java index ba276c6..914d5c4 100644 --- a/javafx-demo1/src/main/java/module-info.java +++ b/javafx-demo1/src/main/java/module-info.java @@ -3,6 +3,7 @@ module ch.tpolgrabia.javashowcase.javafx1demo { requires javafx.controls; requires javafx.graphics; requires javafx.fxml; + requires com.google.common; opens ch.tpolgrabia.javashowcase.javafxdemo to javafx.fxml; opens ch.tpolgrabia.javashowcase.javafxdemo.controllers to javafx.fxml;