How selection models work

Java has three components (with SwingX one more) which uses a selection model. There is the JList, JTable, JTree and J(X)TreeTable. The selection model maintains the set of selected elements and the anchor and lead selection. The anchor and lead do not need to be part of the set of selected elements. Changing the selection requires the invocation of either the setSelectionXXX, addSelectionXXX or removeSelectionXXX methods.

The user changes the selection either through the keyboard or the mouse. Based on what modifier keys he presses the selection change behaves different. If he doesn’t use any modifier or the shift key the setSelectionXXX method will be invoked for all cases. If he uses the ctrl key either add- or removeSelectionXXX will be invoked based on whether the elements where part of the selection or not.

If he doesn’t use any modifier there will only be one element which should be selected. The selection model should clear the set of selected elements and add this element. It should also set the anchor and lead selection to this element.

If he presses the shift key then the first element will be the element of the anchor selection. The selection model should clear the set of selected elements and add the elements between the given index0 and index1. The anchor selection is the element at index0 (first element in path array) and the lead selection is the one at index1 (last element in path array).

If he presses the ctrl key and the selection should be removed (invocation of removeSelectionXXX) the elements between index0 and index1 should be removed from the set of selected elements. The anchor selection is the element at index0 and the lead selection is the one at index1. In this case the elements of the anchor and lead selection are not part of the set of selected elements. If the user presses the ctrl key and the selection should be added (invocation of addSelectionXXX) the elements between index0 and index1 should be added to the set of selected elements. The anchor selection is the element at index0 and the lead selection the one at index1. The element of the anchor and lead selection are part of the set of selected elements.

What if some elements shouldn’t be part of the set of selected elements? The solution depends on how a selection change should behave. If the selection should follow the common scheme, then the anchor should be the beginning of the last contiguous interval which is part of the selection and the lead is the end of the last contiguous interval. The next selection would then start from the anchor. This also works with adding or removing elements from the selection. Removing elements is quite easy because there doesn’t need to be a verification. When setting or adding elements the set of selected/added elements has to be computed based on the anchor and lead selection. When adding elements the anchor and lead of the current selection need to be used, in the other case it is the first and last element of the selection. Caution must be used because the order is important, it defines whether the user selected up or down and also is input to the computation of the new anchor and lead selection! The anchor and lead is then the beginning and end of the last contiguous interval in the set of selected elements.

Based on this explanation I think it is really hard to understand how a selection works (should behave), especially when elements should be left out in between. Should the next selection start at the last interval or somewhere else?

Schlagwörter: ,

Schreibe einen Kommentar

Trage deine Daten unten ein oder klicke ein Icon um dich einzuloggen:

WordPress.com-Logo

Du kommentierst mit Deinem WordPress.com-Konto. Abmelden / Ändern )

Twitter-Bild

Du kommentierst mit Deinem Twitter-Konto. Abmelden / Ändern )

Facebook-Foto

Du kommentierst mit Deinem Facebook-Konto. Abmelden / Ändern )

Google+ Foto

Du kommentierst mit Deinem Google+-Konto. Abmelden / Ändern )

Verbinde mit %s

%d Bloggern gefällt das: