diff options
Diffstat (limited to 'javax/swing/DefaultListSelectionModel.java')
-rw-r--r-- | javax/swing/DefaultListSelectionModel.java | 117 |
1 files changed, 93 insertions, 24 deletions
diff --git a/javax/swing/DefaultListSelectionModel.java b/javax/swing/DefaultListSelectionModel.java index 998aee452..482ce2cc2 100644 --- a/javax/swing/DefaultListSelectionModel.java +++ b/javax/swing/DefaultListSelectionModel.java @@ -150,9 +150,14 @@ public class DefaultListSelectionModel implements Cloneable, boolean setLeadCalledFromAdd = false; /** - * Gets the value of the {@link #selectionMode} property. - * - * @return The current value of the property + * Returns the selection mode, which is one of {@link #SINGLE_SELECTION}, + * {@link #SINGLE_INTERVAL_SELECTION} and + * {@link #MULTIPLE_INTERVAL_SELECTION}. The default value is + * {@link #MULTIPLE_INTERVAL_SELECTION}. + * + * @return The selection mode. + * + * @see #setSelectionMode(int) */ public int getSelectionMode() { @@ -187,13 +192,19 @@ public class DefaultListSelectionModel implements Cloneable, /** * Sets the value of the {@link #anchorSelectionIndex} property. * - * @param anchorIndex The new property value + * @param index The new property value * * @see #getAnchorSelectionIndex */ - public void setAnchorSelectionIndex(int anchorIndex) + public void setAnchorSelectionIndex(int index) { - anchorSelectionIndex = anchorIndex; + if (anchorSelectionIndex != index) + { + int old = anchorSelectionIndex; + anchorSelectionIndex = index; + if (leadAnchorNotificationEnabled) + fireValueChanged(index, old); + } } /** @@ -459,12 +470,14 @@ public class DefaultListSelectionModel implements Cloneable, if (index0 == -1 || index1 == -1) return; + if (selectionMode == SINGLE_SELECTION) + setSelectionInterval(index0, index1); + else + { int lo = Math.min(index0, index1); int hi = Math.max(index0, index1); oldSel = sel.clone(); - if (selectionMode == SINGLE_SELECTION) - setSelectionInterval(index0, index1); // COMPAT: Like Sun (but not like IBM), we allow calls to // addSelectionInterval when selectionMode is @@ -503,6 +516,7 @@ public class DefaultListSelectionModel implements Cloneable, sel.set(lo, hi+1); fireDifference(sel, (BitSet) oldSel); } + } } @@ -588,27 +602,82 @@ public class DefaultListSelectionModel implements Cloneable, * the current selection mode is <code>SINGLE_SELECTION</code> only the * index <code>index2</code> is selected. * - * @param index0 The low end of the new selection - * @param index1 The high end of the new selection + * @param anchor the anchor selection index. + * @param lead the lead selection index. */ - public void setSelectionInterval(int index0, int index1) + public void setSelectionInterval(int anchor, int lead) { - if (index0 == -1 || index1 == -1) + if (anchor == -1 || lead == -1) return; - - BitSet oldSel = (BitSet) sel.clone(); - sel.clear(); if (selectionMode == SINGLE_SELECTION) - index0 = index1; - - int lo = Math.min(index0, index1); - int hi = Math.max(index0, index1); - sel.set(lo, hi+1); - // update the anchorSelectionIndex and leadSelectionIndex variables - setAnchorSelectionIndex(index0); - leadSelectionIndex=index1; + { + int lo = lead; + int hi = lead; + int selected = sel.nextSetBit(0); + if (selected == lead) + return; // the selection is not changing + if (selected >= 0) + { + lo = Math.min(lo, selected); + hi = Math.max(hi, selected); + } + if (anchorSelectionIndex >= 0) + { + lo = Math.min(lo, anchorSelectionIndex); + hi = Math.max(hi, anchorSelectionIndex); + } + sel.clear(); + sel.set(lead); + leadSelectionIndex = lead; + anchorSelectionIndex = lead; + fireValueChanged(lo, hi); + } + else if (selectionMode == SINGLE_INTERVAL_SELECTION) + { + // determine the current interval + int first = sel.nextSetBit(0); + int last = first; + if (first >= 0) + last += (sel.cardinality() - 1); + + // update the selection + int lo = Math.min(anchor, lead); + int hi = Math.max(anchor, lead); + if (lo == first && hi == last) + return; // selected interval is not being changed + sel.clear(); + sel.set(lo, hi + 1); + + // include the old selection in the event range + if (first >= 0) + lo = Math.min(lo, first); + if (last >= 0) + hi = Math.max(hi, last); + if (anchorSelectionIndex >= 0) + { + lo = Math.min(lo, anchorSelectionIndex); + hi = Math.max(hi, anchorSelectionIndex); + } + anchorSelectionIndex = anchor; + leadSelectionIndex = lead; + fireValueChanged(lo, hi); + } + else + { + BitSet oldSel = (BitSet) sel.clone(); + sel.clear(); + if (selectionMode == SINGLE_SELECTION) + anchor = lead; + + int lo = Math.min(anchor, lead); + int hi = Math.max(anchor, lead); + sel.set(lo, hi+1); + // update the anchorSelectionIndex and leadSelectionIndex variables + setAnchorSelectionIndex(anchor); + leadSelectionIndex = lead; - fireDifference(sel, oldSel); + fireDifference(sel, oldSel); + } } /** |