Merge pull request #3 from informatik-ag-ngl/f/user_list
Added user list to chat window
This commit is contained in:
commit
aab15213d7
@ -17,14 +17,19 @@ import javax.xml.datatype.DatatypeConfigurationException;
|
||||
import javax.xml.datatype.DatatypeFactory;
|
||||
|
||||
import envoy.schema.Message;
|
||||
import envoy.schema.Message.MetaData.MessageState;
|
||||
import envoy.schema.Messages;
|
||||
import envoy.schema.ObjectFactory;
|
||||
import envoy.schema.Users;
|
||||
|
||||
/**
|
||||
* Project: <strong>envoy-client</strong><br>
|
||||
* File: <strong>EnvoyClient.java</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 {
|
||||
@ -82,6 +87,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();
|
||||
@ -97,11 +103,11 @@ 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(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();
|
||||
@ -120,4 +126,17 @@ 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",
|
||||
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.GridBagLayout;
|
||||
import java.awt.Insets;
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.swing.DefaultListModel;
|
||||
import javax.swing.JButton;
|
||||
@ -16,25 +17,37 @@ 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.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;
|
||||
import envoy.schema.Users;
|
||||
|
||||
/**
|
||||
* Project: <strong>envoy-client</strong><br>
|
||||
* File: <strong>ChatWindow.java</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 {
|
||||
|
||||
private static final long serialVersionUID = 6865098428255463649L;
|
||||
|
||||
private long recipientID = 0;
|
||||
private JPanel contentPane = new JPanel();
|
||||
private EnvoyClient envoyClient = new EnvoyClient();
|
||||
|
||||
public DefaultListModel<String> listModel = new DefaultListModel<>();
|
||||
private DefaultListModel<String> messageListModel = new DefaultListModel<>();
|
||||
|
||||
public ChatWindow() {
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
@ -62,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));
|
||||
@ -119,8 +132,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");
|
||||
|
||||
// TODO: Acquire proper sender id
|
||||
if (!messageEnterTextfield.getText().isEmpty() && recipientID != 0) try {
|
||||
final Message message = envoyClient.createMessage(1, recipientID, messageEnterTextfield.getText());
|
||||
envoyClient.sendMessage(message);
|
||||
appendMessageToChat(message);
|
||||
messageEnterTextfield.setText("");
|
||||
@ -132,6 +148,50 @@ public class ChatWindow extends JFrame {
|
||||
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
|
||||
* @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.
|
||||
@ -150,12 +208,13 @@ public class ChatWindow extends JFrame {
|
||||
* @param message The message to append
|
||||
*/
|
||||
private void appendMessageToChat(Message message) {
|
||||
listModel.addElement("<html>" + "<p style=\"color:#d2d235\"> <b> <small>" + message.getMetaData().getSender()
|
||||
+ "</b> </small>" + "<br>" + "<p style=\"color:white\">" + getFirstTextContent(message)
|
||||
+ "</span></html>");
|
||||
messageListModel.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)
|
||||
throws ClientProtocolException, IOException, SAXException, ParserConfigurationException {
|
||||
EventQueue.invokeLater(() -> {
|
||||
try {
|
||||
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