2020-03-18 08:03:35 +01:00
|
|
|
package envoy.client.ui.container;
|
2019-12-07 10:44:25 +01:00
|
|
|
|
2019-12-21 07:54:15 +01:00
|
|
|
import java.awt.*;
|
2020-03-18 08:03:35 +01:00
|
|
|
import java.awt.datatransfer.StringSelection;
|
2020-03-14 11:17:43 +01:00
|
|
|
import java.awt.event.*;
|
2020-02-05 07:09:25 +01:00
|
|
|
import java.io.IOException;
|
2020-03-23 21:04:32 +01:00
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
2019-12-07 10:44:25 +01:00
|
|
|
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;
|
2020-02-09 16:26:36 +01:00
|
|
|
import javax.swing.event.DocumentEvent;
|
|
|
|
import javax.swing.event.DocumentListener;
|
2019-12-07 10:44:25 +01:00
|
|
|
|
2020-02-05 20:58:30 +01:00
|
|
|
import envoy.client.data.Chat;
|
2020-03-24 18:38:47 +01:00
|
|
|
import envoy.client.data.LocalDB;
|
2020-02-12 07:12:22 +01:00
|
|
|
import envoy.client.data.Settings;
|
2020-02-11 17:17:22 +01:00
|
|
|
import envoy.client.event.MessageCreationEvent;
|
|
|
|
import envoy.client.event.ThemeChangeEvent;
|
2020-02-04 19:46:18 +01:00
|
|
|
import envoy.client.net.Client;
|
2020-02-06 21:28:02 +01:00
|
|
|
import envoy.client.net.WriteProxy;
|
2020-03-21 19:26:11 +01:00
|
|
|
import envoy.client.ui.Theme;
|
2020-01-26 22:10:15 +01:00
|
|
|
import envoy.client.ui.list.ComponentList;
|
2020-03-20 18:32:57 +01:00
|
|
|
import envoy.client.ui.list.ComponentList.SelectionMode;
|
2020-03-21 16:10:59 +01:00
|
|
|
import envoy.client.ui.list.Model;
|
2020-03-21 19:26:11 +01:00
|
|
|
import envoy.client.ui.list_component.ContactSearchComponent;
|
|
|
|
import envoy.client.ui.list_component.MessageComponent;
|
2020-03-14 21:34:17 +01:00
|
|
|
import envoy.client.ui.primary.PrimaryButton;
|
|
|
|
import envoy.client.ui.primary.PrimaryScrollPane;
|
|
|
|
import envoy.client.ui.primary.PrimaryTextArea;
|
|
|
|
import envoy.client.ui.renderer.UserListRenderer;
|
2019-12-21 00:29:16 +01:00
|
|
|
import envoy.client.ui.settings.SettingsScreen;
|
2019-12-28 21:20:43 +01:00
|
|
|
import envoy.data.Message;
|
2020-02-05 07:09:25 +01:00
|
|
|
import envoy.data.Message.MessageStatus;
|
2019-12-31 10:27:52 +01:00
|
|
|
import envoy.data.MessageBuilder;
|
2019-12-28 21:20:43 +01:00
|
|
|
import envoy.data.User;
|
2020-02-09 16:26:36 +01:00
|
|
|
import envoy.event.*;
|
2020-03-05 14:27:40 +01:00
|
|
|
import envoy.util.EnvoyLog;
|
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
|
2020-03-23 21:52:33 +01:00
|
|
|
* @since Envoy Client v0.1-alpha
|
2019-12-07 10:44:25 +01:00
|
|
|
*/
|
|
|
|
public class ChatWindow extends JFrame {
|
|
|
|
|
2020-03-14 11:17:43 +01:00
|
|
|
/**
|
2020-03-21 16:10:59 +01:00
|
|
|
* This integer defines the maximum amount of chars allowed per message.
|
2020-03-14 11:17:43 +01:00
|
|
|
*
|
|
|
|
* @since Envoy 0.1-beta
|
|
|
|
*/
|
2020-03-14 17:04:51 +01:00
|
|
|
public static final int MAX_MESSAGE_LENGTH = 200;
|
2020-03-14 11:17:43 +01:00
|
|
|
|
2019-12-15 16:26:11 +01:00
|
|
|
// User specific objects
|
2020-02-06 21:28:02 +01:00
|
|
|
private Client client;
|
|
|
|
private WriteProxy writeProxy;
|
2020-03-24 18:38:47 +01:00
|
|
|
private LocalDB localDB;
|
2020-03-14 11:17:43 +01:00
|
|
|
private Chat currentChat;
|
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<>();
|
2020-02-09 16:26:36 +01:00
|
|
|
private DefaultListModel<User> userListModel = new DefaultListModel<>();
|
2020-03-17 10:08:58 +01:00
|
|
|
private ComponentList<Message> messageList = new ComponentList<>();
|
2020-01-27 20:11:47 +01:00
|
|
|
private PrimaryScrollPane scrollPane = new PrimaryScrollPane();
|
|
|
|
private JTextPane textPane = new JTextPane();
|
|
|
|
private PrimaryButton postButton = new PrimaryButton("Post");
|
|
|
|
private PrimaryButton settingsButton = new PrimaryButton("Settings");
|
2020-03-15 19:11:02 +01:00
|
|
|
private JPopupMenu contextMenu;
|
2019-12-07 13:02:38 +01:00
|
|
|
|
2020-02-06 22:19:33 +01:00
|
|
|
// Contacts Header
|
2020-02-08 11:43:37 +01:00
|
|
|
private JPanel contactsHeader = new JPanel();
|
|
|
|
private JTextPane contactsDisplay = new JTextPane();
|
2020-02-06 22:19:33 +01:00
|
|
|
private PrimaryButton addContact = new PrimaryButton("+");
|
|
|
|
|
|
|
|
// Search Contacts
|
2020-03-21 16:10:59 +01:00
|
|
|
private final JPanel searchPane = new JPanel();
|
|
|
|
private final PrimaryButton cancelButton = new PrimaryButton("x");
|
|
|
|
private final PrimaryTextArea searchField = new PrimaryTextArea(space);
|
|
|
|
private final PrimaryScrollPane scrollForPossibleContacts = new PrimaryScrollPane();
|
|
|
|
private final Model<User> contactsModel = new Model<>();
|
|
|
|
private final ComponentList<User> contactList = new ComponentList<User>().setRenderer(ContactSearchComponent::new);
|
2020-02-06 22:19:33 +01:00
|
|
|
|
2020-02-18 07:16:35 +01:00
|
|
|
private static final Logger logger = EnvoyLog.getLogger(ChatWindow.class);
|
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);
|
|
|
|
|
2020-03-26 16:06:18 +01:00
|
|
|
private static final long serialVersionUID = 0L;
|
2020-01-27 20:11:47 +01:00
|
|
|
|
2019-12-20 20:25:54 +01:00
|
|
|
/**
|
|
|
|
* Initializes a {@link JFrame} with UI elements used to send and read messages
|
|
|
|
* to different users.
|
|
|
|
*
|
2020-03-23 21:52:33 +01:00
|
|
|
* @since Envoy Client 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);
|
2020-03-18 22:13:58 +01:00
|
|
|
setMinimumSize(new Dimension(400, 300));
|
2019-12-07 10:44:25 +01:00
|
|
|
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 };
|
2020-02-06 22:19:33 +01:00
|
|
|
gbl_contentPane.rowHeights = new int[] { 1, 1, 1, 1 };
|
2020-03-14 11:17:43 +01:00
|
|
|
gbl_contentPane.columnWeights = new double[] { 0.03, 1.0, 0.1 };
|
2020-03-22 17:20:05 +01:00
|
|
|
gbl_contentPane.rowWeights = new double[] { 0.03, 0.001, 1.0, 0.001 };
|
2019-12-07 10:44:25 +01:00
|
|
|
contentPane.setLayout(gbl_contentPane);
|
|
|
|
|
2020-03-21 13:20:45 +01:00
|
|
|
messageList.setBorder(new EmptyBorder(space, space, space, space));
|
|
|
|
messageList.setSelectionMode(SelectionMode.SINGLE);
|
2020-03-21 16:10:59 +01:00
|
|
|
messageList.setSelectionHandler((message, comp, isSelected) -> {
|
|
|
|
final var theme = Settings.getInstance().getCurrentTheme();
|
|
|
|
comp.setBackground(isSelected ? theme.getSelectionColor() : theme.getCellColor());
|
2020-03-22 17:20:05 +01:00
|
|
|
|
|
|
|
// ContextMenu
|
2020-03-23 21:04:32 +01:00
|
|
|
Map<String, ActionListener> commands = Map.of("forward selected message", evt -> {
|
|
|
|
final Message selectedMessage = messageList.getSingleSelectedElement();
|
|
|
|
List<User> chosenContacts = ContactsChooserDialog
|
2020-03-24 18:38:47 +01:00
|
|
|
.showForwardingDialog("Forward selected message to", null, selectedMessage, localDB.getUsers().values());
|
2020-03-23 21:04:32 +01:00
|
|
|
if (chosenContacts != null && chosenContacts.size() > 0) forwardMessage(selectedMessage, chosenContacts.toArray(new User[0]));
|
|
|
|
}, "copy", evt -> {
|
|
|
|
// TODO should be enhanced to allow also copying of message attachments,
|
|
|
|
// especially pictures
|
|
|
|
StringSelection copy = new StringSelection(messageList.getSingleSelectedElement().getText());
|
|
|
|
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(copy, copy);
|
|
|
|
// TODO insert implementation to edit and delete messages
|
|
|
|
}, "delete", evt -> {}, "edit", evt -> {}, "quote", evt -> {});
|
|
|
|
|
2020-03-22 17:20:05 +01:00
|
|
|
if (isSelected) {
|
|
|
|
contextMenu = new ContextMenu(null, comp, commands, null, null).build();
|
|
|
|
contextMenu.show(comp, 0, 0);
|
|
|
|
}
|
2020-03-21 16:10:59 +01:00
|
|
|
});
|
2020-03-21 13:20:45 +01:00
|
|
|
|
2019-12-07 10:44:25 +01:00
|
|
|
scrollPane.setViewportView(messageList);
|
2020-03-22 16:51:44 +01:00
|
|
|
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
|
2020-03-14 11:17:43 +01:00
|
|
|
scrollPane.addComponentListener(new ComponentAdapter() {
|
|
|
|
|
2020-03-18 22:13:58 +01:00
|
|
|
// Update list elements when scroll pane (and thus list) is resized
|
2020-03-14 11:17:43 +01:00
|
|
|
@Override
|
2020-03-18 22:13:58 +01:00
|
|
|
public void componentResized(ComponentEvent e) {
|
|
|
|
messageList.setMaximumSize(new Dimension(scrollPane.getWidth(), Integer.MAX_VALUE));
|
|
|
|
messageList.synchronizeModel();
|
|
|
|
}
|
2020-03-14 11:17:43 +01:00
|
|
|
});
|
2019-12-07 10:44:25 +01:00
|
|
|
|
|
|
|
GridBagConstraints gbc_scrollPane = new GridBagConstraints();
|
|
|
|
gbc_scrollPane.fill = GridBagConstraints.BOTH;
|
|
|
|
gbc_scrollPane.gridwidth = 2;
|
2020-02-06 22:19:33 +01:00
|
|
|
gbc_scrollPane.gridheight = 2;
|
2019-12-07 10:44:25 +01:00
|
|
|
gbc_scrollPane.gridx = 1;
|
|
|
|
gbc_scrollPane.gridy = 1;
|
|
|
|
|
2020-01-27 20:11:47 +01:00
|
|
|
gbc_scrollPane.insets = insets;
|
2020-02-06 22:19:33 +01:00
|
|
|
|
|
|
|
drawChatBox(gbc_scrollPane);
|
2019-12-07 10:44:25 +01:00
|
|
|
|
2020-03-14 11:17:43 +01:00
|
|
|
// MessageEnterTextArea
|
|
|
|
messageEnterTextArea.addInputMethodListener(new InputMethodListener() {
|
|
|
|
|
|
|
|
@Override
|
2020-03-22 17:20:05 +01:00
|
|
|
public void inputMethodTextChanged(InputMethodEvent event) {
|
|
|
|
checkMessageTextLength();
|
|
|
|
checkPostButton(messageEnterTextArea.getText());
|
|
|
|
}
|
2020-03-14 11:17:43 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void caretPositionChanged(InputMethodEvent event) {}
|
|
|
|
});
|
|
|
|
|
2019-12-07 10:44:25 +01:00
|
|
|
messageEnterTextArea.addKeyListener(new KeyAdapter() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void keyReleased(KeyEvent e) {
|
|
|
|
if (e.getKeyCode() == KeyEvent.VK_ENTER
|
2020-03-22 17:20:05 +01:00
|
|
|
&& (Settings.getInstance().isEnterToSend() && e.getModifiersEx() == 0 || e.getModifiersEx() == KeyEvent.CTRL_DOWN_MASK)
|
|
|
|
&& postButton.isEnabled())
|
2020-01-26 22:10:15 +01:00
|
|
|
postMessage();
|
2020-03-14 11:17:43 +01:00
|
|
|
// Checking if text is too long
|
|
|
|
checkMessageTextLength();
|
2020-03-22 17:20:05 +01:00
|
|
|
checkPostButton(messageEnterTextArea.getText());
|
2019-12-07 10:44:25 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-03-22 17:20:05 +01:00
|
|
|
GridBagConstraints gbc_messageEnterTextArea = new GridBagConstraints();
|
|
|
|
gbc_messageEnterTextArea.fill = GridBagConstraints.BOTH;
|
|
|
|
gbc_messageEnterTextArea.gridx = 1;
|
|
|
|
gbc_messageEnterTextArea.gridy = 3;
|
2020-03-23 21:04:32 +01:00
|
|
|
gbc_messageEnterTextArea.insets = insets;
|
2020-03-22 17:20:05 +01:00
|
|
|
contentPane.add(messageEnterTextArea, gbc_messageEnterTextArea);
|
2019-12-07 10:44:25 +01:00
|
|
|
|
|
|
|
// Post Button
|
2020-03-14 11:17:43 +01:00
|
|
|
GridBagConstraints gbc_postButton = new GridBagConstraints();
|
|
|
|
gbc_postButton.fill = GridBagConstraints.BOTH;
|
|
|
|
gbc_postButton.gridx = 2;
|
|
|
|
gbc_postButton.gridy = 3;
|
2020-03-23 21:04:32 +01:00
|
|
|
gbc_postButton.insets = insets;
|
2020-01-26 22:10:15 +01:00
|
|
|
postButton.addActionListener((evt) -> { postMessage(); });
|
2020-03-22 17:20:05 +01:00
|
|
|
postButton.setEnabled(false);
|
2020-03-14 11:17:43 +01:00
|
|
|
contentPane.add(postButton, gbc_postButton);
|
2019-12-07 10:44:25 +01:00
|
|
|
|
|
|
|
// 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
|
|
|
|
2020-02-11 19:35:23 +01:00
|
|
|
settingsButton.addActionListener(evt -> new SettingsScreen().setVisible(true));
|
2019-12-07 10:44:25 +01:00
|
|
|
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) -> {
|
2020-03-24 18:38:47 +01:00
|
|
|
if (client != null && localDB != null && !listSelectionEvent.getValueIsAdjusting()) {
|
2019-12-07 10:44:25 +01:00
|
|
|
final JList<User> selectedUserList = (JList<User>) listSelectionEvent.getSource();
|
|
|
|
final User user = selectedUserList.getSelectedValue();
|
|
|
|
|
2020-03-14 11:17:43 +01:00
|
|
|
for (int i = 0; i < contentPane.getComponents().length; i++)
|
|
|
|
if (contentPane.getComponent(i).equals(searchPane)) drawChatBox(gbc_scrollPane);
|
2020-02-06 22:19:33 +01:00
|
|
|
if (user != null) {
|
|
|
|
// Select current chat
|
2020-03-26 16:06:18 +01:00
|
|
|
currentChat = localDB.getChats().stream().filter(chat -> chat.getRecipient().getID() == user.getID()).findFirst().get();
|
2019-12-07 10:44:25 +01:00
|
|
|
|
2020-02-06 22:19:33 +01:00
|
|
|
// Read current chat
|
|
|
|
readCurrentChat();
|
2019-12-07 10:44:25 +01:00
|
|
|
|
2020-02-06 22:19:33 +01:00
|
|
|
// Set chat title
|
|
|
|
textPane.setText(currentChat.getRecipient().getName());
|
2019-12-07 10:44:25 +01:00
|
|
|
|
2020-02-06 22:19:33 +01:00
|
|
|
// Update model and scroll down
|
2020-03-28 15:32:24 +01:00
|
|
|
// messageList.setModel(currentChat.getModel());
|
2020-02-06 22:19:33 +01:00
|
|
|
scrollPane.setChatOpened(true);
|
2020-01-29 07:44:25 +01:00
|
|
|
|
2020-02-06 22:19:33 +01:00
|
|
|
messageList.synchronizeModel();
|
|
|
|
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();
|
2020-02-08 11:43:37 +01:00
|
|
|
gbc_userList.fill = GridBagConstraints.VERTICAL;
|
|
|
|
gbc_userList.gridx = 0;
|
|
|
|
gbc_userList.gridy = 2;
|
|
|
|
gbc_userList.gridheight = 2;
|
|
|
|
gbc_userList.anchor = GridBagConstraints.PAGE_START;
|
|
|
|
gbc_userList.insets = insets;
|
2019-12-14 14:58:07 +01:00
|
|
|
|
2019-12-07 10:44:25 +01:00
|
|
|
contentPane.add(userList, gbc_userList);
|
|
|
|
contentPane.revalidate();
|
|
|
|
|
2020-02-06 22:19:33 +01:00
|
|
|
// Contacts Search
|
|
|
|
GridBagConstraints gbc_searchPane = new GridBagConstraints();
|
|
|
|
gbc_searchPane.fill = GridBagConstraints.BOTH;
|
|
|
|
gbc_searchPane.gridwidth = 2;
|
|
|
|
gbc_searchPane.gridheight = 2;
|
|
|
|
gbc_searchPane.gridx = 1;
|
|
|
|
gbc_searchPane.gridy = 1;
|
|
|
|
|
|
|
|
gbc_searchPane.insets = insets;
|
|
|
|
|
|
|
|
GridBagLayout gbl_contactsSearch = new GridBagLayout();
|
|
|
|
gbl_contactsSearch.columnWidths = new int[] { 1, 1 };
|
|
|
|
gbl_contactsSearch.rowHeights = new int[] { 1, 1 };
|
|
|
|
gbl_contactsSearch.columnWeights = new double[] { 1, 0.1 };
|
|
|
|
gbl_contactsSearch.rowWeights = new double[] { 0.001, 1 };
|
|
|
|
searchPane.setLayout(gbl_contactsSearch);
|
|
|
|
|
|
|
|
GridBagConstraints gbc_searchField = new GridBagConstraints();
|
|
|
|
gbc_searchField.fill = GridBagConstraints.BOTH;
|
|
|
|
gbc_searchField.gridx = 0;
|
|
|
|
gbc_searchField.gridy = 0;
|
|
|
|
gbc_searchField.insets = new Insets(7, 4, 4, 4);
|
2020-02-08 11:43:37 +01:00
|
|
|
|
2020-02-06 22:19:33 +01:00
|
|
|
searchPane.add(searchField, gbc_searchField);
|
2020-02-08 11:43:37 +01:00
|
|
|
|
2020-02-09 16:26:36 +01:00
|
|
|
// Sends event to server, if input has changed
|
|
|
|
searchField.getDocument().addDocumentListener(new DocumentListener() {
|
|
|
|
|
|
|
|
@Override
|
2020-02-11 19:35:23 +01:00
|
|
|
public void removeUpdate(DocumentEvent evt) {
|
2020-03-14 11:17:43 +01:00
|
|
|
if (client.isOnline()) if (searchField.getText().isEmpty()) {
|
|
|
|
contactsModel.clear();
|
|
|
|
revalidate();
|
|
|
|
repaint();
|
|
|
|
} else try {
|
|
|
|
client.sendEvent(new ContactSearchRequest(searchField.getText()));
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
2020-02-09 16:26:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-02-11 19:35:23 +01:00
|
|
|
public void insertUpdate(DocumentEvent evt) {
|
2020-03-14 11:17:43 +01:00
|
|
|
if (client.isOnline()) try {
|
|
|
|
client.sendEvent(new ContactSearchRequest(searchField.getText()));
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
2020-02-09 16:26:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-02-11 19:35:23 +01:00
|
|
|
public void changedUpdate(DocumentEvent evt) {}
|
2020-02-09 16:26:36 +01:00
|
|
|
});
|
|
|
|
|
2020-02-06 22:19:33 +01:00
|
|
|
GridBagConstraints gbc_cancelButton = new GridBagConstraints();
|
|
|
|
gbc_cancelButton.fill = GridBagConstraints.BOTH;
|
|
|
|
gbc_cancelButton.gridx = 1;
|
|
|
|
gbc_cancelButton.gridy = 0;
|
|
|
|
gbc_cancelButton.insets = new Insets(7, 4, 4, 4);
|
|
|
|
|
|
|
|
cancelButton.addActionListener((evt) -> { drawChatBox(gbc_scrollPane); });
|
|
|
|
|
|
|
|
searchPane.add(cancelButton, gbc_cancelButton);
|
|
|
|
|
2020-02-09 16:26:36 +01:00
|
|
|
contactList.setModel(contactsModel);
|
2020-03-14 11:17:43 +01:00
|
|
|
scrollForPossibleContacts.setBorder(new EmptyBorder(space, space, space, space));
|
|
|
|
scrollForPossibleContacts.setViewportView(contactList);
|
2020-02-06 22:19:33 +01:00
|
|
|
|
|
|
|
GridBagConstraints gbc_possibleContacts = new GridBagConstraints();
|
|
|
|
gbc_possibleContacts.fill = GridBagConstraints.BOTH;
|
|
|
|
gbc_possibleContacts.gridwidth = 2;
|
|
|
|
gbc_possibleContacts.gridx = 0;
|
|
|
|
gbc_possibleContacts.gridy = 1;
|
|
|
|
|
|
|
|
gbc_possibleContacts.insets = insets;
|
2020-02-08 11:43:37 +01:00
|
|
|
|
2020-03-14 11:17:43 +01:00
|
|
|
searchPane.add(scrollForPossibleContacts, gbc_possibleContacts);
|
2020-02-06 22:19:33 +01:00
|
|
|
|
|
|
|
// Contacts Header
|
|
|
|
GridBagConstraints gbc_contactsHeader = new GridBagConstraints();
|
|
|
|
gbc_contactsHeader.fill = GridBagConstraints.BOTH;
|
|
|
|
gbc_contactsHeader.gridx = 0;
|
|
|
|
gbc_contactsHeader.gridy = 1;
|
|
|
|
gbc_contactsHeader.insets = insets;
|
|
|
|
|
|
|
|
GridBagLayout gbl_contactHeader = new GridBagLayout();
|
|
|
|
gbl_contactHeader.columnWidths = new int[] { 1, 1 };
|
|
|
|
gbl_contactHeader.rowHeights = new int[] { 1 };
|
|
|
|
gbl_contactHeader.columnWeights = new double[] { 1, 1 };
|
|
|
|
gbl_contactHeader.rowWeights = new double[] { 1 };
|
|
|
|
contactsHeader.setLayout(gbl_contactHeader);
|
|
|
|
|
|
|
|
contactsDisplay.setEditable(false);
|
|
|
|
contactsDisplay.setFont(new Font("Arial", Font.PLAIN, 12));
|
|
|
|
contactsDisplay.setText("Contacts");
|
|
|
|
|
|
|
|
GridBagConstraints gbc_contactsDisplay = new GridBagConstraints();
|
|
|
|
gbc_contactsDisplay.fill = GridBagConstraints.BOTH;
|
|
|
|
gbc_contactsDisplay.gridx = 0;
|
|
|
|
gbc_contactsDisplay.gridy = 0;
|
|
|
|
|
|
|
|
contactsHeader.add(contactsDisplay, gbc_contactsDisplay);
|
|
|
|
|
|
|
|
addContact.setFont(new Font("Arial", Font.PLAIN, 15));
|
|
|
|
|
|
|
|
GridBagConstraints gbc_addContact = new GridBagConstraints();
|
|
|
|
gbc_addContact.fill = GridBagConstraints.BOTH;
|
|
|
|
gbc_addContact.gridx = 1;
|
|
|
|
gbc_addContact.gridy = 0;
|
|
|
|
gbc_addContact.insets = insets;
|
2020-02-08 11:43:37 +01:00
|
|
|
|
2020-03-21 16:10:59 +01:00
|
|
|
addContact.addActionListener(evt -> drawContactSearch(gbc_searchPane));
|
2020-02-06 22:19:33 +01:00
|
|
|
|
|
|
|
contactsHeader.add(addContact, gbc_addContact);
|
|
|
|
|
2020-03-21 16:10:59 +01:00
|
|
|
applyTheme(Settings.getInstance().getCurrentTheme());
|
2020-02-06 22:19:33 +01:00
|
|
|
|
|
|
|
contentPane.add(contactsHeader, gbc_contactsHeader);
|
|
|
|
contentPane.revalidate();
|
|
|
|
|
2020-01-29 07:44:25 +01:00
|
|
|
// Listen to theme changes
|
2020-02-12 06:12:04 +01:00
|
|
|
EventBus.getInstance().register(ThemeChangeEvent.class, evt -> applyTheme(evt.get()));
|
2019-12-07 10:44:25 +01:00
|
|
|
|
2020-02-07 15:57:41 +01:00
|
|
|
// Listen to user status changes
|
2020-02-12 06:12:04 +01:00
|
|
|
EventBus.getInstance().register(UserStatusChangeEvent.class, evt -> { userList.revalidate(); userList.repaint(); });
|
2020-02-07 15:27:26 +01:00
|
|
|
|
2020-01-29 07:44:25 +01:00
|
|
|
// Listen to received messages
|
2020-02-12 06:12:04 +01:00
|
|
|
EventBus.getInstance().register(MessageCreationEvent.class, evt -> {
|
|
|
|
Message message = evt.get();
|
2020-03-26 16:06:18 +01:00
|
|
|
Chat chat = localDB.getChats().stream().filter(c -> c.getRecipient().getID() == message.getSenderID()).findFirst().get();
|
2020-03-28 15:32:24 +01:00
|
|
|
// chat.appendMessage(message);
|
2020-02-05 17:23:30 +01:00
|
|
|
|
|
|
|
// Read message and update UI if in current chat
|
2020-02-05 20:58:30 +01:00
|
|
|
if (chat == currentChat) readCurrentChat();
|
2020-02-05 17:23:30 +01:00
|
|
|
|
2020-02-01 10:20:06 +01:00
|
|
|
revalidate();
|
|
|
|
repaint();
|
2020-01-29 07:44:25 +01:00
|
|
|
});
|
|
|
|
|
2020-02-05 07:09:25 +01:00
|
|
|
// Listen to message status changes
|
2020-02-12 06:12:04 +01:00
|
|
|
EventBus.getInstance().register(MessageStatusChangeEvent.class, evt -> {
|
2020-03-26 16:06:18 +01:00
|
|
|
final long id = evt.getID();
|
2020-02-12 06:12:04 +01:00
|
|
|
final MessageStatus status = evt.get();
|
2020-02-05 07:09:25 +01:00
|
|
|
|
2020-03-24 18:38:47 +01:00
|
|
|
for (Chat c : localDB.getChats())
|
2020-03-28 15:32:24 +01:00
|
|
|
// for (Message m : c.getModel())
|
|
|
|
// if (m.getID() == id) {
|
|
|
|
//
|
|
|
|
// // Update message status
|
|
|
|
// m.setStatus(status);
|
|
|
|
//
|
|
|
|
// // Update model and scroll down if current chat
|
|
|
|
// if (c == currentChat) {
|
|
|
|
// messageList.setModel(currentChat.getModel());
|
|
|
|
// scrollPane.setChatOpened(true);
|
|
|
|
// } else messageList.synchronizeModel();
|
|
|
|
// }
|
2020-02-05 20:08:24 +01:00
|
|
|
|
|
|
|
revalidate();
|
|
|
|
repaint();
|
2020-02-05 07:09:25 +01:00
|
|
|
});
|
|
|
|
|
2020-02-11 19:35:23 +01:00
|
|
|
// Listen to contact search results
|
2020-03-14 11:17:43 +01:00
|
|
|
EventBus.getInstance()
|
|
|
|
.register(ContactSearchResult.class,
|
|
|
|
evt -> {
|
|
|
|
contactsModel.clear();
|
|
|
|
final java.util.List<User> contacts = evt.get();
|
|
|
|
contacts.forEach(contactsModel::add);
|
|
|
|
revalidate();
|
|
|
|
repaint();
|
|
|
|
});
|
2020-02-09 16:26:36 +01:00
|
|
|
|
2020-02-11 19:35:23 +01:00
|
|
|
// Add new contacts to the contact list
|
2020-02-12 06:12:04 +01:00
|
|
|
EventBus.getInstance().register(ContactOperationEvent.class, evt -> {
|
|
|
|
User contact = evt.get();
|
2020-02-11 17:17:22 +01:00
|
|
|
|
2020-02-10 23:21:06 +01:00
|
|
|
// Clearing the search field and the searchResultList
|
|
|
|
searchField.setText("");
|
|
|
|
contactsModel.clear();
|
|
|
|
|
2020-02-10 19:51:50 +01:00
|
|
|
// Update LocalDB
|
|
|
|
userListModel.addElement(contact);
|
2020-03-24 18:38:47 +01:00
|
|
|
localDB.getUsers().put(contact.getName(), contact);
|
|
|
|
localDB.getChats().add(new Chat(contact));
|
2020-02-09 16:26:36 +01:00
|
|
|
|
|
|
|
revalidate();
|
|
|
|
repaint();
|
|
|
|
});
|
|
|
|
|
2020-02-01 10:20:06 +01:00
|
|
|
revalidate();
|
2020-02-08 11:43:37 +01:00
|
|
|
repaint();
|
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
|
2020-03-23 21:52:33 +01:00
|
|
|
* @since Envoy Client 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-03-21 19:26:11 +01:00
|
|
|
messageList.setForeground(theme.getTextColor());
|
2019-12-07 14:50:20 +01:00
|
|
|
messageList.setBackground(theme.getCellColor());
|
2020-03-22 16:51:44 +01:00
|
|
|
messageList.synchronizeModel();
|
2019-12-07 14:50:20 +01:00
|
|
|
// scrollPane
|
2019-12-15 17:44:13 +01:00
|
|
|
scrollPane.applyTheme(theme);
|
2019-12-15 20:18:43 +01:00
|
|
|
scrollPane.autoscroll();
|
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());
|
2020-02-06 22:19:33 +01:00
|
|
|
// contacts header
|
|
|
|
contactsHeader.setBackground(theme.getCellColor());
|
|
|
|
contactsDisplay.setBackground(theme.getCellColor());
|
|
|
|
contactsDisplay.setForeground(theme.getUserNameColor());
|
|
|
|
addContact.setBackground(theme.getInteractableBackgroundColor());
|
|
|
|
addContact.setForeground(theme.getInteractableForegroundColor());
|
|
|
|
// SearchPane
|
|
|
|
searchPane.setBackground(theme.getCellColor());
|
|
|
|
searchField.setBackground(theme.getBackgroundColor());
|
|
|
|
searchField.setForeground(theme.getUserNameColor());
|
|
|
|
cancelButton.setBackground(theme.getInteractableBackgroundColor());
|
|
|
|
cancelButton.setForeground(theme.getInteractableForegroundColor());
|
2020-03-21 19:26:11 +01:00
|
|
|
contactList.setForeground(theme.getTextColor());
|
2020-02-08 11:43:37 +01:00
|
|
|
contactList.setBackground(theme.getCellColor());
|
2020-03-14 11:17:43 +01:00
|
|
|
scrollForPossibleContacts.applyTheme(theme);
|
2019-12-07 14:50:20 +01:00
|
|
|
}
|
2019-12-07 17:58:59 +01:00
|
|
|
|
2020-03-14 22:44:03 +01:00
|
|
|
/**
|
|
|
|
* Sends a new message to the server based on the text entered in the textArea.
|
|
|
|
*
|
2020-03-23 21:52:33 +01:00
|
|
|
* @since Envoy Client v0.1-beta
|
2020-03-14 22:44:03 +01:00
|
|
|
*/
|
2020-01-26 22:10:15 +01:00
|
|
|
private void postMessage() {
|
2020-02-04 19:46:18 +01:00
|
|
|
if (userList.isSelectionEmpty()) {
|
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
|
|
|
}
|
2020-03-21 19:26:11 +01:00
|
|
|
String text = messageEnterTextArea.getText().trim();
|
|
|
|
if (!text.isEmpty()) checkMessageTextLength();
|
2019-12-07 10:44:25 +01:00
|
|
|
|
2020-03-14 22:44:03 +01:00
|
|
|
// Create message
|
2020-03-26 16:06:18 +01:00
|
|
|
final Message message = new MessageBuilder(localDB.getUser().getID(), currentChat.getRecipient().getID(), localDB.getIDGenerator())
|
2020-03-21 19:26:11 +01:00
|
|
|
.setText(text)
|
2020-03-14 22:44:03 +01:00
|
|
|
.build();
|
|
|
|
sendMessage(message);
|
|
|
|
// Clear text field
|
|
|
|
messageEnterTextArea.setText("");
|
2020-03-22 17:20:05 +01:00
|
|
|
postButton.setEnabled(false);
|
2020-03-14 22:44:03 +01:00
|
|
|
}
|
2019-12-31 10:57:11 +01:00
|
|
|
|
2020-03-14 22:44:03 +01:00
|
|
|
/**
|
|
|
|
* Forwards a message.
|
|
|
|
*
|
2020-03-22 16:51:44 +01:00
|
|
|
* @param message the message to forward
|
2020-03-14 22:44:03 +01:00
|
|
|
* @param recipient the new recipient of the message
|
2020-03-23 21:52:33 +01:00
|
|
|
* @since Envoy Client v0.1-beta
|
2020-03-14 22:44:03 +01:00
|
|
|
*/
|
2020-03-22 16:51:44 +01:00
|
|
|
private void forwardMessage(Message message, User... recipients) {
|
|
|
|
Arrays.stream(recipients).forEach(recipient -> {
|
2020-03-26 16:06:18 +01:00
|
|
|
if (message != null && recipients != null) sendMessage(new MessageBuilder(message, recipient.getID(), localDB.getIDGenerator()).build());
|
2020-03-22 16:51:44 +01:00
|
|
|
else throw new NullPointerException("No recipient or no message selected");
|
|
|
|
});
|
2020-03-14 22:44:03 +01:00
|
|
|
|
2020-03-18 08:03:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("unused")
|
2020-03-22 16:51:44 +01:00
|
|
|
private void forwardMessages(Collection<Message> messages, User... recipients) {
|
|
|
|
messages.forEach(message -> { forwardMessage(message, recipients); });
|
2020-03-18 08:03:35 +01:00
|
|
|
}
|
|
|
|
|
2020-03-14 22:44:03 +01:00
|
|
|
/**
|
|
|
|
* Sends a {@link Message} to the server.
|
|
|
|
*
|
|
|
|
* @param message the message to send
|
2020-03-23 21:52:33 +01:00
|
|
|
* @since Envoy Client v0.1-beta
|
2020-03-14 22:44:03 +01:00
|
|
|
*/
|
|
|
|
private void sendMessage(final Message message) {
|
|
|
|
try {
|
2019-12-31 10:57:11 +01:00
|
|
|
// Send message
|
2020-02-06 21:28:02 +01:00
|
|
|
writeProxy.writeMessage(message);
|
2019-12-31 10:57:11 +01:00
|
|
|
|
2020-03-24 18:38:47 +01:00
|
|
|
// Add message to PersistentLocalDB and update UI
|
2020-03-28 15:32:24 +01:00
|
|
|
// currentChat.appendMessage(message);
|
2019-12-07 10:44:25 +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
|
|
|
|
2020-02-03 21:52:48 +01:00
|
|
|
// Request a new id generator if all IDs were used
|
2020-03-26 16:06:18 +01:00
|
|
|
if (!localDB.getIDGenerator().hasNext()) client.requestIdGenerator();
|
2020-01-29 07:44:25 +01:00
|
|
|
|
2019-12-07 10:44:25 +01:00
|
|
|
} catch (Exception e) {
|
2020-02-05 07:09:25 +01:00
|
|
|
JOptionPane.showMessageDialog(this, "Error sending message:\n" + e.toString(), "Message sending error", JOptionPane.ERROR_MESSAGE);
|
2019-12-07 10:44:25 +01:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-05 20:58:30 +01:00
|
|
|
private void readCurrentChat() {
|
|
|
|
try {
|
2020-02-06 21:28:02 +01:00
|
|
|
currentChat.read(writeProxy);
|
2020-03-18 22:13:58 +01:00
|
|
|
if (messageList.getRenderer() != null) messageList.synchronizeModel();
|
2020-02-05 20:58:30 +01:00
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
logger.log(Level.WARNING, "Couldn't notify server about message status change", e);
|
|
|
|
}
|
|
|
|
}
|
2020-02-08 11:43:37 +01:00
|
|
|
|
2020-02-06 22:19:33 +01:00
|
|
|
private void drawChatBox(GridBagConstraints gbc_scrollPane) {
|
|
|
|
contentPane.remove(searchPane);
|
|
|
|
contentPane.add(scrollPane, gbc_scrollPane);
|
|
|
|
contentPane.revalidate();
|
|
|
|
contentPane.repaint();
|
|
|
|
}
|
2020-02-08 11:43:37 +01:00
|
|
|
|
2020-02-06 22:19:33 +01:00
|
|
|
private void drawContactSearch(GridBagConstraints gbc_searchPane) {
|
|
|
|
currentChat = null;
|
|
|
|
userList.removeSelectionInterval(0, userList.getModel().getSize() - 1);
|
|
|
|
messageList.setModel(null);
|
|
|
|
textPane.setText("");
|
|
|
|
contentPane.remove(scrollPane);
|
|
|
|
contentPane.add(searchPane, gbc_searchPane);
|
|
|
|
contentPane.revalidate();
|
|
|
|
contentPane.repaint();
|
|
|
|
}
|
2020-02-05 20:58:30 +01:00
|
|
|
|
2019-12-07 10:44:25 +01:00
|
|
|
/**
|
2020-02-06 21:28:02 +01:00
|
|
|
* Initializes the components responsible server communication and
|
|
|
|
* persistence.<br>
|
|
|
|
* <br>
|
|
|
|
* This will trigger the display of the contact list.
|
2019-12-21 19:00:29 +01:00
|
|
|
*
|
2020-02-06 21:28:02 +01:00
|
|
|
* @param client the client used to send and receive messages
|
2020-03-24 18:38:47 +01:00
|
|
|
* @param localDB the local database used to manage stored messages
|
2020-02-06 21:28:02 +01:00
|
|
|
* and users
|
|
|
|
* @param writeProxy the write proxy used to send messages and status change
|
|
|
|
* events to the server or cache them inside the local
|
|
|
|
* database
|
2020-03-23 21:52:33 +01:00
|
|
|
* @since Envoy Client v0.3-alpha
|
2019-12-21 19:00:29 +01:00
|
|
|
*/
|
2020-03-24 18:38:47 +01:00
|
|
|
public void initContent(Client client, LocalDB localDB, WriteProxy writeProxy) {
|
2020-02-06 21:28:02 +01:00
|
|
|
this.client = client;
|
2020-03-24 18:38:47 +01:00
|
|
|
this.localDB = localDB;
|
2020-02-06 21:28:02 +01:00
|
|
|
this.writeProxy = writeProxy;
|
2020-03-14 16:58:19 +01:00
|
|
|
|
2020-03-26 16:06:18 +01:00
|
|
|
messageList.setRenderer((list, message) -> new MessageComponent(list, message, client.getSender().getID()));
|
2020-03-18 22:13:58 +01:00
|
|
|
|
2020-03-14 16:58:19 +01:00
|
|
|
// Load users and chats
|
|
|
|
new Thread(() -> {
|
2020-03-24 18:38:47 +01:00
|
|
|
localDB.getUsers().values().forEach(user -> {
|
2020-03-14 16:58:19 +01:00
|
|
|
userListModel.addElement(user);
|
|
|
|
|
|
|
|
// Check if user exists in local DB
|
2020-03-26 16:06:18 +01:00
|
|
|
if (localDB.getChats().stream().noneMatch(c -> c.getRecipient().getID() == user.getID())) localDB.getChats().add(new Chat(user));
|
2020-03-14 16:58:19 +01:00
|
|
|
});
|
|
|
|
SwingUtilities.invokeLater(() -> userList.setModel(userListModel));
|
|
|
|
|
|
|
|
revalidate();
|
|
|
|
repaint();
|
|
|
|
}).start();
|
2019-12-21 19:00:29 +01:00
|
|
|
}
|
2020-03-14 11:17:43 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether the length of the text inside messageEnterTextArea >=
|
2020-03-14 17:04:51 +01:00
|
|
|
* {@link ChatWindow#MAX_MESSAGE_LENGTH}
|
2020-03-14 11:17:43 +01:00
|
|
|
* and splits the text into the allowed part, if that is the case.
|
|
|
|
*
|
2020-03-23 21:52:33 +01:00
|
|
|
* @since Envoy Client v0.1-beta
|
2020-03-14 11:17:43 +01:00
|
|
|
*/
|
|
|
|
private void checkMessageTextLength() {
|
|
|
|
String input = messageEnterTextArea.getText();
|
2020-03-14 17:04:51 +01:00
|
|
|
if (input.length() >= MAX_MESSAGE_LENGTH) {
|
|
|
|
messageEnterTextArea.setText(input.substring(0, MAX_MESSAGE_LENGTH - 1));
|
2020-03-14 11:17:43 +01:00
|
|
|
// TODO: current notification is like being hit with a hammer, maybe it should
|
|
|
|
// be replaced with a more subtle notification
|
|
|
|
JOptionPane.showMessageDialog(messageEnterTextArea,
|
|
|
|
"the maximum length for a message has been reached",
|
|
|
|
"maximum message length reached",
|
|
|
|
JOptionPane.WARNING_MESSAGE);
|
|
|
|
}
|
|
|
|
}
|
2020-03-22 17:20:05 +01:00
|
|
|
|
|
|
|
private void checkPostButton(String text) { postButton.setEnabled(!text.trim().isBlank()); }
|
2019-12-07 10:44:25 +01:00
|
|
|
}
|