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
This commit is contained in:
@ -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<String> messageListModel = new DefaultListModel<>();
|
||||
private DefaultListModel<Message> messageListModel = new DefaultListModel<>();
|
||||
private List<Chat> partnerChatList = new ArrayList<Chat>();
|
||||
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<String> elementList = new JList<>();
|
||||
elementList.setFocusTraversalKeysEnabled(false);
|
||||
JList<Message> 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<User> selectedUserList = (JList<User>) listSelectionEvent.getSource();
|
||||
recipientID = selectedUserList.getModel().getElementAt(selectedUserList.getSelectedIndex()).getID();
|
||||
Chat selectedChat = null;
|
||||
final JList<User> selectedUserList = (JList<User>) 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("<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