From 930da62b79ea55ac6db1718ded24134373f409a5 Mon Sep 17 00:00:00 2001 From: delvh Date: Mon, 23 Mar 2020 21:04:32 +0100 Subject: [PATCH] Added renderer to ContactsChooserDialog --- .../envoy/client/ui/container/ChatWindow.java | 44 ++++++++----------- .../ui/container/ContactsChooserDialog.java | 22 +++++++++- .../envoy/client/ui/list/ComponentList.java | 5 ++- 3 files changed, 41 insertions(+), 30 deletions(-) diff --git a/src/main/java/envoy/client/ui/container/ChatWindow.java b/src/main/java/envoy/client/ui/container/ChatWindow.java index a2c2f9a..4d92a03 100644 --- a/src/main/java/envoy/client/ui/container/ChatWindow.java +++ b/src/main/java/envoy/client/ui/container/ChatWindow.java @@ -4,7 +4,10 @@ import java.awt.*; import java.awt.datatransfer.StringSelection; import java.awt.event.*; import java.io.IOException; -import java.util.*; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; +import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; @@ -126,30 +129,19 @@ public class ChatWindow extends JFrame { comp.setBackground(isSelected ? theme.getSelectionColor() : theme.getCellColor()); // ContextMenu - Map commands = new HashMap<>() { + Map commands = Map.of("forward selected message", evt -> { + final Message selectedMessage = messageList.getSingleSelectedElement(); + List chosenContacts = ContactsChooserDialog + .showForwardingDialog("Forward selected message to", null, selectedMessage, localDb.getUsers().values()); + 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 -> {}); - private static final long serialVersionUID = -2755235774946990126L; - - { - put("forward selected message", evt -> { - final Message selectedMessage = messageList.getSingleSelectedElement(); - forwardMessage(selectedMessage, - ContactsChooserDialog - .showForwardingDialog("Forward selected message to", null, selectedMessage, localDb.getUsers().values()) - .toArray(new User[0])); - }); - put("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 - put("delete", evt -> {}); - put("edit", evt -> {}); - put("quote", evt -> {}); - } - }; if (isSelected) { contextMenu = new ContextMenu(null, comp, commands, null, null).build(); contextMenu.show(comp, 0, 0); @@ -210,7 +202,7 @@ public class ChatWindow extends JFrame { gbc_messageEnterTextArea.fill = GridBagConstraints.BOTH; gbc_messageEnterTextArea.gridx = 1; gbc_messageEnterTextArea.gridy = 3; - gbc_messageEnterTextArea.insets = insets; + gbc_messageEnterTextArea.insets = insets; contentPane.add(messageEnterTextArea, gbc_messageEnterTextArea); // Post Button @@ -218,7 +210,7 @@ public class ChatWindow extends JFrame { gbc_postButton.fill = GridBagConstraints.BOTH; gbc_postButton.gridx = 2; gbc_postButton.gridy = 3; - gbc_postButton.insets = insets; + gbc_postButton.insets = insets; postButton.addActionListener((evt) -> { postMessage(); }); postButton.setEnabled(false); contentPane.add(postButton, gbc_postButton); diff --git a/src/main/java/envoy/client/ui/container/ContactsChooserDialog.java b/src/main/java/envoy/client/ui/container/ContactsChooserDialog.java index b507cbc..5832e25 100755 --- a/src/main/java/envoy/client/ui/container/ContactsChooserDialog.java +++ b/src/main/java/envoy/client/ui/container/ContactsChooserDialog.java @@ -18,6 +18,7 @@ import envoy.client.ui.Theme; import envoy.client.ui.list.ComponentList; import envoy.client.ui.list.ComponentList.SelectionMode; import envoy.client.ui.list.Model; +import envoy.client.ui.list_component.UserComponent; import envoy.data.Message; import envoy.data.User; @@ -35,7 +36,8 @@ public class ContactsChooserDialog extends JDialog { private static final long serialVersionUID = -5774558118579032256L; - private ComponentList contactList = new ComponentList().setModel(new Model()); + private ComponentList contactList = new ComponentList().setModel(new Model()) + .setRenderer((list, user) -> new UserComponent(user)); private JButton okButton = new JButton("Ok"); private JButton cancelButton = new JButton("Cancel"); @@ -77,6 +79,7 @@ public class ContactsChooserDialog extends JDialog { dialog.addOkButtonActionListener(e -> { results.addAll(dialog.getContactList().getSelectedElements()); dialog.dispose(); }); Model contactListModel = dialog.getContactList().getModel(); users.forEach(contactListModel::add); + dialog.setModalityType(ModalityType.APPLICATION_MODAL); dialog.setVisible(true); dialog.repaint(); dialog.revalidate(); @@ -90,12 +93,16 @@ public class ContactsChooserDialog extends JDialog { */ private ContactsChooserDialog(Component parent) { contactList.setSelectionMode(SelectionMode.MULTIPLE); + contactList.setSelectionHandler((user, comp, isSelected) -> { + final var theme = Settings.getInstance().getCurrentTheme(); + comp.setBackground(isSelected ? theme.getSelectionColor() : theme.getCellColor()); + }); // setBounds(100, 100, 450, 300); - setModal(true); setLocationRelativeTo(parent); getContentPane().setLayout(new BorderLayout()); setBackground(theme.getBackgroundColor()); setForeground(theme.getTextColor()); + setSize(400, 400); contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); getContentPane().add(contentPanel, BorderLayout.CENTER); contentPanel.setLayout(new BorderLayout(0, 0)); @@ -117,6 +124,17 @@ public class ContactsChooserDialog extends JDialog { buttonPane.add(cancelButton, BorderLayout.WEST); } } + applyTheme(Settings.getInstance().getCurrentTheme()); + } + + private void applyTheme(Theme theme) { + contentPanel.setBackground(theme.getBackgroundColor()); + contentPanel.setForeground(theme.getTextColor()); + contactList.setBackground(theme.getCellColor()); + okButton.setBackground(theme.getInteractableBackgroundColor()); + okButton.setForeground(theme.getTextColor()); + cancelButton.setBackground(theme.getInteractableBackgroundColor()); + cancelButton.setForeground(theme.getTextColor()); } /** diff --git a/src/main/java/envoy/client/ui/list/ComponentList.java b/src/main/java/envoy/client/ui/list/ComponentList.java index 68b92a2..c8ff3c0 100644 --- a/src/main/java/envoy/client/ui/list/ComponentList.java +++ b/src/main/java/envoy/client/ui/list/ComponentList.java @@ -83,17 +83,18 @@ public class ComponentList extends JPanel { */ public void selectElement(int index) { final JComponent element = getComponent(index); - if (selection.contains(index)) { - // Remove selection of element at index + // Deselect if clicked again if (selectionHandler != null) selectionHandler.selectionChanged(model.get(index), element, true); selection.remove(index); + } else { // Remove old selection if single selection is enabled if (selectionMode == SelectionMode.SINGLE) clearSelection(); + // Select item if (selectionMode != SelectionMode.NONE) { // Assign new selection