The attached patch fixes an issue with the JLayeredPane.setPosition().
This method was not working correctly and in some cases threw exceptions
when it should not. I rewrote this method and now it should work
correctly.

I also removed a method in Container that was not specified and only used
from the JLayeredPane.setPosition() method. I reimplemented this method
as a helper method in JLayeredPane.

2006-01-27  Roman Kennke  <[EMAIL PROTECTED]>

        * java/awt/Container.java
        (swapComponents): Removed unspecified method.
        * javax/swing/JLayeredPane.java
        (setPosition): Reimplemented correctly.
        (swapComponents): New helper method.

/Roman
Index: javax/swing/JLayeredPane.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/JLayeredPane.java,v
retrieving revision 1.36
diff -u -r1.36 JLayeredPane.java
--- javax/swing/JLayeredPane.java	27 Jan 2006 12:37:59 -0000	1.36
+++ javax/swing/JLayeredPane.java	27 Jan 2006 15:46:58 -0000
@@ -411,39 +411,16 @@
    */
   public void setPosition(Component c, int position)
   {
-    int layer = getLayer (c);
-    int[] range = layerToRange(new Integer(layer));
-    if (range[0] == range[1])
-      throw new IllegalArgumentException ();
-
-    int top = range[0];
-    int bot = range[1];
-    if (position == -1)
-      position = (bot - top) - 1;
-    int targ = Math.min(top + position, bot-1);
-    int curr = -1;
-
-    Component[] comps = getComponents();
-    for (int i = top; i < bot; ++i)
-      {
-        if (comps[i] == c)
-          {
-            curr = i;
-            break;
-          }
-      }
-    if (curr == -1)
-      // should have found it
-      throw new IllegalArgumentException();
+    int currentPos = getPosition(c);
+    if (currentPos == position)
+      return;
 
-    if (curr == 0)
-      super.swapComponents(curr, targ);
-    else
-      while (curr > 0)
-        super.swapComponents (curr, --curr);
-    
-    revalidate();
-    repaint();
+    int layer = getLayer(c);
+    int i1 = position;
+    int i2 = getPosition(c);
+    int incr = (i1 - i2) / Math.abs(i1 - i2);
+    for (int p = i2; p != i1; p += incr)
+      swapComponents(p, p + incr, layer);
   }
     
   /**
@@ -746,4 +723,28 @@
     }
     return result;
   }
+
+  /**
+   * Swaps the components at position i and j, in the specified layer.
+   *
+   * @param i the position of the 1st component in its layer
+   * @param j the position of the 2nd component in its layer
+   * @param layer the layer in which the components reside
+   */
+  private void swapComponents (int i, int j, int layer)
+  {
+    int p1 = Math.min(i, j);
+    int p2 = Math.max(i, j);
+    Component[] layerComps = getComponentsInLayer(layer);
+    int layerOffs = getIndexOf(layerComps[0]);
+    Component c1 = layerComps[p1];
+    Component c2 = layerComps[p2];
+    // remove() wants the real index.
+    remove(p2 + layerOffs);
+    remove(p1 + layerOffs);
+    // add() wants the position within the layer.
+    Integer l = new Integer(layer);
+    add(c2, l, p1);
+    add(c1, l, p2);
+  }
 }
Index: java/awt/Container.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/awt/Container.java,v
retrieving revision 1.76
diff -u -r1.76 Container.java
--- java/awt/Container.java	22 Jan 2006 20:23:52 -0000	1.76
+++ java/awt/Container.java	27 Jan 2006 15:46:58 -0000
@@ -188,25 +188,6 @@
   }
 
   /**
-   * Swaps the components at position i and j, in the container.
-   */
-
-  protected void swapComponents (int i, int j)
-  {   
-    synchronized (getTreeLock ())
-      {
-        if (i < 0 
-            || i >= component.length
-            || j < 0 
-            || j >= component.length)
-          throw new ArrayIndexOutOfBoundsException ();
-        Component tmp = component[i];
-        component[i] = component[j];
-        component[j] = tmp;
-      }
-  }
-
-  /**
    * Returns the insets for this container, which is the space used for
    * borders, the margin, etc.
    *

Reply via email to