2019-12-07 10:44:25 +01:00
|
|
|
package envoy.client.ui;
|
|
|
|
|
2019-12-21 07:54:15 +01:00
|
|
|
import java.awt.*;
|
2019-12-07 10:44:25 +01:00
|
|
|
import java.awt.event.KeyAdapter;
|
|
|
|
import java.awt.event.KeyEvent;
|
|
|
|
import java.util.logging.Level;
|
2019-12-20 11:59:11 +01:00
|
|
|
import java.util.logging.Logger;
|
2019-12-07 10:44:25 +01:00
|
|
|
|
2019-12-21 07:54:15 +01:00
|
|
|
import javax.swing.*;
|
2019-12-07 10:44:25 +01:00
|
|
|
import javax.swing.border.EmptyBorder;
|
|
|
|
|
2019-12-21 07:54:15 +01:00
|
|
|
import envoy.client.*;
|
2020-01-29 07:44:25 +01:00
|
|
|
import envoy.client.event.MessageCreationEvent;
|
2019-12-16 09:41:21 +01:00
|
|
|
import envoy.client.event.ThemeChangeEvent;
|
2020-01-26 22:10:15 +01:00
|
|
|
import envoy.client.ui.list.ComponentList;
|
2019-12-21 00:29:16 +01:00
|
|
|
import envoy.client.ui.settings.SettingsScreen;
|
2019-12-20 15:05:31 +01:00
|
|
|
import envoy.client.util.EnvoyLog;
|
2019-12-28 21:20:43 +01:00
|
|
|
import envoy.data.Message;
|
2019-12-31 10:27:52 +01:00
|
|
|
import envoy.data.MessageBuilder;
|
2019-12-28 21:20:43 +01:00
|
|
|
import envoy.data.User;
|
2019-12-29 11:54:05 +01:00
|
|
|
import envoy.event.EventBus;
|
2019-12-07 10:44:25 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Project: <strong>envoy-client</strong><br>
|
|
|
|
* File: <strong>ChatWindow.java</strong><br>
|
|
|
|
* Created: <strong>28 Sep 2019</strong><br>
|
2019-12-07 11:22:47 +01:00
|
|
|
*
|
2019-12-07 10:44:25 +01:00
|
|
|
* @author Kai S. K. Engelbart
|
|
|
|
* @author Maximilian Käfer
|
|
|
|
* @author Leon Hofmeister
|
|
|
|
* @since Envoy v0.1-alpha
|
|
|
|
*/
|
|
|
|
public class ChatWindow extends JFrame {
|
|
|
|
|
2019-12-15 16:26:11 +01:00
|
|
|
// User specific objects
|
2019-12-07 10:44:25 +01:00
|
|
|
private Client client;
|
|
|
|
private LocalDB localDB;
|
2019-12-15 16:26:11 +01:00
|
|
|
|
2019-12-07 14:50:20 +01:00
|
|
|
// GUI components
|
2020-01-27 20:11:47 +01:00
|
|
|
private JPanel contentPane = new JPanel();
|
|
|
|
private PrimaryTextArea messageEnterTextArea = new PrimaryTextArea(space);
|
|
|
|
private JList<User> userList = new JList<>();
|
|
|
|
private Chat currentChat;
|
|
|
|
private ComponentList<Message> messageList = new ComponentList<>(new MessageListRenderer());
|
|
|
|
private PrimaryScrollPane scrollPane = new PrimaryScrollPane();
|
|
|
|
private JTextPane textPane = new JTextPane();
|
|
|
|
private PrimaryButton postButton = new PrimaryButton("Post");
|
|
|
|
private PrimaryButton settingsButton = new PrimaryButton("Settings");
|
2019-12-07 13:02:38 +01:00
|
|
|
|
2019-12-20 11:59:11 +01:00
|
|
|
private static final Logger logger = EnvoyLog.getLogger(ChatWindow.class.getSimpleName());
|
2019-12-07 10:44:25 +01:00
|
|
|
|
2020-01-27 20:11:47 +01:00
|
|
|
// GUI component spacing
|
|
|
|
private final static int space = 4;
|
|
|
|
private static final Insets insets = new Insets(space, space, space, space);
|
|
|
|
|
|
|
|
private static final long serialVersionUID = 6865098428255463649L;
|
|
|
|
|
2019-12-20 20:25:54 +01:00
|
|
|
/**
|
|
|
|
* Initializes a {@link JFrame} with UI elements used to send and read messages
|
|
|
|
* to different users.
|
|
|
|
*
|
2019-12-21 00:43:38 +01:00
|
|
|
* @since Envoy v0.1-alpha
|
2019-12-20 20:25:54 +01:00
|
|
|
*/
|
2019-12-21 19:00:29 +01:00
|
|
|
public ChatWindow() {
|
2019-12-07 10:44:25 +01:00
|
|
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
|
|
setBounds(100, 100, 600, 800);
|
|
|
|
setTitle("Envoy");
|
|
|
|
setLocationRelativeTo(null);
|
2019-12-07 13:02:38 +01:00
|
|
|
setIconImage(Toolkit.getDefaultToolkit().createImage(getClass().getClassLoader().getResource("envoy_logo.png")));
|
2019-12-07 10:44:25 +01:00
|
|
|
|
2019-12-07 14:50:20 +01:00
|
|
|
contentPane.setBorder(new EmptyBorder(space, space, space, space));
|
2019-12-07 10:44:25 +01:00
|
|
|
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, 0.07 };
|
|
|
|
contentPane.setLayout(gbl_contentPane);
|
|
|
|
|
2020-01-26 22:10:15 +01:00
|
|
|
// TODO: messageList.setFocusTraversalKeysEnabled(false);
|
|
|
|
// messageList.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
|
2019-12-07 10:44:25 +01:00
|
|
|
|
2020-01-26 22:10:15 +01:00
|
|
|
// messageList.setFont(new Font("Arial", Font.PLAIN, 17));
|
|
|
|
// messageList.setFixedCellHeight(60);
|
2019-12-07 14:50:20 +01:00
|
|
|
messageList.setBorder(new EmptyBorder(space, space, space, space));
|
2019-12-07 10:44:25 +01:00
|
|
|
|
|
|
|
scrollPane.setViewportView(messageList);
|
|
|
|
|
|
|
|
GridBagConstraints gbc_scrollPane = new GridBagConstraints();
|
|
|
|
gbc_scrollPane.fill = GridBagConstraints.BOTH;
|
|
|
|
gbc_scrollPane.gridwidth = 2;
|
|
|
|
gbc_scrollPane.gridx = 1;
|
|
|
|
gbc_scrollPane.gridy = 1;
|
|
|
|
|
2020-01-27 20:11:47 +01:00
|
|
|
gbc_scrollPane.insets = insets;
|
2019-12-07 10:44:25 +01:00
|
|
|
contentPane.add(scrollPane, gbc_scrollPane);
|
|
|
|
|
|
|
|
// Message enter field
|
|
|
|
messageEnterTextArea.addKeyListener(new KeyAdapter() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void keyReleased(KeyEvent e) {
|
|
|
|
if (e.getKeyCode() == KeyEvent.VK_ENTER
|
2020-01-27 20:11:47 +01:00
|
|
|
&& (Settings.getInstance().isEnterToSend() && e.getModifiersEx() == 0 || e.getModifiersEx() == KeyEvent.CTRL_DOWN_MASK))
|
2020-01-26 22:10:15 +01:00
|
|
|
postMessage();
|
2019-12-07 10:44:25 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
GridBagConstraints gbc_messageEnterTextfield = new GridBagConstraints();
|
|
|
|
gbc_messageEnterTextfield.fill = GridBagConstraints.BOTH;
|
|
|
|
gbc_messageEnterTextfield.gridx = 1;
|
|
|
|
gbc_messageEnterTextfield.gridy = 2;
|
|
|
|
|
2020-01-27 20:11:47 +01:00
|
|
|
gbc_messageEnterTextfield.insets = insets;
|
2019-12-07 10:44:25 +01:00
|
|
|
|
|
|
|
contentPane.add(messageEnterTextArea, gbc_messageEnterTextfield);
|
|
|
|
|
|
|
|
// Post Button
|
|
|
|
GridBagConstraints gbc_moveSelectionPostButton = new GridBagConstraints();
|
|
|
|
|
|
|
|
gbc_moveSelectionPostButton.fill = GridBagConstraints.BOTH;
|
|
|
|
gbc_moveSelectionPostButton.gridx = 2;
|
|
|
|
gbc_moveSelectionPostButton.gridy = 2;
|
|
|
|
|
2020-01-27 20:11:47 +01:00
|
|
|
gbc_moveSelectionPostButton.insets = insets;
|
2019-12-07 10:44:25 +01:00
|
|
|
|
2020-01-26 22:10:15 +01:00
|
|
|
postButton.addActionListener((evt) -> { postMessage(); });
|
2019-12-07 10:44:25 +01:00
|
|
|
contentPane.add(postButton, gbc_moveSelectionPostButton);
|
|
|
|
|
|
|
|
// Settings Button
|
|
|
|
GridBagConstraints gbc_moveSelectionSettingsButton = new GridBagConstraints();
|
|
|
|
|
|
|
|
gbc_moveSelectionSettingsButton.fill = GridBagConstraints.BOTH;
|
|
|
|
gbc_moveSelectionSettingsButton.gridx = 2;
|
|
|
|
gbc_moveSelectionSettingsButton.gridy = 0;
|
|
|
|
|
2020-01-27 20:11:47 +01:00
|
|
|
gbc_moveSelectionSettingsButton.insets = insets;
|
2019-12-07 10:44:25 +01:00
|
|
|
|
|
|
|
settingsButton.addActionListener((evt) -> {
|
|
|
|
try {
|
2019-12-14 14:58:07 +01:00
|
|
|
new SettingsScreen().setVisible(true);
|
|
|
|
} catch (Exception e) {
|
2019-12-07 10:44:25 +01:00
|
|
|
logger.log(Level.WARNING, "An error occured while opening the settings screen", e);
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
contentPane.add(settingsButton, gbc_moveSelectionSettingsButton);
|
|
|
|
|
|
|
|
// Partner name display
|
|
|
|
textPane.setFont(new Font("Arial", Font.PLAIN, 20));
|
2019-12-07 14:50:20 +01:00
|
|
|
textPane.setEditable(false);
|
2019-12-07 10:44:25 +01:00
|
|
|
|
|
|
|
GridBagConstraints gbc_partnerName = new GridBagConstraints();
|
|
|
|
gbc_partnerName.fill = GridBagConstraints.HORIZONTAL;
|
|
|
|
gbc_partnerName.gridx = 1;
|
|
|
|
gbc_partnerName.gridy = 0;
|
|
|
|
|
2020-01-27 20:11:47 +01:00
|
|
|
gbc_partnerName.insets = insets;
|
2019-12-07 10:44:25 +01:00
|
|
|
contentPane.add(textPane, gbc_partnerName);
|
|
|
|
|
|
|
|
userList.setCellRenderer(new UserListRenderer());
|
|
|
|
userList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
|
|
|
userList.addListSelectionListener((listSelectionEvent) -> {
|
2019-12-21 19:00:29 +01:00
|
|
|
if (client != null && localDB != null && !listSelectionEvent.getValueIsAdjusting()) {
|
2019-12-07 10:44:25 +01:00
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
final JList<User> selectedUserList = (JList<User>) listSelectionEvent.getSource();
|
|
|
|
final User user = selectedUserList.getSelectedValue();
|
|
|
|
|
2019-12-28 21:20:43 +01:00
|
|
|
currentChat = localDB.getChats().stream().filter(chat -> chat.getRecipient().getId() == user.getId()).findFirst().get();
|
2019-12-07 10:44:25 +01:00
|
|
|
|
|
|
|
// Set all unread messages in the chat to read
|
2020-02-01 10:20:06 +01:00
|
|
|
currentChat.read();
|
2019-12-07 10:44:25 +01:00
|
|
|
|
|
|
|
client.setRecipient(user);
|
|
|
|
textPane.setText(currentChat.getRecipient().getName());
|
|
|
|
|
|
|
|
messageList.setModel(currentChat.getModel());
|
2019-12-15 17:44:13 +01:00
|
|
|
scrollPane.setChatOpened(true);
|
2020-01-29 07:44:25 +01:00
|
|
|
|
|
|
|
revalidate();
|
|
|
|
repaint();
|
2019-12-07 10:44:25 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
userList.setFont(new Font("Arial", Font.PLAIN, 17));
|
2019-12-07 14:50:20 +01:00
|
|
|
userList.setBorder(new EmptyBorder(space, space, space, space));
|
2019-12-07 10:44:25 +01:00
|
|
|
|
|
|
|
GridBagConstraints gbc_userList = new GridBagConstraints();
|
|
|
|
gbc_userList.fill = GridBagConstraints.VERTICAL;
|
|
|
|
gbc_userList.gridx = 0;
|
|
|
|
gbc_userList.gridy = 1;
|
|
|
|
gbc_userList.anchor = GridBagConstraints.PAGE_START;
|
2020-01-27 20:11:47 +01:00
|
|
|
gbc_userList.insets = insets;
|
2019-12-07 10:44:25 +01:00
|
|
|
|
2019-12-21 19:00:29 +01:00
|
|
|
applyTheme(Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()));
|
2019-12-14 14:58:07 +01:00
|
|
|
|
2019-12-07 10:44:25 +01:00
|
|
|
contentPane.add(userList, gbc_userList);
|
|
|
|
contentPane.revalidate();
|
|
|
|
|
2020-01-29 07:44:25 +01:00
|
|
|
// Listen to theme changes
|
2019-12-21 19:00:29 +01:00
|
|
|
EventBus.getInstance().register(ThemeChangeEvent.class, (evt) -> applyTheme((Theme) evt.get()));
|
2019-12-07 10:44:25 +01:00
|
|
|
|
2020-01-29 07:44:25 +01:00
|
|
|
// Listen to received messages
|
|
|
|
EventBus.getInstance().register(MessageCreationEvent.class, (evt) -> {
|
|
|
|
Message message = ((MessageCreationEvent) evt).get();
|
2020-02-01 10:41:59 +01:00
|
|
|
localDB.getChats().stream().filter(c -> c.getRecipient().getId() == message.getSenderId()).findFirst().get().appendMessage(message);
|
2020-02-01 10:20:06 +01:00
|
|
|
revalidate();
|
|
|
|
repaint();
|
2020-01-29 07:44:25 +01:00
|
|
|
});
|
|
|
|
|
2020-02-01 10:20:06 +01:00
|
|
|
revalidate();
|
2019-12-07 10:44:25 +01:00
|
|
|
}
|
|
|
|
|
2019-12-07 14:50:20 +01:00
|
|
|
/**
|
2020-01-29 07:44:25 +01:00
|
|
|
* Used to immediately reload the {@link ChatWindow} when settings were changed.
|
2019-12-20 20:25:54 +01:00
|
|
|
*
|
|
|
|
* @param theme the theme to change colors into
|
2019-12-21 19:00:29 +01:00
|
|
|
* @since Envoy v0.2-alpha
|
2019-12-07 14:50:20 +01:00
|
|
|
*/
|
2019-12-21 19:00:29 +01:00
|
|
|
private void applyTheme(Theme theme) {
|
2019-12-07 14:50:20 +01:00
|
|
|
// contentPane
|
|
|
|
contentPane.setBackground(theme.getBackgroundColor());
|
|
|
|
contentPane.setForeground(theme.getUserNameColor());
|
|
|
|
// messageList
|
2020-01-26 22:10:15 +01:00
|
|
|
// messageList.setSelectionForeground(theme.getUserNameColor());
|
|
|
|
// messageList.setSelectionBackground(theme.getSelectionColor());
|
2019-12-07 14:50:20 +01:00
|
|
|
messageList.setForeground(theme.getMessageColorChat());
|
|
|
|
messageList.setBackground(theme.getCellColor());
|
|
|
|
// scrollPane
|
2019-12-15 17:44:13 +01:00
|
|
|
scrollPane.applyTheme(theme);
|
2019-12-15 20:18:43 +01:00
|
|
|
scrollPane.autoscroll();
|
2019-12-15 12:48:40 +01:00
|
|
|
|
2019-12-07 14:50:20 +01:00
|
|
|
// messageEnterTextArea
|
|
|
|
messageEnterTextArea.setCaretColor(theme.getTypingMessageColor());
|
|
|
|
messageEnterTextArea.setForeground(theme.getTypingMessageColor());
|
|
|
|
messageEnterTextArea.setBackground(theme.getCellColor());
|
|
|
|
// postButton
|
|
|
|
postButton.setForeground(theme.getInteractableForegroundColor());
|
|
|
|
postButton.setBackground(theme.getInteractableBackgroundColor());
|
|
|
|
// settingsButton
|
|
|
|
settingsButton.setForeground(theme.getInteractableForegroundColor());
|
|
|
|
settingsButton.setBackground(theme.getInteractableBackgroundColor());
|
|
|
|
// textPane
|
|
|
|
textPane.setBackground(theme.getBackgroundColor());
|
|
|
|
textPane.setForeground(theme.getUserNameColor());
|
|
|
|
// userList
|
|
|
|
userList.setSelectionForeground(theme.getUserNameColor());
|
|
|
|
userList.setSelectionBackground(theme.getSelectionColor());
|
|
|
|
userList.setForeground(theme.getUserNameColor());
|
|
|
|
userList.setBackground(theme.getCellColor());
|
|
|
|
}
|
2019-12-07 17:58:59 +01:00
|
|
|
|
2020-01-26 22:10:15 +01:00
|
|
|
private void postMessage() {
|
2019-12-07 10:44:25 +01:00
|
|
|
if (!client.hasRecipient()) {
|
2019-12-14 14:58:07 +01:00
|
|
|
JOptionPane.showMessageDialog(this, "Please select a recipient!", "Cannot send message", JOptionPane.INFORMATION_MESSAGE);
|
2019-12-07 11:22:47 +01:00
|
|
|
return;
|
2019-12-07 10:44:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!messageEnterTextArea.getText().isEmpty()) try {
|
|
|
|
|
2019-12-31 10:57:11 +01:00
|
|
|
// Create message
|
2020-01-29 07:44:25 +01:00
|
|
|
final Message message = new MessageBuilder(localDB.getUser().getId(), currentChat.getRecipient().getId(), localDB.getIdGenerator())
|
2020-01-06 14:00:44 +01:00
|
|
|
.setText(messageEnterTextArea.getText())
|
|
|
|
.build();
|
2019-12-31 10:57:11 +01:00
|
|
|
|
|
|
|
// Send message
|
2020-01-27 20:11:47 +01:00
|
|
|
// TODO: Store offline messages
|
2019-12-31 10:57:11 +01:00
|
|
|
client.sendMessage(message);
|
|
|
|
|
|
|
|
// Add message to LocalDB and update UI
|
2019-12-07 10:44:25 +01:00
|
|
|
currentChat.appendMessage(message);
|
2020-01-27 20:11:47 +01:00
|
|
|
// messageList.setModel(currentChat.getModel());
|
2019-12-07 10:44:25 +01:00
|
|
|
|
|
|
|
// Clear text field
|
|
|
|
messageEnterTextArea.setText("");
|
2020-01-27 20:11:47 +01:00
|
|
|
|
2020-01-29 07:44:25 +01:00
|
|
|
// Update UI
|
2020-01-27 20:11:47 +01:00
|
|
|
revalidate();
|
|
|
|
repaint();
|
2020-01-29 07:44:25 +01:00
|
|
|
|
|
|
|
// Request a new id generator if all ids were used
|
|
|
|
if (!localDB.getIdGenerator().hasNext()) client.requestIdGenerator();
|
|
|
|
|
2019-12-07 10:44:25 +01:00
|
|
|
} catch (Exception e) {
|
|
|
|
JOptionPane.showMessageDialog(this,
|
2020-01-29 07:44:25 +01:00
|
|
|
"Error sending message:\n" + e.toString(),
|
|
|
|
"Message sending error",
|
2019-12-07 10:44:25 +01:00
|
|
|
JOptionPane.ERROR_MESSAGE);
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes the elements of the user list by downloading them from the
|
|
|
|
* server.
|
2019-12-07 11:22:47 +01:00
|
|
|
*
|
2019-12-07 10:44:25 +01:00
|
|
|
* @since Envoy v0.1-alpha
|
|
|
|
*/
|
|
|
|
private void loadUsersAndChats() {
|
|
|
|
new Thread(() -> {
|
2019-12-14 10:53:20 +01:00
|
|
|
DefaultListModel<User> userListModel = new DefaultListModel<>();
|
|
|
|
localDB.getUsers().values().forEach(user -> {
|
2019-12-07 10:44:25 +01:00
|
|
|
userListModel.addElement(user);
|
|
|
|
|
|
|
|
// Check if user exists in local DB
|
2019-12-28 21:20:43 +01:00
|
|
|
if (localDB.getChats().stream().filter(c -> c.getRecipient().getId() == user.getId()).count() == 0)
|
2019-12-07 10:44:25 +01:00
|
|
|
localDB.getChats().add(new Chat(user));
|
|
|
|
});
|
|
|
|
SwingUtilities.invokeLater(() -> userList.setModel(userListModel));
|
|
|
|
}).start();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-02-01 10:20:06 +01:00
|
|
|
* Sets the {@link Client} used by this {@link ChatWindow}.
|
2019-12-21 19:00:29 +01:00
|
|
|
*
|
|
|
|
* @param client the {@link Client} used to send and receive messages
|
2019-12-21 21:07:18 +01:00
|
|
|
* @since Envoy v0.2-alpha
|
2019-12-21 19:00:29 +01:00
|
|
|
*/
|
|
|
|
public void setClient(Client client) {
|
|
|
|
this.client = client;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the {@link LocalDB} used by this {@link ChatWindow}. After invoking this
|
|
|
|
* method, users and chats will be loaded from the database into the GUI.
|
|
|
|
*
|
|
|
|
* @param localDB the {@link LocalDB} used to manage stored messages and users
|
2019-12-21 21:07:18 +01:00
|
|
|
* @since Envoy v0.2-alpha
|
2019-12-21 19:00:29 +01:00
|
|
|
*/
|
|
|
|
public void setLocalDB(LocalDB localDB) {
|
|
|
|
this.localDB = localDB;
|
|
|
|
loadUsersAndChats();
|
|
|
|
}
|
2019-12-07 10:44:25 +01:00
|
|
|
}
|