From 819f0ec4f24a6bcdcb4f004f59a0d8c8e82b743c Mon Sep 17 00:00:00 2001 From: delvh Date: Sat, 14 Mar 2020 22:44:03 +0100 Subject: [PATCH] Implemented method to forward a message --- src/main/java/envoy/client/ui/ChatWindow.java | 42 +++++++++++++++---- 1 file changed, 33 insertions(+), 9 deletions(-) diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java index 2db2d3d..e95de1c 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -501,28 +501,52 @@ public class ChatWindow extends JFrame { scrollForPossibleContacts.applyTheme(theme); } + /** + * Sends a new message to the server based on the text entered in the textArea. + * + * @since Envoy v0.1-beta + */ private void postMessage() { if (userList.isSelectionEmpty()) { JOptionPane.showMessageDialog(this, "Please select a recipient!", "Cannot send message", JOptionPane.INFORMATION_MESSAGE); return; } - if (!messageEnterTextArea.getText().isEmpty()) try { - checkMessageTextLength(); - // Create message - final Message message = new MessageBuilder(localDb.getUser().getId(), currentChat.getRecipient().getId(), localDb.getIdGenerator()) - .setText(messageEnterTextArea.getText()) - .build(); + if (!messageEnterTextArea.getText().isEmpty()) checkMessageTextLength(); + // Create message + final Message message = new MessageBuilder(localDb.getUser().getId(), currentChat.getRecipient().getId(), localDb.getIdGenerator()) + .setText(messageEnterTextArea.getText()) + .build(); + sendMessage(message); + // Clear text field + messageEnterTextArea.setText(""); + } + /** + * Forwards a message. + * + * @param msg the message to forward + * @param recipient the new recipient of the message + * @since Envoy v0.1-beta + */ + private void forwardMessage(Message msg, User recipient) { + sendMessage(new MessageBuilder(msg, recipient.getId(), localDb.getIdGenerator()).build()); + } + + /** + * Sends a {@link Message} to the server. + * + * @param message the message to send + * @since Envoy v0.1-beta + */ + private void sendMessage(final Message message) { + try { // Send message writeProxy.writeMessage(message); // Add message to PersistentLocalDb and update UI currentChat.appendMessage(message); - // Clear text field - messageEnterTextArea.setText(""); - // Update UI revalidate(); repaint();