This is the mail archive of the java-patches@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[patch] fix to JLayeredPane


hi,

this patch adapts JLayeredPane to the changes djee@redhat.com recently made to Container; notably he discovered that the drawing order in container does not match sun's, so reversed it. re-reading the javadocs, I see that I did not implement JLayeredPane correctly, and that its documentation makes more sense considering this "correct" order he discovered. so I've rewritten it to work in that order.

ok to apply?

(and before you ask, yes, the mauve tests are confirmed to work both ways -- hooray for the "layer" abstraction -- but I'll be extending them to check the underlying mapping too).

-graydon

2004-01-08 Graydon Hoare <graydon@redhat.com>

	* javax/swing/JLayeredPane.java: Rewrite to accomodate
	djee@redhat.com's recent inverse ordering of Container elements.

--- javax/swing/JLayeredPane.java	6 Jan 2004 18:02:23 -0000	1.4
+++ javax/swing/JLayeredPane.java	9 Jan 2004 02:59:28 -0000
@@ -69,16 +69,10 @@
  * this class:</p>
  * 
  * <dl>
- * <dt>Internal Component Index:</dt> 
+ * <dt>Component Index:</dt> 
  * <dd>An offset into the <code>component</code> array held in our ancestor,
  * {@link java.awt.Container}, from <code>[0 .. component.length)</code>. The drawing
- * rule with internal indices is that 0 is drawn first.</dd>
- *
- * <dt>External Component Index:</dt>
- * <dd>An offset into the "logical drawing order" of this container. If <code>I</code>
- * is the internal index of a component, the external index <tt>E =
- * component.length - I</tt>. The rule with external indices is that 0 is
- * drawn last.</dd>
+ * rule with indices is that 0 is drawn last.</dd>
  *
  * <dt>Layer Number:</dt>
  * <dd>A general <code>int</code> specifying a layer within this component.  Negative
@@ -90,6 +84,9 @@
  * is drawn last. Layer position -1 is a synonym for the first layer
  * position (the logical "bottom").</dd>
  *
+ * <p><b>Note:</b> the layer numbering order is the <em>reverse</em> of the
+ * component indexing and position order</p>
+ *
  * @author Graydon Hoare <graydon@redhat.com>
  */
 
@@ -117,7 +114,7 @@
 
 
   /** 
-   * Looks up the layer a child component is currently assigned to.
+   * <p>Looks up the layer a child component is currently assigned to.</p>
    *
    * @param c the component to look up.
    * @return the layer the component is currently assigned to, in this container.
@@ -132,16 +129,16 @@
   }
 
   /**
-   * Returns a pair of ints representing a half-open interval
-   * <code>[bottom, top)</code>, which is the range of internal component
-   * indices the provided layer number corresponds to.
+   * <p>Returns a pair of ints representing a half-open interval 
+   * <code>[top, bottom)</code>, which is the range of component indices 
+   * the provided layer number corresponds to.</p>
    *
-   * Note that "top" is <em>not</em> included in the interval of 
+   * <p>Note that "bottom" is <em>not</em> included in the interval of
    * component indices in this layer: a layer with 0 elements in it has 
-   * <code>ret[0] == ret[1]</code>.
+   * <code>ret[0] == ret[1]</code>.</p>
    *
    * @param layer the layer to look up.
-   * @return the half-open range of internal indices this layer spans.
+   * @return the half-open range of indices this layer spans.
    * @throws IllegalArgumentException if layer does not refer to an active layer
    * in this container.
    */
@@ -149,6 +146,7 @@
   protected int[] layerToRange (Integer layer)
   {
     int[] ret = new int[2];	
+    ret[1] = getComponents ().length;
     Iterator i = layers.entrySet ().iterator ();
     while (i.hasNext())
 	    {
@@ -157,12 +155,12 @@
         Integer layerSz = (Integer) pair.getValue ();
         if (layerNum == layer)
           {
-            ret[1] = ret[0] + layerSz.intValue ();
+            ret[0] = ret[1] - layerSz.intValue ();
             return ret;
           }
         else
           {
-            ret[0] += layerSz.intValue ();
+            ret[1] -= layerSz.intValue ();
           }
 	    }
     // should have found the layer during iteration
@@ -170,7 +168,7 @@
   }
 
   /**
-   * Increments the recorded size of a given layer.
+   * <p>Increments the recorded size of a given layer.</p>
    *
    * @param layer the layer number to increment.
    * @see #incrLayer()
@@ -185,7 +183,7 @@
   }
 
   /**
-   * Decrements the recorded size of a given layer.
+   * <p>Decrements the recorded size of a given layer.</p>
    *
    * @param layer the layer number to decrement.
    * @see #decrLayer()
@@ -200,8 +198,8 @@
   }
 
   /**
-   * Return the greatest layer number currently in use, in this container.
-   * This number may legally be positive <em>or</em> negative.
+   * <p>Return the greatest layer number currently in use, in this container.
+   * This number may legally be positive <em>or</em> negative.</p>
    *
    * @return the least layer number.
    * @see #lowestLayer()
@@ -215,8 +213,8 @@
   }
 
   /**
-   * Return the least layer number currently in use, in this container.
-   * This number may legally be positive <em>or</em> negative.
+   * <p>Return the least layer number currently in use, in this container.
+   * This number may legally be positive <em>or</em> negative.</p>
    *
    * @return the least layer number.
    * @see #highestLayer()
@@ -230,10 +228,10 @@
   }
 
   /**
-   * Moves a component to the "front" of its layer. The "front" is a
+   * <p>Moves a component to the "front" of its layer. The "front" is a
    * synonym for position 0, which is also the last position drawn in each
    * layer, so is usually the component which occludes the most other
-   * components in its layer.
+   * components in its layer.</p>
    *
    * @param c the component to move to the front of its layer.
    * @throws IllegalArgumentException if the component is not a child of
@@ -267,9 +265,9 @@
   }
 
   /**
-   * Return the position of a component within its layer. Positions are assigned
+   * <p>Return the position of a component within its layer. Positions are assigned
    * from the "front" (position 0) to the "back" (position N-1), and drawn from 
-   * the back towards the front.
+   * the back towards the front.</p>
    *
    * @param c the component to get the position of.
    * @throws IllegalArgumentException if the component is not a child of
@@ -281,21 +279,22 @@
   {
     Integer layer = getLayer (c);
     int[] range = layerToRange (layer);
-    int top = (range[1] - 1);
+    int top = range[0];
+    int bot = range[1];
     Component[] comps = getComponents ();
-    for (int i = range[0]; i < range[1]; ++i)
+    for (int i = top; i < bot; ++i)
 	    {
         if (comps[i] == c)
-          return top - i;
+          return i - top;
 	    }
     // should have found it
     throw new IllegalArgumentException ();
   }
 
   /**
-   * Change the position of a component within its layer. Positions are assigned
+   * <p>Change the position of a component within its layer. Positions are assigned
    * from the "front" (position 0) to the "back" (position N-1), and drawn from 
-   * the back towards the front.
+   * the back towards the front.</p>
    *
    * @param c the component to change the position of.
    * @param position the position to assign the component to.
@@ -311,14 +310,15 @@
     if (range[0] == range[1])
 	    throw new IllegalArgumentException ();
 
-    int top = (range[1] - 1);
+    int top = range[0];
+    int bot = range[1];
     if (position == -1)
-	    position = top - range[0];
-    int targ = top - position;
+	    position = (bot - top) - 1;
+    int targ = top + position;
     int curr = -1;
 
     Component[] comps = getComponents();
-    for (int i = range[0]; i < range[1]; ++i)
+    for (int i = top; i < bot; ++i)
 	    {
         if (comps[i] == c)
           {
@@ -336,9 +336,9 @@
   }
     
   /**
-   * Return an array of all components within a layer of this
-   * container. Components are ordered back-to-front, with the "back"
-   * element (which draws first) at position 0 of the returned array.
+   * <p>Return an array of all components within a layer of this
+   * container. Components are ordered front-to-back, with the "front"
+   * element (which draws last) at position 0 of the returned array.</p>
    *
    * @param layer the layer to return components from.
    * @return the components in the layer.
@@ -352,7 +352,7 @@
     else
 	    {
         Component[] comps = getComponents ();
-        int sz = (range[1] - 1) - range[0];
+        int sz = range[1] - range[0];
         Component[] nc = new Component[sz];
         for (int i = 0; i < sz; ++i)
           nc[i] = comps[range[0] + i];
@@ -361,8 +361,8 @@
   }
 
   /**
-   * Return the number of components within a layer of this
-   * container. 
+   * <p>Return the number of components within a layer of this
+   * container.</p>
    *
    * @param layer the layer count components in.
    * @return the number of components in the layer.
@@ -378,8 +378,8 @@
   }
 
   /**
-   * Return a hashtable mapping child components of this container to
-   * Integer objects representing the component's layer assignments.
+   * <p>Return a hashtable mapping child components of this container to
+   * Integer objects representing the component's layer assignments.</p>
    */
 
   protected Hashtable getComponentToLayer()
@@ -387,20 +387,19 @@
     return componentToLayer;
   }
 
