Added multiple selection capability to the component list
This commit is contained in:
		@@ -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<E> extends JPanel {
 | 
			
		||||
 | 
			
		||||
	private ComponentListModel<E>			model;
 | 
			
		||||
	private ComponentListCellRenderer<E>	renderer;
 | 
			
		||||
	private boolean							multipleSelectionEnabled	= false;
 | 
			
		||||
 | 
			
		||||
	private int currentSelection = -1;
 | 
			
		||||
	private List<Integer> currentSelections = new ArrayList<>();
 | 
			
		||||
 | 
			
		||||
	private static final long serialVersionUID = 1759644503942876737L;
 | 
			
		||||
 | 
			
		||||
@@ -137,27 +140,38 @@ public class ComponentList<E> 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<E> 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<E> getSelected() {
 | 
			
		||||
		List<E> 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();
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user