added inelegant capability to switch scenes

This commit is contained in:
delvh 2020-04-18 19:46:04 +02:00
parent 621d07a300
commit 828673d00c
3 changed files with 107 additions and 20 deletions

View File

@ -5,6 +5,14 @@ import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.VBox;
import envoy.client.data.Chat;
import envoy.client.data.LocalDB;
import envoy.client.event.MessageCreationEvent;
@ -17,12 +25,6 @@ import envoy.event.EventBus;
import envoy.event.MessageStatusChangeEvent;
import envoy.event.UserStatusChangeEvent;
import envoy.util.EnvoyLog;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
/**
* Project: <strong>envoy-client</strong><br>
@ -58,6 +60,8 @@ public final class ChatSceneController {
private Chat currentChat;
private Startup startup;
private static final EventBus eventBus = EventBus.getInstance();
private static final Logger logger = EnvoyLog.getLogger(ChatSceneController.class);
@ -91,10 +95,11 @@ public final class ChatSceneController {
eventBus.register(UserStatusChangeEvent.class, e -> Platform.runLater(() -> userList.refresh()));
}
void initializeData(LocalDB localDB, Client client, WriteProxy writeProxy) {
this.localDB = localDB;
this.client = client;
this.writeProxy = writeProxy;
void initializeData(Startup startup, LocalDB localDB, Client client, WriteProxy writeProxy) {
this.startup = startup;
this.localDB = localDB;
this.client = client;
this.writeProxy = writeProxy;
// TODO: handle offline mode
userList.setItems(FXCollections.observableList(localDB.getUser().getContacts().stream().collect(Collectors.toList())));
@ -123,7 +128,10 @@ public final class ChatSceneController {
private void postButtonClicked() { postMessage(); }
@FXML
private void settingsButtonClicked() { logger.info("Settings Button clicked."); }
private void settingsButtonClicked() {
startup.changeScene("/fxml/SettingsScene.fxml", new VBox(), true);
Platform.runLater(() -> { ((SettingsSceneController) startup.getCurrentController()).initializeData(startup); });
}
@FXML
private void messageTextUpdated(KeyEvent e) {

View File

@ -7,25 +7,36 @@ import javafx.scene.control.*;
* Project: <strong>envoy-client</strong><br>
* File: <strong>SettingsSceneController.java</strong><br>
* Created: <strong>10.04.2020</strong><br>
*
*
* @author Kai S. K. Engelbart
* @since Envoy Client v0.1-beta
*/
public final class SettingsSceneController {
public class SettingsSceneController {
private Startup startup;
@FXML
private ListView<SettingsPane> settingsList;
private ListView<SettingsPane> settingsList;
@FXML
private TitledPane titledPane;
/**
* initializes the object needed to reset the scene
*
* @param startup the instance of startup that stores the stage
* @since Envoy Client v0.1-beta
*/
public void initializeData(Startup startup) { this.startup = startup; }
@FXML
private void initialize() {
settingsList.setCellFactory(listView -> new ListCell<>() {
@Override
protected void updateItem(SettingsPane item, boolean empty) { if (!empty && item != null) setGraphic(new Label(item.getTitle())); };
protected void updateItem(SettingsPane item, boolean empty) { if (!empty && item != null) setGraphic(new Label(item.getTitle())); }
});
// settingsList.getItems().add(new GeneralSettingsPane());
}
@FXML
@ -34,4 +45,7 @@ public final class SettingsSceneController {
titledPane.setText(pane.getTitle());
titledPane.setContent(pane);
}
@FXML
private void backButtonClicked() { startup.restoreScene(false); }
}

View File

@ -8,12 +8,14 @@ import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.image.Image;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import envoy.client.data.*;
@ -40,6 +42,11 @@ public final class Startup extends Application {
private WriteProxy writeProxy;
private Cache<Message> cache;
private FXMLLoader loader = new FXMLLoader();
private Stage stage;
private Scene previousScene;
private static final ClientConfig config = ClientConfig.getInstance();
private static final Logger logger = EnvoyLog.getLogger(Startup.class);
@ -48,6 +55,7 @@ public final class Startup extends Application {
*/
@Override
public void start(Stage stage) throws Exception {
this.stage = stage;
try {
// Load the configuration from client.properties first
Properties properties = new Properties();
@ -124,20 +132,67 @@ public final class Startup extends Application {
.forEach(u -> u.setStatus(UserStatus.OFFLINE));
// Prepare stage and load ChatScene
var loader = new FXMLLoader(getClass().getResource("/fxml/ChatScene.fxml"));
var anchorPane = loader.<GridPane>load();
var chatScene = new Scene(anchorPane);
changeScene("/fxml/ChatScene.fxml", new GridPane(), false);
Platform.runLater(() -> { ((ChatSceneController) loader.getController()).initializeData(this, localDB, client, writeProxy); });
stage.setTitle("Envoy");
stage.getIcons().add(new Image(getClass().getResourceAsStream("/icons/envoy_logo.png")));
stage.setScene(chatScene);
loader.<ChatSceneController>getController().initializeData(localDB, client, writeProxy);
stage.show();
// Relay unread messages from cache
if (cache != null && client.isOnline()) cache.relay();
}
/**
* Changes the scene of the stage.
*
* @param <T> the type of the layout to use
* @param fxmlLocation the location of the fxml file
* @param layout the layout to use
* @param savePrevious if true, the previous stage will be stored in this
* instance of Startup, else the variable storing it will be
* set to null
* @since Envoy Client v0.1-beta
*/
public <T extends Pane> void changeScene(String fxmlLocation, T layout, boolean savePrevious) {
Platform.runLater(() -> {
try {
// Clearing the loader so that a new Scene can be initialised
loader = new FXMLLoader();
var rootNode = loader.<T>load(getClass().getResourceAsStream(fxmlLocation));
var chatScene = new Scene(rootNode);
previousScene = (savePrevious) ? stage.getScene() : null;
stage.setScene(chatScene);
stage.show();
// return loader.getController();
} catch (IOException e) {
new Alert(AlertType.ERROR, "The screen could not be updated due to reasons. (...bad programming...)");
e.printStackTrace();
logger.severe("Something happened (while loading the new scene from " + fxmlLocation + ")");
}
});
}
/**
* Changes the visual scene back to the saved value. The currently active scene
* can be saved.
*
* @param storeCurrent the old scene to store, if wanted. Can be null
* @since Envoy Client v0.1-beta
*/
public void restoreScene(boolean storeCurrent) {
Platform.runLater(() -> {
if (previousScene == null) throw new IllegalStateException("Someone tried restoring a null scene. (Something happened)");
else {
// switching previous and current
var temp = (storeCurrent) ? stage.getScene() : null;
stage.setScene(previousScene);
previousScene = temp;
stage.show();
}
});
}
/**
* {@inheritDoc}
*/
@ -159,4 +214,14 @@ public final class Startup extends Application {
@SuppressWarnings("javadoc")
public static void main(String[] args) { launch(args); }
/**
* @return the controller of the current scene or a {@link NullPointerException}
* if there is none
* @since Envoy Client v0.1-beta
*/
public Object getCurrentController() {
if (loader.getController() == null) throw new NullPointerException("Cannot deliver current controller as its undefined (duh!)");
else return loader.getController();
}
}