ContactsChooserDialog is now centered around a parent component

additionally:
* fixed bug not updating date color when changing themes
* improved style of forwardMessage-Methoden
This commit is contained in:
delvh 2020-03-22 16:51:44 +01:00
parent 8c55f67eb0
commit 68c7344137
2 changed files with 24 additions and 20 deletions

View File

@ -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<User> recipients) {
recipients.forEach(recipient -> forwardMessage(message, recipient));
}
@SuppressWarnings("unused")
private void forwardMultipleMessagesToMultipleUsers(Collection<Message> messages, Collection<User> recipients) {
messages.forEach(message -> forwardMessageToMultipleUsers(message, recipients));
private void forwardMessages(Collection<Message> messages, User... recipients) {
messages.forEach(message -> { forwardMessage(message, recipients); });
}
/**

View File

@ -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<User> contactList = new ComponentList<>();
private ComponentList<User> contactList = new ComponentList<User>().setModel(new Model<User>());
private JButton okButton = new JButton("Ok");
private JButton cancelButton = new JButton("Cancel");
@ -54,14 +55,17 @@ public class ContactsChooserDialog extends JDialog {
* <code>ArrayList</code>.
*
* @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<User> showForwardingDialog(String title, Message message, Client client) {
ContactsChooserDialog dialog = new ContactsChooserDialog();
public static List<User> showForwardingDialog(String title, Component parent, Message message, Collection<User> 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<User> results = new ArrayList<>();
dialog.addOkButtonActionListener(e -> { results.addAll(dialog.getContactList().getSelectedElements()); dialog.dispose(); });
Model<User> 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());