Refactored scene switching with guava eventbus.

master
Tomasz Półgrabia 2021-01-17 13:40:54 +01:00
parent 213820ec4b
commit 170e35409e
8 changed files with 110 additions and 41 deletions

View File

@ -30,6 +30,7 @@ dependencies {
implementation "org.openjfx:javafx-controls:15.0.1:${platform}" implementation "org.openjfx:javafx-controls:15.0.1:${platform}"
implementation "org.openjfx:javafx-graphics:15.0.1:${platform}" implementation "org.openjfx:javafx-graphics:15.0.1:${platform}"
implementation "org.openjfx:javafx-fxml: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' testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine' testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
} }

View File

@ -1,19 +1,20 @@
package ch.tpolgrabia.javashowcase.javafxdemo; 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.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage; 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 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 { public class JavaFxApplication extends Application {
private Stage stage; private Stage stage;
private FXMLLoader loader; private EventBus eventBus;
private SceneFactory factory;
public static void main(String[] args) { public static void main(String[] args) {
launch(args); launch(args);
@ -22,43 +23,28 @@ public class JavaFxApplication extends Application {
@Override @Override
public void start(Stage stage) { public void start(Stage stage) {
this.stage = stage; this.stage = stage;
this.loader = new FXMLLoader(); this.eventBus = new EventBus();
this.eventBus.register(this);
switchToLogin(); this.factory = new SceneFactoryImpl(eventBus);
eventBus.post(new SwitchToLoginEvent());
} }
@Override @Override
public void stop() { public void stop() {
this.stage = null; this.stage = null;
this.loader = null;
} }
private void switchToLogin() { @Subscribe
try (InputStream is = getClass().getClassLoader().getResourceAsStream("login.fxml")) { public void switchToLogin(SwitchToLoginEvent event) {
var loader = new FXMLLoader(); stage.setScene(factory.loadLoginScene());
Parent root = loader.load(is); stage.show();
LoginController loginController = loader.getController();
loginController.setSwitchToHome(this::switchToHome);
var scene = new Scene(root, 640, 480);
stage.setScene(scene);
stage.show();
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
} }
public void switchToHome() { @Subscribe
try (InputStream is = getClass().getClassLoader().getResourceAsStream("home.fxml")) { public void switchToHome(SwitchToHomeEvent event) {
Parent root = loader.load(is); stage.setScene(factory.loadHomeScene());
var scene = new Scene(root, 640, 480); stage.show();
stage.setScene(scene);
stage.show();
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
} }
} }

View File

@ -1,5 +1,7 @@
package ch.tpolgrabia.javashowcase.javafxdemo.controllers; 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.fxml.FXML;
import javafx.scene.control.PasswordField; import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField; import javafx.scene.control.TextField;
@ -10,16 +12,16 @@ public class LoginController {
@FXML @FXML
private PasswordField password; private PasswordField password;
private Runnable switchToHome; private EventBus eventBus;
public void setSwitchToHome(Runnable switchToHome) {
this.switchToHome = switchToHome;
}
@FXML @FXML
public void handleLogin() { public void handleLogin() {
System.out.printf("Got data login: %s, password: %s\n", System.out.printf("Got data login: %s, password: %s\n",
login.getText(), password.getText()); login.getText(), password.getText());
switchToHome.run(); eventBus.post(new SwitchToHomeEvent());
}
public void setEventBus(EventBus eventBus) {
this.eventBus = eventBus;
} }
} }

View File

@ -0,0 +1,4 @@
package ch.tpolgrabia.javashowcase.javafxdemo.models;
public class SwitchToHomeEvent {
}

View File

@ -0,0 +1,4 @@
package ch.tpolgrabia.javashowcase.javafxdemo.models;
public class SwitchToLoginEvent {
}

View File

@ -0,0 +1,9 @@
package ch.tpolgrabia.javashowcase.javafxdemo.services;
import javafx.scene.Scene;
public interface SceneFactory {
Scene loadHomeScene();
Scene loadLoginScene();
}

View File

@ -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;
}
}
}

View File

@ -3,6 +3,7 @@ module ch.tpolgrabia.javashowcase.javafx1demo {
requires javafx.controls; requires javafx.controls;
requires javafx.graphics; requires javafx.graphics;
requires javafx.fxml; requires javafx.fxml;
requires com.google.common;
opens ch.tpolgrabia.javashowcase.javafxdemo to javafx.fxml; opens ch.tpolgrabia.javashowcase.javafxdemo to javafx.fxml;
opens ch.tpolgrabia.javashowcase.javafxdemo.controllers to javafx.fxml; opens ch.tpolgrabia.javashowcase.javafxdemo.controllers to javafx.fxml;