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: java.awt.CardLayout.show fix - was sometimes showing the wrong component


I've had problems with CardLayout.show showing the wrong component.  This
patch corrects those problems. I've re-structured things a little in
CardLayout, which I think makes it clearer.

Index: libjava/ChangeLog
from  Scott Gilbertson  <scottg@mantatest.com>

 * java.awt.CardLayout.show: moved show-specific code from gotoComponent and
fixed it to
    always set the right component to visible
 * java.awt.CardLayout.first, last, next, previous, gotoComponent: removed
code which was
    specific to show

diff -r -up gcc.patched/libjava/java/awt/CardLayout.java
gcc/libjava/java/awt/CardLayout.java
--- gcc.patched/libjava/java/awt/CardLayout.java 2002-12-02
11:45:19.000000000 -0500
+++ gcc/libjava/java/awt/CardLayout.java 2002-12-02 17:34:59.000000000 -0500
@@ -108,7 +108,7 @@ public class CardLayout implements Layou
    */
   public void first (Container parent)
   {
-    gotoComponent (parent, FIRST, null);
+    gotoComponent (parent, FIRST);
   }

   /** Return this layout manager's horizontal gap.  */
@@ -152,7 +152,7 @@ public class CardLayout implements Layou
    */
   public void last (Container parent)
   {
-    gotoComponent (parent, LAST, null);
+    gotoComponent (parent, LAST);
   }

   /**
@@ -205,7 +205,7 @@ public class CardLayout implements Layou
    */
   public void next (Container parent)
   {
-    gotoComponent (parent, NEXT, null);
+    gotoComponent (parent, NEXT);
   }

   /** Get the preferred layout size of the container.
@@ -223,7 +223,7 @@ public class CardLayout implements Layou
    */
   public void previous (Container parent)
   {
-    gotoComponent (parent, PREV, null);
+    gotoComponent (parent, PREV);
   }

   /** Remove the indicated component from this layout manager.
@@ -268,7 +268,19 @@ public class CardLayout implements Layou
   {
     Object target = tab.get (name);
     if (target != null)
-      gotoComponent (parent, NONE, (Component) target);
+    {
+      int num = parent.ncomponents;
+      // This is more efficient than calling getComponents().
+      Component[] comps = parent.component;
+      // FIXME: consider tracking which component is showing, so we
+      //        can just turn that one off, without using a loop to search
+      for (int i = 0; i < num; ++i)
+      {
+        if (target != comps[i] && comps[i].isVisible ())
+          comps[i].setVisible (false);
+      }
+      ((Component)target).setVisible (true);
+    }
   }

   /**
@@ -281,63 +293,57 @@ public class CardLayout implements Layou
     return getClass ().getName () + "[" + hgap + "," + vgap + "]";
   }

-  // This implements first(), last(), next(), and previous().
-  private void gotoComponent (Container parent, int what,
-         Component target)
+  /** This implements first(), last(), next(), and previous().
+   * @param parent The parent container
+   * @param what The type of goto: FIRST, LAST, NEXT or PREV
+   */
+  private void gotoComponent (Container parent, int what)
   {
     int num = parent.ncomponents;
     // This is more efficient than calling getComponents().
     Component[] comps = parent.component;
     int choice = -1;
-
+
     if (what == FIRST)
       choice = 0;
     else if (what == LAST)
       choice = num - 1;
     else if (what >= 0)
       choice = what;
-
+
     for (int i = 0; i < num; ++i)
+    {
+      if (comps[i].isVisible ())
       {
- // If TARGET is set then we are looking for a specific
- // component.
- if (target != null)
-   {
-     if (target == comps[i])
-       choice = i;
-   }
-
- if (comps[i].isVisible ())
-   {
-     if (what == NEXT)
-       {
-  choice = i + 1;
-  if (choice == num)
-    choice = 0;
-       }
-     else if (what == PREV)
-       {
-  choice = i - 1;
-  if (choice < 0)
-    choice = num - 1;
-       }
-     else if (choice == i)
-       {
-  // Do nothing if we're already looking at the right
-  // component.
-  return;
-       }
-     comps[i].setVisible (false);
-
-     if (choice >= 0)
-       break;
-   }
+        if (what == NEXT)
+        {
+          choice = i + 1;
+          if (choice == num)
+            choice = 0;
+        }
+        else if (what == PREV)
+        {
+          choice = i - 1;
+          if (choice < 0)
+            choice = num - 1;
+        }
+        else if (choice == i)
+        {
+          // Do nothing if we're already looking at the right
+          // component.
+          return;
+        }
+        comps[i].setVisible (false);
+
+        if (choice >= 0)
+          break;
       }
-
+    }
+
     if (choice >= 0 && choice < num)
       comps[choice].setVisible (true);
   }
-
+
   // Compute the size according to WHAT.
   private Dimension getSize (Container parent, int what)
   {
@@ -392,7 +398,6 @@ public class CardLayout implements Layou
   private int LAST = 1;
   private int NEXT = 2;
   private int PREV = 3;
-  private int NONE = 4;

   // These constants are used by the private getSize method.
   private int MIN = 0;



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