From 09dba0fa4f57ff5687562200dbc520b7ed113a6a Mon Sep 17 00:00:00 2001 From: Maxi Date: Mon, 7 Oct 2019 16:14:14 +0200 Subject: [PATCH 1/5] Added user list download --- src/main/java/envoy/client/EnvoyClient.java | 63 +++++++++++ src/main/java/envoy/client/ui/ChatWindow.java | 105 +++++++++++++----- 2 files changed, 139 insertions(+), 29 deletions(-) diff --git a/src/main/java/envoy/client/EnvoyClient.java b/src/main/java/envoy/client/EnvoyClient.java index c659c4e..5380aed 100644 --- a/src/main/java/envoy/client/EnvoyClient.java +++ b/src/main/java/envoy/client/EnvoyClient.java @@ -1,8 +1,12 @@ package envoy.client; +import java.io.BufferedReader; import java.io.IOException; +import java.io.InputStreamReader; +import java.io.StringReader; import java.time.Instant; import java.util.Arrays; +import java.util.List; import java.util.Properties; import javax.ws.rs.client.Client; @@ -15,9 +19,27 @@ import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.datatype.DatatypeConfigurationException; import javax.xml.datatype.DatatypeFactory; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.client.ClientProtocolException; +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.DefaultHttpClient; +import org.apache.http.util.EntityUtils; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; + +import envoy.client.ui.ChatWindow; import envoy.schema.Message; import envoy.schema.Messages; +import envoy.schema.User; +import envoy.schema.Users; import envoy.schema.ObjectFactory; /** @@ -29,6 +51,7 @@ import envoy.schema.ObjectFactory; public class EnvoyClient { + ChatWindow chatWindow = new ChatWindow(); private ObjectFactory objectFactory = new ObjectFactory(); private DatatypeFactory datatypeFactory; @@ -120,4 +143,44 @@ public class EnvoyClient { wrapper.getMessage().addAll(Arrays.asList(messages)); return wrapper; } + + public void getUsersListXml () throws ClientProtocolException, IOException, SAXException, ParserConfigurationException { + + /*HttpClient client = new DefaultHttpClient(); + HttpGet request = new HttpGet(String.format("%s:%s/envoy-server/rest/user", + serverProps.getProperty("server"), + serverProps.getProperty("port"))); + HttpResponse response = client.execute(request); + + BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); + System.out.println(rd.readLine()); + + String inputLine; + StringBuffer response1 = new StringBuffer(); + while((inputLine = rd.readLine()) != null) { + response1.append(inputLine); + } + */ + + Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder() + .parse(String.format("%s:%s/envoy-server/rest/user", + serverProps.getProperty("server"), + serverProps.getProperty("port"))); + NodeList errNodes = doc.getElementsByTagName("User"); + + for (int i = 0; i < errNodes.getLength(); i++) { + if(errNodes.getLength() > 0) { + Element err = (Element)errNodes.item(i); + //System.out.println("Name: " + err.getElementsByTagName("Name").item(0).getTextContent()); + //System.out.println("Id: " + err.getElementsByTagName("ID").item(0).getTextContent()); + User user = new User(); + user.setName(err.getElementsByTagName("Name").item(0).getTextContent()); + user.setID(Long.parseLong(err.getElementsByTagName("ID").item(0).getTextContent())); + chatWindow.addUserToList(user); + + } + } + chatWindow.printUserListElements(); + chatWindow.loadUserJList(); + } } \ No newline at end of file diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java index 8fb801e..056a4c5 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -7,6 +7,9 @@ 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 java.util.ArrayList; import javax.swing.DefaultListModel; import javax.swing.JButton; @@ -17,9 +20,14 @@ import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.border.EmptyBorder; +import javax.xml.parsers.ParserConfigurationException; + +import org.apache.http.client.ClientProtocolException; +import org.xml.sax.SAXException; import envoy.client.EnvoyClient; import envoy.schema.Message; +import envoy.schema.User; /** * Project: envoy-client
@@ -31,11 +39,13 @@ public class ChatWindow extends JFrame { private static final long serialVersionUID = 6865098428255463649L; - private JPanel contentPane = new JPanel(); - private EnvoyClient envoyClient = new EnvoyClient(); + private JPanel contentPane = new JPanel(); + private static EnvoyClient envoyClient = new EnvoyClient(); public DefaultListModel listModel = new DefaultListModel<>(); + public ArrayList userList = new ArrayList(); + public ChatWindow() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 600, 800); @@ -47,10 +57,10 @@ public class ChatWindow extends JFrame { contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); GridBagLayout gbl_contentPane = new GridBagLayout(); - gbl_contentPane.columnWidths = new int[] { 1, 1, 1 }; - gbl_contentPane.rowHeights = new int[] { 1, 1, 1 }; - gbl_contentPane.columnWeights = new double[] { 0.3, 1.0, 0.1 }; - gbl_contentPane.rowWeights = new double[] { 0.05, 1, 0.07 }; + gbl_contentPane.columnWidths = new int[] { 1, 1, 1 }; + gbl_contentPane.rowHeights = new int[] { 1, 1, 1 }; + gbl_contentPane.columnWeights = new double[] { 0.3, 1.0, 0.1 }; + gbl_contentPane.rowWeights = new double[] { 0.05, 1, 0.07 }; contentPane.setLayout(gbl_contentPane); JList elementList = new JList<>(); @@ -74,10 +84,10 @@ public class ChatWindow extends JFrame { scrollPane.setBorder(null); GridBagConstraints gbc_scrollPane = new GridBagConstraints(); - gbc_scrollPane.fill = GridBagConstraints.BOTH; - gbc_scrollPane.gridwidth = 2; - gbc_scrollPane.gridx = 1; - gbc_scrollPane.gridy = 1; + gbc_scrollPane.fill = GridBagConstraints.BOTH; + gbc_scrollPane.gridwidth = 2; + gbc_scrollPane.gridx = 1; + gbc_scrollPane.gridy = 1; gbc_scrollPane.insets = new Insets(10, 10, 10, 10); @@ -94,9 +104,9 @@ public class ChatWindow extends JFrame { messageEnterTextfield.setBorder(new EmptyBorder(5, 5, 5, 5)); GridBagConstraints gbc_moveSelectionMessageEnterTextfield = new GridBagConstraints(); - gbc_moveSelectionMessageEnterTextfield.fill = GridBagConstraints.BOTH; - gbc_moveSelectionMessageEnterTextfield.gridx = 1; - gbc_moveSelectionMessageEnterTextfield.gridy = 2; + gbc_moveSelectionMessageEnterTextfield.fill = GridBagConstraints.BOTH; + gbc_moveSelectionMessageEnterTextfield.gridx = 1; + gbc_moveSelectionMessageEnterTextfield.gridy = 2; gbc_moveSelectionMessageEnterTextfield.insets = new Insets(10, 10, 10, 10); @@ -110,28 +120,50 @@ public class ChatWindow extends JFrame { GridBagConstraints gbc_moveSelectionPostButton = new GridBagConstraints(); - gbc_moveSelectionPostButton.fill = GridBagConstraints.BOTH; - gbc_moveSelectionPostButton.gridx = 2; - gbc_moveSelectionPostButton.gridy = 2; + gbc_moveSelectionPostButton.fill = GridBagConstraints.BOTH; + gbc_moveSelectionPostButton.gridx = 2; + gbc_moveSelectionPostButton.gridy = 2; gbc_moveSelectionPostButton.insets = new Insets(10, 10, 10, 10); contentPane.add(postButton, gbc_moveSelectionPostButton); postButton.addActionListener((evt) -> { - if (!messageEnterTextfield.getText().isEmpty()) try { - final Message message = envoyClient.createMessage("Kai", "Maxi", messageEnterTextfield.getText()); - envoyClient.sendMessage(message); - appendMessageToChat(message); - messageEnterTextfield.setText(""); - } catch (Exception e) { - JOptionPane.showMessageDialog(this, - "An exception occured while sending a message. See the log for more details.", - "Exception occured", - JOptionPane.ERROR_MESSAGE); - e.printStackTrace(); - } + if (!messageEnterTextfield.getText().isEmpty()) + try { + final Message message = envoyClient.createMessage("Kai", "Maxi", messageEnterTextfield.getText()); + envoyClient.sendMessage(message); + appendMessageToChat(message); + messageEnterTextfield.setText(""); + } catch (Exception e) { + JOptionPane.showMessageDialog(this, + "An exception occured while sending a message. See the log for more details.", + "Exception occured", JOptionPane.ERROR_MESSAGE); + e.printStackTrace(); + } }); + + } + + public void loadUserJList() { + // User List + JList userJList = new JList<>(); + DefaultListModel userJListModel = new DefaultListModel<>(); + for (int i = 0; i < userList.size(); i++) { + userJListModel.addElement(userList.get(i)); + } + userJList.setModel(userJListModel); + + GridBagConstraints gbc_userList = new GridBagConstraints(); + gbc_userList.fill = GridBagConstraints.BOTH; + gbc_userList.gridx = 0; + gbc_userList.gridy = 1; + + // gbc_userList.insets = new Insets(10, 10, 10, 10); + System.out.println(userJListModel.getSize()); + System.out.println("test"); + + contentPane.add(userJList, gbc_userList); } /** @@ -155,7 +187,20 @@ public class ChatWindow extends JFrame { + ""); } - public static void main(String[] args) { + public void addUserToList(User user) { + userList.add(user); + + } + + public void printUserListElements() { + for (int i = 0; i < userList.size(); i++) { + System.out.println(userList.get(i).getName()); + System.out.println(userList.get(i).getID()); + } + } + + public static void main(String[] args) + throws ClientProtocolException, IOException, SAXException, ParserConfigurationException { EventQueue.invokeLater(() -> { try { ChatWindow frame = new ChatWindow(); @@ -164,5 +209,7 @@ public class ChatWindow extends JFrame { e.printStackTrace(); } }); + envoyClient.getUsersListXml(); + System.out.println("asd"); } } \ No newline at end of file From 5c495e5bd8667a80dfc1cde7dc6b0c79bcbcca4f Mon Sep 17 00:00:00 2001 From: kske Date: Mon, 7 Oct 2019 17:35:57 +0200 Subject: [PATCH 2/5] Using JAX-RS to retrieve user list --- src/main/java/envoy/client/EnvoyClient.java | 69 ++--------- src/main/java/envoy/client/ui/ChatWindow.java | 114 ++++++++---------- 2 files changed, 60 insertions(+), 123 deletions(-) diff --git a/src/main/java/envoy/client/EnvoyClient.java b/src/main/java/envoy/client/EnvoyClient.java index 5380aed..5d9dca4 100644 --- a/src/main/java/envoy/client/EnvoyClient.java +++ b/src/main/java/envoy/client/EnvoyClient.java @@ -1,12 +1,8 @@ package envoy.client; -import java.io.BufferedReader; import java.io.IOException; -import java.io.InputStreamReader; -import java.io.StringReader; import java.time.Instant; import java.util.Arrays; -import java.util.List; import java.util.Properties; import javax.ws.rs.client.Client; @@ -19,28 +15,12 @@ import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.datatype.DatatypeConfigurationException; import javax.xml.datatype.DatatypeFactory; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.ParserConfigurationException; - -import org.apache.http.HttpEntity; -import org.apache.http.HttpResponse; -import org.apache.http.client.ClientProtocolException; -import org.apache.http.client.HttpClient; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.impl.client.DefaultHttpClient; -import org.apache.http.util.EntityUtils; -import org.w3c.dom.Document; -import org.w3c.dom.Element; -import org.w3c.dom.NodeList; -import org.xml.sax.InputSource; -import org.xml.sax.SAXException; import envoy.client.ui.ChatWindow; import envoy.schema.Message; import envoy.schema.Messages; -import envoy.schema.User; -import envoy.schema.Users; import envoy.schema.ObjectFactory; +import envoy.schema.Users; /** * Project: envoy-client
@@ -51,7 +31,7 @@ import envoy.schema.ObjectFactory; public class EnvoyClient { - ChatWindow chatWindow = new ChatWindow(); + ChatWindow chatWindow = new ChatWindow(); private ObjectFactory objectFactory = new ObjectFactory(); private DatatypeFactory datatypeFactory; @@ -143,44 +123,17 @@ public class EnvoyClient { wrapper.getMessage().addAll(Arrays.asList(messages)); return wrapper; } - - public void getUsersListXml () throws ClientProtocolException, IOException, SAXException, ParserConfigurationException { - /*HttpClient client = new DefaultHttpClient(); - HttpGet request = new HttpGet(String.format("%s:%s/envoy-server/rest/user", + public Users getUsersListXml() { + Client client = ClientBuilder.newClient(); + WebTarget target = client.target(String.format("%s:%s/envoy-server/rest/user", serverProps.getProperty("server"), serverProps.getProperty("port"))); - HttpResponse response = client.execute(request); - - BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); - System.out.println(rd.readLine()); - - String inputLine; - StringBuffer response1 = new StringBuffer(); - while((inputLine = rd.readLine()) != null) { - response1.append(inputLine); - } - */ - - Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder() - .parse(String.format("%s:%s/envoy-server/rest/user", - serverProps.getProperty("server"), - serverProps.getProperty("port"))); - NodeList errNodes = doc.getElementsByTagName("User"); - - for (int i = 0; i < errNodes.getLength(); i++) { - if(errNodes.getLength() > 0) { - Element err = (Element)errNodes.item(i); - //System.out.println("Name: " + err.getElementsByTagName("Name").item(0).getTextContent()); - //System.out.println("Id: " + err.getElementsByTagName("ID").item(0).getTextContent()); - User user = new User(); - user.setName(err.getElementsByTagName("Name").item(0).getTextContent()); - user.setID(Long.parseLong(err.getElementsByTagName("ID").item(0).getTextContent())); - chatWindow.addUserToList(user); - - } - } - chatWindow.printUserListElements(); - chatWindow.loadUserJList(); + Response response = target.request("application/xml").get(); + Users users = response.readEntity(Users.class); + System.out.println("Response code: " + response.getStatus()); + response.close(); + client.close(); + return users; } } \ No newline at end of file diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java index 056a4c5..2398e52 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -7,9 +7,7 @@ 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 java.util.ArrayList; import javax.swing.DefaultListModel; import javax.swing.JButton; @@ -19,6 +17,7 @@ import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; +import javax.swing.SwingUtilities; import javax.swing.border.EmptyBorder; import javax.xml.parsers.ParserConfigurationException; @@ -28,6 +27,7 @@ import org.xml.sax.SAXException; import envoy.client.EnvoyClient; import envoy.schema.Message; import envoy.schema.User; +import envoy.schema.Users; /** * Project: envoy-client
@@ -39,13 +39,11 @@ public class ChatWindow extends JFrame { private static final long serialVersionUID = 6865098428255463649L; - private JPanel contentPane = new JPanel(); - private static EnvoyClient envoyClient = new EnvoyClient(); + private JPanel contentPane = new JPanel(); + private static EnvoyClient envoyClient = new EnvoyClient(); public DefaultListModel listModel = new DefaultListModel<>(); - public ArrayList userList = new ArrayList(); - public ChatWindow() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 600, 800); @@ -57,10 +55,10 @@ public class ChatWindow extends JFrame { contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); GridBagLayout gbl_contentPane = new GridBagLayout(); - gbl_contentPane.columnWidths = new int[] { 1, 1, 1 }; - gbl_contentPane.rowHeights = new int[] { 1, 1, 1 }; - gbl_contentPane.columnWeights = new double[] { 0.3, 1.0, 0.1 }; - gbl_contentPane.rowWeights = new double[] { 0.05, 1, 0.07 }; + gbl_contentPane.columnWidths = new int[] { 1, 1, 1 }; + gbl_contentPane.rowHeights = new int[] { 1, 1, 1 }; + gbl_contentPane.columnWeights = new double[] { 0.3, 1.0, 0.1 }; + gbl_contentPane.rowWeights = new double[] { 0.05, 1, 0.07 }; contentPane.setLayout(gbl_contentPane); JList elementList = new JList<>(); @@ -84,10 +82,10 @@ public class ChatWindow extends JFrame { scrollPane.setBorder(null); GridBagConstraints gbc_scrollPane = new GridBagConstraints(); - gbc_scrollPane.fill = GridBagConstraints.BOTH; - gbc_scrollPane.gridwidth = 2; - gbc_scrollPane.gridx = 1; - gbc_scrollPane.gridy = 1; + gbc_scrollPane.fill = GridBagConstraints.BOTH; + gbc_scrollPane.gridwidth = 2; + gbc_scrollPane.gridx = 1; + gbc_scrollPane.gridy = 1; gbc_scrollPane.insets = new Insets(10, 10, 10, 10); @@ -104,9 +102,9 @@ public class ChatWindow extends JFrame { messageEnterTextfield.setBorder(new EmptyBorder(5, 5, 5, 5)); GridBagConstraints gbc_moveSelectionMessageEnterTextfield = new GridBagConstraints(); - gbc_moveSelectionMessageEnterTextfield.fill = GridBagConstraints.BOTH; - gbc_moveSelectionMessageEnterTextfield.gridx = 1; - gbc_moveSelectionMessageEnterTextfield.gridy = 2; + gbc_moveSelectionMessageEnterTextfield.fill = GridBagConstraints.BOTH; + gbc_moveSelectionMessageEnterTextfield.gridx = 1; + gbc_moveSelectionMessageEnterTextfield.gridy = 2; gbc_moveSelectionMessageEnterTextfield.insets = new Insets(10, 10, 10, 10); @@ -120,50 +118,52 @@ public class ChatWindow extends JFrame { GridBagConstraints gbc_moveSelectionPostButton = new GridBagConstraints(); - gbc_moveSelectionPostButton.fill = GridBagConstraints.BOTH; - gbc_moveSelectionPostButton.gridx = 2; - gbc_moveSelectionPostButton.gridy = 2; + gbc_moveSelectionPostButton.fill = GridBagConstraints.BOTH; + gbc_moveSelectionPostButton.gridx = 2; + gbc_moveSelectionPostButton.gridy = 2; gbc_moveSelectionPostButton.insets = new Insets(10, 10, 10, 10); contentPane.add(postButton, gbc_moveSelectionPostButton); postButton.addActionListener((evt) -> { - if (!messageEnterTextfield.getText().isEmpty()) - try { - final Message message = envoyClient.createMessage("Kai", "Maxi", messageEnterTextfield.getText()); - envoyClient.sendMessage(message); - appendMessageToChat(message); - messageEnterTextfield.setText(""); - } catch (Exception e) { - JOptionPane.showMessageDialog(this, - "An exception occured while sending a message. See the log for more details.", - "Exception occured", JOptionPane.ERROR_MESSAGE); - e.printStackTrace(); - } + if (!messageEnterTextfield.getText().isEmpty()) try { + final Message message = envoyClient.createMessage("Kai", "Maxi", messageEnterTextfield.getText()); + envoyClient.sendMessage(message); + appendMessageToChat(message); + messageEnterTextfield.setText(""); + } catch (Exception e) { + JOptionPane.showMessageDialog(this, + "An exception occured while sending a message. See the log for more details.", + "Exception occured", + JOptionPane.ERROR_MESSAGE); + e.printStackTrace(); + } }); - + new Thread(() -> loadUserJList()).start(); } public void loadUserJList() { - // User List - JList userJList = new JList<>(); - DefaultListModel userJListModel = new DefaultListModel<>(); - for (int i = 0; i < userList.size(); i++) { - userJListModel.addElement(userList.get(i)); - } - userJList.setModel(userJListModel); + Users users = envoyClient.getUsersListXml(); - GridBagConstraints gbc_userList = new GridBagConstraints(); - gbc_userList.fill = GridBagConstraints.BOTH; - gbc_userList.gridx = 0; - gbc_userList.gridy = 1; + SwingUtilities.invokeLater(() -> { + // User List + JList userJList = new JList<>(); + DefaultListModel userJListModel = new DefaultListModel<>(); + users.getUser().forEach(user -> userJListModel.addElement(user)); + userJList.setModel(userJListModel); - // gbc_userList.insets = new Insets(10, 10, 10, 10); - System.out.println(userJListModel.getSize()); - System.out.println("test"); + GridBagConstraints gbc_userList = new GridBagConstraints(); + gbc_userList.fill = GridBagConstraints.BOTH; + gbc_userList.gridx = 0; + gbc_userList.gridy = 1; - contentPane.add(userJList, gbc_userList); + // gbc_userList.insets = new Insets(10, 10, 10, 10); + System.out.println(userJListModel.getSize()); + System.out.println("test"); + + contentPane.add(userJList, gbc_userList); + }); } /** @@ -172,9 +172,7 @@ public class ChatWindow extends JFrame { * @param message The message from which to return the text content * @return The first content of type 'text' */ - public String getFirstTextContent(Message message) { - return message.getContent().get(0).getText(); - } + public String getFirstTextContent(Message message) { return message.getContent().get(0).getText(); } /** * Appends a message with sender and message content to the message list. @@ -187,18 +185,6 @@ public class ChatWindow extends JFrame { + ""); } - public void addUserToList(User user) { - userList.add(user); - - } - - public void printUserListElements() { - for (int i = 0; i < userList.size(); i++) { - System.out.println(userList.get(i).getName()); - System.out.println(userList.get(i).getID()); - } - } - public static void main(String[] args) throws ClientProtocolException, IOException, SAXException, ParserConfigurationException { EventQueue.invokeLater(() -> { @@ -209,7 +195,5 @@ public class ChatWindow extends JFrame { e.printStackTrace(); } }); - envoyClient.getUsersListXml(); - System.out.println("asd"); } } \ No newline at end of file From 6390be5e8bd121462a7547ffde8ee32e11b2f880 Mon Sep 17 00:00:00 2001 From: Maxi Date: Wed, 9 Oct 2019 21:54:31 +0200 Subject: [PATCH 3/5] Added user list (abstractListModel), setting the recipientID (in message object) by selecting user from the list. --- src/main/java/envoy/client/EnvoyClient.java | 5 +- src/main/java/envoy/client/ui/ChatWindow.java | 67 ++++++++++++++++--- .../java/envoy/client/ui/UserJListModel.java | 38 +++++++++++ 3 files changed, 97 insertions(+), 13 deletions(-) create mode 100644 src/main/java/envoy/client/ui/UserJListModel.java diff --git a/src/main/java/envoy/client/EnvoyClient.java b/src/main/java/envoy/client/EnvoyClient.java index 5d9dca4..8b368b5 100644 --- a/src/main/java/envoy/client/EnvoyClient.java +++ b/src/main/java/envoy/client/EnvoyClient.java @@ -85,6 +85,7 @@ public class EnvoyClient { WebTarget target = client.target(String.format("%s:%s/envoy-server/rest/message/send", serverProps.getProperty("server"), serverProps.getProperty("port"))); + Response response = target.request().post(Entity.entity(messages, "application/xml")); System.out.println("Response code: " + response.getStatus()); response.close(); @@ -100,7 +101,7 @@ public class EnvoyClient { * @param textContent The content (text) of the message * @return Prepared {@link Message} object */ - public Message createMessage(String senderID, String recipientID, String textContent) { + public Message createMessage(String senderID, long recipientID, String textContent) { Message.MetaData metaData = objectFactory.createMessageMetaData(); metaData.setSender(senderID); metaData.setRecipient(recipientID); @@ -123,7 +124,7 @@ public class EnvoyClient { wrapper.getMessage().addAll(Arrays.asList(messages)); return wrapper; } - + public Users getUsersListXml() { Client client = ClientBuilder.newClient(); WebTarget target = client.target(String.format("%s:%s/envoy-server/rest/user", diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java index 2398e52..af0db13 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -7,8 +7,10 @@ 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; @@ -17,8 +19,11 @@ import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; 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; @@ -37,10 +42,13 @@ import envoy.schema.Users; */ 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(); public DefaultListModel listModel = new DefaultListModel<>(); @@ -115,6 +123,7 @@ 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(); @@ -127,8 +136,11 @@ public class ChatWindow extends JFrame { contentPane.add(postButton, gbc_moveSelectionPostButton); postButton.addActionListener((evt) -> { - if (!messageEnterTextfield.getText().isEmpty()) try { - final Message message = envoyClient.createMessage("Kai", "Maxi", messageEnterTextfield.getText()); + if(recipientID == 0) + System.out.println("Please select recipient"); + + if (!messageEnterTextfield.getText().isEmpty() && recipientID != 0) try { + final Message message = envoyClient.createMessage("Kai", recipientID, messageEnterTextfield.getText()); envoyClient.sendMessage(message); appendMessageToChat(message); messageEnterTextfield.setText(""); @@ -140,29 +152,62 @@ public class ChatWindow extends JFrame { e.printStackTrace(); } }); + new Thread(() -> loadUserJList()).start(); + } - + public void loadUserJList() { - Users users = envoyClient.getUsersListXml(); + //Users users = envoyClient.getUsersListXml(); + SwingUtilities.invokeLater(() -> { + // User List JList userJList = new JList<>(); - DefaultListModel userJListModel = new DefaultListModel<>(); - users.getUser().forEach(user -> userJListModel.addElement(user)); - userJList.setModel(userJListModel); + + //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.fill = GridBagConstraints.BOTH; gbc_userList.gridx = 0; gbc_userList.gridy = 1; + gbc_userList.anchor = GridBagConstraints.PAGE_START; - // gbc_userList.insets = new Insets(10, 10, 10, 10); - System.out.println(userJListModel.getSize()); - System.out.println("test"); + 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(); }); } diff --git a/src/main/java/envoy/client/ui/UserJListModel.java b/src/main/java/envoy/client/ui/UserJListModel.java new file mode 100644 index 0000000..05473e9 --- /dev/null +++ b/src/main/java/envoy/client/ui/UserJListModel.java @@ -0,0 +1,38 @@ +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(); + } + + +} From a3e2d6e3116eadd6e749a2af17c7ed7f6c38e8ae Mon Sep 17 00:00:00 2001 From: kske Date: Sat, 12 Oct 2019 08:19:00 +0200 Subject: [PATCH 4/5] 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; + } +} From bc0d519ccef79cd3404dd83cd7c357282944e677 Mon Sep 17 00:00:00 2001 From: kske Date: Sat, 12 Oct 2019 08:24:15 +0200 Subject: [PATCH 5/5] Added Javadoc, changed getFirstTextContent from public to private --- src/main/java/envoy/client/ui/ChatWindow.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java index 1b5768a..178c738 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -179,6 +179,12 @@ public class ChatWindow extends JFrame { loadUserList(userList); } + /** + * Initializes the elements of the user list by downloading them from the + * server. + * + * @param userList The {@link JList} to put the elements in + */ private void loadUserList(JList userList) { new Thread(() -> { Users users = envoyClient.getUsersListXml(); @@ -194,7 +200,7 @@ public class ChatWindow extends JFrame { * @param message The message from which to return the text content * @return The first content of type 'text' */ - public String getFirstTextContent(Message message) { return message.getContent().get(0).getText(); } + private String getFirstTextContent(Message message) { return message.getContent().get(0).getText(); } /** * Appends a message with sender and message content to the message list.