From 2761e178da4b1737729e39aa8736310c99d56d73 Mon Sep 17 00:00:00 2001 From: Maxi Date: Sat, 19 Oct 2019 09:59:45 +0200 Subject: [PATCH 1/5] 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 From ea42bcc63b493a1f21bb25a7a8dfa03667b46ecd Mon Sep 17 00:00:00 2001 From: delvh Date: Sat, 19 Oct 2019 10:14:46 +0200 Subject: [PATCH 2/5] Formatted Chat file Formatted Getters and Setters --- src/main/java/envoy/client/Chat.java | 36 +++++++--------------------- 1 file changed, 9 insertions(+), 27 deletions(-) diff --git a/src/main/java/envoy/client/Chat.java b/src/main/java/envoy/client/Chat.java index 2d8c519..a38c3bb 100644 --- a/src/main/java/envoy/client/Chat.java +++ b/src/main/java/envoy/client/Chat.java @@ -18,41 +18,23 @@ public class Chat { messageList = new ArrayList(); } - public void setUserID(long givenUserID) { - chatPartnerID = givenUserID; - } + public void setUserID(long givenUserID) { chatPartnerID = givenUserID;} - public long getUserID() { - return chatPartnerID; - } + public long getUserID() { return chatPartnerID;} - public int getSize() { - return chatModel.getSize(); - } + public int getSize() { return chatModel.getSize();} - public void addMessage(String message) { - chatModel.addElement(message); - } + public void addMessage(String message) { chatModel.addElement(message);} - public String getMessageAt(int index) { - return (String) chatModel.getElementAt(index); - } + public String getMessageAt(int index) { return (String) chatModel.getElementAt(index);} @SuppressWarnings("unchecked") - public DefaultListModel getModel() { - return chatModel; - } + public DefaultListModel getModel() { return chatModel;} - public int getListSize() { - return messageList.size(); - } + public int getListSize() { return messageList.size();} - public void addMessageElement(Message message) { - messageList.add(message); - } + public void addMessageElement(Message message) { messageList.add(message);} - public Message getMessageElement(int index) { - return messageList.get(index); - } + public Message getMessageElement(int index) { return messageList.get(index);} } From 78f9f4c8830bfe049187d80026f779ba05b93f62 Mon Sep 17 00:00:00 2001 From: kske Date: Sat, 19 Oct 2019 12:10:52 +0200 Subject: [PATCH 3/5] Changed message list to use JLabels as elements * Using JLabel as message list element * Managing sender and recipient in Client * Reduced amount of ReST requests --- src/main/java/envoy/client/Chat.java | 44 ++---- src/main/java/envoy/client/Client.java | 25 ++-- src/main/java/envoy/client/ui/ChatWindow.java | 136 +++++++----------- .../envoy/client/ui/MessageListRenderer.java | 42 ++++++ 4 files changed, 116 insertions(+), 131 deletions(-) create mode 100644 src/main/java/envoy/client/ui/MessageListRenderer.java diff --git a/src/main/java/envoy/client/Chat.java b/src/main/java/envoy/client/Chat.java index a38c3bb..3f48c7c 100644 --- a/src/main/java/envoy/client/Chat.java +++ b/src/main/java/envoy/client/Chat.java @@ -1,40 +1,20 @@ package envoy.client; -import java.util.ArrayList; -import java.util.List; - import javax.swing.DefaultListModel; import envoy.schema.Message; +import envoy.schema.User; 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);} - -} + private User recipient; + private DefaultListModel model = new DefaultListModel<>(); + + public Chat(User recipient) { this.recipient = recipient; } + + public User getRecipient() { return recipient; } + + public void appendMessage(Message message) { model.addElement(message); } + + public DefaultListModel getModel() { return model; } +} \ No newline at end of file diff --git a/src/main/java/envoy/client/Client.java b/src/main/java/envoy/client/Client.java index 149db18..8468c56 100644 --- a/src/main/java/envoy/client/Client.java +++ b/src/main/java/envoy/client/Client.java @@ -36,7 +36,7 @@ public class Client { private ObjectFactory objectFactory = new ObjectFactory(); private DatatypeFactory datatypeFactory; private Config config; - private User user; + private User sender, recipient; public Client(Config config, String username) { this.config = config; @@ -45,8 +45,7 @@ public class Client { } catch (DatatypeConfigurationException e) { e.printStackTrace(); } - user = getUser(username); - System.out.printf("Mein Name ist %s und ich habe die ID %d%n", user.getName(), user.getID()); + sender = getUser(username); } /** @@ -89,15 +88,13 @@ public class Client { /** * Creates a {@link Message} object serializable to XML. * - * @param senderID The ID of the sender - * @param recipientID The ID of the recipient * @param textContent The content (text) of the message * @return prepared {@link Message} object */ - public Message createMessage(long senderID, long recipientID, String textContent) { + public Message createMessage(String textContent) { Message.MetaData metaData = objectFactory.createMessageMetaData(); - metaData.setSender(senderID); - metaData.setRecipient(recipientID); + metaData.setSender(sender.getID()); + metaData.setRecipient(recipient.getID()); metaData.setState(MessageState.WAITING); metaData.setDate(datatypeFactory.newXMLGregorianCalendar(Instant.now().toString())); @@ -150,10 +147,14 @@ public class Client { } /** - * @return the user id of this client + * @return the sender object that represents this client * @since Envoy 0.1 */ - public long getSenderID() { return user.getID(); } - - public User getClientUser() { return user;} + public User getSender() { return sender; } + + public User getRecipient() { return recipient; } + + public void setRecipient(User recipient) { this.recipient = recipient; } + + public boolean hasRecipient() { return recipient != null; } } \ 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 94ec2d4..9b07a6c 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -42,25 +42,21 @@ public class ChatWindow extends JFrame { private static final long serialVersionUID = 6865098428255463649L; - private long recipientID = 0; - private JPanel contentPane = new JPanel(); + private JPanel contentPane = new JPanel(); private Client client; - private DefaultListModel messageListModel = new DefaultListModel<>(); + private DefaultListModel messageListModel = new DefaultListModel<>(); private List partnerChatList = new ArrayList(); + private Chat currentChat; - @SuppressWarnings("null") public ChatWindow(Client client) { this.client = client; + // Initialize chat list and current chat 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); - } + chatUsers.getUser().forEach(user -> partnerChatList.add(new Chat(user))); + if (partnerChatList.size() > 0) currentChat = partnerChatList.get(0); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 600, 800); @@ -78,24 +74,25 @@ public class ChatWindow extends JFrame { gbl_contentPane.rowWeights = new double[] { 0.05, 1, 0.07 }; contentPane.setLayout(gbl_contentPane); - JList elementList = new JList<>(); - elementList.setFocusTraversalKeysEnabled(false); + JList messageList = new JList<>(); + messageList.setCellRenderer(new MessageListRenderer()); - elementList.setSelectionForeground(new Color(255, 255, 255)); - elementList.setSelectionBackground(new Color(102, 0, 153)); - elementList.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT); - elementList.setForeground(new Color(255, 255, 255)); - elementList.setBackground(new Color(51, 51, 51)); + messageList.setFocusTraversalKeysEnabled(false); + messageList.setSelectionForeground(new Color(255, 255, 255)); + messageList.setSelectionBackground(new Color(102, 0, 153)); + messageList.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT); + messageList.setForeground(new Color(255, 255, 255)); + messageList.setBackground(new Color(51, 51, 51)); - elementList.setModel(messageListModel); - elementList.setFont(new Font("Arial", Font.PLAIN, 17)); - elementList.setFixedCellHeight(60); - elementList.setBorder(new EmptyBorder(5, 5, 5, 5)); + messageList.setModel(messageListModel); + messageList.setFont(new Font("Arial", Font.PLAIN, 17)); + messageList.setFixedCellHeight(60); + messageList.setBorder(new EmptyBorder(5, 5, 5, 5)); JScrollPane scrollPane = new JScrollPane(); scrollPane.setForeground(new Color(0, 0, 0)); scrollPane.setBackground(new Color(51, 51, 51)); - scrollPane.setViewportView(elementList); + scrollPane.setViewportView(messageList); scrollPane.setBorder(null); GridBagConstraints gbc_scrollPane = new GridBagConstraints(); @@ -141,27 +138,27 @@ public class ChatWindow extends JFrame { gbc_moveSelectionPostButton.insets = new Insets(10, 10, 10, 10); - contentPane.add(postButton, gbc_moveSelectionPostButton); - postButton.addActionListener((evt) -> { - if (recipientID == 0) System.out.println("Please select recipient"); + if (!client.hasRecipient()) { + JOptionPane.showMessageDialog(this, + "Please select a recipient!", + "Cannot send message", + JOptionPane.INFORMATION_MESSAGE); + return; + } - // TODO: Acquire proper sender id - if (!messageEnterTextfield.getText().isEmpty() && recipientID != 0) try { - final Message message = client - .createMessage(client.getSenderID(), recipientID, messageEnterTextfield.getText()); + if (!messageEnterTextfield.getText().isEmpty()) try { + + // Create and send message object + final Message message = client.createMessage(messageEnterTextfield.getText()); client.sendMessage(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); + // Append message object to chat + currentChat.appendMessage(message); + messageList.setModel(currentChat.getModel()); + + // Clear text field messageEnterTextfield.setText(""); - elementList.setModel(partnerChat.getModel()); contentPane.revalidate(); } catch (Exception e) { JOptionPane.showMessageDialog(this, @@ -172,6 +169,8 @@ public class ChatWindow extends JFrame { } }); + contentPane.add(postButton, gbc_moveSelectionPostButton); + // Partner name display JTextPane textPane = new JTextPane(); textPane.setBackground(new Color(0, 0, 0)); @@ -194,22 +193,20 @@ public class ChatWindow extends JFrame { userList.addListSelectionListener((listSelectionEvent) -> { if (!listSelectionEvent.getValueIsAdjusting()) { @SuppressWarnings("unchecked") - JList selectedUserList = (JList) listSelectionEvent.getSource(); - recipientID = selectedUserList.getModel().getElementAt(selectedUserList.getSelectedIndex()).getID(); - Chat selectedChat = null; + final JList selectedUserList = (JList) listSelectionEvent.getSource(); + final User user = selectedUserList.getSelectedValue(); + client.setRecipient(user); - for (int i = 0; i < partnerChatList.size(); i++) { - if (selectedUserList.getSelectedIndex() == partnerChatList.get(i).getUserID() - 1) { - selectedChat = partnerChatList.get(i); + currentChat = partnerChatList.stream() + .filter(chat -> chat.getRecipient().getID() == user.getID()) + .findFirst() + .get(); - } - } - User partner = getPartner(); - System.out.println(partner.getName()); - textPane.setText(partner.getName()); - elementList.setModel(selectedChat.getModel()); + client.setRecipient(user); + + textPane.setText(currentChat.getRecipient().getName()); + messageList.setModel(currentChat.getModel()); contentPane.revalidate(); - } }); @@ -221,7 +218,7 @@ 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.fill = GridBagConstraints.VERTICAL; gbc_userList.gridx = 0; gbc_userList.gridy = 1; gbc_userList.anchor = GridBagConstraints.PAGE_START; @@ -247,39 +244,4 @@ public class ChatWindow extends JFrame { SwingUtilities.invokeLater(() -> userList.setModel(userListModel)); }).start(); } - - /** - * Extracts the first text content from a message. - * - * @param message The message from which to return the text content - * @return The first content of type 'text' - */ - private String getFirstTextContent(Message message) { return message.getContent().get(0).getText(); } - - /** - * Appends a message with sender and message content to the message list. - * - * @param message The message to append - */ - 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 diff --git a/src/main/java/envoy/client/ui/MessageListRenderer.java b/src/main/java/envoy/client/ui/MessageListRenderer.java new file mode 100644 index 0000000..09c3c83 --- /dev/null +++ b/src/main/java/envoy/client/ui/MessageListRenderer.java @@ -0,0 +1,42 @@ +package envoy.client.ui; + +import java.awt.Component; + +import javax.swing.JLabel; +import javax.swing.JList; +import javax.swing.ListCellRenderer; + +import envoy.schema.Message; + +/** + * Project: envoy-client
+ * File: UserListRenderer.java
+ * Created: 19 Oct 2019
+ * + * @author Kai S. K. Engelbart + */ +public class MessageListRenderer extends JLabel implements ListCellRenderer { + + private static final long serialVersionUID = 5164417379767181198L; + + @Override + public Component getListCellRendererComponent(JList list, Message value, int index, + boolean isSelected, + boolean cellHasFocus) { + if (isSelected) { + setBackground(list.getSelectionBackground()); + setForeground(list.getSelectionForeground()); + } else { + setBackground(list.getBackground()); + setForeground(list.getForeground()); + } + + // Enable background rendering + setOpaque(true); + + setText(value.getContent().get(0).getText()); + setFont(list.getFont()); + + return this; + } +} From aeb138334e056a6265acd67bb37cc5889163af5f Mon Sep 17 00:00:00 2001 From: Maxi Date: Sat, 19 Oct 2019 15:09:32 +0200 Subject: [PATCH 4/5] Requesting unread messages an assigning them to the right chat element. --- src/main/java/envoy/client/Client.java | 14 +++++++++++++- src/main/java/envoy/client/ui/ChatWindow.java | 15 +++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/main/java/envoy/client/Client.java b/src/main/java/envoy/client/Client.java index 8468c56..ed8ebb1 100644 --- a/src/main/java/envoy/client/Client.java +++ b/src/main/java/envoy/client/Client.java @@ -137,7 +137,7 @@ public class Client { private User getUser(String name) { javax.ws.rs.client.Client client = ClientBuilder.newClient(); WebTarget target = client.target(String - .format("%s:%d/envoy-server/rest/user/sender?name=" + name, config.getServer(), config.getPort())); + .format("%s:%d/envoy-server/rest/user/sender?name=%s", config.getServer(), config.getPort(), name)); Response response = target.request("application/xml").get(); Users users = response.readEntity(Users.class); System.out.println("Response code: " + response.getStatus()); @@ -146,6 +146,18 @@ public class Client { return users.getUser().get(0); } + public Messages getUnreadMessages(long userId) { + javax.ws.rs.client.Client client = ClientBuilder.newClient(); + WebTarget target = client.target(String + .format("%s:%d/envoy-server/rest/message/receive?userId=%d", config.getServer(), config.getPort(), userId)); + Response response = target.request("application/xml").get(); + Messages unreadMessages = response.readEntity(Messages.class); + System.out.println("Response code: " + response.getStatus()); + response.close(); + client.close(); + return unreadMessages; + } + /** * @return the sender object that represents this client * @since Envoy 0.1 diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java index 9b07a6c..8f11ec2 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -20,11 +20,13 @@ import javax.swing.JTextArea; import javax.swing.JTextPane; import javax.swing.ListSelectionModel; import javax.swing.SwingUtilities; +import javax.swing.Timer; import javax.swing.border.EmptyBorder; import envoy.client.Chat; import envoy.client.Client; import envoy.schema.Message; +import envoy.schema.Messages; import envoy.schema.User; import envoy.schema.Users; @@ -228,6 +230,19 @@ public class ChatWindow extends JFrame { contentPane.revalidate(); loadUserList(userList); + + new Timer(5000, (evt) -> { + Messages unreadMessages = client.getUnreadMessages(client.getSender().getID()); + System.out.println(unreadMessages.getMessage().get(0)); + for (int i = 0; i < unreadMessages.getMessage().size(); i++) { + for (int j = 0; j < partnerChatList.size(); j++) { + if(partnerChatList.get(j).getRecipient().getID() == unreadMessages.getMessage().get(i).getMetaData().getSender()) { + partnerChatList.get(j).appendMessage(unreadMessages.getMessage().get(i)); + } + } + } + }).start(); + contentPane.revalidate(); } /** From 18a078475d21689f1516fdcf28cdabb91f42fbf4 Mon Sep 17 00:00:00 2001 From: kske Date: Wed, 23 Oct 2019 05:49:15 +0200 Subject: [PATCH 5/5] Added HTML formatting to message list elements --- .../java/envoy/client/ui/MessageListRenderer.java | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/main/java/envoy/client/ui/MessageListRenderer.java b/src/main/java/envoy/client/ui/MessageListRenderer.java index 09c3c83..ad965c4 100644 --- a/src/main/java/envoy/client/ui/MessageListRenderer.java +++ b/src/main/java/envoy/client/ui/MessageListRenderer.java @@ -1,6 +1,7 @@ package envoy.client.ui; import java.awt.Component; +import java.text.SimpleDateFormat; import javax.swing.JLabel; import javax.swing.JList; @@ -21,8 +22,7 @@ public class MessageListRenderer extends JLabel implements ListCellRenderer list, Message value, int index, - boolean isSelected, - boolean cellHasFocus) { + boolean isSelected, boolean cellHasFocus) { if (isSelected) { setBackground(list.getSelectionBackground()); setForeground(list.getSelectionForeground()); @@ -31,11 +31,13 @@ public class MessageListRenderer extends JLabel implements ListCellRenderer

%s

%s", + new SimpleDateFormat("dd.MM.yyyy hh:mm ") + .format(value.getMetaData().getDate().toGregorianCalendar().getTime()), + value.getContent().get(0).getText())); return this; }