This repository has been archived on 2021-12-05. You can view files and clone it, but cannot push or open issues or pull requests.
envoy/client/src/main/java/envoy/client/ui/settings/UserSettingsPane.java

88 lines
3.0 KiB
Java

package envoy.client.ui.settings;
import javafx.event.EventHandler;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.TextField;
import javafx.scene.input.InputEvent;
import javafx.scene.layout.HBox;
import envoy.client.event.SendEvent;
import envoy.client.ui.SceneContext;
import envoy.data.User;
import envoy.event.*;
import envoy.util.Bounds;
/**
* Project: <strong>envoy-client</strong><br>
* File: <strong>UserSettingsPane.java</strong><br>
* Created: <strong>31.07.2020</strong><br>
*
* @author Leon Hofmeister
* @since Envoy Client v0.2-beta
*/
public class UserSettingsPane extends SettingsPane {
private boolean profilePicChanged, usernameChanged, passwordChanged, validPassword;
private byte[] currentImageBytes;
private String newUsername, newPassword;
/**
* Creates a new {@code UserSettingsPane}.
*
* @param sceneContext the {@code SceneContext} to block input to Envoy
* @param user the user who wants to customize his profile
* @since Envoy Client v0.2-beta
*/
public UserSettingsPane(SceneContext sceneContext, User user) {
super("User"); // Display of profile picture change mechanism
final var hbox = new HBox();
// Displaying the username change mechanism
final var username = user.getName();
final var usernameTextField = new TextField(username);
final EventHandler<? super InputEvent> textChanged = e -> {
newUsername = usernameTextField.getText();
usernameChanged = newUsername != username;
};
usernameTextField.setOnInputMethodTextChanged(textChanged);
usernameTextField.setOnKeyTyped(textChanged);
hbox.getChildren().add(usernameTextField);
vbox.getChildren().add(hbox);
}
/**
* Saves the given input and sends the changed input to the server
*
* @param username the new username
* @since Envoy Client v0.2-beta
*/
private void save(long userID, String oldPassword) {
final var eventbus = EventBus.getInstance();
// The profile pic was changed
if (profilePicChanged) eventbus.dispatch(new SendEvent(new ProfilePicChange(currentImageBytes, userID)));
// The username was changed
final var validContactName = Bounds.isValidContactName(newUsername);
if (usernameChanged && validContactName) eventbus.dispatch(new SendEvent(new NameChange(userID, newUsername)));
else if (!validContactName) {
final var alert = new Alert(AlertType.ERROR);
alert.setTitle("Invalid username");
alert.setContentText("The entered username does not conform with the naming limitations: " + Bounds.CONTACT_NAME_PATTERN);
alert.showAndWait();
return;
}
// The password was changed
if (passwordChanged && validPassword) eventbus.dispatch(new SendEvent(new PasswordChangeRequest(newPassword, oldPassword, userID)));
else if (!(validPassword || newPassword != null && newPassword.isBlank())) {
final var alert = new Alert(AlertType.ERROR);
alert.setTitle("Unequal Password");
alert.setContentText("Repeated password is unequal to the chosen new password");
alert.showAndWait();
return;
}
}
}