From 432a58330b552b31b43f146159962455ceb45efb Mon Sep 17 00:00:00 2001 From: delvh Date: Sun, 15 Mar 2020 18:55:07 +0100 Subject: [PATCH] Added multiple selection capability to the component list --- .../envoy/client/ui/list/ComponentList.java | 67 ++++++++++++++++--- 1 file changed, 58 insertions(+), 9 deletions(-) diff --git a/src/main/java/envoy/client/ui/list/ComponentList.java b/src/main/java/envoy/client/ui/list/ComponentList.java index 9b5b749..372f33b 100644 --- a/src/main/java/envoy/client/ui/list/ComponentList.java +++ b/src/main/java/envoy/client/ui/list/ComponentList.java @@ -3,6 +3,8 @@ package envoy.client.ui.list; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; +import java.util.ArrayList; +import java.util.List; import javax.swing.*; @@ -23,8 +25,9 @@ public class ComponentList extends JPanel { private ComponentListModel model; private ComponentListCellRenderer renderer; + private boolean multipleSelectionEnabled = false; - private int currentSelection = -1; + private List currentSelections = new ArrayList<>(); private static final long serialVersionUID = 1759644503942876737L; @@ -137,27 +140,38 @@ public class ComponentList extends JPanel { * @since Envoy v0.1-beta */ private void componentSelected(int index) { - if (index == currentSelection) { + // removing selection of element at index + if (currentSelections.contains(index)) { // Clear selection - update(currentSelection, false); - currentSelection = -1; + updateSelection(index, false); + currentSelections.remove(Integer.valueOf(index)); } else { - // Remove old selection - if (currentSelection >= 0) update(currentSelection, false); + // Remove old selection if multipleSelection is disabled + if (!multipleSelectionEnabled && currentSelections.size() > 0) clearSelections(); // Assign new selection - currentSelection = index; + currentSelections.add(index); // Update current selection - update(currentSelection, true); + updateSelection(index, true); } revalidate(); repaint(); } + /** + * Clears all currently active selections. + * + * @since Envoy v0.1-beta + */ + private void clearSelections() { + currentSelections.forEach(index2 -> updateSelection(index2, false)); + currentSelections.clear(); + } + /** * Replaces a list element with a newly rendered instance of its contents. * @@ -165,8 +179,43 @@ public class ComponentList extends JPanel { * @param isSelected the selection state passed to the {@link ListCellRenderer} * @since Envoy v0.1-beta */ - private void update(int index, boolean isSelected) { + private void updateSelection(int index, boolean isSelected) { remove(index); add(model.get(index), index, isSelected); } + + /** + * @return the selected elements or null if none is selected + * @since Envoy v0.1-beta + */ + public List getSelected() { + List selected = new ArrayList<>(); + currentSelections.forEach(index -> selected.add(model.get(index))); + return selected; + } + + /** + * @return the multipleSelectionEnabled + * @since Envoy v0.1-beta + */ + public boolean isMultipleSelectionEnabled() { return multipleSelectionEnabled; } + + /** + * Enables the selection of multiple elements. + * + * @see ComponentList#disableMultipleSelection + * @since Envoy v0.1-beta + */ + public void enableMultipleSelection() { this.multipleSelectionEnabled = true; } + + /** + * Allows only one element to be selected. True by default. + * + * @see ComponentList#enableMultipleSelection + * @since Envoy v0.1-beta + */ + public void disableMultipleSelection() { + this.multipleSelectionEnabled = false; + clearSelections(); + } }