Add attachment creation support, display picture messages

This commit is contained in:
Kai S. K. Engelbart 2020-07-09 16:05:09 +02:00
parent 7c5cc2f220
commit e67665f0bd
3 changed files with 62 additions and 6 deletions

View File

@ -2,7 +2,9 @@ package envoy.client.ui.controller;
import java.awt.Toolkit; import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection; import java.awt.datatransfer.StringSelection;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -16,6 +18,7 @@ import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode; import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent; import javafx.scene.input.KeyEvent;
import javafx.scene.paint.Color; import javafx.scene.paint.Color;
import javafx.stage.FileChooser;
import envoy.client.data.*; import envoy.client.data.*;
import envoy.client.data.audio.AudioRecorder; import envoy.client.data.audio.AudioRecorder;
@ -60,6 +63,9 @@ public final class ChatScene implements Restorable {
@FXML @FXML
private Button voiceButton; private Button voiceButton;
@FXML
private Button attachmentButton;
@FXML @FXML
private Button settingsButton; private Button settingsButton;
@ -234,6 +240,7 @@ public final class ChatScene implements Restorable {
} }
messageTextArea.setDisable(currentChat == null || postingPermanentlyDisabled); messageTextArea.setDisable(currentChat == null || postingPermanentlyDisabled);
voiceButton.setDisable(!recorder.isSupported()); voiceButton.setDisable(!recorder.isSupported());
attachmentButton.setDisable(false);
} }
/** /**
@ -287,6 +294,48 @@ public final class ChatScene implements Restorable {
}).start(); }).start();
} }
@FXML
private void attachmentButtonClicked() {
// Display file chooser
final var fileChooser = new FileChooser();
fileChooser.setTitle("Add Attachment");
fileChooser.setInitialDirectory(new File(System.getProperty("user.home")));
fileChooser.getExtensionFilters()
.addAll(new FileChooser.ExtensionFilter("Pictures", "*.png", "*.jpg"),
new FileChooser.ExtensionFilter("Videos", "*.mp4"),
new FileChooser.ExtensionFilter("All Files", "*.*"));
final var file = fileChooser.showOpenDialog(sceneContext.getStage());
if(file != null) {
// Check max file size
if (file.length() > 16E6) {
new Alert(AlertType.WARNING, "The selected file exceeds the size limit of 16MB!").showAndWait();
return;
}
// Get attachment type (default is document)
AttachmentType type = AttachmentType.DOCUMENT;
switch (fileChooser.getSelectedExtensionFilter().getDescription()) {
case "Pictures":
type = AttachmentType.PICTURE;
break;
case "Videos":
type = AttachmentType.VIDEO;
break;
}
// Create the pending attachment
try {
pendingAttachment = new Attachment(Files.readAllBytes(file.toPath()), type);
attachmentView.setVisible(true);
} catch (IOException e) {
new Alert(AlertType.ERROR, "The selected file could not be loaded!").showAndWait();
}
}
}
/** /**
* Checks the text length of the {@code messageTextArea}, adjusts the * Checks the text length of the {@code messageTextArea}, adjusts the
* {@code remainingChars} label and checks whether to send the message * {@code remainingChars} label and checks whether to send the message
@ -373,16 +422,18 @@ public final class ChatScene implements Restorable {
final var text = messageTextArea.getText().strip(); final var text = messageTextArea.getText().strip();
try { try {
// Creating the message and its metadata // 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); .setText(text);
// Setting an attachment, if present // Setting an attachment, if present
if (pendingAttachment != null) { if (pendingAttachment != null) {
builder.setAttachment(pendingAttachment); builder.setAttachment(pendingAttachment);
pendingAttachment = null; pendingAttachment = null;
attachmentView.setVisible(false); attachmentView.setVisible(false);
} }
// Building the final message // 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(); : builder.build();
// Send message // Send message
@ -429,7 +480,7 @@ public final class ChatScene implements Restorable {
} }
// Context menu actions // Context menu actions
@FXML @FXML
private void deleteContact() { try {} catch (final NullPointerException e) {} } private void deleteContact() { try {} catch (final NullPointerException e) {} }

View File

@ -2,6 +2,7 @@ package envoy.client.ui.listcell;
import java.awt.Toolkit; import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection; import java.awt.datatransfer.StringSelection;
import java.io.ByteArrayInputStream;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.Map; import java.util.Map;
import java.util.logging.Level; import java.util.logging.Level;
@ -67,6 +68,7 @@ public class MessageControl extends Label {
if (message.hasAttachment()) { if (message.hasAttachment()) {
switch (message.getAttachment().getType()) { switch (message.getAttachment().getType()) {
case PICTURE: case PICTURE:
vbox.getChildren().add(new ImageView(new Image(new ByteArrayInputStream(message.getAttachment().getData()))));
break; break;
case VIDEO: case VIDEO:
break; break;

View File

@ -100,6 +100,9 @@
<Insets right="10.0" /> <Insets right="10.0" />
</GridPane.margin> </GridPane.margin>
<buttons> <buttons>
<Button fx:id="attachmentButton" disable="true"
mnemonicParsing="false" onAction="#attachmentButtonClicked"
text="Add Attachment" />
<Button fx:id="voiceButton" disable="true" <Button fx:id="voiceButton" disable="true"
onAction="#voiceButtonClicked"> onAction="#voiceButtonClicked">
<padding> <padding>