diff --git a/src/main/java/envoy/client/ui/LoginDialog.fxml b/src/main/java/envoy/client/ui/LoginDialog.fxml
index 9a4c8a0..a1503f4 100644
--- a/src/main/java/envoy/client/ui/LoginDialog.fxml
+++ b/src/main/java/envoy/client/ui/LoginDialog.fxml
@@ -54,19 +54,8 @@
visible="false" GridPane.columnIndex="1" GridPane.rowIndex="2" />
-
-
-
-
-
-
-
+
diff --git a/src/main/java/envoy/client/ui/LoginDialog.java b/src/main/java/envoy/client/ui/LoginDialog.java
index ad748ff..5ef2ed9 100644
--- a/src/main/java/envoy/client/ui/LoginDialog.java
+++ b/src/main/java/envoy/client/ui/LoginDialog.java
@@ -17,6 +17,7 @@ import envoy.event.HandshakeRejectionEvent;
import envoy.exception.EnvoyException;
import envoy.util.EnvoyLog;
import javafx.application.Platform;
+import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.*;
@@ -47,9 +48,6 @@ public final class LoginDialog extends Dialog {
@FXML
private CheckBox registerCheckBox;
- @FXML
- private Button loginButton;
-
private final Client client;
private final LocalDB localDB;
private final Cache receivedMessageCache;
@@ -75,27 +73,43 @@ public final class LoginDialog extends Dialog {
final var loader = new FXMLLoader(getClass().getResource("LoginDialog.fxml"));
loader.setController(this);
- setDialogPane(loader.load());
+ final var dialogPane = loader.load();
+
+ // Configure dialog buttons
+ dialogPane.getButtonTypes().addAll(ButtonType.CLOSE, ButtonType.OK);
+
+ // Close button
+ dialogPane.lookupButton(ButtonType.CLOSE).addEventHandler(ActionEvent.ACTION, e -> abortLogin());
+
+ // Login button
+ final var loginButton = (Button) dialogPane.lookupButton(ButtonType.OK);
+ loginButton.setText("Login");
+ loginButton.addEventFilter(ActionEvent.ACTION, e -> {
+ e.consume();
+
+ // Prevent registration with unequal passwords
+ if (registerCheckBox.isSelected() && !passwordField.getText().equals(repeatPasswordField.getText())) {
+ clearPasswordFields();
+ new Alert(AlertType.ERROR, "The entered password is unequal to the repeated one").showAndWait();
+ } else
+ performHandshake(new LoginCredentials(userTextField.getText(), passwordField.getText().toCharArray(), registerCheckBox.isSelected()));
+ });
+
+ // Perform automatic login if configured
+ setOnShown(e -> { if (config.hasLoginCredentials()) performHandshake(config.getLoginCredentials()); });
+
+ setDialogPane(dialogPane);
}
@FXML
private void initialize() {
- // Close the dialog when the user exits the dialog
- getDialogPane().getScene().getWindow().setOnCloseRequest(e -> abortLogin());
-
// Show an alert after an unsuccessful handshake
eventBus.register(HandshakeRejectionEvent.class,
e -> Platform.runLater(() -> { clearPasswordFields(); new Alert(AlertType.ERROR, e.get()).showAndWait(); }));
// Set initial cursor
userTextField.requestFocus();
-
- // Perform automatic login if configured
- if (config.hasLoginCredentials()) {
- performHandshake(config.getLoginCredentials());
- hide();
- }
}
@FXML
@@ -114,22 +128,12 @@ public final class LoginDialog extends Dialog {
System.exit(0);
}
- @FXML
- private void loginButtonClicked() {
-
- // Prevent registration with unequal passwords
- if (registerCheckBox.isSelected() && !passwordField.getText().equals(repeatPasswordField.getText())) {
- clearPasswordFields();
- new Alert(AlertType.ERROR, "The entered password is unequal to the repeated one").showAndWait();
- } else performHandshake(new LoginCredentials(userTextField.getText(), passwordField.getText().toCharArray(), registerCheckBox.isSelected()));
- }
-
private void performHandshake(LoginCredentials credentials) {
try {
client.performHandshake(credentials, receivedMessageCache);
if (client.isOnline()) {
client.initReceiver(localDB, receivedMessageCache);
- hide();
+ Platform.runLater(this::hide);
}
} catch (IOException | InterruptedException | TimeLimitExceededException e) {
logger.warning("Could not connect to server. Trying offline mode...");
@@ -140,10 +144,13 @@ public final class LoginDialog extends Dialog {
User clientUser = (User) localDB.getUsers().get(credentials.getIdentifier());
if (clientUser == null) throw new EnvoyException("Could not enter offline mode: user name unknown");
client.setSender(clientUser);
- new Alert(AlertType.WARNING, "A connection to the server could not be established. Starting in offline mode.\n" + e).showAndWait();
- hide();
+ Platform.runLater(() -> {
+ new Alert(AlertType.WARNING, "A connection to the server could not be established. Starting in offline mode.\n" + e)
+ .showAndWait();
+ hide();
+ });
} catch (Exception e1) {
- new Alert(AlertType.ERROR, "Client error: " + e.toString()).showAndWait();
+ Platform.runLater(() -> new Alert(AlertType.ERROR, "Client error: " + e.toString()).showAndWait());
System.exit(1);
}
}