From 68c73441372e3df9fabe6534f6f905c626b91ab8 Mon Sep 17 00:00:00 2001 From: delvh Date: Sun, 22 Mar 2020 16:51:44 +0100 Subject: [PATCH] ContactsChooserDialog is now centered around a parent component additionally: * fixed bug not updating date color when changing themes * improved style of forwardMessage-Methoden --- .../envoy/client/ui/container/ChatWindow.java | 23 ++++++++----------- .../ui/container/ContactsChooserDialog.java | 21 +++++++++++------ 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/src/main/java/envoy/client/ui/container/ChatWindow.java b/src/main/java/envoy/client/ui/container/ChatWindow.java index 6f16af4..b762ceb 100644 --- a/src/main/java/envoy/client/ui/container/ChatWindow.java +++ b/src/main/java/envoy/client/ui/container/ChatWindow.java @@ -154,6 +154,7 @@ public class ChatWindow extends JFrame { }); scrollPane.setViewportView(messageList); + scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); scrollPane.addComponentListener(new ComponentAdapter() { // Update list elements when scroll pane (and thus list) is resized @@ -163,7 +164,6 @@ public class ChatWindow extends JFrame { messageList.synchronizeModel(); } }); - scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); GridBagConstraints gbc_scrollPane = new GridBagConstraints(); gbc_scrollPane.fill = GridBagConstraints.BOTH; @@ -213,7 +213,6 @@ public class ChatWindow extends JFrame { gbc_postButton.fill = GridBagConstraints.BOTH; gbc_postButton.gridx = 2; gbc_postButton.gridy = 3; - gbc_postButton.insets = insets; postButton.addActionListener((evt) -> { postMessage(); }); @@ -494,6 +493,7 @@ public class ChatWindow extends JFrame { // messageList messageList.setForeground(theme.getTextColor()); messageList.setBackground(theme.getCellColor()); + messageList.synchronizeModel(); // scrollPane scrollPane.applyTheme(theme); scrollPane.autoscroll(); @@ -544,9 +544,6 @@ public class ChatWindow extends JFrame { } String text = messageEnterTextArea.getText().trim(); if (!text.isEmpty()) checkMessageTextLength(); - // delete final line break, if present (especially if "Enter" is used to send - // the message) - if (text.endsWith(System.getProperty("line.separator"))) text = text.substring(0, text.lastIndexOf(System.getProperty("line.separator"))); // Create message final Message message = new MessageBuilder(localDb.getUser().getId(), currentChat.getRecipient().getId(), localDb.getIdGenerator()) @@ -560,21 +557,21 @@ public class ChatWindow extends JFrame { /** * Forwards a message. * - * @param msg the message to forward + * @param message the message to forward * @param recipient the new recipient of the message * @since Envoy v0.1-beta */ - private void forwardMessage(Message msg, User recipient) { - sendMessage(new MessageBuilder(msg, recipient.getId(), localDb.getIdGenerator()).build()); - } + private void forwardMessage(Message message, User... recipients) { + Arrays.stream(recipients).forEach(recipient -> { + if (message != null && recipients != null) sendMessage(new MessageBuilder(message, recipient.getId(), localDb.getIdGenerator()).build()); + else throw new NullPointerException("No recipient or no message selected"); + }); - private void forwardMessageToMultipleUsers(Message message, Collection recipients) { - recipients.forEach(recipient -> forwardMessage(message, recipient)); } @SuppressWarnings("unused") - private void forwardMultipleMessagesToMultipleUsers(Collection messages, Collection recipients) { - messages.forEach(message -> forwardMessageToMultipleUsers(message, recipients)); + private void forwardMessages(Collection messages, User... recipients) { + messages.forEach(message -> { forwardMessage(message, recipients); }); } /** diff --git a/src/main/java/envoy/client/ui/container/ContactsChooserDialog.java b/src/main/java/envoy/client/ui/container/ContactsChooserDialog.java index 5677e7d..b507cbc 100755 --- a/src/main/java/envoy/client/ui/container/ContactsChooserDialog.java +++ b/src/main/java/envoy/client/ui/container/ContactsChooserDialog.java @@ -1,9 +1,11 @@ package envoy.client.ui.container; import java.awt.BorderLayout; +import java.awt.Component; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.util.ArrayList; +import java.util.Collection; import java.util.List; import javax.swing.JButton; @@ -12,7 +14,6 @@ import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import envoy.client.data.Settings; -import envoy.client.net.Client; import envoy.client.ui.Theme; import envoy.client.ui.list.ComponentList; import envoy.client.ui.list.ComponentList.SelectionMode; @@ -34,7 +35,7 @@ public class ContactsChooserDialog extends JDialog { private static final long serialVersionUID = -5774558118579032256L; - private ComponentList contactList = new ComponentList<>(); + private ComponentList contactList = new ComponentList().setModel(new Model()); private JButton okButton = new JButton("Ok"); private JButton cancelButton = new JButton("Cancel"); @@ -54,14 +55,17 @@ public class ContactsChooserDialog extends JDialog { * ArrayList. * * @param title the title of the dialog + * @param parent this @{@link Component} will be parsed to + * {@link java.awt.Window#setLocationRelativeTo(Component)} in + * order to change the location of the dialog * @param message the {@link Message} to display on top of the Dialog - * @param client the client whose contacts should be displayed + * @param users the users that should be displayed * @return the selected Element (yet has to be casted to the wanted type due to * the Generics limitations in Java) * @since Envoy v0.1-beta */ - public static List showForwardingDialog(String title, Message message, Client client) { - ContactsChooserDialog dialog = new ContactsChooserDialog(); + public static List showForwardingDialog(String title, Component parent, Message message, Collection users) { + ContactsChooserDialog dialog = new ContactsChooserDialog(parent); dialog.setTitle(title); dialog.setDefaultCloseOperation(DISPOSE_ON_CLOSE); dialog.addCancelButtonActionListener(e -> dialog.dispose()); @@ -72,7 +76,7 @@ public class ContactsChooserDialog extends JDialog { List results = new ArrayList<>(); dialog.addOkButtonActionListener(e -> { results.addAll(dialog.getContactList().getSelectedElements()); dialog.dispose(); }); Model contactListModel = dialog.getContactList().getModel(); - client.getContacts().getContacts().forEach(user -> contactListModel.add(user)); + users.forEach(contactListModel::add); dialog.setVisible(true); dialog.repaint(); dialog.revalidate(); @@ -80,12 +84,15 @@ public class ContactsChooserDialog extends JDialog { } /** + * @param parent this @{@link Component} will be parsed to + * {@link java.awt.Window#setLocationRelativeTo(Component)} * @since Envoy v0.1-beta */ - private ContactsChooserDialog() { + private ContactsChooserDialog(Component parent) { contactList.setSelectionMode(SelectionMode.MULTIPLE); // setBounds(100, 100, 450, 300); setModal(true); + setLocationRelativeTo(parent); getContentPane().setLayout(new BorderLayout()); setBackground(theme.getBackgroundColor()); setForeground(theme.getTextColor());