From 00effd8b5527894950022dbedc83a0d773fb0bb0 Mon Sep 17 00:00:00 2001 From: Ziedelth Date: Wed, 8 Dec 2021 09:02:25 +0100 Subject: [PATCH 1/7] Add connection to server --- pom.xml | 21 ++++- resources/connect_gui.fxml | 38 +++++++++ src/fr/univ/lyon1/gui/ClientPanel.java | 12 +-- src/fr/univ/lyon1/gui/MainGui.java | 44 ++++++++-- .../gui/controller/ConnectGuiController.java | 82 +++++++++++++++++++ src/module-info.java | 6 +- 6 files changed, 187 insertions(+), 16 deletions(-) create mode 100644 resources/connect_gui.fxml create mode 100644 src/fr/univ/lyon1/gui/controller/ConnectGuiController.java diff --git a/pom.xml b/pom.xml index b0e06c3..b170004 100644 --- a/pom.xml +++ b/pom.xml @@ -34,21 +34,33 @@ org.junit.jupiter junit-jupiter-api - 5.7.1 + 5.8.1 test org.junit.jupiter junit-jupiter-engine - 5.7.1 + 5.8.1 test + + org.jetbrains + annotations + 22.0.0 + + package src src/test/ + + + resources + + + org.apache.maven.plugins @@ -73,6 +85,11 @@ + + org.apache.maven.plugins + maven-resources-plugin + 3.1.0 + \ No newline at end of file diff --git a/resources/connect_gui.fxml b/resources/connect_gui.fxml new file mode 100644 index 0000000..b574c85 --- /dev/null +++ b/resources/connect_gui.fxml @@ -0,0 +1,38 @@ + + + + + + + + + + + + + + + + diff --git a/src/fr/univ/lyon1/gui/ClientPanel.java b/src/fr/univ/lyon1/gui/ClientPanel.java index e47bd98..045545c 100644 --- a/src/fr/univ/lyon1/gui/ClientPanel.java +++ b/src/fr/univ/lyon1/gui/ClientPanel.java @@ -12,15 +12,13 @@ import javafx.scene.text.Text; import javafx.scene.text.TextFlow; public class ClientPanel extends Parent { - private TextArea textToSend = new TextArea(); - private ScrollPane scrollReceivedText = new ScrollPane(); - private TextFlow receivedText = new TextFlow(); - private Button sendBtn = new Button(); - private Button clearBtn = new Button(); + private final TextArea textToSend = new TextArea(); + private final TextFlow receivedText = new TextFlow(); private final MainGui gui; ClientPanel(MainGui gui) { this.gui = gui; + ScrollPane scrollReceivedText = new ScrollPane(); scrollReceivedText.setLayoutX(20); scrollReceivedText.setLayoutY(20); scrollReceivedText.setPrefWidth(400); @@ -29,13 +27,14 @@ public class ClientPanel extends Parent { scrollReceivedText.vvalueProperty().bind(receivedText.heightProperty()); this.getChildren().add(scrollReceivedText); + Button sendBtn = new Button(); sendBtn.setPrefWidth(80); int btnMargin = 20; textToSend.setLayoutX(scrollReceivedText.getLayoutX()); textToSend.setLayoutY(scrollReceivedText.getLayoutY() + scrollReceivedText.getPrefHeight() + 20); - textToSend.setPrefWidth(400-sendBtn.getPrefWidth()-btnMargin); + textToSend.setPrefWidth(400- sendBtn.getPrefWidth()-btnMargin); textToSend.setPrefHeight(100); this.getChildren().add(textToSend); textToSend.setOnKeyPressed(this::textToSendKeyPressed); @@ -49,6 +48,7 @@ public class ClientPanel extends Parent { this.getChildren().add(sendBtn); sendBtn.setOnAction(this::sendBtnAction); + Button clearBtn = new Button(); clearBtn.setText("Clear"); clearBtn.setLayoutX(sendBtn.getLayoutX()); clearBtn.setPrefWidth(sendBtn.getPrefWidth()); diff --git a/src/fr/univ/lyon1/gui/MainGui.java b/src/fr/univ/lyon1/gui/MainGui.java index 2b1c691..d9e931f 100644 --- a/src/fr/univ/lyon1/gui/MainGui.java +++ b/src/fr/univ/lyon1/gui/MainGui.java @@ -1,22 +1,47 @@ package fr.univ.lyon1.gui; import javafx.application.Application; +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.util.List; public class MainGui extends Application { private ClientPanel clientPanel; + @Nullable private ClientGUI client; @Override - public void start(Stage stage) throws Exception { - List parameters = this.getParameters().getUnnamed(); - client = new ClientGUI(this, parameters.get(0), Integer.parseInt(parameters.get(1))); - //ToDo: error management especially for bad server IP/port - //ToDo: Server IP/port enter by user on the GUI + public void start(Stage stage) { + try { + @NotNull + final List parameters = this.getParameters().getUnnamed(); + + if (parameters.size() != 2) { + // TODO: Error + System.out.println("Error"); + + FXMLLoader fxmlLoader = new FXMLLoader(ClassLoader.getSystemClassLoader().getResource("connect_gui.fxml")); + Scene scene = new Scene(fxmlLoader.load(), 320, 240); + // stage.getIcons().add(new Image(Objects.requireNonNull(HelloApplication.class.getResourceAsStream("icon.jpg")))); + stage.setTitle("Hello!"); + stage.setScene(scene); + stage.show(); + return; + } else { + client = new ClientGUI(this, parameters.get(0), Integer.parseInt(parameters.get(1))); + //ToDo: error management especially for bad server IP/port + //ToDo: Server IP/port enter by user on the GUI + } + } catch (Exception exception) { + System.out.println(exception.getMessage()); + exception.printStackTrace(); + return; + } stage.setTitle("Chat client"); stage.setWidth(440); @@ -28,7 +53,10 @@ public class MainGui extends Application { stage.setScene(scene); stage.show(); - client.run(); + + if (client != null) { + client.run(); + } } public static void main(String[] args) { @@ -36,7 +64,9 @@ public class MainGui extends Application { } public void sendMessage(String msg) { - client.sendMessage(msg); + if (client != null) { + client.sendMessage(msg); + } } public void receiveMessage(String msg) { diff --git a/src/fr/univ/lyon1/gui/controller/ConnectGuiController.java b/src/fr/univ/lyon1/gui/controller/ConnectGuiController.java new file mode 100644 index 0000000..79d0241 --- /dev/null +++ b/src/fr/univ/lyon1/gui/controller/ConnectGuiController.java @@ -0,0 +1,82 @@ +package fr.univ.lyon1.gui.controller; + +import javafx.event.ActionEvent; +import javafx.scene.control.Alert; +import javafx.scene.control.Button; +import javafx.scene.control.TextField; +import org.jetbrains.annotations.NotNull; + +import java.io.IOException; +import java.net.Socket; + +public class ConnectGuiController { + public TextField addressTextField; + public TextField portTextField; + public Button connectButton; + + /** + * Called by submit button in resources/connect_gui.fxml + * @param actionEvent Action Event + */ + public void connect(ActionEvent actionEvent) { + this.connectButton.setDisable(true); + + @NotNull + final String address = this.addressTextField.getText(); + @NotNull + final String port = this.portTextField.getText(); + + System.out.println("Checking if port is valid..."); + // Check if the port is a number and if it's an available port + if (this.isNumber(port) && this.isAvailablePort(Integer.parseInt(port))) { + final int iPort = Integer.parseInt(port); + System.out.println("Port valid, next step..."); + System.out.println("Checking if connection is available..."); + + // Check if the connection is available + if (this.isAccessible(address, iPort)) { + + } else { + System.out.println("Connection not available"); + this.showDialog("Erreur de connexion", "Impossible de se connecter au serveur, veuillez vérifier les informations saisies"); + this.connectButton.setDisable(false); + } + } else { + this.showDialog("Erreur", "Veuillez saisir un numéro de port valide"); + this.connectButton.setDisable(false); + } + } + + private void showDialog(String title, String description) { + @NotNull + final Alert alert = new Alert(Alert.AlertType.WARNING); + alert.setTitle(title); + alert.setContentText(description); + alert.show(); + } + + private boolean isNumber(String text) { + try { + Integer.parseInt(text); + return true; + } catch (NumberFormatException ignored) { + return false; + } + } + + private boolean isAvailablePort(int port) { + return !(port < 0 || port > 0xFFFF); + } + + private boolean isAccessible(String address, int port) { + try { + @NotNull + final Socket socket = new Socket(address, port); + socket.setSoTimeout(5 * 1000); + + return true; + } catch (IOException exception) { + return false; + } + } +} diff --git a/src/module-info.java b/src/module-info.java index 39f3612..1e2cdcd 100644 --- a/src/module-info.java +++ b/src/module-info.java @@ -1,9 +1,13 @@ -module fr.univ.lyon1.gui { +module fr.univ.lyon { requires javafx.controls; requires javafx.fxml; requires org.kordamp.bootstrapfx.core; + requires org.jetbrains.annotations; opens fr.univ.lyon1.gui to javafx.fxml; + + exports fr.univ.lyon1.common; exports fr.univ.lyon1.gui; + exports fr.univ.lyon1.gui.controller; } \ No newline at end of file From a9215ccb9a08bf4496110b4728bea88e547eb6e3 Mon Sep 17 00:00:00 2001 From: Ziedelth Date: Wed, 8 Dec 2021 09:25:21 +0100 Subject: [PATCH 2/7] Update dependencies and save connection information --- pom.xml | 4 +- .../gui/controller/ConnectGuiController.java | 37 +++++++++++++++++-- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/pom.xml b/pom.xml index b170004..59b3bd1 100644 --- a/pom.xml +++ b/pom.xml @@ -34,13 +34,13 @@ org.junit.jupiter junit-jupiter-api - 5.8.1 + 5.8.2 test org.junit.jupiter junit-jupiter-engine - 5.8.1 + 5.8.2 test diff --git a/src/fr/univ/lyon1/gui/controller/ConnectGuiController.java b/src/fr/univ/lyon1/gui/controller/ConnectGuiController.java index 79d0241..5dd3286 100644 --- a/src/fr/univ/lyon1/gui/controller/ConnectGuiController.java +++ b/src/fr/univ/lyon1/gui/controller/ConnectGuiController.java @@ -6,8 +6,11 @@ import javafx.scene.control.Button; import javafx.scene.control.TextField; import org.jetbrains.annotations.NotNull; +import java.io.File; +import java.io.FileWriter; import java.io.IOException; import java.net.Socket; +import java.util.Properties; public class ConnectGuiController { public TextField addressTextField; @@ -35,21 +38,49 @@ public class ConnectGuiController { // Check if the connection is available if (this.isAccessible(address, iPort)) { + System.out.println("Connection available, saving file..."); + // Save server information in properties file + @NotNull + final Properties properties = new Properties(); + properties.setProperty("address", address); + properties.setProperty("port", port); + + try { + @NotNull + final File file = new File("connection.properties"); + properties.store(new FileWriter(file), "Information needed to connect to the server"); + this.showSuccessDialog("Connecté", "Vous êtes bien connecté au serveur"); + System.out.println("File saved, next step..."); + } catch (IOException e) { + this.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()); + } } else { System.out.println("Connection not available"); - this.showDialog("Erreur de connexion", "Impossible de se connecter au serveur, veuillez vérifier les informations saisies"); + this.showErrorDialog("Erreur de connexion", "Impossible de se connecter au serveur, veuillez vérifier les informations saisies"); this.connectButton.setDisable(false); } } else { - this.showDialog("Erreur", "Veuillez saisir un numéro de port valide"); + this.showErrorDialog("Erreur", "Veuillez saisir un numéro de port valide"); this.connectButton.setDisable(false); } } - private void showDialog(String title, String description) { + private void showErrorDialog(String title, String description) { @NotNull final Alert alert = new Alert(Alert.AlertType.WARNING); + extracted(title, description, alert); + } + + private void showSuccessDialog(String title, String description) { + @NotNull + final Alert alert = new Alert(Alert.AlertType.INFORMATION); + extracted(title, description, alert); + } + + private void extracted(String title, String description, @NotNull Alert alert) { alert.setTitle(title); alert.setContentText(description); alert.show(); From bb100489dccaa6c1b96e025d06f9e169b0f0cc56 Mon Sep 17 00:00:00 2001 From: Ziedelth Date: Wed, 8 Dec 2021 09:40:50 +0100 Subject: [PATCH 3/7] Reformat server configuration --- .gitignore | 1 + .../lyon1/common/ServerConfiguration.java | 37 +++++++++++++++ src/fr/univ/lyon1/gui/MainGui.java | 16 ++++--- .../gui/controller/ConnectGuiController.java | 46 ++++--------------- src/fr/univ/lyon1/gui/controller/Dialog.java | 24 ++++++++++ 5 files changed, 80 insertions(+), 44 deletions(-) create mode 100644 src/fr/univ/lyon1/common/ServerConfiguration.java create mode 100644 src/fr/univ/lyon1/gui/controller/Dialog.java diff --git a/.gitignore b/.gitignore index 87afada..fec019b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .idea/ /target/ +/connection.properties diff --git a/src/fr/univ/lyon1/common/ServerConfiguration.java b/src/fr/univ/lyon1/common/ServerConfiguration.java new file mode 100644 index 0000000..7102161 --- /dev/null +++ b/src/fr/univ/lyon1/common/ServerConfiguration.java @@ -0,0 +1,37 @@ +package fr.univ.lyon1.common; + +import org.jetbrains.annotations.NotNull; + +import java.io.*; +import java.util.Properties; + +public record ServerConfiguration(@NotNull String address, int port) { + @NotNull + private static final File file = new File("connection.properties"); + + public ServerConfiguration(@NotNull String address, int port) { + this.address = address; + this.port = port; + } + + public static ServerConfiguration load() throws IOException, NumberFormatException { + @NotNull final Properties properties = new Properties(); + properties.load(new FileReader(file)); + return new ServerConfiguration(properties.getProperty("address"), Integer.parseInt(properties.getProperty("port"))); + } + + public void save() throws IOException { + @NotNull final Properties properties = new Properties(); + properties.setProperty("address", this.address); + properties.setProperty("port", String.valueOf(this.port)); + properties.store(new FileWriter(file), "Information needed to connect to the server"); + } + + public @NotNull String getAddress() { + return address; + } + + public int getPort() { + return port; + } +} diff --git a/src/fr/univ/lyon1/gui/MainGui.java b/src/fr/univ/lyon1/gui/MainGui.java index d9e931f..92332c8 100644 --- a/src/fr/univ/lyon1/gui/MainGui.java +++ b/src/fr/univ/lyon1/gui/MainGui.java @@ -8,6 +8,7 @@ import javafx.stage.Stage; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.io.IOException; import java.util.List; public class MainGui extends Application { @@ -25,12 +26,7 @@ public class MainGui extends Application { // TODO: Error System.out.println("Error"); - FXMLLoader fxmlLoader = new FXMLLoader(ClassLoader.getSystemClassLoader().getResource("connect_gui.fxml")); - Scene scene = new Scene(fxmlLoader.load(), 320, 240); - // stage.getIcons().add(new Image(Objects.requireNonNull(HelloApplication.class.getResourceAsStream("icon.jpg")))); - stage.setTitle("Hello!"); - stage.setScene(scene); - stage.show(); + launchServerConfiguration(stage); return; } else { client = new ClientGUI(this, parameters.get(0), Integer.parseInt(parameters.get(1))); @@ -59,6 +55,14 @@ 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); } diff --git a/src/fr/univ/lyon1/gui/controller/ConnectGuiController.java b/src/fr/univ/lyon1/gui/controller/ConnectGuiController.java index 5dd3286..6cbed7d 100644 --- a/src/fr/univ/lyon1/gui/controller/ConnectGuiController.java +++ b/src/fr/univ/lyon1/gui/controller/ConnectGuiController.java @@ -1,16 +1,12 @@ package fr.univ.lyon1.gui.controller; -import javafx.event.ActionEvent; -import javafx.scene.control.Alert; +import fr.univ.lyon1.common.ServerConfiguration; import javafx.scene.control.Button; import javafx.scene.control.TextField; import org.jetbrains.annotations.NotNull; -import java.io.File; -import java.io.FileWriter; import java.io.IOException; import java.net.Socket; -import java.util.Properties; public class ConnectGuiController { public TextField addressTextField; @@ -19,9 +15,8 @@ public class ConnectGuiController { /** * Called by submit button in resources/connect_gui.fxml - * @param actionEvent Action Event */ - public void connect(ActionEvent actionEvent) { + public void connect() { this.connectButton.setDisable(true); @NotNull @@ -40,52 +35,27 @@ public class ConnectGuiController { if (this.isAccessible(address, iPort)) { System.out.println("Connection available, saving file..."); - // Save server information in properties file - @NotNull - final Properties properties = new Properties(); - properties.setProperty("address", address); - properties.setProperty("port", port); - try { - @NotNull - final File file = new File("connection.properties"); - properties.store(new FileWriter(file), "Information needed to connect to the server"); - this.showSuccessDialog("Connecté", "Vous êtes bien connecté au serveur"); + // Save server configuration + new ServerConfiguration(address, iPort).save(); + Dialog.showSuccessDialog("Connecté", "Vous êtes bien connecté au serveur"); System.out.println("File saved, next step..."); } catch (IOException e) { - this.showErrorDialog("Erreur", "Impossible de sauvegarder les informations de connexion au serveur"); + 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()); } } else { System.out.println("Connection not available"); - this.showErrorDialog("Erreur de connexion", "Impossible de se connecter au serveur, veuillez vérifier les informations saisies"); + Dialog.showErrorDialog("Erreur de connexion", "Impossible de se connecter au serveur, veuillez vérifier les informations saisies"); this.connectButton.setDisable(false); } } else { - this.showErrorDialog("Erreur", "Veuillez saisir un numéro de port valide"); + Dialog.showErrorDialog("Erreur", "Veuillez saisir un numéro de port valide"); this.connectButton.setDisable(false); } } - private void showErrorDialog(String title, String description) { - @NotNull - final Alert alert = new Alert(Alert.AlertType.WARNING); - extracted(title, description, alert); - } - - private void showSuccessDialog(String title, String description) { - @NotNull - final Alert alert = new Alert(Alert.AlertType.INFORMATION); - extracted(title, description, alert); - } - - private void extracted(String title, String description, @NotNull Alert alert) { - alert.setTitle(title); - alert.setContentText(description); - alert.show(); - } - private boolean isNumber(String text) { try { Integer.parseInt(text); diff --git a/src/fr/univ/lyon1/gui/controller/Dialog.java b/src/fr/univ/lyon1/gui/controller/Dialog.java new file mode 100644 index 0000000..6623ab8 --- /dev/null +++ b/src/fr/univ/lyon1/gui/controller/Dialog.java @@ -0,0 +1,24 @@ +package fr.univ.lyon1.gui.controller; + +import javafx.scene.control.Alert; +import org.jetbrains.annotations.NotNull; + +public class Dialog { + public static void showErrorDialog(String title, String description) { + @NotNull + final Alert alert = new Alert(Alert.AlertType.WARNING); + extracted(title, description, alert); + } + + public static void showSuccessDialog(String title, String description) { + @NotNull + final Alert alert = new Alert(Alert.AlertType.INFORMATION); + extracted(title, description, alert); + } + + private static void extracted(String title, String description, @NotNull Alert alert) { + alert.setTitle(title); + alert.setContentText(description); + alert.show(); + } +} From 8e4d54b70f0595e82cef3c5d4d45b2e3934fb1b2 Mon Sep 17 00:00:00 2001 From: Ziedelth Date: Wed, 8 Dec 2021 09:59:07 +0100 Subject: [PATCH 4/7] Add load server configuration on application start --- .../lyon1/common/ServerConfiguration.java | 6 +++ src/fr/univ/lyon1/gui/MainGui.java | 53 ++++++++----------- 2 files changed, 29 insertions(+), 30 deletions(-) diff --git a/src/fr/univ/lyon1/common/ServerConfiguration.java b/src/fr/univ/lyon1/common/ServerConfiguration.java index 7102161..339cf0f 100644 --- a/src/fr/univ/lyon1/common/ServerConfiguration.java +++ b/src/fr/univ/lyon1/common/ServerConfiguration.java @@ -15,6 +15,12 @@ public record ServerConfiguration(@NotNull String address, int port) { } public static ServerConfiguration load() throws IOException, NumberFormatException { + // Check if file non exists, return error to launch server configuration + if (!file.exists()) { + System.out.println("File not exists"); + throw new IOException("File not exists"); + } + @NotNull final Properties properties = new Properties(); properties.load(new FileReader(file)); return new ServerConfiguration(properties.getProperty("address"), Integer.parseInt(properties.getProperty("port"))); diff --git a/src/fr/univ/lyon1/gui/MainGui.java b/src/fr/univ/lyon1/gui/MainGui.java index 92332c8..7e63630 100644 --- a/src/fr/univ/lyon1/gui/MainGui.java +++ b/src/fr/univ/lyon1/gui/MainGui.java @@ -1,5 +1,6 @@ package fr.univ.lyon1.gui; +import fr.univ.lyon1.common.ServerConfiguration; import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Group; @@ -9,7 +10,6 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.io.IOException; -import java.util.List; public class MainGui extends Application { private ClientPanel clientPanel; @@ -20,38 +20,31 @@ public class MainGui extends Application { public void start(Stage stage) { try { @NotNull - final List parameters = this.getParameters().getUnnamed(); + final ServerConfiguration serverConfiguration = ServerConfiguration.load(); + this.client = new ClientGUI(this, serverConfiguration.getAddress(), serverConfiguration.getPort()); - if (parameters.size() != 2) { - // TODO: Error - System.out.println("Error"); + stage.setTitle("Chat client"); + stage.setWidth(440); - launchServerConfiguration(stage); - return; - } else { - client = new ClientGUI(this, parameters.get(0), Integer.parseInt(parameters.get(1))); - //ToDo: error management especially for bad server IP/port - //ToDo: Server IP/port enter by user on the GUI + 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(); + } + } catch (IOException | InterruptedException e) { + // Launch server configuration + try { + launchServerConfiguration(stage); + } catch (IOException ex) { + // Can not launch server configuration, stop application + System.exit(1); } - } catch (Exception exception) { - System.out.println(exception.getMessage()); - exception.printStackTrace(); - return; - } - - stage.setTitle("Chat client"); - stage.setWidth(440); - - clientPanel = new ClientPanel(this); - Group root = new Group(); - root.getChildren().add(clientPanel); - Scene scene = new Scene(root, 600, 500); - - stage.setScene(scene); - stage.show(); - - if (client != null) { - client.run(); } } From 0e36273ab9bb6527f71a1b4618e282c80c8855ff Mon Sep 17 00:00:00 2001 From: Ziedelth Date: Fri, 10 Dec 2021 10:35:17 +0100 Subject: [PATCH 5/7] Fix bug on close --- src/fr/univ/lyon1/gui/MainGui.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/fr/univ/lyon1/gui/MainGui.java b/src/fr/univ/lyon1/gui/MainGui.java index 7e63630..03e4c83 100644 --- a/src/fr/univ/lyon1/gui/MainGui.java +++ b/src/fr/univ/lyon1/gui/MainGui.java @@ -2,6 +2,7 @@ package fr.univ.lyon1.gui; import fr.univ.lyon1.common.ServerConfiguration; import javafx.application.Application; +import javafx.application.Platform; import javafx.fxml.FXMLLoader; import javafx.scene.Group; import javafx.scene.Scene; @@ -46,6 +47,11 @@ public class MainGui extends Application { System.exit(1); } } + + stage.setOnCloseRequest(event -> { + Platform.exit(); + System.exit(0); + }); } private void launchServerConfiguration(Stage stage) throws IOException { From cc3a9033ff0989e989d497e1095d18db178152af Mon Sep 17 00:00:00 2001 From: Ziedelth Date: Wed, 5 Jan 2022 08:37:40 +0100 Subject: [PATCH 6/7] 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 From 68cbef6722556677125d602c88730462f85017c2 Mon Sep 17 00:00:00 2001 From: Ziedelth Date: Wed, 5 Jan 2022 08:38:18 +0100 Subject: [PATCH 7/7] Remove todo and debug print --- src/fr/univ/lyon1/gui/controller/ConnectGuiController.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/fr/univ/lyon1/gui/controller/ConnectGuiController.java b/src/fr/univ/lyon1/gui/controller/ConnectGuiController.java index 6f7ca0e..153bdab 100644 --- a/src/fr/univ/lyon1/gui/controller/ConnectGuiController.java +++ b/src/fr/univ/lyon1/gui/controller/ConnectGuiController.java @@ -49,10 +49,7 @@ public class ConnectGuiController { @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);