123 lines
3.5 KiB
Java
123 lines
3.5 KiB
Java
package envoy.client.ui.controller;
|
|
|
|
import java.util.logging.Level;
|
|
import java.util.logging.Logger;
|
|
|
|
import javafx.application.Platform;
|
|
import javafx.fxml.FXML;
|
|
import javafx.geometry.Insets;
|
|
import javafx.scene.control.*;
|
|
import javafx.scene.control.Alert.AlertType;
|
|
import javafx.scene.image.ImageView;
|
|
|
|
import envoy.client.data.ClientConfig;
|
|
import envoy.client.ui.IconUtil;
|
|
import envoy.client.ui.Startup;
|
|
import envoy.data.LoginCredentials;
|
|
import envoy.event.EventBus;
|
|
import envoy.event.HandshakeRejection;
|
|
import envoy.util.Bounds;
|
|
import envoy.util.EnvoyLog;
|
|
|
|
/**
|
|
* Project: <strong>envoy-client</strong><br>
|
|
* File: <strong>LoginDialog.java</strong><br>
|
|
* Created: <strong>03.04.2020</strong><br>
|
|
*
|
|
* @author Kai S. K. Engelbart
|
|
* @author Maximilian Käfer
|
|
* @since Envoy Client v0.1-beta
|
|
*/
|
|
public final class LoginScene {
|
|
|
|
@FXML
|
|
private TextField userTextField;
|
|
|
|
@FXML
|
|
private PasswordField passwordField;
|
|
|
|
@FXML
|
|
private PasswordField repeatPasswordField;
|
|
|
|
@FXML
|
|
private Button registerSwitch;
|
|
|
|
@FXML
|
|
private Label connectionLabel;
|
|
|
|
@FXML
|
|
private Button loginButton;
|
|
|
|
@FXML
|
|
private Button offlineModeButton;
|
|
|
|
@FXML
|
|
private Label registerTextLabel;
|
|
|
|
@FXML
|
|
private ImageView logo;
|
|
|
|
private boolean registration = false;
|
|
|
|
private static final Logger logger = EnvoyLog.getLogger(LoginScene.class);
|
|
private static final EventBus eventBus = EventBus.getInstance();
|
|
private static final ClientConfig config = ClientConfig.getInstance();
|
|
|
|
@FXML
|
|
private void initialize() {
|
|
connectionLabel.setText("Server: " + config.getServer() + ":" + config.getPort());
|
|
|
|
// Show an alert after an unsuccessful handshake
|
|
eventBus.register(HandshakeRejection.class, e -> Platform.runLater(() -> { new Alert(AlertType.ERROR, e.get()).showAndWait(); }));
|
|
|
|
logo.setImage(IconUtil.loadIcon("envoy_logo"));
|
|
|
|
// Set initial cursor
|
|
userTextField.requestFocus();
|
|
}
|
|
|
|
@FXML
|
|
private void loginButtonPressed() {
|
|
|
|
// Prevent registration with unequal passwords
|
|
if (registration && !passwordField.getText().equals(repeatPasswordField.getText())) {
|
|
new Alert(AlertType.ERROR, "The entered password is unequal to the repeated one").showAndWait();
|
|
repeatPasswordField.clear();
|
|
} else if (!Bounds.isValidContactName(userTextField.getText())) {
|
|
new Alert(AlertType.ERROR, "The entered user name is not valid (" + Bounds.CONTACT_NAME_PATTERN + ")").showAndWait();
|
|
userTextField.clear();
|
|
} else Startup.performHandshake(new LoginCredentials(userTextField.getText(), passwordField.getText(), registration, Startup.VERSION,
|
|
Startup.loadLastSync(userTextField.getText())));
|
|
}
|
|
|
|
@FXML
|
|
private void offlineModeButtonPressed() { Startup.attemptOfflineMode(userTextField.getText()); }
|
|
|
|
@FXML
|
|
private void registerSwitchPressed() {
|
|
if (!registration) {
|
|
// case if the current mode is login
|
|
loginButton.setText("Register");
|
|
loginButton.setPadding(new Insets(2, 116, 2, 116));
|
|
registerTextLabel.setText("Already an account?");
|
|
registerSwitch.setText("Login");
|
|
} else {
|
|
// case if the current mode is registration
|
|
loginButton.setText("Login");
|
|
loginButton.setPadding(new Insets(2, 125, 2, 125));
|
|
registerTextLabel.setText("No account yet?");
|
|
registerSwitch.setText("Register");
|
|
}
|
|
registration = !registration;
|
|
// Make repeat password field and label visible / invisible
|
|
repeatPasswordField.setVisible(registration);
|
|
offlineModeButton.setDisable(registration);
|
|
}
|
|
|
|
@FXML
|
|
private void abortLogin() {
|
|
logger.log(Level.INFO, "The login process has been cancelled. Exiting...");
|
|
System.exit(0);
|
|
}
|
|
}
|