Archived
1
0
Fork 0

Reformat server configuration

This commit is contained in:
Ziedelth 2021-12-08 09:40:50 +01:00
parent a9215ccb9a
commit bb100489dc
5 changed files with 80 additions and 44 deletions

1
.gitignore vendored
View file

@ -1,2 +1,3 @@
.idea/ .idea/
/target/ /target/
/connection.properties

View file

@ -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;
}
}

View file

@ -8,6 +8,7 @@ import javafx.stage.Stage;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.io.IOException;
import java.util.List; import java.util.List;
public class MainGui extends Application { public class MainGui extends Application {
@ -25,12 +26,7 @@ public class MainGui extends Application {
// TODO: Error // TODO: Error
System.out.println("Error"); System.out.println("Error");
FXMLLoader fxmlLoader = new FXMLLoader(ClassLoader.getSystemClassLoader().getResource("connect_gui.fxml")); launchServerConfiguration(stage);
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; return;
} else { } else {
client = new ClientGUI(this, parameters.get(0), Integer.parseInt(parameters.get(1))); 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) { public static void main(String[] args) {
Application.launch(MainGui.class, args); Application.launch(MainGui.class, args);
} }

View file

@ -1,16 +1,12 @@
package fr.univ.lyon1.gui.controller; package fr.univ.lyon1.gui.controller;
import javafx.event.ActionEvent; import fr.univ.lyon1.common.ServerConfiguration;
import javafx.scene.control.Alert;
import javafx.scene.control.Button; import javafx.scene.control.Button;
import javafx.scene.control.TextField; import javafx.scene.control.TextField;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.net.Socket; import java.net.Socket;
import java.util.Properties;
public class ConnectGuiController { public class ConnectGuiController {
public TextField addressTextField; public TextField addressTextField;
@ -19,9 +15,8 @@ public class ConnectGuiController {
/** /**
* Called by submit button in resources/connect_gui.fxml * 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); this.connectButton.setDisable(true);
@NotNull @NotNull
@ -40,52 +35,27 @@ public class ConnectGuiController {
if (this.isAccessible(address, iPort)) { if (this.isAccessible(address, iPort)) {
System.out.println("Connection available, saving file..."); 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 { try {
@NotNull // Save server configuration
final File file = new File("connection.properties"); new ServerConfiguration(address, iPort).save();
properties.store(new FileWriter(file), "Information needed to connect to the server"); Dialog.showSuccessDialog("Connecté", "Vous êtes bien connecté au serveur");
this.showSuccessDialog("Connecté", "Vous êtes bien connecté au serveur");
System.out.println("File saved, next step..."); System.out.println("File saved, next step...");
} catch (IOException e) { } 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); this.connectButton.setDisable(false);
System.out.println("Failed to save file, error: " + e.getMessage()); System.out.println("Failed to save file, error: " + e.getMessage());
} }
} else { } else {
System.out.println("Connection not available"); 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); this.connectButton.setDisable(false);
} }
} else { } 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); 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) { private boolean isNumber(String text) {
try { try {
Integer.parseInt(text); Integer.parseInt(text);

View file

@ -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();
}
}