Added renderer to ContactsChooserDialog

This commit is contained in:
delvh 2020-03-23 21:04:32 +01:00
parent ceb50cd01c
commit 930da62b79
3 changed files with 41 additions and 30 deletions

View File

@ -4,7 +4,10 @@ import java.awt.*;
import java.awt.datatransfer.StringSelection; import java.awt.datatransfer.StringSelection;
import java.awt.event.*; import java.awt.event.*;
import java.io.IOException; 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.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -126,30 +129,19 @@ public class ChatWindow extends JFrame {
comp.setBackground(isSelected ? theme.getSelectionColor() : theme.getCellColor()); comp.setBackground(isSelected ? theme.getSelectionColor() : theme.getCellColor());
// ContextMenu // ContextMenu
Map<String, ActionListener> commands = new HashMap<>() { Map<String, ActionListener> commands = Map.of("forward selected message", evt -> {
private static final long serialVersionUID = -2755235774946990126L;
{
put("forward selected message", evt -> {
final Message selectedMessage = messageList.getSingleSelectedElement(); final Message selectedMessage = messageList.getSingleSelectedElement();
forwardMessage(selectedMessage, List<User> chosenContacts = ContactsChooserDialog
ContactsChooserDialog .showForwardingDialog("Forward selected message to", null, selectedMessage, localDb.getUsers().values());
.showForwardingDialog("Forward selected message to", null, selectedMessage, localDb.getUsers().values()) if (chosenContacts != null && chosenContacts.size() > 0) forwardMessage(selectedMessage, chosenContacts.toArray(new User[0]));
.toArray(new User[0])); }, "copy", evt -> {
});
put("copy", evt -> {
// TODO should be enhanced to allow also copying of message attachments, // TODO should be enhanced to allow also copying of message attachments,
// especially pictures // especially pictures
StringSelection copy = new StringSelection(messageList.getSingleSelectedElement().getText()); StringSelection copy = new StringSelection(messageList.getSingleSelectedElement().getText());
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(copy, copy); Toolkit.getDefaultToolkit().getSystemClipboard().setContents(copy, copy);
});
// TODO insert implementation to edit and delete messages // TODO insert implementation to edit and delete messages
put("delete", evt -> {}); }, "delete", evt -> {}, "edit", evt -> {}, "quote", evt -> {});
put("edit", evt -> {});
put("quote", evt -> {});
}
};
if (isSelected) { if (isSelected) {
contextMenu = new ContextMenu(null, comp, commands, null, null).build(); contextMenu = new ContextMenu(null, comp, commands, null, null).build();
contextMenu.show(comp, 0, 0); contextMenu.show(comp, 0, 0);

View File

@ -18,6 +18,7 @@ import envoy.client.ui.Theme;
import envoy.client.ui.list.ComponentList; import envoy.client.ui.list.ComponentList;
import envoy.client.ui.list.ComponentList.SelectionMode; import envoy.client.ui.list.ComponentList.SelectionMode;
import envoy.client.ui.list.Model; import envoy.client.ui.list.Model;
import envoy.client.ui.list_component.UserComponent;
import envoy.data.Message; import envoy.data.Message;
import envoy.data.User; import envoy.data.User;
@ -35,7 +36,8 @@ public class ContactsChooserDialog extends JDialog {
private static final long serialVersionUID = -5774558118579032256L; private static final long serialVersionUID = -5774558118579032256L;
private ComponentList<User> contactList = new ComponentList<User>().setModel(new Model<User>()); private ComponentList<User> contactList = new ComponentList<User>().setModel(new Model<User>())
.setRenderer((list, user) -> new UserComponent(user));
private JButton okButton = new JButton("Ok"); private JButton okButton = new JButton("Ok");
private JButton cancelButton = new JButton("Cancel"); 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(); }); dialog.addOkButtonActionListener(e -> { results.addAll(dialog.getContactList().getSelectedElements()); dialog.dispose(); });
Model<User> contactListModel = dialog.getContactList().getModel(); Model<User> contactListModel = dialog.getContactList().getModel();
users.forEach(contactListModel::add); users.forEach(contactListModel::add);
dialog.setModalityType(ModalityType.APPLICATION_MODAL);
dialog.setVisible(true); dialog.setVisible(true);
dialog.repaint(); dialog.repaint();
dialog.revalidate(); dialog.revalidate();
@ -90,12 +93,16 @@ public class ContactsChooserDialog extends JDialog {
*/ */
private ContactsChooserDialog(Component parent) { private ContactsChooserDialog(Component parent) {
contactList.setSelectionMode(SelectionMode.MULTIPLE); 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); // setBounds(100, 100, 450, 300);
setModal(true);
setLocationRelativeTo(parent); setLocationRelativeTo(parent);
getContentPane().setLayout(new BorderLayout()); getContentPane().setLayout(new BorderLayout());
setBackground(theme.getBackgroundColor()); setBackground(theme.getBackgroundColor());
setForeground(theme.getTextColor()); setForeground(theme.getTextColor());
setSize(400, 400);
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER); getContentPane().add(contentPanel, BorderLayout.CENTER);
contentPanel.setLayout(new BorderLayout(0, 0)); contentPanel.setLayout(new BorderLayout(0, 0));
@ -117,6 +124,17 @@ public class ContactsChooserDialog extends JDialog {
buttonPane.add(cancelButton, BorderLayout.WEST); 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());
} }
/** /**

View File

@ -83,17 +83,18 @@ public class ComponentList<E> extends JPanel {
*/ */
public void selectElement(int index) { public void selectElement(int index) {
final JComponent element = getComponent(index); final JComponent element = getComponent(index);
if (selection.contains(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); if (selectionHandler != null) selectionHandler.selectionChanged(model.get(index), element, true);
selection.remove(index); selection.remove(index);
} else { } else {
// Remove old selection if single selection is enabled // Remove old selection if single selection is enabled
if (selectionMode == SelectionMode.SINGLE) clearSelection(); if (selectionMode == SelectionMode.SINGLE) clearSelection();
// Select item
if (selectionMode != SelectionMode.NONE) { if (selectionMode != SelectionMode.NONE) {
// Assign new selection // Assign new selection