Merge pull request #10 from informatik-ag-ngl/f/multiple_chats
Implemented multiple chat system
This commit is contained in:
		
							
								
								
									
										20
									
								
								src/main/java/envoy/client/Chat.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								src/main/java/envoy/client/Chat.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,20 @@
 | 
				
			|||||||
 | 
					package envoy.client;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import javax.swing.DefaultListModel;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import envoy.schema.Message;
 | 
				
			||||||
 | 
					import envoy.schema.User;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					public class Chat {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						private User						recipient;
 | 
				
			||||||
 | 
						private DefaultListModel<Message>	model	= new DefaultListModel<>();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						public Chat(User recipient) { this.recipient = recipient; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						public User getRecipient() { return recipient; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						public void appendMessage(Message message) { model.addElement(message); }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						public DefaultListModel<Message> getModel() { return model; }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -36,7 +36,7 @@ public class Client {
 | 
				
			|||||||
	private ObjectFactory	objectFactory	= new ObjectFactory();
 | 
						private ObjectFactory	objectFactory	= new ObjectFactory();
 | 
				
			||||||
	private DatatypeFactory	datatypeFactory;
 | 
						private DatatypeFactory	datatypeFactory;
 | 
				
			||||||
	private Config			config;
 | 
						private Config			config;
 | 
				
			||||||
	private User			user;
 | 
						private User			sender, recipient;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	public Client(Config config, String username) {
 | 
						public Client(Config config, String username) {
 | 
				
			||||||
		this.config = config;
 | 
							this.config = config;
 | 
				
			||||||
@@ -45,8 +45,7 @@ public class Client {
 | 
				
			|||||||
		} catch (DatatypeConfigurationException e) {
 | 
							} catch (DatatypeConfigurationException e) {
 | 
				
			||||||
			e.printStackTrace();
 | 
								e.printStackTrace();
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		user = getUser(username);
 | 
							sender = getUser(username);
 | 
				
			||||||
		System.out.printf("Mein Name ist %s und ich habe die ID %d%n", user.getName(), user.getID());
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
@@ -89,15 +88,13 @@ public class Client {
 | 
				
			|||||||
	/**
 | 
						/**
 | 
				
			||||||
	 * Creates a {@link Message} object serializable to XML.
 | 
						 * 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
 | 
						 * @param textContent The content (text) of the message
 | 
				
			||||||
	 * @return prepared {@link Message} object
 | 
						 * @return prepared {@link Message} object
 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	public Message createMessage(long senderID, long recipientID, String textContent) {
 | 
						public Message createMessage(String textContent) {
 | 
				
			||||||
		Message.MetaData metaData = objectFactory.createMessageMetaData();
 | 
							Message.MetaData metaData = objectFactory.createMessageMetaData();
 | 
				
			||||||
		metaData.setSender(senderID);
 | 
							metaData.setSender(sender.getID());
 | 
				
			||||||
		metaData.setRecipient(recipientID);
 | 
							metaData.setRecipient(recipient.getID());
 | 
				
			||||||
		metaData.setState(MessageState.WAITING);
 | 
							metaData.setState(MessageState.WAITING);
 | 
				
			||||||
		metaData.setDate(datatypeFactory.newXMLGregorianCalendar(Instant.now().toString()));
 | 
							metaData.setDate(datatypeFactory.newXMLGregorianCalendar(Instant.now().toString()));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -140,7 +137,7 @@ public class Client {
 | 
				
			|||||||
	private User getUser(String name) {
 | 
						private User getUser(String name) {
 | 
				
			||||||
		javax.ws.rs.client.Client	client		= ClientBuilder.newClient();
 | 
							javax.ws.rs.client.Client	client		= ClientBuilder.newClient();
 | 
				
			||||||
		WebTarget					target		= client.target(String
 | 
							WebTarget					target		= client.target(String
 | 
				
			||||||
			.format("%s:%d/envoy-server/rest/user/sender?name=" + name, config.getServer(), config.getPort()));
 | 
								.format("%s:%d/envoy-server/rest/user/sender?name=%s", config.getServer(), config.getPort(), name));
 | 
				
			||||||
		Response					response	= target.request("application/xml").get();
 | 
							Response					response	= target.request("application/xml").get();
 | 
				
			||||||
		Users						users		= response.readEntity(Users.class);
 | 
							Users						users		= response.readEntity(Users.class);
 | 
				
			||||||
		System.out.println("Response code: " + response.getStatus());
 | 
							System.out.println("Response code: " + response.getStatus());
 | 
				
			||||||
@@ -149,9 +146,27 @@ public class Client {
 | 
				
			|||||||
		return users.getUser().get(0);
 | 
							return users.getUser().get(0);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						public Messages getUnreadMessages(long userId) {
 | 
				
			||||||
 | 
							javax.ws.rs.client.Client	client			= ClientBuilder.newClient();
 | 
				
			||||||
 | 
							WebTarget					target			= client.target(String
 | 
				
			||||||
 | 
								.format("%s:%d/envoy-server/rest/message/receive?userId=%d", config.getServer(), config.getPort(), userId));
 | 
				
			||||||
 | 
							Response					response		= target.request("application/xml").get();
 | 
				
			||||||
 | 
							Messages					unreadMessages	= response.readEntity(Messages.class);
 | 
				
			||||||
 | 
							System.out.println("Response code: " + response.getStatus());
 | 
				
			||||||
 | 
							response.close();
 | 
				
			||||||
 | 
							client.close();
 | 
				
			||||||
 | 
							return unreadMessages;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
	 * @return the user id of this client
 | 
						 * @return the sender object that represents this client
 | 
				
			||||||
	 * @since Envoy 0.1
 | 
						 * @since Envoy 0.1
 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	public long getSenderID() { return user.getID(); }
 | 
						public User getSender() { return sender; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						public User getRecipient() { return recipient; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						public void setRecipient(User recipient) { this.recipient = recipient; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						public boolean hasRecipient() { return recipient != null; }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -6,6 +6,8 @@ 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.ArrayList;
 | 
				
			||||||
 | 
					import java.util.List;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import javax.swing.DefaultListModel;
 | 
					import javax.swing.DefaultListModel;
 | 
				
			||||||
import javax.swing.JButton;
 | 
					import javax.swing.JButton;
 | 
				
			||||||
@@ -15,12 +17,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.JTextPane;
 | 
				
			||||||
import javax.swing.ListSelectionModel;
 | 
					import javax.swing.ListSelectionModel;
 | 
				
			||||||
import javax.swing.SwingUtilities;
 | 
					import javax.swing.SwingUtilities;
 | 
				
			||||||
 | 
					import javax.swing.Timer;
 | 
				
			||||||
import javax.swing.border.EmptyBorder;
 | 
					import javax.swing.border.EmptyBorder;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import envoy.client.Chat;
 | 
				
			||||||
import envoy.client.Client;
 | 
					import envoy.client.Client;
 | 
				
			||||||
import envoy.schema.Message;
 | 
					import envoy.schema.Message;
 | 
				
			||||||
 | 
					import envoy.schema.Messages;
 | 
				
			||||||
import envoy.schema.User;
 | 
					import envoy.schema.User;
 | 
				
			||||||
import envoy.schema.Users;
 | 
					import envoy.schema.Users;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -38,16 +44,22 @@ 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 Client client;
 | 
						private Client client;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	private DefaultListModel<String> messageListModel = new DefaultListModel<>();
 | 
						private DefaultListModel<Message>	messageListModel	= new DefaultListModel<>();
 | 
				
			||||||
 | 
						private List<Chat>					partnerChatList		= new ArrayList<Chat>();
 | 
				
			||||||
 | 
						private Chat						currentChat;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	public ChatWindow(Client client) {
 | 
						public ChatWindow(Client client) {
 | 
				
			||||||
		this.client = client;
 | 
							this.client = client;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// Initialize chat list and current chat
 | 
				
			||||||
 | 
							Users chatUsers = client.getUsersListXml();
 | 
				
			||||||
 | 
							chatUsers.getUser().forEach(user -> partnerChatList.add(new Chat(user)));
 | 
				
			||||||
 | 
							if (partnerChatList.size() > 0) currentChat = partnerChatList.get(0);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 | 
							setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 | 
				
			||||||
		setBounds(100, 100, 600, 800);
 | 
							setBounds(100, 100, 600, 800);
 | 
				
			||||||
		setTitle("Envoy");
 | 
							setTitle("Envoy");
 | 
				
			||||||
@@ -55,7 +67,7 @@ public class ChatWindow extends JFrame {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
		contentPane.setBackground(new Color(0, 0, 0));
 | 
							contentPane.setBackground(new Color(0, 0, 0));
 | 
				
			||||||
		contentPane.setForeground(Color.white);
 | 
							contentPane.setForeground(Color.white);
 | 
				
			||||||
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
 | 
							contentPane.setBorder(new EmptyBorder(0, 5, 0, 0));
 | 
				
			||||||
		setContentPane(contentPane);
 | 
							setContentPane(contentPane);
 | 
				
			||||||
		GridBagLayout gbl_contentPane = new GridBagLayout();
 | 
							GridBagLayout gbl_contentPane = new GridBagLayout();
 | 
				
			||||||
		gbl_contentPane.columnWidths	= new int[] { 1, 1, 1 };
 | 
							gbl_contentPane.columnWidths	= new int[] { 1, 1, 1 };
 | 
				
			||||||
@@ -64,24 +76,25 @@ 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);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		JList<String> elementList = new JList<>();
 | 
							JList<Message> messageList = new JList<>();
 | 
				
			||||||
		elementList.setFocusTraversalKeysEnabled(false);
 | 
							messageList.setCellRenderer(new MessageListRenderer());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		elementList.setSelectionForeground(new Color(255, 255, 255));
 | 
							messageList.setFocusTraversalKeysEnabled(false);
 | 
				
			||||||
		elementList.setSelectionBackground(new Color(102, 0, 153));
 | 
							messageList.setSelectionForeground(new Color(255, 255, 255));
 | 
				
			||||||
		elementList.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
 | 
							messageList.setSelectionBackground(new Color(102, 0, 153));
 | 
				
			||||||
		elementList.setForeground(new Color(255, 255, 255));
 | 
							messageList.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
 | 
				
			||||||
		elementList.setBackground(new Color(51, 51, 51));
 | 
							messageList.setForeground(new Color(255, 255, 255));
 | 
				
			||||||
 | 
							messageList.setBackground(new Color(51, 51, 51));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		elementList.setModel(messageListModel);
 | 
							messageList.setModel(messageListModel);
 | 
				
			||||||
		elementList.setFont(new Font("Arial", Font.PLAIN, 17));
 | 
							messageList.setFont(new Font("Arial", Font.PLAIN, 17));
 | 
				
			||||||
		elementList.setFixedCellHeight(60);
 | 
							messageList.setFixedCellHeight(60);
 | 
				
			||||||
		elementList.setBorder(new EmptyBorder(5, 5, 5, 5));
 | 
							messageList.setBorder(new EmptyBorder(5, 5, 5, 5));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		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));
 | 
				
			||||||
		scrollPane.setViewportView(elementList);
 | 
							scrollPane.setViewportView(messageList);
 | 
				
			||||||
		scrollPane.setBorder(null);
 | 
							scrollPane.setBorder(null);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		GridBagConstraints gbc_scrollPane = new GridBagConstraints();
 | 
							GridBagConstraints gbc_scrollPane = new GridBagConstraints();
 | 
				
			||||||
@@ -90,7 +103,7 @@ 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(0, 10, 10, 10);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		contentPane.add(scrollPane, gbc_scrollPane);
 | 
							contentPane.add(scrollPane, gbc_scrollPane);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -127,18 +140,28 @@ 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);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		postButton.addActionListener((evt) -> {
 | 
							postButton.addActionListener((evt) -> {
 | 
				
			||||||
			if (recipientID == 0) System.out.println("Please select recipient");
 | 
								if (!client.hasRecipient()) {
 | 
				
			||||||
 | 
									JOptionPane.showMessageDialog(this,
 | 
				
			||||||
 | 
											"Please select a recipient!",
 | 
				
			||||||
 | 
											"Cannot send message",
 | 
				
			||||||
 | 
											JOptionPane.INFORMATION_MESSAGE);
 | 
				
			||||||
 | 
									return;
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			// TODO: Acquire proper sender id
 | 
								if (!messageEnterTextfield.getText().isEmpty()) try {
 | 
				
			||||||
			if (!messageEnterTextfield.getText().isEmpty() && recipientID != 0) try {
 | 
					
 | 
				
			||||||
				final Message message = client
 | 
									// Create and send message object
 | 
				
			||||||
					.createMessage(client.getSenderID(), recipientID, messageEnterTextfield.getText());
 | 
									final Message message = client.createMessage(messageEnterTextfield.getText());
 | 
				
			||||||
				client.sendMessage(message);
 | 
									client.sendMessage(message);
 | 
				
			||||||
				appendMessageToChat(message);
 | 
					
 | 
				
			||||||
 | 
									// Append message object to chat
 | 
				
			||||||
 | 
									currentChat.appendMessage(message);
 | 
				
			||||||
 | 
									messageList.setModel(currentChat.getModel());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									// Clear text field
 | 
				
			||||||
				messageEnterTextfield.setText("");
 | 
									messageEnterTextfield.setText("");
 | 
				
			||||||
 | 
									contentPane.revalidate();
 | 
				
			||||||
			} catch (Exception e) {
 | 
								} catch (Exception e) {
 | 
				
			||||||
				JOptionPane.showMessageDialog(this,
 | 
									JOptionPane.showMessageDialog(this,
 | 
				
			||||||
						"An exception occured while sending a message. See the log for more details.",
 | 
											"An exception occured while sending a message. See the log for more details.",
 | 
				
			||||||
@@ -148,14 +171,44 @@ public class ChatWindow extends JFrame {
 | 
				
			|||||||
			}
 | 
								}
 | 
				
			||||||
		});
 | 
							});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							contentPane.add(postButton, gbc_moveSelectionPostButton);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// Partner name display
 | 
				
			||||||
 | 
							JTextPane textPane = new JTextPane();
 | 
				
			||||||
 | 
							textPane.setBackground(new Color(0, 0, 0));
 | 
				
			||||||
 | 
							textPane.setForeground(new Color(255, 255, 255));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							textPane.setFont(new Font("Arial", Font.PLAIN, 20));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							GridBagConstraints gbc_partnerName = new GridBagConstraints();
 | 
				
			||||||
 | 
							gbc_partnerName.fill		= GridBagConstraints.HORIZONTAL;
 | 
				
			||||||
 | 
							gbc_partnerName.gridwidth	= 2;
 | 
				
			||||||
 | 
							gbc_partnerName.gridx		= 1;
 | 
				
			||||||
 | 
							gbc_partnerName.gridy		= 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							gbc_partnerName.insets = new Insets(0, 10, 0, 10);
 | 
				
			||||||
 | 
							contentPane.add(textPane, gbc_partnerName);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		JList<User> userList = new JList<>();
 | 
							JList<User> userList = new JList<>();
 | 
				
			||||||
		userList.setCellRenderer(new UserListRenderer());
 | 
							userList.setCellRenderer(new UserListRenderer());
 | 
				
			||||||
		userList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
 | 
							userList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
 | 
				
			||||||
		userList.addListSelectionListener((listSelectionEvent) -> {
 | 
							userList.addListSelectionListener((listSelectionEvent) -> {
 | 
				
			||||||
			if (!listSelectionEvent.getValueIsAdjusting()) {
 | 
								if (!listSelectionEvent.getValueIsAdjusting()) {
 | 
				
			||||||
				@SuppressWarnings("unchecked")
 | 
									@SuppressWarnings("unchecked")
 | 
				
			||||||
				JList<User> selectedUserList = (JList<User>) listSelectionEvent.getSource();
 | 
									final JList<User>	selectedUserList	= (JList<User>) listSelectionEvent.getSource();
 | 
				
			||||||
				recipientID = selectedUserList.getModel().getElementAt(selectedUserList.getSelectedIndex()).getID();
 | 
									final User			user				= selectedUserList.getSelectedValue();
 | 
				
			||||||
 | 
									client.setRecipient(user);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									currentChat = partnerChatList.stream()
 | 
				
			||||||
 | 
										.filter(chat -> chat.getRecipient().getID() == user.getID())
 | 
				
			||||||
 | 
										.findFirst()
 | 
				
			||||||
 | 
										.get();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									client.setRecipient(user);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									textPane.setText(currentChat.getRecipient().getName());
 | 
				
			||||||
 | 
									messageList.setModel(currentChat.getModel());
 | 
				
			||||||
 | 
									contentPane.revalidate();
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		});
 | 
							});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -167,15 +220,29 @@ public class ChatWindow extends JFrame {
 | 
				
			|||||||
		userList.setBorder(new EmptyBorder(5, 5, 5, 5));
 | 
							userList.setBorder(new EmptyBorder(5, 5, 5, 5));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		GridBagConstraints gbc_userList = new GridBagConstraints();
 | 
							GridBagConstraints gbc_userList = new GridBagConstraints();
 | 
				
			||||||
 | 
							gbc_userList.fill	= GridBagConstraints.VERTICAL;
 | 
				
			||||||
		gbc_userList.gridx	= 0;
 | 
							gbc_userList.gridx	= 0;
 | 
				
			||||||
		gbc_userList.gridy	= 1;
 | 
							gbc_userList.gridy	= 1;
 | 
				
			||||||
		gbc_userList.anchor	= GridBagConstraints.PAGE_START;
 | 
							gbc_userList.anchor	= GridBagConstraints.PAGE_START;
 | 
				
			||||||
		gbc_userList.insets	= new Insets(10, 0, 10, 0);
 | 
							gbc_userList.insets	= new Insets(0, 0, 10, 0);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		contentPane.add(userList, gbc_userList);
 | 
							contentPane.add(userList, gbc_userList);
 | 
				
			||||||
		contentPane.revalidate();
 | 
							contentPane.revalidate();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		loadUserList(userList);
 | 
							loadUserList(userList);
 | 
				
			||||||
 | 
							
 | 
				
			||||||
 | 
							new Timer(5000, (evt) -> {
 | 
				
			||||||
 | 
								Messages unreadMessages = client.getUnreadMessages(client.getSender().getID());
 | 
				
			||||||
 | 
								System.out.println(unreadMessages.getMessage().get(0));
 | 
				
			||||||
 | 
								for (int i = 0; i < unreadMessages.getMessage().size(); i++) {
 | 
				
			||||||
 | 
									for (int j = 0; j < partnerChatList.size(); j++) {
 | 
				
			||||||
 | 
										if(partnerChatList.get(j).getRecipient().getID() == unreadMessages.getMessage().get(i).getMetaData().getSender()) {
 | 
				
			||||||
 | 
											partnerChatList.get(j).appendMessage(unreadMessages.getMessage().get(i));
 | 
				
			||||||
 | 
										}
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}).start();
 | 
				
			||||||
 | 
							contentPane.revalidate();
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
@@ -192,23 +259,4 @@ public class ChatWindow extends JFrame {
 | 
				
			|||||||
			SwingUtilities.invokeLater(() -> userList.setModel(userListModel));
 | 
								SwingUtilities.invokeLater(() -> userList.setModel(userListModel));
 | 
				
			||||||
		}).start();
 | 
							}).start();
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					 | 
				
			||||||
	/**
 | 
					 | 
				
			||||||
	 * Extracts the first text content from a message.
 | 
					 | 
				
			||||||
	 * 
 | 
					 | 
				
			||||||
	 * @param message The message from which to return the text content
 | 
					 | 
				
			||||||
	 * @return The first content of type 'text'
 | 
					 | 
				
			||||||
	 */
 | 
					 | 
				
			||||||
	private String getFirstTextContent(Message message) { return message.getContent().get(0).getText(); }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	/**
 | 
					 | 
				
			||||||
	 * Appends a message with sender and message content to the message list.
 | 
					 | 
				
			||||||
	 * 
 | 
					 | 
				
			||||||
	 * @param message The message to append
 | 
					 | 
				
			||||||
	 */
 | 
					 | 
				
			||||||
	private void appendMessageToChat(Message message) {
 | 
					 | 
				
			||||||
		messageListModel.addElement("<html>" + "<p style=\"color:#d2d235\"> <b> <small>"
 | 
					 | 
				
			||||||
				+ message.getMetaData().getSender() + "</b> </small>" + "<br>" + "<p style=\"color:white\">"
 | 
					 | 
				
			||||||
				+ getFirstTextContent(message) + "</span></html>");
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
							
								
								
									
										44
									
								
								src/main/java/envoy/client/ui/MessageListRenderer.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										44
									
								
								src/main/java/envoy/client/ui/MessageListRenderer.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,44 @@
 | 
				
			|||||||
 | 
					package envoy.client.ui;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.awt.Component;
 | 
				
			||||||
 | 
					import java.text.SimpleDateFormat;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import javax.swing.JLabel;
 | 
				
			||||||
 | 
					import javax.swing.JList;
 | 
				
			||||||
 | 
					import javax.swing.ListCellRenderer;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import envoy.schema.Message;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * Project: <strong>envoy-client</strong><br>
 | 
				
			||||||
 | 
					 * File: <strong>UserListRenderer.java</strong><br>
 | 
				
			||||||
 | 
					 * Created: <strong>19 Oct 2019</strong><br>
 | 
				
			||||||
 | 
					 * 
 | 
				
			||||||
 | 
					 * @author Kai S. K. Engelbart
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					public class MessageListRenderer extends JLabel implements ListCellRenderer<Message> {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						private static final long serialVersionUID = 5164417379767181198L;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						@Override
 | 
				
			||||||
 | 
						public Component getListCellRendererComponent(JList<? extends Message> list, Message value, int index,
 | 
				
			||||||
 | 
								boolean isSelected, boolean cellHasFocus) {
 | 
				
			||||||
 | 
							if (isSelected) {
 | 
				
			||||||
 | 
								setBackground(list.getSelectionBackground());
 | 
				
			||||||
 | 
								setForeground(list.getSelectionForeground());
 | 
				
			||||||
 | 
							} else {
 | 
				
			||||||
 | 
								setBackground(list.getBackground());
 | 
				
			||||||
 | 
								setForeground(list.getForeground());
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							setOpaque(true);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							setText(String.format(
 | 
				
			||||||
 | 
									"<html><p style=\"color:#d2d235\"><b><small>%s</b></small><br><p style=\"color:white\">%s</html>",
 | 
				
			||||||
 | 
									new SimpleDateFormat("dd.MM.yyyy hh:mm ")
 | 
				
			||||||
 | 
										.format(value.getMetaData().getDate().toGregorianCalendar().getTime()),
 | 
				
			||||||
 | 
									value.getContent().get(0).getText()));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							return this;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user