From 8e4d54b70f0595e82cef3c5d4d45b2e3934fb1b2 Mon Sep 17 00:00:00 2001 From: Ziedelth Date: Wed, 8 Dec 2021 09:59:07 +0100 Subject: [PATCH] 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(); } }