From 5bd80a2ebc69f685ccd89db415687047c9db2c1f Mon Sep 17 00:00:00 2001 From: delvh Date: Thu, 9 Jul 2020 22:47:29 +0200 Subject: [PATCH] Added maximum size for displaying sent images additionally: - made selecting of bmp or gif images possible - defined a DEFAULT_ICON_SIZE in which most icons are loaded - displayed attachment button with attachment icon --- .../envoy/client/ui/controller/ChatScene.java | 26 +++++++++---------- .../client/ui/listcell/MessageControl.java | 6 ++--- src/main/resources/fxml/ChatScene.fxml | 3 +-- 3 files changed, 16 insertions(+), 19 deletions(-) diff --git a/src/main/java/envoy/client/ui/controller/ChatScene.java b/src/main/java/envoy/client/ui/controller/ChatScene.java index 09cbdfd..2c2b5eb 100644 --- a/src/main/java/envoy/client/ui/controller/ChatScene.java +++ b/src/main/java/envoy/client/ui/controller/ChatScene.java @@ -93,12 +93,13 @@ public final class ChatScene implements Restorable { private AudioRecorder recorder; private boolean recording; private Attachment pendingAttachment; - private boolean postingPermanentlyDisabled = false; + private boolean postingPermanentlyDisabled; private static final Settings settings = Settings.getInstance(); private static final EventBus eventBus = EventBus.getInstance(); private static final Logger logger = EnvoyLog.getLogger(ChatScene.class); private static final int MAX_MESSAGE_LENGTH = 255; + private static final int DEFAULT_ICON_SIZE = 16; /** * Initializes the appearance of certain visual components. @@ -112,8 +113,10 @@ public final class ChatScene implements Restorable { messageList.setCellFactory(MessageListCellFactory::new); userList.setCellFactory(ContactListCellFactory::new); - settingsButton.setGraphic(new ImageView(IconUtil.loadIconThemeSensitive("settings", 16))); - voiceButton.setGraphic(new ImageView(IconUtil.loadIconThemeSensitive("microphone", 20))); + settingsButton.setGraphic(new ImageView(IconUtil.loadIconThemeSensitive("settings", DEFAULT_ICON_SIZE))); + voiceButton.setGraphic(new ImageView(IconUtil.loadIconThemeSensitive("microphone", DEFAULT_ICON_SIZE))); + attachmentButton.setGraphic(new ImageView(IconUtil.loadIconThemeSensitive("attachment", DEFAULT_ICON_SIZE))); + attachmentView.setImage(IconUtil.loadIconThemeSensitive("attachment_present", 20)); // Listen to received messages eventBus.register(MessageCreationEvent.class, e -> { @@ -273,17 +276,16 @@ public final class ChatScene implements Restorable { recording = true; Platform.runLater(() -> { voiceButton.setText("Recording"); - voiceButton.setGraphic(new ImageView(IconUtil.loadIcon("microphone_recording", 24))); + voiceButton.setGraphic(new ImageView(IconUtil.loadIcon("microphone_recording", DEFAULT_ICON_SIZE))); }); recorder.start(); } else { pendingAttachment = new Attachment(recorder.finish(), AttachmentType.VOICE); recording = false; Platform.runLater(() -> { - voiceButton.setGraphic(new ImageView(IconUtil.loadIconThemeSensitive("microphone", 20))); + voiceButton.setGraphic(new ImageView(IconUtil.loadIconThemeSensitive("microphone", DEFAULT_ICON_SIZE))); voiceButton.setText(null); checkPostConditions(false); - attachmentView.setImage(IconUtil.loadIconThemeSensitive("attachment_present", 20)); attachmentView.setVisible(true); }); } @@ -302,12 +304,12 @@ public final class ChatScene implements Restorable { fileChooser.setTitle("Add Attachment"); fileChooser.setInitialDirectory(new File(System.getProperty("user.home"))); fileChooser.getExtensionFilters() - .addAll(new FileChooser.ExtensionFilter("Pictures", "*.png", "*.jpg"), + .addAll(new FileChooser.ExtensionFilter("Pictures", "*.png", "*.jpg", "*.bmp", "*.gif"), new FileChooser.ExtensionFilter("Videos", "*.mp4"), new FileChooser.ExtensionFilter("All Files", "*.*")); final var file = fileChooser.showOpenDialog(sceneContext.getStage()); - if(file != null) { + if (file != null) { // Check max file size if (file.length() > 16E6) { @@ -330,7 +332,7 @@ public final class ChatScene implements Restorable { try { pendingAttachment = new Attachment(Files.readAllBytes(file.toPath()), type); attachmentView.setVisible(true); - } catch (IOException e) { + } catch (final IOException e) { new Alert(AlertType.ERROR, "The selected file could not be loaded!").showAndWait(); } } @@ -422,8 +424,7 @@ public final class ChatScene implements Restorable { final var text = messageTextArea.getText().strip(); try { // Creating the message and its metadata - final var builder = new MessageBuilder(localDB.getUser().getID(), currentChat.getRecipient().getID(), localDB - .getIDGenerator()) + final var builder = new MessageBuilder(localDB.getUser().getID(), currentChat.getRecipient().getID(), localDB.getIDGenerator()) .setText(text); // Setting an attachment, if present if (pendingAttachment != null) { @@ -432,8 +433,7 @@ public final class ChatScene implements Restorable { attachmentView.setVisible(false); } // Building the final message - final var message = currentChat.getRecipient() instanceof Group ? builder.buildGroupMessage((Group) currentChat - .getRecipient()) + final var message = currentChat.getRecipient() instanceof Group ? builder.buildGroupMessage((Group) currentChat.getRecipient()) : builder.build(); // Send message diff --git a/src/main/java/envoy/client/ui/listcell/MessageControl.java b/src/main/java/envoy/client/ui/listcell/MessageControl.java index 03cb937..8b7115b 100644 --- a/src/main/java/envoy/client/ui/listcell/MessageControl.java +++ b/src/main/java/envoy/client/ui/listcell/MessageControl.java @@ -68,7 +68,7 @@ public class MessageControl extends Label { if (message.hasAttachment()) { switch (message.getAttachment().getType()) { case PICTURE: - vbox.getChildren().add(new ImageView(new Image(new ByteArrayInputStream(message.getAttachment().getData())))); + vbox.getChildren().add(new ImageView(new Image(new ByteArrayInputStream(message.getAttachment().getData()), 256, 256, true, true))); break; case VIDEO: break; @@ -92,9 +92,7 @@ public class MessageControl extends Label { statusIcon.setPreserveRatio(true); vbox.getChildren().add(statusIcon); getStyleClass().add("own-message"); - } else { - getStyleClass().add("received-message"); - } + } else getStyleClass().add("received-message"); // Adjusting height and weight of the cell to the corresponding ListView paddingProperty().setValue(new Insets(5, 20, 5, 20)); setContextMenu(contextMenu); diff --git a/src/main/resources/fxml/ChatScene.fxml b/src/main/resources/fxml/ChatScene.fxml index 21ded83..7918c4a 100644 --- a/src/main/resources/fxml/ChatScene.fxml +++ b/src/main/resources/fxml/ChatScene.fxml @@ -101,8 +101,7 @@