Using dialog buttons instead of custom ones
This commit is contained in:
		| @@ -54,19 +54,8 @@ | |||||||
| 							visible="false" GridPane.columnIndex="1" GridPane.rowIndex="2" /> | 							visible="false" GridPane.columnIndex="1" GridPane.rowIndex="2" /> | ||||||
| 					</children> | 					</children> | ||||||
| 				</GridPane> | 				</GridPane> | ||||||
| 				<HBox alignment="CENTER_RIGHT" prefHeight="100.0" | 				<CheckBox fx:id="registerCheckBox" mnemonicParsing="false" | ||||||
| 					prefWidth="200.0"> | 					onAction="#registerCheckboxChanged" text="Register" /> | ||||||
| 					<children> |  | ||||||
| 						<CheckBox fx:id="registerCheckBox" |  | ||||||
| 							mnemonicParsing="false" onAction="#registerCheckboxChanged" |  | ||||||
| 							text="Register" /> |  | ||||||
| 						<Button alignment="CENTER_RIGHT" cancelButton="true" |  | ||||||
| 							mnemonicParsing="false" onAction="#abortLogin" text="Close" /> |  | ||||||
| 						<Button alignment="CENTER_RIGHT" defaultButton="true" |  | ||||||
| 							mnemonicParsing="false" text="Login" |  | ||||||
| 							onAction="#loginButtonClicked" fx:id="loginButton" /> |  | ||||||
| 					</children> |  | ||||||
| 				</HBox> |  | ||||||
| 			</children> | 			</children> | ||||||
| 		</VBox> | 		</VBox> | ||||||
| 	</content> | 	</content> | ||||||
|   | |||||||
| @@ -17,6 +17,7 @@ import envoy.event.HandshakeRejectionEvent; | |||||||
| import envoy.exception.EnvoyException; | import envoy.exception.EnvoyException; | ||||||
| import envoy.util.EnvoyLog; | import envoy.util.EnvoyLog; | ||||||
| import javafx.application.Platform; | import javafx.application.Platform; | ||||||
|  | import javafx.event.ActionEvent; | ||||||
| import javafx.fxml.FXML; | import javafx.fxml.FXML; | ||||||
| import javafx.fxml.FXMLLoader; | import javafx.fxml.FXMLLoader; | ||||||
| import javafx.scene.control.*; | import javafx.scene.control.*; | ||||||
| @@ -47,9 +48,6 @@ public final class LoginDialog extends Dialog<Void> { | |||||||
| 	@FXML | 	@FXML | ||||||
| 	private CheckBox registerCheckBox; | 	private CheckBox registerCheckBox; | ||||||
|  |  | ||||||
| 	@FXML |  | ||||||
| 	private Button loginButton; |  | ||||||
|  |  | ||||||
| 	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; | ||||||
| @@ -75,27 +73,43 @@ public final class LoginDialog extends Dialog<Void> { | |||||||
|  |  | ||||||
| 		final var loader = new FXMLLoader(getClass().getResource("LoginDialog.fxml")); | 		final var loader = new FXMLLoader(getClass().getResource("LoginDialog.fxml")); | ||||||
| 		loader.setController(this); | 		loader.setController(this); | ||||||
| 		setDialogPane(loader.load()); | 		final var dialogPane = loader.<DialogPane>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 | 	@FXML | ||||||
| 	private void initialize() { | 	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 | 		// Show an alert after an unsuccessful handshake | ||||||
| 		eventBus.register(HandshakeRejectionEvent.class, | 		eventBus.register(HandshakeRejectionEvent.class, | ||||||
| 				e -> Platform.runLater(() -> { clearPasswordFields(); new Alert(AlertType.ERROR, e.get()).showAndWait(); })); | 				e -> Platform.runLater(() -> { clearPasswordFields(); new Alert(AlertType.ERROR, e.get()).showAndWait(); })); | ||||||
|  |  | ||||||
| 		// Set initial cursor | 		// Set initial cursor | ||||||
| 		userTextField.requestFocus(); | 		userTextField.requestFocus(); | ||||||
|  |  | ||||||
| 		// Perform automatic login if configured |  | ||||||
| 		if (config.hasLoginCredentials()) { |  | ||||||
| 			performHandshake(config.getLoginCredentials()); |  | ||||||
| 			hide(); |  | ||||||
| 		} |  | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	@FXML | 	@FXML | ||||||
| @@ -114,22 +128,12 @@ public final class LoginDialog extends Dialog<Void> { | |||||||
| 		System.exit(0); | 		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) { | 	private void performHandshake(LoginCredentials credentials) { | ||||||
| 		try { | 		try { | ||||||
| 			client.performHandshake(credentials, receivedMessageCache); | 			client.performHandshake(credentials, receivedMessageCache); | ||||||
| 			if (client.isOnline()) { | 			if (client.isOnline()) { | ||||||
| 				client.initReceiver(localDB, receivedMessageCache); | 				client.initReceiver(localDB, receivedMessageCache); | ||||||
| 				hide(); | 				Platform.runLater(this::hide); | ||||||
| 			} | 			} | ||||||
| 		} catch (IOException | InterruptedException | TimeLimitExceededException e) { | 		} catch (IOException | InterruptedException | TimeLimitExceededException e) { | ||||||
| 			logger.warning("Could not connect to server. Trying offline mode..."); | 			logger.warning("Could not connect to server. Trying offline mode..."); | ||||||
| @@ -140,10 +144,13 @@ public final class LoginDialog extends Dialog<Void> { | |||||||
| 				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(); | 				Platform.runLater(() -> { | ||||||
| 				hide(); | 					new Alert(AlertType.WARNING, "A connection to the server could not be established. Starting in offline mode.\n" + e) | ||||||
|  | 						.showAndWait(); | ||||||
|  | 					hide(); | ||||||
|  | 				}); | ||||||
| 			} catch (Exception e1) { | 			} 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); | 				System.exit(1); | ||||||
| 			} | 			} | ||||||
| 		} | 		} | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user