From 33abd461a580e2e7ea7b197ac80b2ac09520d6c7 Mon Sep 17 00:00:00 2001 From: kske Date: Sun, 7 Jun 2020 17:03:02 +0200 Subject: [PATCH] Add offline mode check box and connection label to login dialog --- .../java/envoy/client/ui/LoginDialog.java | 53 ++++++++++++------- src/main/resources/fxml/LoginDialog.fxml | 4 +- 2 files changed, 37 insertions(+), 20 deletions(-) diff --git a/src/main/java/envoy/client/ui/LoginDialog.java b/src/main/java/envoy/client/ui/LoginDialog.java index 4c52b0c..2968727 100644 --- a/src/main/java/envoy/client/ui/LoginDialog.java +++ b/src/main/java/envoy/client/ui/LoginDialog.java @@ -49,6 +49,12 @@ public final class LoginDialog extends Dialog { @FXML private CheckBox registerCheckBox; + @FXML + private CheckBox offlineCheckBox; + + @FXML + private Label connectionLabel; + private final Client client; private final LocalDB localDB; private final Cache receivedMessageCache; @@ -97,8 +103,12 @@ public final class LoginDialog extends Dialog { 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())); + } else { + final var credentials = new LoginCredentials(userTextField.getText(), passwordField.getText().toCharArray(), + registerCheckBox.isSelected()); + if (!offlineCheckBox.isSelected()) performHandshake(credentials); + else attemptOfflineMode(credentials); + } }); // Perform automatic login if configured @@ -112,6 +122,7 @@ public final class LoginDialog extends Dialog { @FXML private void initialize() { + connectionLabel.setText("Server: " + config.getServer() + ":" + config.getPort()); // Show an alert after an unsuccessful handshake eventBus.register(HandshakeRejectionEvent.class, @@ -128,11 +139,6 @@ public final class LoginDialog extends Dialog { clearPasswordFields(); } - private void abortLogin() { - logger.info("The login process has been cancelled. Exiting..."); - System.exit(0); - } - private void performHandshake(LoginCredentials credentials) { try { client.performHandshake(credentials, receivedMessageCache); @@ -143,18 +149,22 @@ public final class LoginDialog extends Dialog { } catch (IOException | InterruptedException | TimeoutException e) { logger.warning("Could not connect to server: " + e); logger.finer("Attempting offline mode..."); - try { - // Try entering offline mode - localDB.loadUsers(); - 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(); - } catch (Exception e1) { - new Alert(AlertType.ERROR, "Client error: " + e).showAndWait(); - System.exit(1); - } + attemptOfflineMode(credentials); + } + } + + private void attemptOfflineMode(LoginCredentials credentials) { + try { + // Try entering offline mode + localDB.loadUsers(); + 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.").showAndWait(); + hide(); + } catch (Exception e) { + new Alert(AlertType.ERROR, "Client error: " + e).showAndWait(); + System.exit(1); } } @@ -162,4 +172,9 @@ public final class LoginDialog extends Dialog { passwordField.clear(); repeatPasswordField.clear(); } + + private void abortLogin() { + logger.info("The login process has been cancelled. Exiting..."); + System.exit(0); + } } diff --git a/src/main/resources/fxml/LoginDialog.fxml b/src/main/resources/fxml/LoginDialog.fxml index 1d3cc2f..b3dd3e4 100644 --- a/src/main/resources/fxml/LoginDialog.fxml +++ b/src/main/resources/fxml/LoginDialog.fxml @@ -11,7 +11,7 @@ - + @@ -40,6 +40,8 @@ +