diff --git a/src/main/java/envoy/client/ui/container/ChatWindow.java b/src/main/java/envoy/client/ui/container/ChatWindow.java index a41a465..7d883c2 100644 --- a/src/main/java/envoy/client/ui/container/ChatWindow.java +++ b/src/main/java/envoy/client/ui/container/ChatWindow.java @@ -25,6 +25,7 @@ import envoy.client.net.WriteProxy; import envoy.client.ui.ContextMenu; import envoy.client.ui.Theme; import envoy.client.ui.list.ComponentList; +import envoy.client.ui.list.ComponentList.SelectionMode; import envoy.client.ui.list.ComponentListModel; import envoy.client.ui.primary.PrimaryButton; import envoy.client.ui.primary.PrimaryScrollPane; @@ -125,18 +126,21 @@ public class ChatWindow extends JFrame { contentPane.setLayout(gbl_contentPane); messageList.setBorder(new EmptyBorder(space, space, space, space)); + messageList.setSelectionMode(SelectionMode.SINGLE); Map commands = new HashMap<>() { private static final long serialVersionUID = -2755235774946990126L; { put("forward selected message", - evt -> forwardMessageToMultipleUsers(messageList.getSelection().get(0), - ContactsChooserDialog.showForwardingDialog("Forward selected message to", messageList.getSelection().get(0), client))); + evt -> forwardMessageToMultipleUsers(messageList + .getSingleSelectedElement(), + ContactsChooserDialog + .showForwardingDialog("Forward selected message to", messageList.getSingleSelectedElement(), client))); put("copy", evt -> { // TODO should be enhanced to allow also copying of message attachments, // especially pictures - StringSelection copy = new StringSelection(messageList.getSelection().get(0).getText()); + StringSelection copy = new StringSelection(messageList.getSingleSelectedElement().getText()); Toolkit.getDefaultToolkit().getSystemClipboard().setContents(copy, copy); }); // TODO insert implementation to edit and delete messages diff --git a/src/main/java/envoy/client/ui/container/ContactsChooserDialog.java b/src/main/java/envoy/client/ui/container/ContactsChooserDialog.java index b85f9a8..467d43f 100644 --- a/src/main/java/envoy/client/ui/container/ContactsChooserDialog.java +++ b/src/main/java/envoy/client/ui/container/ContactsChooserDialog.java @@ -15,6 +15,7 @@ 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; import envoy.client.ui.list.ComponentListModel; import envoy.client.ui.renderer.MessageListRenderer; import envoy.client.ui.renderer.UserComponentListRenderer; @@ -69,7 +70,7 @@ public class ContactsChooserDialog extends JDialog { dialog.getContentPanel() .add(new MessageListRenderer(client.getSender().getId()).getListCellComponent(null, message, false), BorderLayout.NORTH); List results = new ArrayList<>(); - dialog.addOkButtonActionListener(e -> { results.addAll(dialog.getContactList().getSelection()); dialog.dispose(); }); + dialog.addOkButtonActionListener(e -> { results.addAll(dialog.getContactList().getSelectedElements()); dialog.dispose(); }); ComponentListModel contactListModel = dialog.getContactList().getModel(); client.getContacts().getContacts().forEach(user -> contactListModel.add(user)); dialog.setVisible(true); @@ -82,7 +83,7 @@ public class ContactsChooserDialog extends JDialog { * @since Envoy v0.1-beta */ private ContactsChooserDialog() { - contactList.enableMultipleSelection(); + contactList.setSelectionMode(SelectionMode.MULTIPLE); // setBounds(100, 100, 450, 300); setModal(true); getContentPane().setLayout(new BorderLayout()); diff --git a/src/main/java/envoy/client/ui/list/ComponentList.java b/src/main/java/envoy/client/ui/list/ComponentList.java index 2cac513..199946d 100644 --- a/src/main/java/envoy/client/ui/list/ComponentList.java +++ b/src/main/java/envoy/client/ui/list/ComponentList.java @@ -30,8 +30,26 @@ public class ComponentList extends JPanel { 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 { - 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 extends JPanel { */ public Set getSelection() { return selection; } + /** + * @return a set of all selected elements + * @since Envoy v0.1-beta + */ + public Set getSelectedElements() { + var selectedElements = new HashSet(); + 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 * @since Envoy v0.1-beta @@ -221,13 +263,13 @@ public class ComponentList extends JPanel { public void setRenderer(ComponentListCellRenderer renderer) { this.renderer = renderer; } /** - * @return the selectionMode + * @return the selection mode * @since Envoy v0.1-beta */ public SelectionMode getSelectionMode() { return selectionMode; } /** - * @param selectionMode the selectionMode to set + * @param selectionMode the selection mode to set * @since Envoy v0.1-beta */ public void setSelectionMode(SelectionMode selectionMode) { this.selectionMode = selectionMode; }