From 74290051ff0c8b71e65e93b0687aeb69d82ba32e Mon Sep 17 00:00:00 2001 From: delvh Date: Sat, 4 Jul 2020 15:26:12 +0200 Subject: [PATCH] Added ability to execute code when a scene is restored --- src/main/java/envoy/client/ui/Restorable.java | 25 +++++++++++++++++++ .../java/envoy/client/ui/SceneContext.java | 18 ++++++++++--- .../envoy/client/ui/controller/ChatScene.java | 6 ++++- 3 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 src/main/java/envoy/client/ui/Restorable.java diff --git a/src/main/java/envoy/client/ui/Restorable.java b/src/main/java/envoy/client/ui/Restorable.java new file mode 100644 index 0000000..e9e40eb --- /dev/null +++ b/src/main/java/envoy/client/ui/Restorable.java @@ -0,0 +1,25 @@ +package envoy.client.ui; + +/** + * This interface defines an action that should be performed when a scene gets + * restored from the scene stack in {@link SceneContext}. + *

+ * Project: envoy-client
+ * File: Restorable.java
+ * Created: 03.07.2020
+ * + * @author Leon Hofmeister + * @since Envoy Client v0.1-beta + */ +@FunctionalInterface +public interface Restorable { + + /** + * This method is getting called when a scene gets restored.
+ * Hence, it can contain anything that should be done when the underlying scene + * gets restored. + * + * @since Envoy Client v0.1-beta + */ + void onRestore(); +} diff --git a/src/main/java/envoy/client/ui/SceneContext.java b/src/main/java/envoy/client/ui/SceneContext.java index 6b6757e..825c959 100644 --- a/src/main/java/envoy/client/ui/SceneContext.java +++ b/src/main/java/envoy/client/ui/SceneContext.java @@ -90,8 +90,9 @@ public final class SceneContext { } private final Stage stage; - private final FXMLLoader loader = new FXMLLoader(); - private final Stack sceneStack = new Stack<>(); + private final FXMLLoader loader = new FXMLLoader(); + private final Stack sceneStack = new Stack<>(); + private final Stack controllerStack = new Stack<>(); private static final Settings settings = Settings.getInstance(); @@ -120,6 +121,7 @@ public final class SceneContext { try { final var rootNode = (Parent) loader.load(getClass().getResourceAsStream(sceneInfo.path)); final var scene = new Scene(rootNode); + controllerStack.push(loader.getController()); sceneStack.push(scene); stage.setScene(scene); @@ -139,10 +141,18 @@ public final class SceneContext { */ public void pop() { sceneStack.pop(); + controllerStack.pop(); if (!sceneStack.isEmpty()) { - stage.setScene(sceneStack.peek()); + final var newScene = sceneStack.peek(); + stage.setScene(newScene); applyCSS(); stage.sizeToScene(); + // If the controller implements the Restorable interface, + // the actions to perform on restoration will be executed here + try { + final Restorable restorable = (Restorable) controllerStack.peek(); + restorable.onRestore(); + } catch (final ClassCastException e) {} } stage.show(); } @@ -161,7 +171,7 @@ public final class SceneContext { * @return the controller used by the current scene * @since Envoy Client v0.1-beta */ - public T getController() { return loader.getController(); } + public T getController() { return (T) controllerStack.peek(); } /** * @return the stage in which the scenes are displayed diff --git a/src/main/java/envoy/client/ui/controller/ChatScene.java b/src/main/java/envoy/client/ui/controller/ChatScene.java index ed35cda..5ed306a 100644 --- a/src/main/java/envoy/client/ui/controller/ChatScene.java +++ b/src/main/java/envoy/client/ui/controller/ChatScene.java @@ -24,6 +24,7 @@ import envoy.client.event.MessageCreationEvent; import envoy.client.net.Client; import envoy.client.net.WriteProxy; import envoy.client.ui.IconUtil; +import envoy.client.ui.Restorable; import envoy.client.ui.SceneContext; import envoy.client.ui.listcell.ContactListCellFactory; import envoy.client.ui.listcell.MessageControl; @@ -43,7 +44,7 @@ import envoy.util.EnvoyLog; * @author Kai S. K. Engelbart * @since Envoy Client v0.1-beta */ -public final class ChatScene { +public final class ChatScene implements Restorable { @FXML private Label contactLabel; @@ -173,6 +174,9 @@ public final class ChatScene { if (!client.isOnline()) updateInfoLabel("You are offline", "infoLabel-info"); } + @Override + public void onRestore() { updateRemainingCharsLabel(); } + /** * Actions to perform when the list of contacts has been clicked. *