Replaces AbstractListModel with ListCellRenderer, optimized threading

This commit is contained in:
2019-10-12 08:19:00 +02:00
parent 6390be5e8b
commit a3e2d6e311
4 changed files with 100 additions and 118 deletions

View File

@ -7,10 +7,8 @@ import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.List;
import java.io.IOException;
import javax.swing.AbstractListModel;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
@ -22,8 +20,6 @@ import javax.swing.JTextArea;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.http.client.ClientProtocolException;
@ -38,19 +34,20 @@ import envoy.schema.Users;
* Project: <strong>envoy-client</strong><br>
* File: <strong>ChatWindow.java</strong><br>
* Created: <strong>28 Sep 2019</strong><br>
* Author: <strong>Maximilian K&aumlfer &amp; Kai S. K. Engelbart</strong>
*
* @author Kai S. K. Engelbart
* @author Maximilian K&auml;fer
* @since Envoy 0.1
*/
public class ChatWindow extends JFrame {
private static final long serialVersionUID = 6865098428255463649L;
public long recipientID = 0;
private JPanel contentPane = new JPanel();
private static EnvoyClient envoyClient = new EnvoyClient();
private static UserJListModel AuserJListModel = new UserJListModel();
private long recipientID = 0;
private JPanel contentPane = new JPanel();
private EnvoyClient envoyClient = new EnvoyClient();
public DefaultListModel<String> listModel = new DefaultListModel<>();
private DefaultListModel<String> messageListModel = new DefaultListModel<>();
public ChatWindow() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
@ -78,7 +75,7 @@ public class ChatWindow extends JFrame {
elementList.setForeground(new Color(255, 255, 255));
elementList.setBackground(new Color(51, 51, 51));
elementList.setModel(listModel);
elementList.setModel(messageListModel);
elementList.setFont(new Font("Arial", Font.PLAIN, 17));
elementList.setFixedCellHeight(60);
elementList.setBorder(new EmptyBorder(5, 5, 5, 5));
@ -123,7 +120,6 @@ public class ChatWindow extends JFrame {
postButton.setForeground(new Color(255, 255, 255));
postButton.setBackground(new Color(102, 51, 153));
postButton.setBorderPainted(false);
GridBagConstraints gbc_moveSelectionPostButton = new GridBagConstraints();
@ -136,11 +132,11 @@ public class ChatWindow extends JFrame {
contentPane.add(postButton, gbc_moveSelectionPostButton);
postButton.addActionListener((evt) -> {
if(recipientID == 0)
System.out.println("Please select recipient");
if (recipientID == 0) System.out.println("Please select recipient");
// TODO: Acquire proper sender id
if (!messageEnterTextfield.getText().isEmpty() && recipientID != 0) try {
final Message message = envoyClient.createMessage("Kai", recipientID, messageEnterTextfield.getText());
final Message message = envoyClient.createMessage(1, recipientID, messageEnterTextfield.getText());
envoyClient.sendMessage(message);
appendMessageToChat(message);
messageEnterTextfield.setText("");
@ -152,63 +148,44 @@ public class ChatWindow extends JFrame {
e.printStackTrace();
}
});
new Thread(() -> loadUserJList()).start();
}
public void loadUserJList() {
//Users users = envoyClient.getUsersListXml();
SwingUtilities.invokeLater(() -> {
// User List
JList<User> userJList = new JList<>();
//System.out.println(userJListModel.getElementAt(0).getName());
userJList.setModel(AuserJListModel);
userJList.setSelectionForeground(new Color(255, 255, 255));
userJList.setSelectionBackground(new Color(102, 0, 153));
userJList.setForeground(new Color(255, 255, 255));
userJList.setBackground(new Color(51, 51, 51));
userJList.setFont(new Font("Arial", Font.PLAIN, 17));
//userJList.setFixedCellHeight(60);
userJList.setBorder(new EmptyBorder(5, 5, 5, 5));
GridBagConstraints gbc_userList = new GridBagConstraints();
//gbc_userList.fill = GridBagConstraints.BOTH;
gbc_userList.gridx = 0;
gbc_userList.gridy = 1;
gbc_userList.anchor = GridBagConstraints.PAGE_START;
gbc_userList.insets = new Insets(10, 0, 10, 0);
//System.out.println(userJListModel.getSize());
//System.out.println("test");
userJList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
ListSelectionListener listSelectionListener = new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent listSelectionEvent) {;
boolean adjust = listSelectionEvent.getValueIsAdjusting();
if (!adjust) {
JList selectedUserList = (JList) listSelectionEvent.getSource();
int selection = selectedUserList.getSelectedIndex();
//System.out.println(AuserJListModel.getElementID(selection));
recipientID = AuserJListModel.getElementID(selection);
}
}
};
userJList.addListSelectionListener(listSelectionListener);
contentPane.add(userJList, gbc_userList);
contentPane.revalidate();
JList<User> userList = new JList<>();
userList.setCellRenderer(new UserListRenderer());
userList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
userList.addListSelectionListener((listSelectionEvent) -> {
if (!listSelectionEvent.getValueIsAdjusting()) {
@SuppressWarnings("unchecked")
JList<User> selectedUserList = (JList<User>) listSelectionEvent.getSource();
recipientID = selectedUserList.getModel().getElementAt(selectedUserList.getSelectedIndex()).getID();
}
});
userList.setSelectionForeground(new Color(255, 255, 255));
userList.setSelectionBackground(new Color(102, 0, 153));
userList.setForeground(new Color(255, 255, 255));
userList.setBackground(new Color(51, 51, 51));
userList.setFont(new Font("Arial", Font.PLAIN, 17));
userList.setBorder(new EmptyBorder(5, 5, 5, 5));
GridBagConstraints gbc_userList = new GridBagConstraints();
gbc_userList.gridx = 0;
gbc_userList.gridy = 1;
gbc_userList.anchor = GridBagConstraints.PAGE_START;
gbc_userList.insets = new Insets(10, 0, 10, 0);
contentPane.add(userList, gbc_userList);
contentPane.revalidate();
loadUserList(userList);
}
private void loadUserList(JList<User> userList) {
new Thread(() -> {
Users users = envoyClient.getUsersListXml();
DefaultListModel<User> userListModel = new DefaultListModel<>();
users.getUser().forEach(user -> userListModel.addElement(user));
SwingUtilities.invokeLater(() -> userList.setModel(userListModel));
}).start();
}
/**
@ -225,9 +202,9 @@ public class ChatWindow extends JFrame {
* @param message The message to append
*/
private void appendMessageToChat(Message message) {
listModel.addElement("<html>" + "<p style=\"color:#d2d235\"> <b> <small>" + message.getMetaData().getSender()
+ "</b> </small>" + "<br>" + "<p style=\"color:white\">" + getFirstTextContent(message)
+ "</span></html>");
messageListModel.addElement("<html>" + "<p style=\"color:#d2d235\"> <b> <small>"
+ message.getMetaData().getSender() + "</b> </small>" + "<br>" + "<p style=\"color:white\">"
+ getFirstTextContent(message) + "</span></html>");
}
public static void main(String[] args)