Implemented method to forward a message

This commit is contained in:
delvh 2020-03-14 22:44:03 +01:00
parent 384329d00d
commit 819f0ec4f2

View File

@ -501,28 +501,52 @@ public class ChatWindow extends JFrame {
scrollForPossibleContacts.applyTheme(theme); 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() { private void postMessage() {
if (userList.isSelectionEmpty()) { if (userList.isSelectionEmpty()) {
JOptionPane.showMessageDialog(this, "Please select a recipient!", "Cannot send message", JOptionPane.INFORMATION_MESSAGE); JOptionPane.showMessageDialog(this, "Please select a recipient!", "Cannot send message", JOptionPane.INFORMATION_MESSAGE);
return; return;
} }
if (!messageEnterTextArea.getText().isEmpty()) try { if (!messageEnterTextArea.getText().isEmpty()) checkMessageTextLength();
checkMessageTextLength();
// Create message // Create message
final Message message = new MessageBuilder(localDb.getUser().getId(), currentChat.getRecipient().getId(), localDb.getIdGenerator()) final Message message = new MessageBuilder(localDb.getUser().getId(), currentChat.getRecipient().getId(), localDb.getIdGenerator())
.setText(messageEnterTextArea.getText()) .setText(messageEnterTextArea.getText())
.build(); .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 // Send message
writeProxy.writeMessage(message); writeProxy.writeMessage(message);
// Add message to PersistentLocalDb and update UI // Add message to PersistentLocalDb and update UI
currentChat.appendMessage(message); currentChat.appendMessage(message);
// Clear text field
messageEnterTextArea.setText("");
// Update UI // Update UI
revalidate(); revalidate();
repaint(); repaint();