Added ability to execute code when a scene is restored

This commit is contained in:
delvh 2020-07-04 15:26:12 +02:00
parent 4f877d3d5b
commit 74290051ff
3 changed files with 44 additions and 5 deletions

View File

@ -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}.
* <p>
* Project: <strong>envoy-client</strong><br>
* File: <strong>Restorable.java</strong><br>
* Created: <strong>03.07.2020</strong><br>
*
* @author Leon Hofmeister
* @since Envoy Client v0.1-beta
*/
@FunctionalInterface
public interface Restorable {
/**
* This method is getting called when a scene gets restored.<br>
* Hence, it can contain anything that should be done when the underlying scene
* gets restored.
*
* @since Envoy Client v0.1-beta
*/
void onRestore();
}

View File

@ -90,8 +90,9 @@ public final class SceneContext {
}
private final Stage stage;
private final FXMLLoader loader = new FXMLLoader();
private final Stack<Scene> sceneStack = new Stack<>();
private final FXMLLoader loader = new FXMLLoader();
private final Stack<Scene> sceneStack = new Stack<>();
private final Stack<Object> 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> T getController() { return loader.getController(); }
public <T> T getController() { return (T) controllerStack.peek(); }
/**
* @return the stage in which the scenes are displayed

View File

@ -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.
*