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 | 	@FXML | ||||||
| 	private CheckBox registerCheckBox; | 	private CheckBox registerCheckBox; | ||||||
|  |  | ||||||
|  | 	@FXML | ||||||
|  | 	private CheckBox offlineCheckBox; | ||||||
|  |  | ||||||
|  | 	@FXML | ||||||
|  | 	private Label connectionLabel; | ||||||
|  |  | ||||||
| 	private final Client			client; | 	private final Client			client; | ||||||
| 	private final LocalDB			localDB; | 	private final LocalDB			localDB; | ||||||
| 	private final Cache<Message>	receivedMessageCache; | 	private final Cache<Message>	receivedMessageCache; | ||||||
| @@ -97,8 +103,12 @@ public final class LoginDialog extends Dialog<Void> { | |||||||
| 			if (registerCheckBox.isSelected() && !passwordField.getText().equals(repeatPasswordField.getText())) { | 			if (registerCheckBox.isSelected() && !passwordField.getText().equals(repeatPasswordField.getText())) { | ||||||
| 				clearPasswordFields(); | 				clearPasswordFields(); | ||||||
| 				new Alert(AlertType.ERROR, "The entered password is unequal to the repeated one").showAndWait(); | 				new Alert(AlertType.ERROR, "The entered password is unequal to the repeated one").showAndWait(); | ||||||
| 			} else | 			} else { | ||||||
| 				performHandshake(new LoginCredentials(userTextField.getText(), passwordField.getText().toCharArray(), registerCheckBox.isSelected())); | 				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 | 		// Perform automatic login if configured | ||||||
| @@ -112,6 +122,7 @@ public final class LoginDialog extends Dialog<Void> { | |||||||
|  |  | ||||||
| 	@FXML | 	@FXML | ||||||
| 	private void initialize() { | 	private void initialize() { | ||||||
|  | 		connectionLabel.setText("Server: " + config.getServer() + ":" + config.getPort()); | ||||||
|  |  | ||||||
| 		// Show an alert after an unsuccessful handshake | 		// Show an alert after an unsuccessful handshake | ||||||
| 		eventBus.register(HandshakeRejectionEvent.class, | 		eventBus.register(HandshakeRejectionEvent.class, | ||||||
| @@ -128,11 +139,6 @@ public final class LoginDialog extends Dialog<Void> { | |||||||
| 		clearPasswordFields(); | 		clearPasswordFields(); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	private void abortLogin() { |  | ||||||
| 		logger.info("The login process has been cancelled. Exiting..."); |  | ||||||
| 		System.exit(0); |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	private void performHandshake(LoginCredentials credentials) { | 	private void performHandshake(LoginCredentials credentials) { | ||||||
| 		try { | 		try { | ||||||
| 			client.performHandshake(credentials, receivedMessageCache); | 			client.performHandshake(credentials, receivedMessageCache); | ||||||
| @@ -143,23 +149,32 @@ public final class LoginDialog extends Dialog<Void> { | |||||||
| 		} catch (IOException | InterruptedException | TimeoutException e) { | 		} catch (IOException | InterruptedException | TimeoutException e) { | ||||||
| 			logger.warning("Could not connect to server: " + e); | 			logger.warning("Could not connect to server: " + e); | ||||||
| 			logger.finer("Attempting offline mode..."); | 			logger.finer("Attempting offline mode..."); | ||||||
|  | 			attemptOfflineMode(credentials); | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	private void attemptOfflineMode(LoginCredentials credentials) { | ||||||
| 		try { | 		try { | ||||||
| 			// Try entering offline mode | 			// Try entering offline mode | ||||||
| 			localDB.loadUsers(); | 			localDB.loadUsers(); | ||||||
| 			User clientUser = (User) localDB.getUsers().get(credentials.getIdentifier()); | 			User clientUser = (User) localDB.getUsers().get(credentials.getIdentifier()); | ||||||
| 			if (clientUser == null) throw new EnvoyException("Could not enter offline mode: user name unknown"); | 			if (clientUser == null) throw new EnvoyException("Could not enter offline mode: user name unknown"); | ||||||
| 			client.setSender(clientUser); | 			client.setSender(clientUser); | ||||||
| 				new Alert(AlertType.WARNING, "A connection to the server could not be established. Starting in offline mode.\n" + e).showAndWait(); | 			new Alert(AlertType.WARNING, "A connection to the server could not be established. Starting in offline mode.").showAndWait(); | ||||||
| 			hide(); | 			hide(); | ||||||
| 			} catch (Exception e1) { | 		} catch (Exception e) { | ||||||
| 			new Alert(AlertType.ERROR, "Client error: " + e).showAndWait(); | 			new Alert(AlertType.ERROR, "Client error: " + e).showAndWait(); | ||||||
| 			System.exit(1); | 			System.exit(1); | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	private void clearPasswordFields() { | 	private void clearPasswordFields() { | ||||||
| 		passwordField.clear(); | 		passwordField.clear(); | ||||||
| 		repeatPasswordField.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.layout.VBox?> | ||||||
| <?import javafx.scene.text.Font?> | <?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> | 	<content> | ||||||
| 		<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="217.0" prefWidth="545.0"> | 		<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="217.0" prefWidth="545.0"> | ||||||
| 			<children> | 			<children> | ||||||
| @@ -40,6 +40,8 @@ | |||||||
| 					</children> | 					</children> | ||||||
| 				</GridPane> | 				</GridPane> | ||||||
| 				<CheckBox fx:id="registerCheckBox" mnemonicParsing="false" onAction="#registerCheckboxChanged" prefHeight="17.0" prefWidth="181.0" text="Register" /> | 				<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> | 			</children> | ||||||
| 		</VBox> | 		</VBox> | ||||||
| 	</content> | 	</content> | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user