This patch (committed) fixes some failing Mauve tests in the DefaultListSelectionModel class - there's still more to do:

2006-06-21  David Gilbert  <[EMAIL PROTECTED]>

        * javax/swing/DefaultListSelectionModel.java
        (getSelectionMode): Updated API docs,
        (setAnchorSelectionIndex): Added ListSelectionEvent generation,
        (addSelectionInterval): If mode is SINGLE_SELECTION, just call
        setSelectionInterval(),
        (setSelectionInterval): Reimplemented SINGLE_SELECTION and
        SINGLE_INTERVAL_SELECTION cases.

Regards,

Dave
Index: javax/swing/DefaultListSelectionModel.java
===================================================================
RCS file: 
/sources/classpath/classpath/javax/swing/DefaultListSelectionModel.java,v
retrieving revision 1.31
diff -u -r1.31 DefaultListSelectionModel.java
--- javax/swing/DefaultListSelectionModel.java  26 Apr 2006 15:29:34 -0000      
1.31
+++ javax/swing/DefaultListSelectionModel.java  21 Jun 2006 20:47:14 -0000
@@ -150,9 +150,14 @@
   boolean setLeadCalledFromAdd = false;
 
   /**
-   * Gets the value of the [EMAIL PROTECTED] #selectionMode} property.
-   *
-   * @return The current value of the property
+   * Returns the selection mode, which is one of [EMAIL PROTECTED] 
#SINGLE_SELECTION}, 
+   * [EMAIL PROTECTED] #SINGLE_INTERVAL_SELECTION} and 
+   * [EMAIL PROTECTED] #MULTIPLE_INTERVAL_SELECTION}.  The default value is
+   * [EMAIL PROTECTED] #MULTIPLE_INTERVAL_SELECTION}.
+   * 
+   * @return The selection mode.
+   * 
+   * @see #setSelectionMode(int)
    */
   public int getSelectionMode()
   {
@@ -187,13 +192,19 @@
   /**
    * Sets the value of the [EMAIL PROTECTED] #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 @@
     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 @@
         sel.set(lo, hi+1);
         fireDifference(sel, (BitSet) oldSel);
       }
+    }
   }
 
 
@@ -588,27 +602,82 @@
    * 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);
+    }
   }
 
   /**

Reply via email to