Using dialog buttons instead of custom ones
This commit is contained in:
parent
647aae72c6
commit
a0004e53c6
@ -54,19 +54,8 @@
|
||||
visible="false" GridPane.columnIndex="1" GridPane.rowIndex="2" />
|
||||
</children>
|
||||
</GridPane>
|
||||
<HBox alignment="CENTER_RIGHT" prefHeight="100.0"
|
||||
prefWidth="200.0">
|
||||
<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>
|
||||
<CheckBox fx:id="registerCheckBox" mnemonicParsing="false"
|
||||
onAction="#registerCheckboxChanged" text="Register" />
|
||||
</children>
|
||||
</VBox>
|
||||
</content>
|
||||
|
@ -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<Void> {
|
||||
@FXML
|
||||
private CheckBox registerCheckBox;
|
||||
|
||||
@FXML
|
||||
private Button loginButton;
|
||||
|
||||
private final Client client;
|
||||
private final LocalDB localDB;
|
||||
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"));
|
||||
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
|
||||
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<Void> {
|
||||
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<Void> {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user