From fc63ea0a46982019d6d53b4272920cd2af918f49 Mon Sep 17 00:00:00 2001 From: delvh Date: Sat, 1 Aug 2020 10:00:29 +0200 Subject: [PATCH] Added profile pic change mechanism on client and common side --- .../client/ui/settings/UserSettingsPane.java | 44 ++++++++++++++++++- .../java/envoy/event/ProfilePicChange.java | 33 ++++++++++++++ .../processors/ProfilePicChangeProcessor.java | 20 +++++++++ 3 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 common/src/main/java/envoy/event/ProfilePicChange.java create mode 100644 server/src/main/java/envoy/server/processors/ProfilePicChangeProcessor.java diff --git a/client/src/main/java/envoy/client/ui/settings/UserSettingsPane.java b/client/src/main/java/envoy/client/ui/settings/UserSettingsPane.java index 993b6bf..2ea37f4 100644 --- a/client/src/main/java/envoy/client/ui/settings/UserSettingsPane.java +++ b/client/src/main/java/envoy/client/ui/settings/UserSettingsPane.java @@ -1,13 +1,23 @@ package envoy.client.ui.settings; +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; + import javafx.event.EventHandler; +import javafx.scene.Cursor; import javafx.scene.control.Alert; import javafx.scene.control.Alert.AlertType; import javafx.scene.control.TextField; +import javafx.scene.image.Image; +import javafx.scene.image.ImageView; import javafx.scene.input.InputEvent; import javafx.scene.layout.HBox; +import javafx.stage.FileChooser; import envoy.client.event.SendEvent; +import envoy.client.ui.IconUtil; import envoy.client.ui.SceneContext; import envoy.data.User; import envoy.event.*; @@ -35,8 +45,40 @@ public class UserSettingsPane extends SettingsPane { * @since Envoy Client v0.2-beta */ public UserSettingsPane(SceneContext sceneContext, User user) { - super("User"); // Display of profile picture change mechanism + super("User"); + + // Display of profile pic change mechanism final var hbox = new HBox(); + // TODO: display current profile pic + final var profilePic = new ImageView(IconUtil.loadIcon("envoy_logo", 50)); + profilePic.setCursor(Cursor.HAND); + profilePic.setOnMouseClicked(e -> { + final var pictureChooser = new FileChooser(); + + pictureChooser.setTitle("Select a new picture"); + pictureChooser.setInitialDirectory(new File(System.getProperty("user.home"))); + pictureChooser.setSelectedExtensionFilter(new FileChooser.ExtensionFilter("Pictures", "*.png", "*.jpg", "*.bmp", "*.gif")); + + final var file = pictureChooser.showOpenDialog(sceneContext.getStage()); + + if (file != null) { + + // Check max file size + if (file.length() > 5E6) { + new Alert(AlertType.WARNING, "The selected file exceeds the size limit of 5MB!").showAndWait(); + return; + } + + try { + currentImageBytes = Files.readAllBytes(file.toPath()); + profilePic.setImage(new Image(new ByteArrayInputStream(currentImageBytes))); + profilePicChanged = true; + } catch (final IOException e1) { + e1.printStackTrace(); + } + } + }); + hbox.getChildren().add(profilePic); // Displaying the username change mechanism final var username = user.getName(); diff --git a/common/src/main/java/envoy/event/ProfilePicChange.java b/common/src/main/java/envoy/event/ProfilePicChange.java new file mode 100644 index 0000000..52c5941 --- /dev/null +++ b/common/src/main/java/envoy/event/ProfilePicChange.java @@ -0,0 +1,33 @@ +package envoy.event; + +/** + * Project: envoy-common
+ * File: ProfilePicChange.java
+ * Created: 31.07.2020
+ * + * @author Leon Hofmeister + * @since Envoy Common v0.2-beta + */ +public class ProfilePicChange extends Event { + + private final long id; + + private static final long serialVersionUID = 0L; + + /** + * @param value the byte[] of the new image + * @param userID the ID of the user who changed his profile pic + * @since Envoy Common v0.2-beta + */ + public ProfilePicChange(byte[] value, long userID) { + super(value); + id = userID; + } + + /** + * @return the id + * @since Envoy Common v0.2-beta + */ + public long getId() { return id; } + +} diff --git a/server/src/main/java/envoy/server/processors/ProfilePicChangeProcessor.java b/server/src/main/java/envoy/server/processors/ProfilePicChangeProcessor.java new file mode 100644 index 0000000..10069a8 --- /dev/null +++ b/server/src/main/java/envoy/server/processors/ProfilePicChangeProcessor.java @@ -0,0 +1,20 @@ +package envoy.server.processors; + +import java.io.IOException; + +import envoy.event.ProfilePicChange; +import envoy.server.net.ObjectWriteProxy; + +/** + * Project: envoy-server-standalone
+ * File: ProfilePicChangeProcessor.java
+ * Created: 01.08.2020
+ * + * @author Leon Hofmeister + * @since Envoy Server v0.2-beta + */ +public class ProfilePicChangeProcessor implements ObjectProcessor { + + @Override + public void process(ProfilePicChange event, long socketID, ObjectWriteProxy writeProxy) throws IOException {} +}