-
   /**
-   * Return the index of a component within the underlying (contiguous)
+   * <p>Return the index of a component within the underlying (contiguous)
    * array of children. This is a "raw" number which does not represent the
-   * child's position in a layer, but rather its position in the
-   * concatenation of <em>all</em> layers within the container.
+   * child's position in a layer, but rather its position in the logical
+   * drawing order of all children of the container.</p>
    *
    * @param c the component to look up.
-   * @return the internal index of the component.
+   * @return the external index of the component.
    * @throws IllegalArgumentException if the component is not a child of
    * this container.
    */
 
-  protected int getInternalIndexOf(Component c) 
+  public int getIndexOf(Component c) 
   {
     Integer layer = getLayer (c);
     int[] range = layerToRange (layer);
@@ -414,30 +413,10 @@
     throw new IllegalArgumentException ();
   }
 
-
   /**
-   * Return the external index of a component within the underlying
-   * (contiguous) array of children. This is a "raw" number which does not
-   * represent the child's position in a layer, but rather its position in
-   * the logical drawing order of all children of the container.
-   *
-   * @param c the component to look up.
-   * @return the external index of the component.
-   * @throws IllegalArgumentException if the component is not a child of
-   * this container.
-   */
-
-  public int getIndexOf(Component c) 
-  {
-    // returns the *external* index of the component.
-    int top = getComponentCount() - 1;
-    return top - getIndexOf (c);
-  }    
-
-  /**
-   * Return an Integer object which holds the same int value as the
+   * <p>Return an Integer object which holds the same int value as the
    * parameter. This is strictly an optimization to minimize the number of
-   * identical Integer objects which we allocate.
+   * identical Integer objects which we allocate.</p>
    *
    * @param layer the layer number as an int.
    * @return the layer number as an Integer, possibly shared.
@@ -473,13 +452,13 @@
   }
 
   /**
-   * Computes an internal index at which to request the superclass {@link
+   * <p>Computes an index at which to request the superclass {@link
    * java.awt.Container} inserts a component, given an abstract layer and
-   * position number.
+   * position number.</p>
    *
    * @param layer the layer in which to insert a component.
    * @param position the position in the layer at which to insert a component.
-   * @return the internal index at which to insert the component.
+   * @return the index at which to insert the component.
    */
     
   protected int insertIndexForLayer(int layer, int position)
