Improved code style, added debug XML output
This commit is contained in:
parent
b4530df1cc
commit
88a3a785b4
@ -7,10 +7,12 @@ import javax.ws.rs.client.ClientBuilder;
|
|||||||
import javax.ws.rs.client.Entity;
|
import javax.ws.rs.client.Entity;
|
||||||
import javax.ws.rs.client.WebTarget;
|
import javax.ws.rs.client.WebTarget;
|
||||||
import javax.ws.rs.core.Response;
|
import javax.ws.rs.core.Response;
|
||||||
|
import javax.xml.bind.JAXBContext;
|
||||||
|
import javax.xml.bind.JAXBException;
|
||||||
|
import javax.xml.bind.Marshaller;
|
||||||
import javax.xml.datatype.DatatypeConfigurationException;
|
import javax.xml.datatype.DatatypeConfigurationException;
|
||||||
import javax.xml.datatype.DatatypeFactory;
|
import javax.xml.datatype.DatatypeFactory;
|
||||||
|
|
||||||
import envoy.client.ui.ChatWindow;
|
|
||||||
import envoy.schema.Message;
|
import envoy.schema.Message;
|
||||||
import envoy.schema.ObjectFactory;
|
import envoy.schema.ObjectFactory;
|
||||||
|
|
||||||
@ -43,10 +45,21 @@ public class EnvoyClient {
|
|||||||
* @param textContent Content (text) of the message
|
* @param textContent Content (text) of the message
|
||||||
*/
|
*/
|
||||||
public void sendMessage(Message message) {
|
public void sendMessage(Message message) {
|
||||||
//System.out.println(message.getContent().get(0).getText());
|
|
||||||
new Thread(() -> {
|
new Thread(() -> {
|
||||||
|
// Print message XML to console
|
||||||
|
JAXBContext jc;
|
||||||
|
try {
|
||||||
|
jc = JAXBContext.newInstance("envoy.schema");
|
||||||
|
Marshaller m = jc.createMarshaller();
|
||||||
|
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
|
||||||
|
m.marshal(message, System.out);
|
||||||
|
} catch (JAXBException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Send message
|
||||||
Client client = ClientBuilder.newClient();
|
Client client = ClientBuilder.newClient();
|
||||||
WebTarget target = client.target("http://localhost:8080/envoy-server/rest/message/send");
|
WebTarget target = client.target("http://kske.feste-ip.net:43315/envoy-server/rest/message/send");
|
||||||
Response response = target.request().post(Entity.entity(message, "application/xml"));
|
Response response = target.request().post(Entity.entity(message, "application/xml"));
|
||||||
System.out.println("Response code: " + response.getStatus());
|
System.out.println("Response code: " + response.getStatus());
|
||||||
response.close();
|
response.close();
|
||||||
@ -54,7 +67,14 @@ public class EnvoyClient {
|
|||||||
}).start();
|
}).start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a {@link Message} object serializable to XML.
|
||||||
|
*
|
||||||
|
* @param senderID The ID of the sender
|
||||||
|
* @param recipientID The ID of the recipient
|
||||||
|
* @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, String recipientID, String textContent) {
|
||||||
ObjectFactory factory = new ObjectFactory();
|
ObjectFactory factory = new ObjectFactory();
|
||||||
Message.MetaData metaData = factory.createMessageMetaData();
|
Message.MetaData metaData = factory.createMessageMetaData();
|
||||||
@ -71,7 +91,6 @@ public class EnvoyClient {
|
|||||||
message.setMetaData(metaData);
|
message.setMetaData(metaData);
|
||||||
message.getContent().add(content);
|
message.getContent().add(content);
|
||||||
|
|
||||||
|
|
||||||
return message;
|
return message;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,14 +1,12 @@
|
|||||||
package envoy.client.ui;
|
package envoy.client.ui;
|
||||||
|
|
||||||
import java.awt.Color;
|
import java.awt.Color;
|
||||||
import java.awt.Component;
|
import java.awt.ComponentOrientation;
|
||||||
import java.awt.EventQueue;
|
import java.awt.EventQueue;
|
||||||
import java.awt.Font;
|
import java.awt.Font;
|
||||||
import java.awt.GridBagConstraints;
|
import java.awt.GridBagConstraints;
|
||||||
import java.awt.GridBagLayout;
|
import java.awt.GridBagLayout;
|
||||||
import java.awt.Insets;
|
import java.awt.Insets;
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import javax.swing.DefaultListModel;
|
import javax.swing.DefaultListModel;
|
||||||
import javax.swing.JButton;
|
import javax.swing.JButton;
|
||||||
@ -18,30 +16,16 @@ import javax.swing.JOptionPane;
|
|||||||
import javax.swing.JPanel;
|
import javax.swing.JPanel;
|
||||||
import javax.swing.JScrollPane;
|
import javax.swing.JScrollPane;
|
||||||
import javax.swing.JTextArea;
|
import javax.swing.JTextArea;
|
||||||
import javax.swing.ListModel;
|
|
||||||
import javax.swing.border.Border;
|
|
||||||
import javax.swing.border.EmptyBorder;
|
import javax.swing.border.EmptyBorder;
|
||||||
import javax.swing.border.LineBorder;
|
|
||||||
import javax.swing.border.MatteBorder;
|
|
||||||
|
|
||||||
import envoy.client.EnvoyClient;
|
import envoy.client.EnvoyClient;
|
||||||
import envoy.schema.Message;
|
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: <strong>envoy-client</strong><br>
|
* Project: <strong>envoy-client</strong><br>
|
||||||
* File: <strong>ChatWindow.java</strong><br>
|
* File: <strong>ChatWindow.java</strong><br>
|
||||||
* Created: <strong>28 Sep 2019</strong><br>
|
* Created: <strong>28 Sep 2019</strong><br>
|
||||||
* Author: <strong>Maximilian Käfer & Kai S. K. Engelbart & Leon Hofmeister</strong>
|
* Author: <strong>Maximilian Käfer & Kai S. K. Engelbart</strong>
|
||||||
*/
|
*/
|
||||||
public class ChatWindow extends JFrame {
|
public class ChatWindow extends JFrame {
|
||||||
|
|
||||||
@ -51,6 +35,7 @@ public class ChatWindow extends JFrame {
|
|||||||
private EnvoyClient envoyClient = new EnvoyClient();
|
private EnvoyClient envoyClient = new EnvoyClient();
|
||||||
|
|
||||||
public DefaultListModel<String> listModel = new DefaultListModel<>();
|
public DefaultListModel<String> listModel = new DefaultListModel<>();
|
||||||
|
|
||||||
public ChatWindow() {
|
public ChatWindow() {
|
||||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
setBounds(100, 100, 600, 800);
|
setBounds(100, 100, 600, 800);
|
||||||
@ -68,10 +53,6 @@ public class ChatWindow extends JFrame {
|
|||||||
gbl_contentPane.rowWeights = new double[] { 0.05, 1, 0.07 };
|
gbl_contentPane.rowWeights = new double[] { 0.05, 1, 0.07 };
|
||||||
contentPane.setLayout(gbl_contentPane);
|
contentPane.setLayout(gbl_contentPane);
|
||||||
|
|
||||||
|
|
||||||
// Chat------------------
|
|
||||||
List<Message> testMessages = List.of(envoyClient.createMessage("UserA", "UserB", "Das ist eine Testnachricht."), envoyClient.createMessage("UserB", "UserA", "Das ist die Antwort auf die Testnachricht."));
|
|
||||||
|
|
||||||
JList<String> elementList = new JList<>();
|
JList<String> elementList = new JList<>();
|
||||||
elementList.setFocusTraversalKeysEnabled(false);
|
elementList.setFocusTraversalKeysEnabled(false);
|
||||||
|
|
||||||
@ -81,21 +62,11 @@ public class ChatWindow extends JFrame {
|
|||||||
elementList.setForeground(new Color(255, 255, 255));
|
elementList.setForeground(new Color(255, 255, 255));
|
||||||
elementList.setBackground(new Color(51, 51, 51));
|
elementList.setBackground(new Color(51, 51, 51));
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
elementList.setModel(listModel);
|
elementList.setModel(listModel);
|
||||||
elementList.setFont( new Font("Arial", Font.PLAIN, 17));
|
elementList.setFont(new Font("Arial", Font.PLAIN, 17));
|
||||||
elementList.setFixedCellHeight(60);
|
elementList.setFixedCellHeight(60);
|
||||||
elementList.setBorder(new EmptyBorder(5, 5, 5, 5));
|
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("<html>" + "<p style=\"color:#d2d235\"> <b> <small>" + getSenderElement(testMessages.get(i)) + "</b> </small>" + "<br>" + "<p style=\"color:white\">" + getFirstContentElement(testMessages.get(i)) + "</span></html>");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
JScrollPane scrollPane = new JScrollPane();
|
JScrollPane scrollPane = new JScrollPane();
|
||||||
scrollPane.setForeground(new Color(0, 0, 0));
|
scrollPane.setForeground(new Color(0, 0, 0));
|
||||||
scrollPane.setBackground(new Color(51, 51, 51));
|
scrollPane.setBackground(new Color(51, 51, 51));
|
||||||
@ -108,20 +79,18 @@ public class ChatWindow extends JFrame {
|
|||||||
gbc_scrollPane.gridx = 1;
|
gbc_scrollPane.gridx = 1;
|
||||||
gbc_scrollPane.gridy = 1;
|
gbc_scrollPane.gridy = 1;
|
||||||
|
|
||||||
|
|
||||||
gbc_scrollPane.insets = new Insets(10, 10, 10, 10);
|
gbc_scrollPane.insets = new Insets(10, 10, 10, 10);
|
||||||
|
|
||||||
contentPane.add(scrollPane, gbc_scrollPane);
|
contentPane.add(scrollPane, gbc_scrollPane);
|
||||||
|
|
||||||
|
// Message enter field
|
||||||
// Message enter field-----------------
|
|
||||||
JTextArea messageEnterTextfield = new JTextArea();
|
JTextArea messageEnterTextfield = new JTextArea();
|
||||||
messageEnterTextfield.setCaretColor(new Color(255, 255, 255));
|
messageEnterTextfield.setCaretColor(new Color(255, 255, 255));
|
||||||
messageEnterTextfield.setForeground(new Color(255, 255, 255));
|
messageEnterTextfield.setForeground(new Color(255, 255, 255));
|
||||||
messageEnterTextfield.setBackground(new Color(51, 51, 51));
|
messageEnterTextfield.setBackground(new Color(51, 51, 51));
|
||||||
messageEnterTextfield.setLineWrap(true);
|
messageEnterTextfield.setLineWrap(true);
|
||||||
messageEnterTextfield.setBorder(null);
|
messageEnterTextfield.setBorder(null);
|
||||||
messageEnterTextfield.setFont( new Font("Arial", Font.PLAIN, 17));
|
messageEnterTextfield.setFont(new Font("Arial", Font.PLAIN, 17));
|
||||||
messageEnterTextfield.setBorder(new EmptyBorder(5, 5, 5, 5));
|
messageEnterTextfield.setBorder(new EmptyBorder(5, 5, 5, 5));
|
||||||
|
|
||||||
GridBagConstraints gbc_moveSelectionMessageEnterTextfield = new GridBagConstraints();
|
GridBagConstraints gbc_moveSelectionMessageEnterTextfield = new GridBagConstraints();
|
||||||
@ -133,10 +102,7 @@ public class ChatWindow extends JFrame {
|
|||||||
|
|
||||||
contentPane.add(messageEnterTextfield, gbc_moveSelectionMessageEnterTextfield);
|
contentPane.add(messageEnterTextfield, gbc_moveSelectionMessageEnterTextfield);
|
||||||
|
|
||||||
|
// Post Button
|
||||||
|
|
||||||
|
|
||||||
// Post Button-----------------
|
|
||||||
JButton postButton = new JButton("Post");
|
JButton postButton = new JButton("Post");
|
||||||
postButton.setForeground(new Color(255, 255, 255));
|
postButton.setForeground(new Color(255, 255, 255));
|
||||||
postButton.setBackground(new Color(102, 51, 153));
|
postButton.setBackground(new Color(102, 51, 153));
|
||||||
@ -150,13 +116,13 @@ public class ChatWindow extends JFrame {
|
|||||||
|
|
||||||
gbc_moveSelectionPostButton.insets = new Insets(10, 10, 10, 10);
|
gbc_moveSelectionPostButton.insets = new Insets(10, 10, 10, 10);
|
||||||
|
|
||||||
|
|
||||||
contentPane.add(postButton, gbc_moveSelectionPostButton);
|
contentPane.add(postButton, gbc_moveSelectionPostButton);
|
||||||
|
|
||||||
postButton.addActionListener((evt) -> {
|
postButton.addActionListener((evt) -> {
|
||||||
if (!messageEnterTextfield.getText().isEmpty()) try {
|
if (!messageEnterTextfield.getText().isEmpty()) try {
|
||||||
envoyClient.sendMessage(envoyClient.createMessage("Kai", "Maxi", messageEnterTextfield.getText()));
|
final Message message = envoyClient.createMessage("Kai", "Maxi", messageEnterTextfield.getText());
|
||||||
addMessageToChat("Du", messageEnterTextfield.getText());
|
envoyClient.sendMessage(message);
|
||||||
|
appendMessageToChat(message);
|
||||||
messageEnterTextfield.setText("");
|
messageEnterTextfield.setText("");
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
JOptionPane.showMessageDialog(this,
|
JOptionPane.showMessageDialog(this,
|
||||||
@ -169,33 +135,26 @@ public class ChatWindow extends JFrame {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* gets the Sender from the Message
|
* Extracts the first text content from a message.
|
||||||
* @param message
|
*
|
||||||
* @return
|
* @param message The message from which to return the text content
|
||||||
|
* @return The first content of type 'text'
|
||||||
*/
|
*/
|
||||||
public String getSenderElement (Message message) {
|
public String getFirstTextContent(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();
|
return message.getContent().get(0).getText();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* adds the Sender and the First Content Element(text) of the new Message to the listModel
|
* Appends a message with sender and message content to the message list.
|
||||||
* @param message
|
*
|
||||||
* @return
|
* @param message The message to append
|
||||||
*/
|
*/
|
||||||
public void addMessageToChat(String Sender, String Content) {
|
private void appendMessageToChat(Message message) {
|
||||||
listModel.addElement("<html>" + "<p style=\"color:#d2d235\"> <b> <small>" + Sender + "</b> </small>" + "<br>" + "<p style=\"color:white\">" + Content + "</span></html>");
|
listModel.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) {
|
public static void main(String[] args) {
|
||||||
EventQueue.invokeLater(() -> {
|
EventQueue.invokeLater(() -> {
|
||||||
try {
|
try {
|
||||||
|
Reference in New Issue
Block a user