Adjusted ChatWindow and ContactsChooserDialog to new interface

This commit is contained in:
Kai S. K. Engelbart 2020-03-20 18:32:57 +01:00
parent f5ddadf4d0
commit 7dfeab5835
3 changed files with 55 additions and 8 deletions

View File

@ -25,6 +25,7 @@ import envoy.client.net.WriteProxy;
import envoy.client.ui.ContextMenu; import envoy.client.ui.ContextMenu;
import envoy.client.ui.Theme; 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.ComponentListModel; import envoy.client.ui.list.ComponentListModel;
import envoy.client.ui.primary.PrimaryButton; import envoy.client.ui.primary.PrimaryButton;
import envoy.client.ui.primary.PrimaryScrollPane; import envoy.client.ui.primary.PrimaryScrollPane;
@ -125,18 +126,21 @@ public class ChatWindow extends JFrame {
contentPane.setLayout(gbl_contentPane); contentPane.setLayout(gbl_contentPane);
messageList.setBorder(new EmptyBorder(space, space, space, space)); messageList.setBorder(new EmptyBorder(space, space, space, space));
messageList.setSelectionMode(SelectionMode.SINGLE);
Map<String, ActionListener> commands = new HashMap<>() { Map<String, ActionListener> commands = new HashMap<>() {
private static final long serialVersionUID = -2755235774946990126L; private static final long serialVersionUID = -2755235774946990126L;
{ {
put("forward selected message", put("forward selected message",
evt -> forwardMessageToMultipleUsers(messageList.getSelection().get(0), evt -> forwardMessageToMultipleUsers(messageList
ContactsChooserDialog.showForwardingDialog("Forward selected message to", messageList.getSelection().get(0), client))); .getSingleSelectedElement(),
ContactsChooserDialog
.showForwardingDialog("Forward selected message to", messageList.getSingleSelectedElement(), client)));
put("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.getSelection().get(0).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

View File

@ -15,6 +15,7 @@ import envoy.client.data.Settings;
import envoy.client.net.Client; import envoy.client.net.Client;
import envoy.client.ui.Theme; 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.ComponentListModel; import envoy.client.ui.list.ComponentListModel;
import envoy.client.ui.renderer.MessageListRenderer; import envoy.client.ui.renderer.MessageListRenderer;
import envoy.client.ui.renderer.UserComponentListRenderer; import envoy.client.ui.renderer.UserComponentListRenderer;
@ -69,7 +70,7 @@ public class ContactsChooserDialog extends JDialog {
dialog.getContentPanel() dialog.getContentPanel()
.add(new MessageListRenderer(client.getSender().getId()).getListCellComponent(null, message, false), BorderLayout.NORTH); .add(new MessageListRenderer(client.getSender().getId()).getListCellComponent(null, message, false), BorderLayout.NORTH);
List<User> results = new ArrayList<>(); List<User> results = new ArrayList<>();
dialog.addOkButtonActionListener(e -> { results.addAll(dialog.getContactList().getSelection()); dialog.dispose(); }); dialog.addOkButtonActionListener(e -> { results.addAll(dialog.getContactList().getSelectedElements()); dialog.dispose(); });
ComponentListModel<User> contactListModel = dialog.getContactList().getModel(); ComponentListModel<User> contactListModel = dialog.getContactList().getModel();
client.getContacts().getContacts().forEach(user -> contactListModel.add(user)); client.getContacts().getContacts().forEach(user -> contactListModel.add(user));
dialog.setVisible(true); dialog.setVisible(true);
@ -82,7 +83,7 @@ public class ContactsChooserDialog extends JDialog {
* @since Envoy v0.1-beta * @since Envoy v0.1-beta
*/ */
private ContactsChooserDialog() { private ContactsChooserDialog() {
contactList.enableMultipleSelection(); contactList.setSelectionMode(SelectionMode.MULTIPLE);
// setBounds(100, 100, 450, 300); // setBounds(100, 100, 450, 300);
setModal(true); setModal(true);
getContentPane().setLayout(new BorderLayout()); getContentPane().setLayout(new BorderLayout());

View File

@ -30,8 +30,26 @@ public class ComponentList<E> extends JPanel {
private static final long serialVersionUID = 1759644503942876737L; private static final long serialVersionUID = 1759644503942876737L;
/**
* Defines the possible modes of selection that can be performed by the user
*
* @since Envoy v0.1-beta
*/
public static enum SelectionMode { public static enum SelectionMode {
NONE, SINGLE, MULTIPLE /**
* Selection is completely ignored.
*/
NONE,
/**
* Only a single element can be selected.
*/
SINGLE,
/**
* Multiple elements can be selected regardless of their position.
*/
MULTIPLE
} }
/** /**
@ -202,6 +220,30 @@ public class ComponentList<E> extends JPanel {
*/ */
public Set<Integer> getSelection() { return selection; } public Set<Integer> getSelection() { return selection; }
/**
* @return a set of all selected elements
* @since Envoy v0.1-beta
*/
public Set<E> getSelectedElements() {
var selectedElements = new HashSet<E>();
selection.forEach(i -> selectedElements.add(model.get(i)));
return selectedElements;
}
/**
* @return the index of an arbitrary selected element
* @throws java.util.NoSuchElementException if no selection is present
* @since Envoy v0.1-beta
*/
public int getSingleSelection() { return selection.iterator().next(); }
/**
* @return an arbitrary selected element
* @throws java.util.NoSuchElementException if no selection is present
* @since Envoy v0.1-beta
*/
public E getSingleSelectedElement() { return model.get(getSingleSelection()); }
/** /**
* @return the model * @return the model
* @since Envoy v0.1-beta * @since Envoy v0.1-beta
@ -221,13 +263,13 @@ public class ComponentList<E> extends JPanel {
public void setRenderer(ComponentListCellRenderer<E> renderer) { this.renderer = renderer; } public void setRenderer(ComponentListCellRenderer<E> renderer) { this.renderer = renderer; }
/** /**
* @return the selectionMode * @return the selection mode
* @since Envoy v0.1-beta * @since Envoy v0.1-beta
*/ */
public SelectionMode getSelectionMode() { return selectionMode; } public SelectionMode getSelectionMode() { return selectionMode; }
/** /**
* @param selectionMode the selectionMode to set * @param selectionMode the selection mode to set
* @since Envoy v0.1-beta * @since Envoy v0.1-beta
*/ */
public void setSelectionMode(SelectionMode selectionMode) { this.selectionMode = selectionMode; } public void setSelectionMode(SelectionMode selectionMode) { this.selectionMode = selectionMode; }