Merge pull request #3 from informatik-ag-ngl/f/user_list
Added user list to chat window
This commit is contained in:
commit
5d454e05f9
@ -17,14 +17,19 @@ import javax.xml.datatype.DatatypeConfigurationException;
|
|||||||
import javax.xml.datatype.DatatypeFactory;
|
import javax.xml.datatype.DatatypeFactory;
|
||||||
|
|
||||||
import envoy.schema.Message;
|
import envoy.schema.Message;
|
||||||
|
import envoy.schema.Message.MetaData.MessageState;
|
||||||
import envoy.schema.Messages;
|
import envoy.schema.Messages;
|
||||||
import envoy.schema.ObjectFactory;
|
import envoy.schema.ObjectFactory;
|
||||||
|
import envoy.schema.Users;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Project: <strong>envoy-client</strong><br>
|
* Project: <strong>envoy-client</strong><br>
|
||||||
* File: <strong>EnvoyClient.java</strong><br>
|
* File: <strong>EnvoyClient.java</strong><br>
|
||||||
* Created: <strong>28 Sep 2019</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 {
|
public class EnvoyClient {
|
||||||
@ -82,6 +87,7 @@ public class EnvoyClient {
|
|||||||
WebTarget target = client.target(String.format("%s:%s/envoy-server/rest/message/send",
|
WebTarget target = client.target(String.format("%s:%s/envoy-server/rest/message/send",
|
||||||
serverProps.getProperty("server"),
|
serverProps.getProperty("server"),
|
||||||
serverProps.getProperty("port")));
|
serverProps.getProperty("port")));
|
||||||
|
|
||||||
Response response = target.request().post(Entity.entity(messages, "application/xml"));
|
Response response = target.request().post(Entity.entity(messages, "application/xml"));
|
||||||
System.out.println("Response code: " + response.getStatus());
|
System.out.println("Response code: " + response.getStatus());
|
||||||
response.close();
|
response.close();
|
||||||
@ -97,11 +103,11 @@ public class EnvoyClient {
|
|||||||
* @param textContent The content (text) of the message
|
* @param textContent The content (text) of the message
|
||||||
* @return Prepared {@link Message} object
|
* @return Prepared {@link Message} object
|
||||||
*/
|
*/
|
||||||
public Message createMessage(String senderID, String recipientID, String textContent) {
|
public Message createMessage(long senderID, long recipientID, String textContent) {
|
||||||
Message.MetaData metaData = objectFactory.createMessageMetaData();
|
Message.MetaData metaData = objectFactory.createMessageMetaData();
|
||||||
metaData.setSender(senderID);
|
metaData.setSender(senderID);
|
||||||
metaData.setRecipient(recipientID);
|
metaData.setRecipient(recipientID);
|
||||||
metaData.setState(false);
|
metaData.setState(MessageState.WAITING);
|
||||||
metaData.setDate(datatypeFactory.newXMLGregorianCalendar(Instant.now().toString()));
|
metaData.setDate(datatypeFactory.newXMLGregorianCalendar(Instant.now().toString()));
|
||||||
|
|
||||||
Message.Content content = objectFactory.createMessageContent();
|
Message.Content content = objectFactory.createMessageContent();
|
||||||
@ -120,4 +126,17 @@ public class EnvoyClient {
|
|||||||
wrapper.getMessage().addAll(Arrays.asList(messages));
|
wrapper.getMessage().addAll(Arrays.asList(messages));
|
||||||
return wrapper;
|
return wrapper;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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")));
|
||||||
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
@ -7,6 +7,7 @@ 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.io.IOException;
|
||||||
|
|
||||||
import javax.swing.DefaultListModel;
|
import javax.swing.DefaultListModel;
|
||||||
import javax.swing.JButton;
|
import javax.swing.JButton;
|
||||||
@ -16,25 +17,37 @@ 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.ListSelectionModel;
|
||||||
|
import javax.swing.SwingUtilities;
|
||||||
import javax.swing.border.EmptyBorder;
|
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.client.EnvoyClient;
|
||||||
import envoy.schema.Message;
|
import envoy.schema.Message;
|
||||||
|
import envoy.schema.User;
|
||||||
|
import envoy.schema.Users;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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</strong>
|
*
|
||||||
|
* @author Kai S. K. Engelbart
|
||||||
|
* @author Maximilian Käfer
|
||||||
|
* @since Envoy 0.1
|
||||||
*/
|
*/
|
||||||
public class ChatWindow extends JFrame {
|
public class ChatWindow extends JFrame {
|
||||||
|
|
||||||
private static final long serialVersionUID = 6865098428255463649L;
|
private static final long serialVersionUID = 6865098428255463649L;
|
||||||
|
|
||||||
|
private long recipientID = 0;
|
||||||
private JPanel contentPane = new JPanel();
|
private JPanel contentPane = new JPanel();
|
||||||
private EnvoyClient envoyClient = new EnvoyClient();
|
private EnvoyClient envoyClient = new EnvoyClient();
|
||||||
|
|
||||||
public DefaultListModel<String> listModel = new DefaultListModel<>();
|
private DefaultListModel<String> messageListModel = new DefaultListModel<>();
|
||||||
|
|
||||||
public ChatWindow() {
|
public ChatWindow() {
|
||||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
@ -62,7 +75,7 @@ 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(messageListModel);
|
||||||
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));
|
||||||
@ -119,8 +132,11 @@ public class ChatWindow extends JFrame {
|
|||||||
contentPane.add(postButton, gbc_moveSelectionPostButton);
|
contentPane.add(postButton, gbc_moveSelectionPostButton);
|
||||||
|
|
||||||
postButton.addActionListener((evt) -> {
|
postButton.addActionListener((evt) -> {
|
||||||
if (!messageEnterTextfield.getText().isEmpty()) try {
|
if (recipientID == 0) System.out.println("Please select recipient");
|
||||||
final Message message = envoyClient.createMessage("Kai", "Maxi", messageEnterTextfield.getText());
|
|
||||||
|
// TODO: Acquire proper sender id
|
||||||
|
if (!messageEnterTextfield.getText().isEmpty() && recipientID != 0) try {
|
||||||
|
final Message message = envoyClient.createMessage(1, recipientID, messageEnterTextfield.getText());
|
||||||
envoyClient.sendMessage(message);
|
envoyClient.sendMessage(message);
|
||||||
appendMessageToChat(message);
|
appendMessageToChat(message);
|
||||||
messageEnterTextfield.setText("");
|
messageEnterTextfield.setText("");
|
||||||
@ -132,6 +148,50 @@ public class ChatWindow extends JFrame {
|
|||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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<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();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -140,9 +200,7 @@ public class ChatWindow extends JFrame {
|
|||||||
* @param message The message from which to return the text content
|
* @param message The message from which to return the text content
|
||||||
* @return The first content of type 'text'
|
* @return The first content of type 'text'
|
||||||
*/
|
*/
|
||||||
public String getFirstTextContent(Message message) {
|
private String getFirstTextContent(Message message) { return message.getContent().get(0).getText(); }
|
||||||
return message.getContent().get(0).getText();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Appends a message with sender and message content to the message list.
|
* Appends a message with sender and message content to the message list.
|
||||||
@ -150,12 +208,13 @@ public class ChatWindow extends JFrame {
|
|||||||
* @param message The message to append
|
* @param message The message to append
|
||||||
*/
|
*/
|
||||||
private void appendMessageToChat(Message message) {
|
private void appendMessageToChat(Message message) {
|
||||||
listModel.addElement("<html>" + "<p style=\"color:#d2d235\"> <b> <small>" + message.getMetaData().getSender()
|
messageListModel.addElement("<html>" + "<p style=\"color:#d2d235\"> <b> <small>"
|
||||||
+ "</b> </small>" + "<br>" + "<p style=\"color:white\">" + getFirstTextContent(message)
|
+ message.getMetaData().getSender() + "</b> </small>" + "<br>" + "<p style=\"color:white\">"
|
||||||
+ "</span></html>");
|
+ getFirstTextContent(message) + "</span></html>");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args)
|
||||||
|
throws ClientProtocolException, IOException, SAXException, ParserConfigurationException {
|
||||||
EventQueue.invokeLater(() -> {
|
EventQueue.invokeLater(() -> {
|
||||||
try {
|
try {
|
||||||
ChatWindow frame = new ChatWindow();
|
ChatWindow frame = new ChatWindow();
|
||||||
|
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