Replaces AbstractListModel with ListCellRenderer, optimized threading
This commit is contained in:
parent
4b449cef45
commit
f840419345
@ -16,8 +16,8 @@ import javax.xml.bind.Marshaller;
|
||||
import javax.xml.datatype.DatatypeConfigurationException;
|
||||
import javax.xml.datatype.DatatypeFactory;
|
||||
|
||||
import envoy.client.ui.ChatWindow;
|
||||
import envoy.schema.Message;
|
||||
import envoy.schema.Message.MetaData.MessageState;
|
||||
import envoy.schema.Messages;
|
||||
import envoy.schema.ObjectFactory;
|
||||
import envoy.schema.Users;
|
||||
@ -26,12 +26,14 @@ import envoy.schema.Users;
|
||||
* Project: <strong>envoy-client</strong><br>
|
||||
* File: <strong>EnvoyClient.java</strong><br>
|
||||
* Created: <strong>28 Sep 2019</strong><br>
|
||||
* Author: <strong>Kai S. K. Engelbart & Maximilian Käfer</strong>
|
||||
*
|
||||
* @author Kai S. K. Engelbart
|
||||
* @author Maximilian Käfer
|
||||
* @since Envoy 0.1
|
||||
*/
|
||||
|
||||
public class EnvoyClient {
|
||||
|
||||
ChatWindow chatWindow = new ChatWindow();
|
||||
private ObjectFactory objectFactory = new ObjectFactory();
|
||||
private DatatypeFactory datatypeFactory;
|
||||
|
||||
@ -101,11 +103,11 @@ public class EnvoyClient {
|
||||
* @param textContent The content (text) of the message
|
||||
* @return Prepared {@link Message} object
|
||||
*/
|
||||
public Message createMessage(String senderID, long recipientID, String textContent) {
|
||||
public Message createMessage(long senderID, long recipientID, String textContent) {
|
||||
Message.MetaData metaData = objectFactory.createMessageMetaData();
|
||||
metaData.setSender(senderID);
|
||||
metaData.setRecipient(recipientID);
|
||||
metaData.setState(false);
|
||||
metaData.setState(MessageState.WAITING);
|
||||
metaData.setDate(datatypeFactory.newXMLGregorianCalendar(Instant.now().toString()));
|
||||
|
||||
Message.Content content = objectFactory.createMessageContent();
|
||||
|
@ -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äfer & Kai S. K. Engelbart</strong>
|
||||
*
|
||||
* @author Kai S. K. Engelbart
|
||||
* @author Maximilian Kä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)
|
||||
|
@ -1,38 +0,0 @@
|
||||
package envoy.client.ui;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.LinkedList;
|
||||
|
||||
import javax.swing.AbstractListModel;
|
||||
|
||||
import envoy.client.EnvoyClient;
|
||||
import envoy.schema.User;
|
||||
import envoy.schema.Users;
|
||||
|
||||
public class UserJListModel extends AbstractListModel {
|
||||
|
||||
public EnvoyClient envoyClient = new EnvoyClient();
|
||||
Users users = envoyClient.getUsersListXml();
|
||||
private List<User> userList = new LinkedList<>();
|
||||
|
||||
public UserJListModel() {
|
||||
users.getUser().forEach(user -> userList.add(user));
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public int getSize() {
|
||||
return userList.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getElementAt(int index) {
|
||||
return userList.get(index).getName();
|
||||
}
|
||||
|
||||
public long getElementID(int index) {
|
||||
return userList.get(index).getID();
|
||||
}
|
||||
|
||||
|
||||
}
|
41
src/main/java/envoy/client/ui/UserListRenderer.java
Normal file
41
src/main/java/envoy/client/ui/UserListRenderer.java
Normal file
@ -0,0 +1,41 @@
|
||||
package envoy.client.ui;
|
||||
|
||||
import java.awt.Component;
|
||||
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JList;
|
||||
import javax.swing.ListCellRenderer;
|
||||
|
||||
import envoy.schema.User;
|
||||
|
||||
/**
|
||||
* Project: <strong>envoy-client</strong><br>
|
||||
* File: <strong>UserListRenderer.java</strong><br>
|
||||
* Created: <strong>12 Oct 2019</strong><br>
|
||||
*
|
||||
* @author Kai S. K. Engelbart
|
||||
*/
|
||||
public class UserListRenderer extends JLabel implements ListCellRenderer<User> {
|
||||
|
||||
private static final long serialVersionUID = 5164417379767181198L;
|
||||
|
||||
@Override
|
||||
public Component getListCellRendererComponent(JList<? extends User> list, User 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.getName());
|
||||
setFont(list.getFont());
|
||||
|
||||
return this;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user