Add offline mode check box and connection label to login dialog
This commit is contained in:
		@@ -49,6 +49,12 @@ public final class LoginDialog extends Dialog<Void> {
 | 
			
		||||
	@FXML
 | 
			
		||||
	private CheckBox registerCheckBox;
 | 
			
		||||
 | 
			
		||||
	@FXML
 | 
			
		||||
	private CheckBox offlineCheckBox;
 | 
			
		||||
 | 
			
		||||
	@FXML
 | 
			
		||||
	private Label connectionLabel;
 | 
			
		||||
 | 
			
		||||
	private final Client			client;
 | 
			
		||||
	private final LocalDB			localDB;
 | 
			
		||||
	private final Cache<Message>	receivedMessageCache;
 | 
			
		||||
@@ -97,8 +103,12 @@ public final class LoginDialog extends Dialog<Void> {
 | 
			
		||||
			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<Void> {
 | 
			
		||||
 | 
			
		||||
	@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<Void> {
 | 
			
		||||
		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<Void> {
 | 
			
		||||
		} 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<Void> {
 | 
			
		||||
		passwordField.clear();
 | 
			
		||||
		repeatPasswordField.clear();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	private void abortLogin() {
 | 
			
		||||
		logger.info("The login process has been cancelled. Exiting...");
 | 
			
		||||
		System.exit(0);
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -11,7 +11,7 @@
 | 
			
		||||
<?import javafx.scene.layout.VBox?>
 | 
			
		||||
<?import javafx.scene.text.Font?>
 | 
			
		||||
 | 
			
		||||
<DialogPane prefHeight="201.0" prefWidth="525.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1">
 | 
			
		||||
<DialogPane prefHeight="265.0" prefWidth="545.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1">
 | 
			
		||||
	<content>
 | 
			
		||||
		<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="217.0" prefWidth="545.0">
 | 
			
		||||
			<children>
 | 
			
		||||
@@ -40,6 +40,8 @@
 | 
			
		||||
					</children>
 | 
			
		||||
				</GridPane>
 | 
			
		||||
				<CheckBox fx:id="registerCheckBox" mnemonicParsing="false" onAction="#registerCheckboxChanged" prefHeight="17.0" prefWidth="181.0" text="Register" />
 | 
			
		||||
            <Label fx:id="connectionLabel" />
 | 
			
		||||
            <CheckBox fx:id="offlineCheckBox" layoutX="20.0" layoutY="144.0" mnemonicParsing="false" prefHeight="17.0" prefWidth="181.0" text="Offline mode" />
 | 
			
		||||
			</children>
 | 
			
		||||
		</VBox>
 | 
			
		||||
	</content>
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user