From 7f32cd9072c9e67741781d955c51a011164200cd Mon Sep 17 00:00:00 2001 From: Maxi Date: Sat, 5 Oct 2019 10:07:52 +0200 Subject: [PATCH] Added Message List --- src/main/java/envoy/client/EnvoyClient.java | 41 +++--- src/main/java/envoy/client/ui/ChatWindow.java | 124 +++++++++++++++++- 2 files changed, 141 insertions(+), 24 deletions(-) diff --git a/src/main/java/envoy/client/EnvoyClient.java b/src/main/java/envoy/client/EnvoyClient.java index a7f4e6d..9316504 100644 --- a/src/main/java/envoy/client/EnvoyClient.java +++ b/src/main/java/envoy/client/EnvoyClient.java @@ -10,6 +10,7 @@ import javax.ws.rs.core.Response; import javax.xml.datatype.DatatypeConfigurationException; import javax.xml.datatype.DatatypeFactory; +import envoy.client.ui.ChatWindow; import envoy.schema.Message; import envoy.schema.ObjectFactory; @@ -41,24 +42,9 @@ public class EnvoyClient { * @param recipient Name of the recipient * @param textContent Content (text) of the message */ - public void sendMessage(String sender, String recipient, String textContent) { + public void sendMessage(Message message) { + //System.out.println(message.getContent().get(0).getText()); new Thread(() -> { - ObjectFactory factory = new ObjectFactory(); - - Message.MetaData metaData = factory.createMessageMetaData(); - metaData.setSender(sender); - metaData.setRecipient(recipient); - metaData.setState(false); - metaData.setDate(datatypeFactory.newXMLGregorianCalendar(Instant.now().toString())); - - Message.Content content = factory.createMessageContent(); - content.setType("text"); - content.setText(textContent); - - Message message = factory.createMessage(); - message.setMetaData(metaData); - message.getContent().add(content); - Client client = ClientBuilder.newClient(); WebTarget target = client.target("http://localhost:8080/envoy-server/rest/message/send"); Response response = target.request().post(Entity.entity(message, "application/xml")); @@ -67,4 +53,25 @@ public class EnvoyClient { client.close(); }).start(); } + + + public Message createMessage(String senderID, String recipientID, String textContent) { + ObjectFactory factory = new ObjectFactory(); + Message.MetaData metaData = factory.createMessageMetaData(); + metaData.setSender(senderID); + metaData.setRecipient(recipientID); + metaData.setState(false); + metaData.setDate(datatypeFactory.newXMLGregorianCalendar(Instant.now().toString())); + + Message.Content content = factory.createMessageContent(); + content.setType("text"); + content.setText(textContent); + + Message message = factory.createMessage(); + message.setMetaData(metaData); + message.getContent().add(content); + + + return message; + } } \ 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 10fa35a..dde872c 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -1,25 +1,47 @@ package envoy.client.ui; import java.awt.Color; +import java.awt.Component; import java.awt.EventQueue; +import java.awt.Font; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; +import java.util.Iterator; +import java.util.List; +import javax.swing.DefaultListModel; import javax.swing.JButton; import javax.swing.JFrame; +import javax.swing.JList; import javax.swing.JOptionPane; import javax.swing.JPanel; +import javax.swing.JScrollPane; import javax.swing.JTextArea; +import javax.swing.ListModel; +import javax.swing.border.Border; import javax.swing.border.EmptyBorder; +import javax.swing.border.LineBorder; +import javax.swing.border.MatteBorder; import envoy.client.EnvoyClient; +import envoy.schema.Message; + +import javax.swing.ListSelectionModel; +import javax.swing.UIManager; + +import java.awt.Rectangle; +import java.awt.geom.RoundRectangle2D; +import java.awt.ComponentOrientation; +import java.awt.Point; +import java.awt.Dimension; +import javax.swing.border.BevelBorder; /** * Project: envoy-client
* File: ChatWindow.java
* Created: 28 Sep 2019
- * Author: Maximilian Käfer & Kai S. K. Engelbart + * Author: Maximilian Käfer & Kai S. K. Engelbart & Leon Hofmeister */ public class ChatWindow extends JFrame { @@ -28,13 +50,14 @@ public class ChatWindow extends JFrame { private JPanel contentPane = new JPanel(); private EnvoyClient envoyClient = new EnvoyClient(); + public DefaultListModel listModel = new DefaultListModel<>(); public ChatWindow() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 600, 800); setTitle("Envoy"); setLocationRelativeTo(null); - contentPane.setBackground(new Color(220, 220, 220)); + contentPane.setBackground(new Color(0, 0, 0)); contentPane.setForeground(Color.white); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); @@ -45,9 +68,61 @@ public class ChatWindow extends JFrame { gbl_contentPane.rowWeights = new double[] { 0.05, 1, 0.07 }; contentPane.setLayout(gbl_contentPane); - // Message enter field + + // Chat------------------ + List testMessages = List.of(envoyClient.createMessage("UserA", "UserB", "Das ist eine Testnachricht."), envoyClient.createMessage("UserB", "UserA", "Das ist die Antwort auf die Testnachricht.")); + + JList elementList = new JList<>(); + elementList.setFocusTraversalKeysEnabled(false); + + 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)); + + + + elementList.setModel(listModel); + elementList.setFont( new Font("Arial", Font.PLAIN, 17)); + elementList.setFixedCellHeight(60); + elementList.setBorder(new EmptyBorder(5, 5, 5, 5)); + + + //Only temporary for the predefined messages in the List + for (int i = 0; i < testMessages.size(); i++) { + listModel.addElement("" + "

" + getSenderElement(testMessages.get(i)) + " " + "
" + "

" + getFirstContentElement(testMessages.get(i)) + ""); + } + + + + JScrollPane scrollPane = new JScrollPane(); + scrollPane.setForeground(new Color(0, 0, 0)); + scrollPane.setBackground(new Color(51, 51, 51)); + scrollPane.setViewportView(elementList); + 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.insets = new Insets(10, 10, 10, 10); + + contentPane.add(scrollPane, gbc_scrollPane); + + + // Message enter field----------------- JTextArea messageEnterTextfield = new JTextArea(); + messageEnterTextfield.setCaretColor(new Color(255, 255, 255)); + messageEnterTextfield.setForeground(new Color(255, 255, 255)); + messageEnterTextfield.setBackground(new Color(51, 51, 51)); messageEnterTextfield.setLineWrap(true); + messageEnterTextfield.setBorder(null); + messageEnterTextfield.setFont( new Font("Arial", Font.PLAIN, 17)); + messageEnterTextfield.setBorder(new EmptyBorder(5, 5, 5, 5)); GridBagConstraints gbc_moveSelectionMessageEnterTextfield = new GridBagConstraints(); gbc_moveSelectionMessageEnterTextfield.fill = GridBagConstraints.BOTH; @@ -58,10 +133,14 @@ public class ChatWindow extends JFrame { contentPane.add(messageEnterTextfield, gbc_moveSelectionMessageEnterTextfield); - // Post Button + + + + // Post Button----------------- JButton postButton = new JButton("Post"); postButton.setForeground(new Color(255, 255, 255)); - postButton.setBackground(new Color(0, 100, 0)); + postButton.setBackground(new Color(102, 51, 153)); + postButton.setBorderPainted(false); GridBagConstraints gbc_moveSelectionPostButton = new GridBagConstraints(); @@ -71,11 +150,14 @@ public class ChatWindow extends JFrame { gbc_moveSelectionPostButton.insets = new Insets(10, 10, 10, 10); + contentPane.add(postButton, gbc_moveSelectionPostButton); postButton.addActionListener((evt) -> { if (!messageEnterTextfield.getText().isEmpty()) try { - envoyClient.sendMessage("Kai", "Maxi", messageEnterTextfield.getText()); + envoyClient.sendMessage(envoyClient.createMessage("Kai", "Maxi", messageEnterTextfield.getText())); + addMessageToChat("Du", messageEnterTextfield.getText()); + messageEnterTextfield.setText(""); } catch (Exception e) { JOptionPane.showMessageDialog(this, "An exception occured while sending a message. See the log for more details.", @@ -85,7 +167,35 @@ public class ChatWindow extends JFrame { } }); } - + + /** + * gets the Sender from the Message + * @param message + * @return + */ + public String getSenderElement (Message message) { + return message.getMetaData().getSender(); + } + + /** + * gets the First Content Element(text) from the Message + * @param message + * @return + */ + public String getFirstContentElement (Message message) { + return message.getContent().get(0).getText(); + } + + /** + * adds the Sender and the First Content Element(text) of the new Message to the listModel + * @param message + * @return + */ + public void addMessageToChat(String Sender, String Content) { + listModel.addElement("" + "

" + Sender + " " + "
" + "

" + Content + ""); + } + + public static void main(String[] args) { EventQueue.invokeLater(() -> { try {