This repository has been archived on 2021-12-05. You can view files and clone it, but cannot push or open issues or pull requests.
envoy/src/main/java/envoy/client/ui/ChatWindow.java

270 lines
9.4 KiB
Java

package envoy.client.ui;
import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
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.JTextPane;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.border.EmptyBorder;
import envoy.client.Chat;
import envoy.client.Client;
import envoy.client.LocalDB;
import envoy.schema.Message;
import envoy.schema.Messages;
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 Kai S. K. Engelbart
* @author Maximilian K&auml;fer
* @author Leon Hofmeister
* @since Envoy 0.1
*/
public class ChatWindow extends JFrame {
private static final long serialVersionUID = 6865098428255463649L;
private JPanel contentPane = new JPanel();
private Client client;
private LocalDB localDB;
private JList<User> userList = new JList<>();
private Chat currentChat;
public ChatWindow(Client client, LocalDB localDB) {
this.client = client;
this.localDB = localDB;
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 600, 800);
setTitle("Envoy");
setLocationRelativeTo(null);
// Save chats when window closes
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) { localDB.saveToLocalDB(); }
});
contentPane.setBackground(new Color(0, 0, 0));
contentPane.setForeground(Color.white);
contentPane.setBorder(new EmptyBorder(0, 5, 0, 0));
setContentPane(contentPane);
GridBagLayout gbl_contentPane = new GridBagLayout();
gbl_contentPane.columnWidths = new int[] { 1, 1, 1 };
gbl_contentPane.rowHeights = new int[] { 1, 1, 1 };
gbl_contentPane.columnWeights = new double[] { 0.3, 1.0, 0.1 };
gbl_contentPane.rowWeights = new double[] { 0.05, 1, 0.07 };
contentPane.setLayout(gbl_contentPane);
JList<Message> messageList = new JList<>();
messageList.setCellRenderer(new MessageListRenderer());
messageList.setFocusTraversalKeysEnabled(false);
messageList.setSelectionForeground(new Color(255, 255, 255));
messageList.setSelectionBackground(new Color(102, 0, 153));
messageList.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
messageList.setForeground(new Color(255, 255, 255));
messageList.setBackground(new Color(51, 51, 51));
DefaultListModel<Message> messageListModel = new DefaultListModel<>();
messageList.setModel(messageListModel);
messageList.setFont(new Font("Arial", Font.PLAIN, 17));
messageList.setFixedCellHeight(60);
messageList.setBorder(new EmptyBorder(5, 5, 5, 5));
JScrollPane scrollPane = new JScrollPane();
scrollPane.setForeground(new Color(0, 0, 0));
scrollPane.setBackground(new Color(51, 51, 51));
scrollPane.setViewportView(messageList);
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(0, 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;
gbc_moveSelectionMessageEnterTextfield.gridx = 1;
gbc_moveSelectionMessageEnterTextfield.gridy = 2;
gbc_moveSelectionMessageEnterTextfield.insets = new Insets(10, 10, 10, 10);
contentPane.add(messageEnterTextfield, gbc_moveSelectionMessageEnterTextfield);
// Post Button
JButton postButton = new JButton("Post");
postButton.setForeground(new Color(255, 255, 255));
postButton.setBackground(new Color(102, 51, 153));
postButton.setBorderPainted(false);
GridBagConstraints gbc_moveSelectionPostButton = new GridBagConstraints();
gbc_moveSelectionPostButton.fill = GridBagConstraints.BOTH;
gbc_moveSelectionPostButton.gridx = 2;
gbc_moveSelectionPostButton.gridy = 2;
gbc_moveSelectionPostButton.insets = new Insets(10, 10, 10, 10);
postButton.addActionListener((evt) -> {
if (!client.hasRecipient()) {
JOptionPane.showMessageDialog(this, "Please select a recipient!", "Cannot send message", JOptionPane.INFORMATION_MESSAGE);
return;
}
if (!messageEnterTextfield.getText().isEmpty()) try {
// Create and send message object
final Message message = client.createMessage(messageEnterTextfield.getText());
client.sendMessage(message);
// Append message object to chat
currentChat.appendMessage(message);
messageList.setModel(currentChat.getModel());
// Clear text field
messageEnterTextfield.setText("");
contentPane.revalidate();
} catch (Exception e) {
JOptionPane.showMessageDialog(this,
"An exception occured while sending a message. See the log for more details.",
"Exception occured",
JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
}
});
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);
userList.setCellRenderer(new UserListRenderer());
userList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
userList.addListSelectionListener((listSelectionEvent) -> {
if (!listSelectionEvent.getValueIsAdjusting()) {
@SuppressWarnings("unchecked")
final JList<User> selectedUserList = (JList<User>) listSelectionEvent.getSource();
final User user = selectedUserList.getSelectedValue();
client.setRecipient(user);
currentChat = localDB.getChats().stream().filter(chat -> chat.getRecipient().getID() == user.getID()).findFirst().get();
client.setRecipient(user);
textPane.setText(currentChat.getRecipient().getName());
messageList.setModel(currentChat.getModel());
contentPane.revalidate();
}
});
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.fill = GridBagConstraints.VERTICAL;
gbc_userList.gridx = 0;
gbc_userList.gridy = 1;
gbc_userList.anchor = GridBagConstraints.PAGE_START;
gbc_userList.insets = new Insets(0, 0, 10, 0);
contentPane.add(userList, gbc_userList);
contentPane.revalidate();
loadUsersAndChats();
startReceiverThread(5000);
contentPane.revalidate();
}
/**
* Initializes the elements of the user list by downloading them from the
* server.
*/
private void loadUsersAndChats() {
new Thread(() -> {
Users users = client.getUsersListXml();
DefaultListModel<User> userListModel = new DefaultListModel<>();
users.getUser().forEach(user -> {
userListModel.addElement(user);
// Check if user exists in local DB
if (localDB.getChats().stream().filter(c -> c.getRecipient().getID() == user.getID()).count() == 0)
localDB.getChats().add(new Chat(user));
});
SwingUtilities.invokeLater(() -> userList.setModel(userListModel));
}).start();
}
/**
* Checks for new messages and adds them to the chat list.
*
* @param timeout the amount of time that passes between two requests sent to
* the server
*/
private void startReceiverThread(int timeout) {
new Timer(timeout, (evt) -> {
Messages unreadMessages = client.getUnreadMessages(client.getSender().getID());
for (int i = 0; i < unreadMessages.getMessage().size(); i++)
for (int j = 0; j < localDB.getChats().size(); j++)
if (localDB.getChats().get(j).getRecipient().getID() == unreadMessages.getMessage().get(i).getMetaData().getSender())
localDB.getChats().get(j).appendMessage(unreadMessages.getMessage().get(i));
}).start();
}
}