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:
parent
dacfdeff1e
commit
ce3d69c6b0
58
src/main/java/envoy/client/Chat.java
Normal file
58
src/main/java/envoy/client/Chat.java
Normal 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);
|
||||
}
|
||||
|
||||
}
|
@ -154,4 +154,6 @@ public class Client {
|
||||
* @since Envoy 0.1
|
||||
*/
|
||||
public long getSenderID() { return user.getID(); }
|
||||
|
||||
public User getClientUser() { return user;}
|
||||
}
|
@ -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<String> messageListModel = new DefaultListModel<>();
|
||||
private DefaultListModel<String> messageListModel = new DefaultListModel<>();
|
||||
private List<Chat> partnerChatList = new ArrayList<Chat>();
|
||||
|
||||
@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<User> userList = new JList<>();
|
||||
userList.setCellRenderer(new UserListRenderer());
|
||||
userList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
||||
@ -156,6 +196,20 @@ public class ChatWindow extends JFrame {
|
||||
@SuppressWarnings("unchecked")
|
||||
JList<User> selectedUserList = (JList<User>) 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("<html>" + "<p style=\"color:#d2d235\"> <b> <small>"
|
||||
+ message.getMetaData().getSender() + "</b> </small>" + "<br>" + "<p style=\"color:white\">"
|
||||
+ getFirstTextContent(message) + "</span></html>");
|
||||
private void appendMessageToChat(Message message, Chat partnerChat) {
|
||||
partnerChat.addMessage("<html>"
|
||||
+ "<p style=\"color:#d2d235\"> <b> <small>"
|
||||
+ 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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user