Implemented multiple chat system

* Local saving of these and displaying the partner name
* Added display of timestamp in chat.

Closes #9
This commit is contained in:
Maxi 2019-10-19 09:59:45 +02:00
parent 2584c6b368
commit 2761e178da
3 changed files with 140 additions and 9 deletions

View File

@ -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<Message> messageList;
DefaultListModel chatModel;
public Chat() {
chatPartnerID = 0;
chatModel = new DefaultListModel();
messageList = new ArrayList<Message>();
}
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<String> 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);
}
}

View File

@ -154,4 +154,6 @@ public class Client {
* @since Envoy 0.1 * @since Envoy 0.1
*/ */
public long getSenderID() { return user.getID(); } public long getSenderID() { return user.getID(); }
public User getClientUser() { return user;}
} }

View File

@ -6,6 +6,8 @@ import java.awt.Font;
import java.awt.GridBagConstraints; import java.awt.GridBagConstraints;
import java.awt.GridBagLayout; import java.awt.GridBagLayout;
import java.awt.Insets; import java.awt.Insets;
import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultListModel; import javax.swing.DefaultListModel;
import javax.swing.JButton; import javax.swing.JButton;
@ -15,10 +17,12 @@ import javax.swing.JOptionPane;
import javax.swing.JPanel; import javax.swing.JPanel;
import javax.swing.JScrollPane; import javax.swing.JScrollPane;
import javax.swing.JTextArea; import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.ListSelectionModel; import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities; import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder; import javax.swing.border.EmptyBorder;
import envoy.client.Chat;
import envoy.client.Client; import envoy.client.Client;
import envoy.schema.Message; import envoy.schema.Message;
import envoy.schema.User; import envoy.schema.User;
@ -44,10 +48,20 @@ public class ChatWindow extends JFrame {
private Client client; private Client client;
private DefaultListModel<String> messageListModel = new DefaultListModel<>(); private DefaultListModel<String> messageListModel = new DefaultListModel<>();
private List<Chat> partnerChatList = new ArrayList<Chat>();
@SuppressWarnings("null")
public ChatWindow(Client client) { public ChatWindow(Client client) {
this.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); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 600, 800); setBounds(100, 100, 600, 800);
setTitle("Envoy"); setTitle("Envoy");
@ -55,7 +69,7 @@ public class ChatWindow extends JFrame {
contentPane.setBackground(new Color(0, 0, 0)); contentPane.setBackground(new Color(0, 0, 0));
contentPane.setForeground(Color.white); contentPane.setForeground(Color.white);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); contentPane.setBorder(new EmptyBorder(0, 5, 0, 0));
setContentPane(contentPane); setContentPane(contentPane);
GridBagLayout gbl_contentPane = new GridBagLayout(); GridBagLayout gbl_contentPane = new GridBagLayout();
gbl_contentPane.columnWidths = new int[] { 1, 1, 1 }; gbl_contentPane.columnWidths = new int[] { 1, 1, 1 };
@ -90,7 +104,7 @@ public class ChatWindow extends JFrame {
gbc_scrollPane.gridx = 1; gbc_scrollPane.gridx = 1;
gbc_scrollPane.gridy = 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); contentPane.add(scrollPane, gbc_scrollPane);
@ -137,8 +151,18 @@ public class ChatWindow extends JFrame {
final Message message = client final Message message = client
.createMessage(client.getSenderID(), recipientID, messageEnterTextfield.getText()); .createMessage(client.getSenderID(), recipientID, messageEnterTextfield.getText());
client.sendMessage(message); 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(""); messageEnterTextfield.setText("");
elementList.setModel(partnerChat.getModel());
contentPane.revalidate();
} catch (Exception e) { } catch (Exception e) {
JOptionPane.showMessageDialog(this, JOptionPane.showMessageDialog(this,
"An exception occured while sending a message. See the log for more details.", "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<User> userList = new JList<>(); JList<User> userList = new JList<>();
userList.setCellRenderer(new UserListRenderer()); userList.setCellRenderer(new UserListRenderer());
userList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); userList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
@ -156,6 +196,20 @@ public class ChatWindow extends JFrame {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
JList<User> selectedUserList = (JList<User>) listSelectionEvent.getSource(); JList<User> selectedUserList = (JList<User>) listSelectionEvent.getSource();
recipientID = selectedUserList.getModel().getElementAt(selectedUserList.getSelectedIndex()).getID(); 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)); userList.setBorder(new EmptyBorder(5, 5, 5, 5));
GridBagConstraints gbc_userList = new GridBagConstraints(); GridBagConstraints gbc_userList = new GridBagConstraints();
gbc_userList.fill = GridBagConstraints.VERTICAL;
gbc_userList.gridx = 0; gbc_userList.gridx = 0;
gbc_userList.gridy = 1; gbc_userList.gridy = 1;
gbc_userList.anchor = GridBagConstraints.PAGE_START; 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.add(userList, gbc_userList);
contentPane.revalidate(); contentPane.revalidate();
@ -206,9 +261,25 @@ public class ChatWindow extends JFrame {
* *
* @param message The message to append * @param message The message to append
*/ */
private void appendMessageToChat(Message message) { private void appendMessageToChat(Message message, Chat partnerChat) {
messageListModel.addElement("<html>" + "<p style=\"color:#d2d235\"> <b> <small>" partnerChat.addMessage("<html>"
+ message.getMetaData().getSender() + "</b> </small>" + "<br>" + "<p style=\"color:white\">" + "<p style=\"color:#d2d235\"> <b> <small>"
+ getFirstTextContent(message) + "</span></html>"); + 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
+ "</b> </small>" + "<br>" + "<p style=\"color:white\">" + getFirstTextContent(message)
+ "</span></html>");
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;
} }
} }