Reformat server configuration
This commit is contained in:
parent
a9215ccb9a
commit
bb100489dc
5 changed files with 80 additions and 44 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,2 +1,3 @@
|
|||
.idea/
|
||||
/target/
|
||||
/connection.properties
|
||||
|
|
37
src/fr/univ/lyon1/common/ServerConfiguration.java
Normal file
37
src/fr/univ/lyon1/common/ServerConfiguration.java
Normal 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;
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
24
src/fr/univ/lyon1/gui/controller/Dialog.java
Normal file
24
src/fr/univ/lyon1/gui/controller/Dialog.java
Normal 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();
|
||||
}
|
||||
}
|
Reference in a new issue