added inelegant capability to switch scenes
This commit is contained in:
parent
e4e903b8bf
commit
970f190389
@ -5,6 +5,14 @@ import java.util.logging.Level;
|
|||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
import java.util.stream.Collectors;
|
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.Chat;
|
||||||
import envoy.client.data.LocalDB;
|
import envoy.client.data.LocalDB;
|
||||||
import envoy.client.event.MessageCreationEvent;
|
import envoy.client.event.MessageCreationEvent;
|
||||||
@ -17,12 +25,6 @@ import envoy.event.EventBus;
|
|||||||
import envoy.event.MessageStatusChangeEvent;
|
import envoy.event.MessageStatusChangeEvent;
|
||||||
import envoy.event.UserStatusChangeEvent;
|
import envoy.event.UserStatusChangeEvent;
|
||||||
import envoy.util.EnvoyLog;
|
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>
|
* Project: <strong>envoy-client</strong><br>
|
||||||
@ -58,6 +60,8 @@ public final class ChatSceneController {
|
|||||||
|
|
||||||
private Chat currentChat;
|
private Chat currentChat;
|
||||||
|
|
||||||
|
private Startup startup;
|
||||||
|
|
||||||
private static final EventBus eventBus = EventBus.getInstance();
|
private static final EventBus eventBus = EventBus.getInstance();
|
||||||
private static final Logger logger = EnvoyLog.getLogger(ChatSceneController.class);
|
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()));
|
eventBus.register(UserStatusChangeEvent.class, e -> Platform.runLater(() -> userList.refresh()));
|
||||||
}
|
}
|
||||||
|
|
||||||
void initializeData(LocalDB localDB, Client client, WriteProxy writeProxy) {
|
void initializeData(Startup startup, LocalDB localDB, Client client, WriteProxy writeProxy) {
|
||||||
this.localDB = localDB;
|
this.startup = startup;
|
||||||
this.client = client;
|
this.localDB = localDB;
|
||||||
this.writeProxy = writeProxy;
|
this.client = client;
|
||||||
|
this.writeProxy = writeProxy;
|
||||||
|
|
||||||
// TODO: handle offline mode
|
// TODO: handle offline mode
|
||||||
userList.setItems(FXCollections.observableList(localDB.getUser().getContacts().stream().collect(Collectors.toList())));
|
userList.setItems(FXCollections.observableList(localDB.getUser().getContacts().stream().collect(Collectors.toList())));
|
||||||
@ -123,7 +128,10 @@ public final class ChatSceneController {
|
|||||||
private void postButtonClicked() { postMessage(); }
|
private void postButtonClicked() { postMessage(); }
|
||||||
|
|
||||||
@FXML
|
@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
|
@FXML
|
||||||
private void messageTextUpdated(KeyEvent e) {
|
private void messageTextUpdated(KeyEvent e) {
|
||||||
|
@ -11,21 +11,32 @@ import javafx.scene.control.*;
|
|||||||
* @author Kai S. K. Engelbart
|
* @author Kai S. K. Engelbart
|
||||||
* @since Envoy Client v0.1-beta
|
* @since Envoy Client v0.1-beta
|
||||||
*/
|
*/
|
||||||
public final class SettingsSceneController {
|
public class SettingsSceneController {
|
||||||
|
|
||||||
|
private Startup startup;
|
||||||
@FXML
|
@FXML
|
||||||
private ListView<SettingsPane> settingsList;
|
private ListView<SettingsPane> settingsList;
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
private TitledPane titledPane;
|
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
|
@FXML
|
||||||
private void initialize() {
|
private void initialize() {
|
||||||
settingsList.setCellFactory(listView -> new ListCell<>() {
|
settingsList.setCellFactory(listView -> new ListCell<>() {
|
||||||
|
|
||||||
@Override
|
@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
|
@FXML
|
||||||
@ -34,4 +45,7 @@ public final class SettingsSceneController {
|
|||||||
titledPane.setText(pane.getTitle());
|
titledPane.setText(pane.getTitle());
|
||||||
titledPane.setContent(pane);
|
titledPane.setContent(pane);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@FXML
|
||||||
|
private void backButtonClicked() { startup.restoreScene(false); }
|
||||||
}
|
}
|
||||||
|
@ -8,12 +8,14 @@ import java.util.logging.Level;
|
|||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import javafx.application.Application;
|
import javafx.application.Application;
|
||||||
|
import javafx.application.Platform;
|
||||||
import javafx.fxml.FXMLLoader;
|
import javafx.fxml.FXMLLoader;
|
||||||
import javafx.scene.Scene;
|
import javafx.scene.Scene;
|
||||||
import javafx.scene.control.Alert;
|
import javafx.scene.control.Alert;
|
||||||
import javafx.scene.control.Alert.AlertType;
|
import javafx.scene.control.Alert.AlertType;
|
||||||
import javafx.scene.image.Image;
|
import javafx.scene.image.Image;
|
||||||
import javafx.scene.layout.GridPane;
|
import javafx.scene.layout.GridPane;
|
||||||
|
import javafx.scene.layout.Pane;
|
||||||
import javafx.stage.Stage;
|
import javafx.stage.Stage;
|
||||||
|
|
||||||
import envoy.client.data.*;
|
import envoy.client.data.*;
|
||||||
@ -40,6 +42,11 @@ public final class Startup extends Application {
|
|||||||
private WriteProxy writeProxy;
|
private WriteProxy writeProxy;
|
||||||
private Cache<Message> cache;
|
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 ClientConfig config = ClientConfig.getInstance();
|
||||||
private static final Logger logger = EnvoyLog.getLogger(Startup.class);
|
private static final Logger logger = EnvoyLog.getLogger(Startup.class);
|
||||||
|
|
||||||
@ -48,6 +55,7 @@ public final class Startup extends Application {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void start(Stage stage) throws Exception {
|
public void start(Stage stage) throws Exception {
|
||||||
|
this.stage = stage;
|
||||||
try {
|
try {
|
||||||
// Load the configuration from client.properties first
|
// Load the configuration from client.properties first
|
||||||
Properties properties = new Properties();
|
Properties properties = new Properties();
|
||||||
@ -124,20 +132,67 @@ public final class Startup extends Application {
|
|||||||
.forEach(u -> u.setStatus(UserStatus.OFFLINE));
|
.forEach(u -> u.setStatus(UserStatus.OFFLINE));
|
||||||
|
|
||||||
// Prepare stage and load ChatScene
|
// Prepare stage and load ChatScene
|
||||||
var loader = new FXMLLoader(getClass().getResource("/fxml/ChatScene.fxml"));
|
changeScene("/fxml/ChatScene.fxml", new GridPane(), false);
|
||||||
var anchorPane = loader.<GridPane>load();
|
Platform.runLater(() -> { ((ChatSceneController) loader.getController()).initializeData(this, localDB, client, writeProxy); });
|
||||||
var chatScene = new Scene(anchorPane);
|
|
||||||
stage.setTitle("Envoy");
|
stage.setTitle("Envoy");
|
||||||
stage.getIcons().add(new Image(getClass().getResourceAsStream("/icons/envoy_logo.png")));
|
stage.getIcons().add(new Image(getClass().getResourceAsStream("/icons/envoy_logo.png")));
|
||||||
stage.setScene(chatScene);
|
|
||||||
|
|
||||||
loader.<ChatSceneController>getController().initializeData(localDB, client, writeProxy);
|
|
||||||
stage.show();
|
stage.show();
|
||||||
|
|
||||||
// Relay unread messages from cache
|
// Relay unread messages from cache
|
||||||
if (cache != null && client.isOnline()) cache.relay();
|
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}
|
* {@inheritDoc}
|
||||||
*/
|
*/
|
||||||
@ -159,4 +214,14 @@ public final class Startup extends Application {
|
|||||||
|
|
||||||
@SuppressWarnings("javadoc")
|
@SuppressWarnings("javadoc")
|
||||||
public static void main(String[] args) { launch(args); }
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user