2020-06-06 18:30:09 +02:00
|
|
|
package envoy.client.ui;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.Stack;
|
|
|
|
|
|
|
|
import javafx.fxml.FXMLLoader;
|
|
|
|
import javafx.scene.Parent;
|
|
|
|
import javafx.scene.Scene;
|
|
|
|
import javafx.stage.Stage;
|
|
|
|
|
|
|
|
import envoy.client.data.Settings;
|
|
|
|
import envoy.client.event.ThemeChangeEvent;
|
|
|
|
import envoy.event.EventBus;
|
|
|
|
|
|
|
|
/**
|
2020-06-08 08:45:15 +02:00
|
|
|
* Manages a stack of scenes. The most recently added scene is displayed inside
|
|
|
|
* a stage. When a scene is removed from the stack, its predecessor is
|
2020-06-08 09:14:57 +02:00
|
|
|
* displayed.
|
|
|
|
* <p>
|
|
|
|
* When a scene is loaded, the style sheet for the current theme is applied to
|
|
|
|
* it.
|
|
|
|
* <p>
|
2020-06-06 18:30:09 +02:00
|
|
|
* Project: <strong>envoy-client</strong><br>
|
|
|
|
* File: <strong>SceneContext.java</strong><br>
|
|
|
|
* Created: <strong>06.06.2020</strong><br>
|
|
|
|
*
|
|
|
|
* @author Kai S. K. Engelbart
|
|
|
|
* @since Envoy Client v0.1-beta
|
|
|
|
*/
|
|
|
|
public final class SceneContext {
|
|
|
|
|
2020-06-08 11:58:57 +02:00
|
|
|
/**
|
|
|
|
* Contains information about different scenes and their FXML resource files.
|
|
|
|
*
|
|
|
|
* @author Kai S. K. Engelbart
|
|
|
|
* @since Envoy Client v0.1-beta
|
|
|
|
*/
|
2020-06-08 10:36:30 +02:00
|
|
|
public static enum SceneInfo {
|
2020-06-06 18:30:09 +02:00
|
|
|
|
2020-06-08 11:58:57 +02:00
|
|
|
/**
|
|
|
|
* The main scene in which chats are displayed.
|
|
|
|
*
|
|
|
|
* @since Envoy Client v0.1-beta
|
|
|
|
*/
|
|
|
|
CHAT_SCENE("/fxml/ChatScene.fxml"),
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The scene in which settings are displayed.
|
|
|
|
*
|
|
|
|
* @since Envoy Client v0.1-beta
|
|
|
|
*/
|
|
|
|
SETTINGS_SCENE("/fxml/SettingsScene.fxml"),
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The scene in which the contact search is displayed.
|
|
|
|
*
|
|
|
|
* @since Envoy Client v0.1-beta
|
|
|
|
*/
|
|
|
|
CONTACT_SEARCH_SCENE("/fxml/ContactSearchScene.fxml"),
|
|
|
|
|
2020-06-09 21:22:45 +02:00
|
|
|
/**
|
|
|
|
* The scene in which the group creation screen is displayed.
|
|
|
|
*
|
|
|
|
* @since Envoy Client v0.1-beta
|
|
|
|
*/
|
|
|
|
GROUP_CREATION_SCENE("/fxml/GroupCreationScene.fxml"),
|
|
|
|
|
2020-06-08 11:58:57 +02:00
|
|
|
/**
|
|
|
|
* The scene in which the login screen is displayed.
|
|
|
|
*
|
|
|
|
* @since Envoy Client v0.1-beta
|
|
|
|
*/
|
|
|
|
LOGIN_SCENE("/fxml/LoginScene.fxml");
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The path to the FXML resource.
|
|
|
|
*/
|
2020-06-06 18:30:09 +02:00
|
|
|
public final String path;
|
|
|
|
|
|
|
|
SceneInfo(String path) { this.path = path; }
|
|
|
|
}
|
|
|
|
|
|
|
|
private final Stage stage;
|
|
|
|
private final FXMLLoader loader = new FXMLLoader();
|
|
|
|
private final Stack<Scene> sceneStack = new Stack<>();
|
|
|
|
|
|
|
|
private static final Settings settings = Settings.getInstance();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes the scene context.
|
|
|
|
*
|
|
|
|
* @param stage the stage in which scenes will be displayed
|
|
|
|
* @since Envoy Client v0.1-beta
|
|
|
|
*/
|
|
|
|
public SceneContext(Stage stage) {
|
|
|
|
this.stage = stage;
|
|
|
|
EventBus.getInstance().register(ThemeChangeEvent.class, theme -> applyCSS());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads a new scene specified by a scene info.
|
|
|
|
*
|
|
|
|
* @param sceneInfo specifies the scene to load
|
2020-06-08 11:58:57 +02:00
|
|
|
* @throws RuntimeException if the loading process fails
|
2020-06-06 18:30:09 +02:00
|
|
|
* @since Envoy Client v0.1-beta
|
|
|
|
*/
|
2020-06-08 11:58:57 +02:00
|
|
|
public void load(SceneInfo sceneInfo) {
|
2020-06-06 18:30:09 +02:00
|
|
|
loader.setRoot(null);
|
|
|
|
loader.setController(null);
|
|
|
|
|
2020-06-08 11:58:57 +02:00
|
|
|
try {
|
|
|
|
final var rootNode = (Parent) loader.load(getClass().getResourceAsStream(sceneInfo.path));
|
|
|
|
final var scene = new Scene(rootNode);
|
2020-06-06 18:30:09 +02:00
|
|
|
|
2020-06-08 11:58:57 +02:00
|
|
|
sceneStack.push(scene);
|
|
|
|
stage.setScene(scene);
|
|
|
|
applyCSS();
|
2020-06-10 10:05:25 +02:00
|
|
|
stage.sizeToScene();
|
2020-06-08 11:58:57 +02:00
|
|
|
stage.show();
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
2020-06-06 18:30:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes the current scene and displays the previous one.
|
|
|
|
*
|
|
|
|
* @since Envoy Client v0.1-beta
|
|
|
|
*/
|
|
|
|
public void pop() {
|
|
|
|
sceneStack.pop();
|
2020-06-08 08:45:15 +02:00
|
|
|
if (!sceneStack.isEmpty()) {
|
|
|
|
stage.setScene(sceneStack.peek());
|
|
|
|
applyCSS();
|
2020-06-10 10:05:25 +02:00
|
|
|
stage.sizeToScene();
|
2020-06-08 08:45:15 +02:00
|
|
|
}
|
2020-06-06 18:30:09 +02:00
|
|
|
stage.show();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void applyCSS() {
|
|
|
|
if (!sceneStack.isEmpty()) {
|
|
|
|
final var styleSheets = stage.getScene().getStylesheets();
|
2020-06-08 10:02:39 +02:00
|
|
|
final var themeCSS = "/css/" + settings.getCurrentTheme() + ".css";
|
2020-06-06 18:30:09 +02:00
|
|
|
styleSheets.clear();
|
|
|
|
styleSheets.addAll(getClass().getResource("/css/base.css").toExternalForm(), getClass().getResource(themeCSS).toExternalForm());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param <T> the type of the controller
|
|
|
|
* @return the controller used by the current scene
|
|
|
|
* @since Envoy Client v0.1-beta
|
|
|
|
*/
|
|
|
|
public <T> T getController() { return loader.getController(); }
|
2020-06-08 11:58:57 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the stage in which the scenes are displayed
|
|
|
|
* @since Envoy Client v0.1-beta
|
|
|
|
*/
|
|
|
|
public Stage getStage() { return stage; }
|
2020-06-06 18:30:09 +02:00
|
|
|
}
|