From cc3a9033ff0989e989d497e1095d18db178152af Mon Sep 17 00:00:00 2001 From: Ziedelth Date: Wed, 5 Jan 2022 08:37:40 +0100 Subject: [PATCH] Can switch between scene --- src/fr/univ/lyon1/gui/ClientGUI.java | 7 +-- src/fr/univ/lyon1/gui/ClientPanel.java | 5 +- src/fr/univ/lyon1/gui/MainGui.java | 51 ++----------------- .../gui/controller/ConnectGuiController.java | 15 ++++++ src/fr/univ/lyon1/gui/handlers/Handler.java | 9 ++++ .../univ/lyon1/gui/handlers/MainHandler.java | 50 ++++++++++++++++++ .../handlers/ServerConfigurationHandler.java | 18 +++++++ src/module-info.java | 1 + 8 files changed, 104 insertions(+), 52 deletions(-) create mode 100644 src/fr/univ/lyon1/gui/handlers/Handler.java create mode 100644 src/fr/univ/lyon1/gui/handlers/MainHandler.java create mode 100644 src/fr/univ/lyon1/gui/handlers/ServerConfigurationHandler.java diff --git a/src/fr/univ/lyon1/gui/ClientGUI.java b/src/fr/univ/lyon1/gui/ClientGUI.java index 634aefc..9c870c0 100644 --- a/src/fr/univ/lyon1/gui/ClientGUI.java +++ b/src/fr/univ/lyon1/gui/ClientGUI.java @@ -3,15 +3,16 @@ package fr.univ.lyon1.gui; import fr.univ.lyon1.client.Client; import fr.univ.lyon1.client.ClientReceive; import fr.univ.lyon1.common.Message; +import fr.univ.lyon1.gui.handlers.MainHandler; import java.io.IOException; public class ClientGUI extends Client { - private final MainGui gui; + private final MainHandler gui; - public ClientGUI(MainGui gui, String address, int port) throws IOException, InterruptedException { + public ClientGUI(MainHandler handler, String address, int port) throws IOException, InterruptedException { super(address, port); - this.gui = gui; + this.gui = handler; } @Override diff --git a/src/fr/univ/lyon1/gui/ClientPanel.java b/src/fr/univ/lyon1/gui/ClientPanel.java index 045545c..701de6b 100644 --- a/src/fr/univ/lyon1/gui/ClientPanel.java +++ b/src/fr/univ/lyon1/gui/ClientPanel.java @@ -1,5 +1,6 @@ package fr.univ.lyon1.gui; +import fr.univ.lyon1.gui.handlers.MainHandler; import javafx.application.Platform; import javafx.event.ActionEvent; import javafx.scene.Parent; @@ -14,9 +15,9 @@ import javafx.scene.text.TextFlow; public class ClientPanel extends Parent { private final TextArea textToSend = new TextArea(); private final TextFlow receivedText = new TextFlow(); - private final MainGui gui; + private final MainHandler gui; - ClientPanel(MainGui gui) { + public ClientPanel(MainHandler gui) { this.gui = gui; ScrollPane scrollReceivedText = new ScrollPane(); scrollReceivedText.setLayoutX(20); diff --git a/src/fr/univ/lyon1/gui/MainGui.java b/src/fr/univ/lyon1/gui/MainGui.java index 03e4c83..4db7857 100644 --- a/src/fr/univ/lyon1/gui/MainGui.java +++ b/src/fr/univ/lyon1/gui/MainGui.java @@ -1,47 +1,22 @@ package fr.univ.lyon1.gui; -import fr.univ.lyon1.common.ServerConfiguration; +import fr.univ.lyon1.gui.handlers.MainHandler; +import fr.univ.lyon1.gui.handlers.ServerConfigurationHandler; import javafx.application.Application; import javafx.application.Platform; -import javafx.fxml.FXMLLoader; -import javafx.scene.Group; -import javafx.scene.Scene; import javafx.stage.Stage; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; import java.io.IOException; public class MainGui extends Application { - private ClientPanel clientPanel; - @Nullable - private ClientGUI client; - @Override public void start(Stage stage) { try { - @NotNull - final ServerConfiguration serverConfiguration = ServerConfiguration.load(); - this.client = new ClientGUI(this, serverConfiguration.getAddress(), serverConfiguration.getPort()); - - stage.setTitle("Chat client"); - stage.setWidth(440); - - this.clientPanel = new ClientPanel(this); - Group root = new Group(); - root.getChildren().add(this.clientPanel); - Scene scene = new Scene(root, 600, 500); - - stage.setScene(scene); - stage.show(); - - if (this.client != null) { - this.client.run(); - } + new MainHandler().launch(stage); } catch (IOException | InterruptedException e) { // Launch server configuration try { - launchServerConfiguration(stage); + new ServerConfigurationHandler().launch(stage); } catch (IOException ex) { // Can not launch server configuration, stop application System.exit(1); @@ -54,25 +29,7 @@ public class MainGui extends Application { }); } - private void launchServerConfiguration(Stage stage) throws IOException { - FXMLLoader fxmlLoader = new FXMLLoader(ClassLoader.getSystemClassLoader().getResource("connect_gui.fxml")); - Scene scene = new Scene(fxmlLoader.load(), 320, 240); - stage.setTitle("Configuration du serveur"); - stage.setScene(scene); - stage.show(); - } - public static void main(String[] args) { Application.launch(MainGui.class, args); } - - public void sendMessage(String msg) { - if (client != null) { - client.sendMessage(msg); - } - } - - public void receiveMessage(String msg) { - clientPanel.addMessage(msg); - } } diff --git a/src/fr/univ/lyon1/gui/controller/ConnectGuiController.java b/src/fr/univ/lyon1/gui/controller/ConnectGuiController.java index 6cbed7d..6f7ca0e 100644 --- a/src/fr/univ/lyon1/gui/controller/ConnectGuiController.java +++ b/src/fr/univ/lyon1/gui/controller/ConnectGuiController.java @@ -1,16 +1,22 @@ package fr.univ.lyon1.gui.controller; import fr.univ.lyon1.common.ServerConfiguration; +import fr.univ.lyon1.gui.handlers.MainHandler; +import javafx.fxml.FXML; import javafx.scene.control.Button; import javafx.scene.control.TextField; +import javafx.stage.Stage; import org.jetbrains.annotations.NotNull; import java.io.IOException; import java.net.Socket; public class ConnectGuiController { + @FXML public TextField addressTextField; + @FXML public TextField portTextField; + @FXML public Button connectButton; /** @@ -40,10 +46,19 @@ public class ConnectGuiController { new ServerConfiguration(address, iPort).save(); Dialog.showSuccessDialog("Connecté", "Vous êtes bien connecté au serveur"); System.out.println("File saved, next step..."); + + @NotNull + final Stage stage = (Stage) this.connectButton.getScene().getWindow(); + // TODO: Change application scene + new MainHandler().launch(stage); + + System.out.println("Get title : " + stage.getTitle()); } catch (IOException e) { Dialog.showErrorDialog("Erreur", "Impossible de sauvegarder les informations de connexion au serveur"); this.connectButton.setDisable(false); System.out.println("Failed to save file, error: " + e.getMessage()); + } catch (InterruptedException e) { + e.printStackTrace(); } } else { System.out.println("Connection not available"); diff --git a/src/fr/univ/lyon1/gui/handlers/Handler.java b/src/fr/univ/lyon1/gui/handlers/Handler.java new file mode 100644 index 0000000..a2fd0b5 --- /dev/null +++ b/src/fr/univ/lyon1/gui/handlers/Handler.java @@ -0,0 +1,9 @@ +package fr.univ.lyon1.gui.handlers; + +import javafx.stage.Stage; + +import java.io.IOException; + +public interface Handler { + void launch(Stage stage) throws IOException, InterruptedException; +} diff --git a/src/fr/univ/lyon1/gui/handlers/MainHandler.java b/src/fr/univ/lyon1/gui/handlers/MainHandler.java new file mode 100644 index 0000000..761f65b --- /dev/null +++ b/src/fr/univ/lyon1/gui/handlers/MainHandler.java @@ -0,0 +1,50 @@ +package fr.univ.lyon1.gui.handlers; + +import fr.univ.lyon1.common.ServerConfiguration; +import fr.univ.lyon1.gui.ClientGUI; +import fr.univ.lyon1.gui.ClientPanel; +import javafx.scene.Group; +import javafx.scene.Scene; +import javafx.stage.Stage; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.io.IOException; + +public class MainHandler implements Handler { + private ClientPanel clientPanel; + @Nullable + private ClientGUI client; + + @Override + public void launch(Stage stage) throws IOException, InterruptedException { + @NotNull + final ServerConfiguration serverConfiguration = ServerConfiguration.load(); + this.client = new ClientGUI(this, serverConfiguration.getAddress(), serverConfiguration.getPort()); + + stage.setTitle("Chat client"); + stage.setWidth(440); + + this.clientPanel = new ClientPanel(this); + Group root = new Group(); + root.getChildren().add(this.clientPanel); + Scene scene = new Scene(root, 600, 500); + + stage.setScene(scene); + stage.show(); + + if (this.client != null) { + this.client.run(); + } + } + + public void sendMessage(String msg) { + if (client != null) { + client.sendMessage(msg); + } + } + + public void receiveMessage(String msg) { + clientPanel.addMessage(msg); + } +} diff --git a/src/fr/univ/lyon1/gui/handlers/ServerConfigurationHandler.java b/src/fr/univ/lyon1/gui/handlers/ServerConfigurationHandler.java new file mode 100644 index 0000000..5c4211d --- /dev/null +++ b/src/fr/univ/lyon1/gui/handlers/ServerConfigurationHandler.java @@ -0,0 +1,18 @@ +package fr.univ.lyon1.gui.handlers; + +import javafx.fxml.FXMLLoader; +import javafx.scene.Scene; +import javafx.stage.Stage; + +import java.io.IOException; + +public class ServerConfigurationHandler implements Handler { + @Override + public void launch(Stage stage) throws IOException { + FXMLLoader fxmlLoader = new FXMLLoader(ClassLoader.getSystemClassLoader().getResource("connect_gui.fxml")); + Scene scene = new Scene(fxmlLoader.load(), 320, 240); + stage.setTitle("Configuration du serveur"); + stage.setScene(scene); + stage.show(); + } +} diff --git a/src/module-info.java b/src/module-info.java index 1e2cdcd..0192096 100644 --- a/src/module-info.java +++ b/src/module-info.java @@ -10,4 +10,5 @@ module fr.univ.lyon { exports fr.univ.lyon1.common; exports fr.univ.lyon1.gui; exports fr.univ.lyon1.gui.controller; + exports fr.univ.lyon1.gui.handlers; } \ No newline at end of file