From f840419345abc0e9040235d65a0a266bc2b74ae7 Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Sat, 12 Oct 2019 08:19:00 +0200 Subject: [PATCH] Replaces AbstractListModel with ListCellRenderer, optimized threading --- src/main/java/envoy/client/EnvoyClient.java | 12 +- src/main/java/envoy/client/ui/ChatWindow.java | 127 +++++++----------- .../java/envoy/client/ui/UserJListModel.java | 38 ------ .../envoy/client/ui/UserListRenderer.java | 41 ++++++ 4 files changed, 100 insertions(+), 118 deletions(-) delete mode 100644 src/main/java/envoy/client/ui/UserJListModel.java create mode 100644 src/main/java/envoy/client/ui/UserListRenderer.java diff --git a/src/main/java/envoy/client/EnvoyClient.java b/src/main/java/envoy/client/EnvoyClient.java index 8b368b5..7bef3e5 100644 --- a/src/main/java/envoy/client/EnvoyClient.java +++ b/src/main/java/envoy/client/EnvoyClient.java @@ -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: envoy-client
* File: EnvoyClient.java
* Created: 28 Sep 2019
- * Author: Kai S. K. Engelbart & Maximilian Käfer + * + * @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(); diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java index af0db13..1b5768a 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -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: envoy-client
* File: ChatWindow.java
* Created: 28 Sep 2019
- * Author: Maximilian Käfer & Kai S. K. Engelbart + * + * @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 listModel = new DefaultListModel<>(); + private DefaultListModel 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 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 userList = new JList<>(); + userList.setCellRenderer(new UserListRenderer()); + userList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); + userList.addListSelectionListener((listSelectionEvent) -> { + if (!listSelectionEvent.getValueIsAdjusting()) { + @SuppressWarnings("unchecked") + JList selectedUserList = (JList) 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 userList) { + new Thread(() -> { + Users users = envoyClient.getUsersListXml(); + DefaultListModel 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("" + "

" + message.getMetaData().getSender() - + " " + "
" + "

" + getFirstTextContent(message) - + ""); + messageListModel.addElement("" + "

" + + message.getMetaData().getSender() + " " + "
" + "

" + + getFirstTextContent(message) + ""); } public static void main(String[] args) diff --git a/src/main/java/envoy/client/ui/UserJListModel.java b/src/main/java/envoy/client/ui/UserJListModel.java deleted file mode 100644 index 05473e9..0000000 --- a/src/main/java/envoy/client/ui/UserJListModel.java +++ /dev/null @@ -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 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(); - } - - -} diff --git a/src/main/java/envoy/client/ui/UserListRenderer.java b/src/main/java/envoy/client/ui/UserListRenderer.java new file mode 100644 index 0000000..3c62e48 --- /dev/null +++ b/src/main/java/envoy/client/ui/UserListRenderer.java @@ -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: envoy-client
+ * File: UserListRenderer.java
+ * Created: 12 Oct 2019
+ * + * @author Kai S. K. Engelbart + */ +public class UserListRenderer extends JLabel implements ListCellRenderer { + + private static final long serialVersionUID = 5164417379767181198L; + + @Override + public Component getListCellRendererComponent(JList 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; + } +}