From ce3d69c6b092c901db666e11ccda4357ce8eeb05 Mon Sep 17 00:00:00 2001 From: Maxi Date: Sat, 19 Oct 2019 09:59:45 +0200 Subject: [PATCH] Implemented multiple chat system * Local saving of these and displaying the partner name * Added display of timestamp in chat. Closes #9 --- src/main/java/envoy/client/Chat.java | 58 ++++++++++++ src/main/java/envoy/client/Client.java | 2 + src/main/java/envoy/client/ui/ChatWindow.java | 89 +++++++++++++++++-- 3 files changed, 140 insertions(+), 9 deletions(-) create mode 100644 src/main/java/envoy/client/Chat.java diff --git a/src/main/java/envoy/client/Chat.java b/src/main/java/envoy/client/Chat.java new file mode 100644 index 0000000..2d8c519 --- /dev/null +++ b/src/main/java/envoy/client/Chat.java @@ -0,0 +1,58 @@ +package envoy.client; + +import java.util.ArrayList; +import java.util.List; + +import javax.swing.DefaultListModel; + +import envoy.schema.Message; + +public class Chat { + + public long chatPartnerID; + public List messageList; + DefaultListModel chatModel; + public Chat() { + chatPartnerID = 0; + chatModel = new DefaultListModel(); + messageList = new ArrayList(); + } + + public void setUserID(long givenUserID) { + chatPartnerID = givenUserID; + } + + public long getUserID() { + return chatPartnerID; + } + + public int getSize() { + return chatModel.getSize(); + } + + public void addMessage(String message) { + chatModel.addElement(message); + } + + public String getMessageAt(int index) { + return (String) chatModel.getElementAt(index); + } + + @SuppressWarnings("unchecked") + public DefaultListModel getModel() { + return chatModel; + } + + public int getListSize() { + return messageList.size(); + } + + public void addMessageElement(Message message) { + messageList.add(message); + } + + public Message getMessageElement(int index) { + return messageList.get(index); + } + +} diff --git a/src/main/java/envoy/client/Client.java b/src/main/java/envoy/client/Client.java index 53a7865..149db18 100644 --- a/src/main/java/envoy/client/Client.java +++ b/src/main/java/envoy/client/Client.java @@ -154,4 +154,6 @@ public class Client { * @since Envoy 0.1 */ public long getSenderID() { return user.getID(); } + + public User getClientUser() { return user;} } \ No newline at end of file diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java index 0e9bc40..94ec2d4 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -6,6 +6,8 @@ import java.awt.Font; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; +import java.util.ArrayList; +import java.util.List; import javax.swing.DefaultListModel; import javax.swing.JButton; @@ -15,10 +17,12 @@ import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; +import javax.swing.JTextPane; import javax.swing.ListSelectionModel; import javax.swing.SwingUtilities; import javax.swing.border.EmptyBorder; +import envoy.client.Chat; import envoy.client.Client; import envoy.schema.Message; import envoy.schema.User; @@ -43,11 +47,21 @@ public class ChatWindow extends JFrame { private Client client; - private DefaultListModel messageListModel = new DefaultListModel<>(); + private DefaultListModel messageListModel = new DefaultListModel<>(); + private List partnerChatList = new ArrayList(); + @SuppressWarnings("null") public ChatWindow(Client client) { this.client = client; + Users chatUsers = client.getUsersListXml(); + + for (int i = 0; i < chatUsers.getUser().size(); i++) { + Chat userChat = new Chat(); + userChat.setUserID(chatUsers.getUser().get(i).getID()); + partnerChatList.add(userChat); + } + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 600, 800); setTitle("Envoy"); @@ -55,7 +69,7 @@ public class ChatWindow extends JFrame { contentPane.setBackground(new Color(0, 0, 0)); contentPane.setForeground(Color.white); - contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); + contentPane.setBorder(new EmptyBorder(0, 5, 0, 0)); setContentPane(contentPane); GridBagLayout gbl_contentPane = new GridBagLayout(); gbl_contentPane.columnWidths = new int[] { 1, 1, 1 }; @@ -90,7 +104,7 @@ public class ChatWindow extends JFrame { gbc_scrollPane.gridx = 1; gbc_scrollPane.gridy = 1; - gbc_scrollPane.insets = new Insets(10, 10, 10, 10); + gbc_scrollPane.insets = new Insets(0, 10, 10, 10); contentPane.add(scrollPane, gbc_scrollPane); @@ -137,8 +151,18 @@ public class ChatWindow extends JFrame { final Message message = client .createMessage(client.getSenderID(), recipientID, messageEnterTextfield.getText()); client.sendMessage(message); - appendMessageToChat(message); + Chat partnerChat = null; + for (int i = 0; i < partnerChatList.size(); i++) { + if (recipientID == partnerChatList.get(i).getUserID()) { + partnerChat = partnerChatList.get(i); + // System.out.println("partnerChatID: " + partnerChatList.get(i).getUserID()); + + } + } + appendMessageToChat(message, partnerChat); messageEnterTextfield.setText(""); + elementList.setModel(partnerChat.getModel()); + contentPane.revalidate(); } catch (Exception e) { JOptionPane.showMessageDialog(this, "An exception occured while sending a message. See the log for more details.", @@ -148,6 +172,22 @@ public class ChatWindow extends JFrame { } }); + // Partner name display + JTextPane textPane = new JTextPane(); + textPane.setBackground(new Color(0, 0, 0)); + textPane.setForeground(new Color(255, 255, 255)); + + textPane.setFont(new Font("Arial", Font.PLAIN, 20)); + + GridBagConstraints gbc_partnerName = new GridBagConstraints(); + gbc_partnerName.fill = GridBagConstraints.HORIZONTAL; + gbc_partnerName.gridwidth = 2; + gbc_partnerName.gridx = 1; + gbc_partnerName.gridy = 0; + + gbc_partnerName.insets = new Insets(0, 10, 0, 10); + contentPane.add(textPane, gbc_partnerName); + JList userList = new JList<>(); userList.setCellRenderer(new UserListRenderer()); userList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); @@ -156,6 +196,20 @@ public class ChatWindow extends JFrame { @SuppressWarnings("unchecked") JList selectedUserList = (JList) listSelectionEvent.getSource(); recipientID = selectedUserList.getModel().getElementAt(selectedUserList.getSelectedIndex()).getID(); + Chat selectedChat = null; + + for (int i = 0; i < partnerChatList.size(); i++) { + if (selectedUserList.getSelectedIndex() == partnerChatList.get(i).getUserID() - 1) { + selectedChat = partnerChatList.get(i); + + } + } + User partner = getPartner(); + System.out.println(partner.getName()); + textPane.setText(partner.getName()); + elementList.setModel(selectedChat.getModel()); + contentPane.revalidate(); + } }); @@ -167,10 +221,11 @@ public class ChatWindow extends JFrame { userList.setBorder(new EmptyBorder(5, 5, 5, 5)); GridBagConstraints gbc_userList = new GridBagConstraints(); + gbc_userList.fill = GridBagConstraints.VERTICAL; gbc_userList.gridx = 0; gbc_userList.gridy = 1; gbc_userList.anchor = GridBagConstraints.PAGE_START; - gbc_userList.insets = new Insets(10, 0, 10, 0); + gbc_userList.insets = new Insets(0, 0, 10, 0); contentPane.add(userList, gbc_userList); contentPane.revalidate(); @@ -206,9 +261,25 @@ public class ChatWindow extends JFrame { * * @param message The message to append */ - private void appendMessageToChat(Message message) { - messageListModel.addElement("" + "

" - + message.getMetaData().getSender() + " " + "
" + "

" - + getFirstTextContent(message) + ""); + private void appendMessageToChat(Message message, Chat partnerChat) { + partnerChat.addMessage("" + + "

" + + client.getClientUser().getName() + " " + + (message.getMetaData().getDate().getHour() + 2) + ":" //Dont know why this parameter 2 hours back. + + message.getMetaData().getDate().getMinute() + ":" + + message.getMetaData().getDate().getSecond() + //Dont know + + " " + "
" + "

" + getFirstTextContent(message) + + ""); + partnerChat.addMessageElement(message); + } + + public User getPartner() { + Users users = client.getUsersListXml(); + User partner = null; + for (int i = 0; i < users.getUser().size(); i++) { + if (users.getUser().get(i).getID() == recipientID) { partner = users.getUser().get(i); } + } + return partner; } } \ No newline at end of file