Reuse the same scene in SceneContext by switching root nodes
This commit is contained in:
parent
4d4865570d
commit
e3052a2133
@ -4,7 +4,6 @@ import java.io.IOException;
|
|||||||
import java.util.Stack;
|
import java.util.Stack;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
|
|
||||||
import javafx.application.Platform;
|
|
||||||
import javafx.fxml.FXMLLoader;
|
import javafx.fxml.FXMLLoader;
|
||||||
import javafx.scene.*;
|
import javafx.scene.*;
|
||||||
import javafx.stage.Stage;
|
import javafx.stage.Stage;
|
||||||
@ -29,11 +28,10 @@ import envoy.client.event.*;
|
|||||||
public final class SceneContext implements EventListener {
|
public final class SceneContext implements EventListener {
|
||||||
|
|
||||||
private final Stage stage;
|
private final Stage stage;
|
||||||
private final FXMLLoader loader = new FXMLLoader();
|
private final Stack<Parent> roots = new Stack<>();
|
||||||
private final Stack<Scene> sceneStack = new Stack<>();
|
private final Stack<Object> controllers = new Stack<>();
|
||||||
private final Stack<Object> controllerStack = new Stack<>();
|
|
||||||
|
|
||||||
private static final Settings settings = Settings.getInstance();
|
private Scene scene;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes the scene context.
|
* Initializes the scene context.
|
||||||
@ -49,41 +47,47 @@ public final class SceneContext implements EventListener {
|
|||||||
/**
|
/**
|
||||||
* Loads a new scene specified by a scene info.
|
* Loads a new scene specified by a scene info.
|
||||||
*
|
*
|
||||||
* @param sceneInfo specifies the scene to load
|
* @param info specifies the scene to load
|
||||||
* @throws RuntimeException if the loading process fails
|
* @throws RuntimeException if the loading process fails
|
||||||
* @since Envoy Client v0.1-beta
|
* @since Envoy Client v0.1-beta
|
||||||
*/
|
*/
|
||||||
public void load(SceneInfo sceneInfo) {
|
public void load(SceneInfo info) {
|
||||||
EnvoyLog.getLogger(SceneContext.class).log(Level.FINER, "Loading scene " + sceneInfo);
|
EnvoyLog.getLogger(SceneContext.class).log(Level.FINER, "Loading scene " + info);
|
||||||
loader.setRoot(null);
|
|
||||||
loader.setController(null);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
final var rootNode =
|
|
||||||
(Parent) loader.load(getClass().getResourceAsStream(sceneInfo.path));
|
|
||||||
final var scene = new Scene(rootNode);
|
|
||||||
final var controller = loader.getController();
|
|
||||||
controllerStack.push(controller);
|
|
||||||
|
|
||||||
sceneStack.push(scene);
|
// Load root node and controller
|
||||||
stage.setScene(scene);
|
var loader = new FXMLLoader();
|
||||||
|
Parent root = loader.load(getClass().getResourceAsStream(info.path));
|
||||||
|
Object controller = loader.getController();
|
||||||
|
roots.push(root);
|
||||||
|
controllers.push(controller);
|
||||||
|
|
||||||
|
if (scene == null) {
|
||||||
|
|
||||||
|
// One-time scene initialization
|
||||||
|
scene = new Scene(root);
|
||||||
|
applyCSS();
|
||||||
|
stage.setScene(scene);
|
||||||
|
} else {
|
||||||
|
scene.setRoot(root);
|
||||||
|
stage.sizeToScene();
|
||||||
|
}
|
||||||
|
|
||||||
|
stage.setResizable(info.resizable);
|
||||||
|
|
||||||
|
// Remove previous keyboard shortcuts
|
||||||
|
scene.getAccelerators().clear();
|
||||||
|
|
||||||
// Supply the global custom keyboard shortcuts for that scene
|
// Supply the global custom keyboard shortcuts for that scene
|
||||||
scene.getAccelerators()
|
scene.getAccelerators()
|
||||||
.putAll(GlobalKeyShortcuts.getInstance().getKeyboardShortcuts(sceneInfo));
|
.putAll(GlobalKeyShortcuts.getInstance().getKeyboardShortcuts(info));
|
||||||
|
|
||||||
// Supply the scene specific keyboard shortcuts
|
// Supply the scene specific keyboard shortcuts
|
||||||
if (controller instanceof KeyboardMapping)
|
if (controller instanceof KeyboardMapping)
|
||||||
scene.getAccelerators()
|
scene.getAccelerators()
|
||||||
.putAll(((KeyboardMapping) controller).getKeyboardShortcuts());
|
.putAll(((KeyboardMapping) controller).getKeyboardShortcuts());
|
||||||
|
} catch (IOException e) {
|
||||||
Platform.runLater(() -> stage.setResizable(sceneInfo.resizable));
|
|
||||||
stage.sizeToScene();
|
|
||||||
applyCSS();
|
|
||||||
stage.show();
|
|
||||||
} catch (final IOException e) {
|
|
||||||
EnvoyLog.getLogger(SceneContext.class).log(Level.SEVERE,
|
|
||||||
String.format("Could not load scene for %s: ", sceneInfo), e);
|
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -95,29 +99,30 @@ public final class SceneContext implements EventListener {
|
|||||||
*/
|
*/
|
||||||
public void pop() {
|
public void pop() {
|
||||||
|
|
||||||
// Pop scene and controller
|
// Pop current root node and controller
|
||||||
sceneStack.pop();
|
roots.pop();
|
||||||
controllerStack.pop();
|
controllers.pop();
|
||||||
|
|
||||||
// Apply new scene if present
|
// Apply new scene if present
|
||||||
if (!sceneStack.isEmpty()) {
|
if (!roots.isEmpty()) {
|
||||||
final var newScene = sceneStack.peek();
|
scene.setRoot(roots.peek());
|
||||||
stage.setScene(newScene);
|
|
||||||
applyCSS();
|
// Invoke restore if controller is restorable
|
||||||
stage.sizeToScene();
|
var controller = controllers.peek();
|
||||||
// If the controller implements the Restorable interface,
|
|
||||||
// the actions to perform on restoration will be executed here
|
|
||||||
final var controller = controllerStack.peek();
|
|
||||||
if (controller instanceof Restorable)
|
if (controller instanceof Restorable)
|
||||||
((Restorable) controller).onRestore();
|
((Restorable) controller).onRestore();
|
||||||
|
} else {
|
||||||
|
|
||||||
|
// Remove the current scene entirely
|
||||||
|
scene = null;
|
||||||
|
stage.setScene(null);
|
||||||
}
|
}
|
||||||
stage.show();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void applyCSS() {
|
private void applyCSS() {
|
||||||
if (!sceneStack.isEmpty()) {
|
if (scene != null) {
|
||||||
final var styleSheets = stage.getScene().getStylesheets();
|
var styleSheets = scene.getStylesheets();
|
||||||
final var themeCSS = "/css/" + settings.getCurrentTheme() + ".css";
|
var themeCSS = "/css/" + Settings.getInstance().getCurrentTheme() + ".css";
|
||||||
styleSheets.clear();
|
styleSheets.clear();
|
||||||
styleSheets.addAll(getClass().getResource("/css/base.css").toExternalForm(),
|
styleSheets.addAll(getClass().getResource("/css/base.css").toExternalForm(),
|
||||||
getClass().getResource(themeCSS).toExternalForm());
|
getClass().getResource(themeCSS).toExternalForm());
|
||||||
@ -126,8 +131,8 @@ public final class SceneContext implements EventListener {
|
|||||||
|
|
||||||
@Event(eventType = Logout.class, priority = 150)
|
@Event(eventType = Logout.class, priority = 150)
|
||||||
private void onLogout() {
|
private void onLogout() {
|
||||||
sceneStack.clear();
|
roots.clear();
|
||||||
controllerStack.clear();
|
controllers.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Event(priority = 150, eventType = ThemeChangeEvent.class)
|
@Event(priority = 150, eventType = ThemeChangeEvent.class)
|
||||||
@ -140,7 +145,7 @@ public final class SceneContext implements EventListener {
|
|||||||
* @return the controller used by the current scene
|
* @return the controller used by the current scene
|
||||||
* @since Envoy Client v0.1-beta
|
* @since Envoy Client v0.1-beta
|
||||||
*/
|
*/
|
||||||
public <T> T getController() { return (T) controllerStack.peek(); }
|
public <T> T getController() { return (T) controllers.peek(); }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return the stage in which the scenes are displayed
|
* @return the stage in which the scenes are displayed
|
||||||
@ -152,5 +157,5 @@ public final class SceneContext implements EventListener {
|
|||||||
* @return whether the scene stack is empty
|
* @return whether the scene stack is empty
|
||||||
* @since Envoy Client v0.2-beta
|
* @since Envoy Client v0.2-beta
|
||||||
*/
|
*/
|
||||||
public boolean isEmpty() { return sceneStack.isEmpty(); }
|
public boolean isEmpty() { return roots.isEmpty(); }
|
||||||
}
|
}
|
||||||
|
@ -93,7 +93,7 @@ public final class Startup extends Application {
|
|||||||
final var sceneContext = new SceneContext(stage);
|
final var sceneContext = new SceneContext(stage);
|
||||||
context.setSceneContext(sceneContext);
|
context.setSceneContext(sceneContext);
|
||||||
|
|
||||||
// Authenticate with token if present
|
// Authenticate with token if present or load login scene
|
||||||
if (localDB.getAuthToken() != null) {
|
if (localDB.getAuthToken() != null) {
|
||||||
logger.info("Attempting authentication with token...");
|
logger.info("Attempting authentication with token...");
|
||||||
localDB.loadUserData();
|
localDB.loadUserData();
|
||||||
@ -102,8 +102,9 @@ public final class Startup extends Application {
|
|||||||
VERSION, localDB.getLastSync())))
|
VERSION, localDB.getLastSync())))
|
||||||
sceneContext.load(SceneInfo.LOGIN_SCENE);
|
sceneContext.load(SceneInfo.LOGIN_SCENE);
|
||||||
} else
|
} else
|
||||||
// Load login scene
|
|
||||||
sceneContext.load(SceneInfo.LOGIN_SCENE);
|
sceneContext.load(SceneInfo.LOGIN_SCENE);
|
||||||
|
|
||||||
|
stage.show();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user