I'm trying to write my own implementation of ListSelectionModel and currently I'm stuck while trying to implement insertIndexInterval. I don't understand the result of this method in Sun's/Oracle's DefautListSelectionModel implementation. Here is an example:
ListSelectionModel model = new DefaultListSelectionModel();
model.setSelectionInterval(3, 5);
model.addListSelectionListener(new ListSelectionListener()
{
public void valueChanged(ListSelectionEvent e)
{
System.out.println("Changed range reported by event: " +
e.getFirstIndex() + "-" + e.getLastIndex());
}
});
System.out.print("Selected indices before insert: ");
for (int i = model.getMinSelectionIndex(); i <= model.getMaxSelectionIndex(); i++)
if (model.isSelectedIndex(i)) System.out.print(i + " ");
System.out.println();
model.insertIndexInterval(3, 3, true);
System.out.print("Selected indices after insert: ");
for (int i = model.getMinSelectionIndex(); i <= model.getMaxSelectionIndex(); i++)
if (model.isSelectedIndex(i)) System.out.print(i + " ");
System.out.println();
When you run this code you will get this output:
Selected indices before insert: 3 4 5
Changed range reported by event: 3-8
Selected indices after insert: 3 4 5 6 7 8
So the initial selection was 3-5 and it was expanded to 3-8 when inserting new indices. But 3-5 were already selected so the real change is only 6-8 so why is the event telling me that the range 3-8 has been changed? It is even more confusing when you change the insertIndexInterval call to this:
model.insertIndexInterval(3, 3, false);
Now the output is this:
Selected indices before insert: 3 4 5
Changed range reported by event: 5-8
Selected indices after insert: 3 4 5 6 7 8
I have no idea why the reported change is 5-8.
The API documentation of this method is much too short to understand what is going on there. Especially this before parameter is a mystery to me because it never has any effect on the selection but it seems to have some effect on the event and on the lead and anchor indexes.
I can't even write a unit test for my implementation because I simply don't know the expected results.
So can someone explain in detail what this method (And especially the beforeflag) is doing and what side effects it has on the selection model and the ListSelectionEvent?
It is used when new data is added to the data model (so the current selection indexes should be moved). If 2 means 'newly added and selected' than your output would be:
[0,0,0,1,1,1,0,0,0,0] == [3A,4,5L]
-> add after [0,0,0,1,2,2,2,1,1,0] == [3A,4,5,6,7,8L]
-> add before [0,0,0,2,2,2,1,1,1,0] == [3,4,5,6A,7,8L]
What you see here is a feature* of DefaultListSelectionModel - adding indices inside a current selection, automatically expands the selection.
Start with for example with index 1 selected, then insert three rows at index three:
[1,0,0,0,0,0,0,0,0,0]
-> add after [1,0,0,0,0,0,0,0,0,0]
Note that your selection representation is misleading, the zeros aren't really there. A better way to print the selection state would be:
private static void printSelection(ListSelectionModel model) {
System.out.print("[");
for (int i = model.getMinSelectionIndex(); i <= model.getMaxSelectionIndex(); i++) {
if(i > model.getMinSelectionIndex()) {
System.out.print(",");
}
if(model.isSelectedIndex(i)) {
System.out.print(i);
if(i == model.getAnchorSelectionIndex()) {
System.out.print("A");
}
if(i == model.getLeadSelectionIndex()) {
System.out.print("L");
}
}
}
System.out.println("]");
}
*) The documentation of DefaultListSelectionModel#insertIndexInterval differs from the interface, see also https://bugs.java.com/bugdatabase/view_bug?bug_id=4700643 and http://java.net/jira/browse/SWINGX-272
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With