@@ -492,20 +471,20 @@
     if (range[0] == range[1])
 	    return range[0];
 	
-    int bottom = range[0];
-    int top = range[1] - 1;
+    int top = range[0];
+    int bot = range[1];
 	
-    if (position == -1 || position > (top - bottom))
-	    return bottom;
+    if (position == -1 || position > (bot - top))
+        return bot;
     else
-	    return top - position;
+        return top + position;
   }
 
   /**
-   * Removes a child from this container. The child is specified by
-   * internal index. After removal, the child no longer occupies a layer.
+   * <p>Removes a child from this container. The child is specified by
+   * index. After removal, the child no longer occupies a layer.</p>
    *
-   * @param index the internal index of the child component to remove.
+   * @param index the index of the child component to remove.
    */
     
   public void remove (int index)
@@ -518,15 +497,15 @@
   }
 
   /**
-   * Removes a child from this container. The child is specified directly.
-   * After removal, the child no longer occupies a layer.
+   * <p>Removes a child from this container. The child is specified directly.
+   * After removal, the child no longer occupies a layer.</p>
    *
    * @param comp the child to remove.
    */
 	
   public void remove (Component comp)
   {
-    remove (getInternalIndexOf (comp));
+    remove (getIndexOf (comp));
   }
 
   /**
@@ -547,7 +526,7 @@
   }
 
   /**
-   * Set the layer and position of a component, within this container. 
+   * <p>Set the layer and position of a component, within this container.</p>
    *
    * @param c the child component to set the layer property for.
    * @param layer the layer number to assign to the component.
@@ -565,10 +544,10 @@
   }
 
   /**
-   * Overrides the default implementation from {@link java.awt.Container}
+   * <p>Overrides the default implementation from {@link java.awt.Container}
    * such that <code>layerConstraint</code> is interpreted as an {@link
    * Integer}, specifying the layer to which the component will be added
-   * (at the bottom position).
+   * (at the bottom position).</p>
    *
    * @param comp the component to add.
    * @param layerConstraint an integer specifying the layer to add the component to.
@@ -586,6 +565,7 @@
 	    layer = DEFAULT_LAYER;
 
     int newIdx = insertIndexForLayer(layer.intValue (), -1);
+
     componentToLayer.put (comp, layer);
     incrLayer (layer);
 	

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]