/*
* ToggleSelectionModel.java
*
* Created on March 6, 2006, 3:26 PM
*
*/
package ika.gui;
import javax.swing.*;
/**
* ToggleSelectionModel toggles the selection state of an item clicked in a JList.
* modified version of http://java.sun.com/products/jfc/tsc/tech_topics/jlist_1/jlist.html
*/
public class ToggleSelectionModel extends DefaultListSelectionModel {
// toggling should only occur for the first update generated by a mouse
// press-drag-release gesture. The gestureStarted flag keeps track of this.
boolean gestureStarted = false;
public void setSelectionInterval(int index0, int index1) {
// in single selection mode, only the second argument is used.
if (this.getSelectionMode() == this.SINGLE_SELECTION) {
if (this.isSelectedIndex(index1) && !gestureStarted)
super.removeSelectionInterval(index0, index1);
else
super.setSelectionInterval(index0, index1);
} else {
// multiple selection
if (isSelectedIndex(index0) && index0 == index1 && !gestureStarted) {
super.removeSelectionInterval(index0, index1);
} else {
super.addSelectionInterval(index0, index1);
}
}
gestureStarted = true;
}
public void setValueIsAdjusting(boolean isAdjusting) {
if (isAdjusting == false) {
gestureStarted = false;
}
}
}