test.ca.odell.glazedlists.swing.EventSelectionModelTest Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of glazedlists_java15 Show documentation
Show all versions of glazedlists_java15 Show documentation
Event-driven lists for dynamically filtered and sorted tables
/* Glazed Lists (c) 2003-2006 */
/* http://publicobject.com/glazedlists/ publicobject.com,*/
/* O'Dell Engineering Ltd.*/
package ca.odell.glazedlists.swing;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import javax.swing.DefaultListSelectionModel;
import javax.swing.JList;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import ca.odell.glazedlists.BasicEventList;
import ca.odell.glazedlists.DelayList;
import ca.odell.glazedlists.EventList;
import ca.odell.glazedlists.FilterList;
import ca.odell.glazedlists.GlazedLists;
import ca.odell.glazedlists.SortedList;
import ca.odell.glazedlists.ThreadRecorderEventList;
import ca.odell.glazedlists.TreeList;
import ca.odell.glazedlists.impl.testing.GlazedListsTests;
import ca.odell.glazedlists.matchers.Matchers;
/**
* This test verifies that the EventSelectionModel works.
*
* @author Jesse Wilson
*/
public class EventSelectionModelTest extends SwingTestCase {
/**
* Tests that selection survives a sorting.
*/
public void guiTestSort() {
EventList list = new BasicEventList();
SortedList sorted = new SortedList(list, null);
EventSelectionModel eventSelectionModel = new EventSelectionModel(sorted);
// populate the list
list.addAll(GlazedListsTests.delimitedStringToList("E C F B A D"));
assertEquals(Collections.EMPTY_LIST, eventSelectionModel.getSelected());
assertEquals(Collections.EMPTY_LIST, eventSelectionModel.getTogglingSelected());
assertEquals(list, eventSelectionModel.getDeselected());
assertEquals(list, eventSelectionModel.getTogglingDeselected());
// select the vowels
eventSelectionModel.addSelectionInterval(0, 0);
eventSelectionModel.addSelectionInterval(4, 4);
assertEquals(GlazedListsTests.delimitedStringToList("E A"), eventSelectionModel.getSelected());
assertEquals(GlazedListsTests.delimitedStringToList("E A"), eventSelectionModel.getTogglingSelected());
assertEquals(GlazedListsTests.delimitedStringToList("C F B D"), eventSelectionModel.getDeselected());
assertEquals(GlazedListsTests.delimitedStringToList("C F B D"), eventSelectionModel.getTogglingDeselected());
// flip the list
sorted.setComparator(GlazedLists.comparableComparator());
assertEquals(GlazedListsTests.delimitedStringToList("A E"), eventSelectionModel.getSelected());
assertEquals(GlazedListsTests.delimitedStringToList("A E"), eventSelectionModel.getTogglingSelected());
assertEquals(GlazedListsTests.delimitedStringToList("B C D F"), eventSelectionModel.getDeselected());
assertEquals(GlazedListsTests.delimitedStringToList("B C D F"), eventSelectionModel.getTogglingDeselected());
// flip the list again
sorted.setComparator(GlazedLists.reverseComparator());
assertEquals(GlazedListsTests.delimitedStringToList("E A"), eventSelectionModel.getSelected());
assertEquals(GlazedListsTests.delimitedStringToList("E A"), eventSelectionModel.getTogglingSelected());
assertEquals(GlazedListsTests.delimitedStringToList("F D C B"), eventSelectionModel.getDeselected());
assertEquals(GlazedListsTests.delimitedStringToList("F D C B"), eventSelectionModel.getTogglingDeselected());
}
/**
* Verifies that the selected index is cleared when the selection is cleared.
*/
public void guiTestClear() {
EventList list = new BasicEventList();
EventSelectionModel eventSelectionModel = new EventSelectionModel(list);
// populate the list
list.addAll(GlazedListsTests.delimitedStringToList("A B C D E F"));
// make a selection
eventSelectionModel.addSelectionInterval(1, 4);
// test the selection
assertEquals(list.subList(1, 5), eventSelectionModel.getSelected());
assertEquals(list.subList(1, 5), eventSelectionModel.getTogglingSelected());
assertEquals(GlazedListsTests.delimitedStringToList("A F"), eventSelectionModel.getDeselected());
assertEquals(GlazedListsTests.delimitedStringToList("A F"), eventSelectionModel.getTogglingDeselected());
// clear the selection
eventSelectionModel.clearSelection();
// test the selection
assertEquals(Collections.EMPTY_LIST, eventSelectionModel.getSelected());
assertEquals(Collections.EMPTY_LIST, eventSelectionModel.getTogglingSelected());
assertEquals(-1, eventSelectionModel.getMinSelectionIndex());
assertEquals(-1, eventSelectionModel.getMaxSelectionIndex());
assertEquals(true, eventSelectionModel.isSelectionEmpty());
assertEquals(list, eventSelectionModel.getDeselected());
assertEquals(list, eventSelectionModel.getTogglingDeselected());
}
/**
* Tests the lists {@link EventSelectionModel#getTogglingSelected()} and
* {@link EventSelectionModel#getTogglingDeselected()} for programmatic selection control.
*/
public void guiTestToggleSelection() {
EventList list = new BasicEventList();
EventSelectionModel eventSelectionModel = new EventSelectionModel(list);
assertEquals(Collections.EMPTY_LIST, eventSelectionModel.getSelected());
assertEquals(Collections.EMPTY_LIST, eventSelectionModel.getTogglingSelected());
assertEquals(Collections.EMPTY_LIST, eventSelectionModel.getDeselected());
assertEquals(Collections.EMPTY_LIST, eventSelectionModel.getTogglingDeselected());
// populate the list
list.addAll(GlazedListsTests.delimitedStringToList("A B C D E F"));
assertEquals(Collections.EMPTY_LIST, eventSelectionModel.getSelected());
assertEquals(Collections.EMPTY_LIST, eventSelectionModel.getTogglingSelected());
assertEquals(list, eventSelectionModel.getDeselected());
assertEquals(list, eventSelectionModel.getTogglingDeselected());
// remove on TogglingDeselected selects
eventSelectionModel.getTogglingDeselected().remove("A");
eventSelectionModel.getTogglingDeselected().remove(1);
eventSelectionModel.getTogglingDeselected().removeAll(GlazedListsTests.delimitedStringToList("F D"));
assertEquals(GlazedListsTests.delimitedStringToList("B E"), eventSelectionModel.getDeselected());
assertEquals(GlazedListsTests.delimitedStringToList("B E"), eventSelectionModel.getTogglingDeselected());
assertEquals(GlazedListsTests.delimitedStringToList("A C D F"), eventSelectionModel.getSelected());
assertEquals(GlazedListsTests.delimitedStringToList("A C D F"), eventSelectionModel.getTogglingSelected());
// add on TogglingDeselected deselects
eventSelectionModel.getTogglingDeselected().add("F");
eventSelectionModel.getTogglingDeselected().addAll(GlazedListsTests.delimitedStringToList("C D"));
assertEquals(GlazedListsTests.delimitedStringToList("B C D E F"), eventSelectionModel.getDeselected());
assertEquals(GlazedListsTests.delimitedStringToList("B C D E F"), eventSelectionModel.getTogglingDeselected());
assertEquals(GlazedListsTests.delimitedStringToList("A"), eventSelectionModel.getSelected());
assertEquals(GlazedListsTests.delimitedStringToList("A"), eventSelectionModel.getTogglingSelected());
// add on TogglingSelected selects
eventSelectionModel.getTogglingSelected().add("F");
eventSelectionModel.getTogglingSelected().addAll(GlazedListsTests.delimitedStringToList("C D"));
assertEquals(GlazedListsTests.delimitedStringToList("B E"), eventSelectionModel.getDeselected());
assertEquals(GlazedListsTests.delimitedStringToList("B E"), eventSelectionModel.getTogglingDeselected());
assertEquals(GlazedListsTests.delimitedStringToList("A C D F"), eventSelectionModel.getSelected());
assertEquals(GlazedListsTests.delimitedStringToList("A C D F"), eventSelectionModel.getTogglingSelected());
// remove on TogglingSelected deselects
eventSelectionModel.getTogglingSelected().remove("A");
eventSelectionModel.getTogglingSelected().remove(1);
eventSelectionModel.getTogglingSelected().removeAll(GlazedListsTests.delimitedStringToList("F"));
assertEquals(GlazedListsTests.delimitedStringToList("A B D E F"), eventSelectionModel.getDeselected());
assertEquals(GlazedListsTests.delimitedStringToList("A B D E F"), eventSelectionModel.getTogglingDeselected());
assertEquals(GlazedListsTests.delimitedStringToList("C"), eventSelectionModel.getSelected());
assertEquals(GlazedListsTests.delimitedStringToList("C"), eventSelectionModel.getTogglingSelected());
// remove on source list
list.remove("C");
list.removeAll(GlazedListsTests.delimitedStringToList("B E"));
assertEquals(GlazedListsTests.delimitedStringToList("A D F"), eventSelectionModel.getDeselected());
assertEquals(GlazedListsTests.delimitedStringToList("A D F"), eventSelectionModel.getTogglingDeselected());
assertEquals(Collections.EMPTY_LIST, eventSelectionModel.getSelected());
assertEquals(Collections.EMPTY_LIST, eventSelectionModel.getTogglingSelected());
// add on source list
list.add("E");
list.addAll(GlazedListsTests.delimitedStringToList("C B"));
assertEquals(GlazedListsTests.delimitedStringToList("A D F E C B"), eventSelectionModel.getDeselected());
assertEquals(GlazedListsTests.delimitedStringToList("A D F E C B"), eventSelectionModel.getTogglingDeselected());
assertEquals(Collections.EMPTY_LIST, eventSelectionModel.getSelected());
assertEquals(Collections.EMPTY_LIST, eventSelectionModel.getTogglingSelected());
// clear on TogglingDeselected selects all deselected
eventSelectionModel.getTogglingDeselected().clear();
assertEquals(Collections.EMPTY_LIST, eventSelectionModel.getDeselected());
assertEquals(Collections.EMPTY_LIST, eventSelectionModel.getTogglingDeselected());
assertEquals(GlazedListsTests.delimitedStringToList("A D F E C B"), eventSelectionModel.getSelected());
assertEquals(GlazedListsTests.delimitedStringToList("A D F E C B"), eventSelectionModel.getTogglingSelected());
// clear on TogglingSelected deselects all selected
eventSelectionModel.getTogglingSelected().clear();
assertEquals(GlazedListsTests.delimitedStringToList("A D F E C B"), eventSelectionModel.getDeselected());
assertEquals(GlazedListsTests.delimitedStringToList("A D F E C B"), eventSelectionModel.getTogglingDeselected());
assertEquals(Collections.EMPTY_LIST, eventSelectionModel.getSelected());
assertEquals(Collections.EMPTY_LIST, eventSelectionModel.getTogglingSelected());
}
/**
* Tests a problem where the {@link EventSelectionModel} fails to fire events
*
* This test was contributed by: Sergey Bogatyrjov
*/
public void guiTestSelectionModel() {
EventList
© 2015 - 2025 Weber Informatics LLC | Privacy Policy