Added multiple selection capability to the component list
This commit is contained in:
parent
819f0ec4f2
commit
432a58330b
@ -3,6 +3,8 @@ package envoy.client.ui.list;
|
|||||||
import java.awt.event.MouseAdapter;
|
import java.awt.event.MouseAdapter;
|
||||||
import java.awt.event.MouseEvent;
|
import java.awt.event.MouseEvent;
|
||||||
import java.awt.event.MouseListener;
|
import java.awt.event.MouseListener;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
|
|
||||||
@ -23,8 +25,9 @@ public class ComponentList<E> extends JPanel {
|
|||||||
|
|
||||||
private ComponentListModel<E> model;
|
private ComponentListModel<E> model;
|
||||||
private ComponentListCellRenderer<E> renderer;
|
private ComponentListCellRenderer<E> renderer;
|
||||||
|
private boolean multipleSelectionEnabled = false;
|
||||||
|
|
||||||
private int currentSelection = -1;
|
private List<Integer> currentSelections = new ArrayList<>();
|
||||||
|
|
||||||
private static final long serialVersionUID = 1759644503942876737L;
|
private static final long serialVersionUID = 1759644503942876737L;
|
||||||
|
|
||||||
@ -137,27 +140,38 @@ public class ComponentList<E> extends JPanel {
|
|||||||
* @since Envoy v0.1-beta
|
* @since Envoy v0.1-beta
|
||||||
*/
|
*/
|
||||||
private void componentSelected(int index) {
|
private void componentSelected(int index) {
|
||||||
if (index == currentSelection) {
|
// removing selection of element at index
|
||||||
|
if (currentSelections.contains(index)) {
|
||||||
|
|
||||||
// Clear selection
|
// Clear selection
|
||||||
update(currentSelection, false);
|
updateSelection(index, false);
|
||||||
currentSelection = -1;
|
currentSelections.remove(Integer.valueOf(index));
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
// Remove old selection
|
// Remove old selection if multipleSelection is disabled
|
||||||
if (currentSelection >= 0) update(currentSelection, false);
|
if (!multipleSelectionEnabled && currentSelections.size() > 0) clearSelections();
|
||||||
|
|
||||||
// Assign new selection
|
// Assign new selection
|
||||||
currentSelection = index;
|
currentSelections.add(index);
|
||||||
|
|
||||||
// Update current selection
|
// Update current selection
|
||||||
update(currentSelection, true);
|
updateSelection(index, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
revalidate();
|
revalidate();
|
||||||
repaint();
|
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.
|
* 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}
|
* @param isSelected the selection state passed to the {@link ListCellRenderer}
|
||||||
* @since Envoy v0.1-beta
|
* @since Envoy v0.1-beta
|
||||||
*/
|
*/
|
||||||
private void update(int index, boolean isSelected) {
|
private void updateSelection(int index, boolean isSelected) {
|
||||||
remove(index);
|
remove(index);
|
||||||
add(model.get(index), index, isSelected);
|
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