]> source.dussan.org Git - vaadin-framework.git/commitdiff
Fixed batch for #7668 - addComponent methods *must* add the component to the componen...
authorJohannes Dahlström <johannes.dahlstrom@vaadin.com>
Thu, 24 Nov 2011 13:48:31 +0000 (13:48 +0000)
committerJohannes Dahlström <johannes.dahlstrom@vaadin.com>
Thu, 24 Nov 2011 13:48:31 +0000 (13:48 +0000)
svn changeset:22122/svn branch:6.7

src/com/vaadin/ui/AbstractOrderedLayout.java
src/com/vaadin/ui/CssLayout.java

index fb232abe3cb3398eb6189ebabdbebd7fbdc4fe1d..8898dbad3f4e849eb1b769693cac7b87a546ba6d 100644 (file)
@@ -53,9 +53,16 @@ public abstract class AbstractOrderedLayout extends AbstractLayout implements
      */
     @Override
     public void addComponent(Component c) {
-        super.addComponent(c);
+        // Add to components before calling super.addComponent
+        // so that it is available to AttachListeners 
         components.add(c);
-        requestRepaint();
+        try {
+            super.addComponent(c);
+            requestRepaint();
+        } catch (IllegalArgumentException e) {
+            components.remove(c);
+            throw e;
+        }
     }
 
     /**
@@ -66,9 +73,19 @@ public abstract class AbstractOrderedLayout extends AbstractLayout implements
      *            the component to be added.
      */
     public void addComponentAsFirst(Component c) {
-        super.addComponent(c);
+        // If c is already in this, we must remove it before proceeding
+        // see ticket #7668
+        if(c.getParent() == this) {
+            removeComponent(c);
+        }
         components.addFirst(c);
-        requestRepaint();
+        try {
+            super.addComponent(c);
+            requestRepaint();
+        } catch (IllegalArgumentException e) {
+            components.remove(c);
+            throw e;
+        }
     }
 
     /**
@@ -77,18 +94,27 @@ public abstract class AbstractOrderedLayout extends AbstractLayout implements
      * @param c
      *            the component to be added.
      * @param index
-     *            the Index of the component position. The components currently
+     *            the index of the component position. The components currently
      *            in and after the position are shifted forwards.
      */
     public void addComponent(Component c, int index) {
-        // if this already contains c, super.addComponent() will remove it
-        // so everything after c's original position shifts one place down
-        if(this == c.getParent() && index > getComponentIndex(c)) {
-            index--;
+        // If c is already in this, we must remove it before proceeding
+        // see ticket #7668
+        if(c.getParent() == this) {
+            // When c is removed, all components after it are shifted down
+            if(index > getComponentIndex(c)) {
+                index--;
+            }
+            removeComponent(c);
         }
-        super.addComponent(c);
         components.add(index, c);
-        requestRepaint();
+        try {
+            super.addComponent(c);
+            requestRepaint();
+        } catch (IllegalArgumentException e) {
+            components.remove(c);
+            throw e;
+        }
     }
 
     /**
index bdc4db80800bd2753724903785d17365157b0910..52b0e6dc27a4b4edab8bf3d8428e07390dac7f54 100644 (file)
@@ -76,9 +76,16 @@ public class CssLayout extends AbstractLayout implements LayoutClickNotifier {
      */
     @Override
     public void addComponent(Component c) {
-        super.addComponent(c);
+        // Add to components before calling super.addComponent
+        // so that it is available to AttachListeners 
         components.add(c);
-        requestRepaint();
+        try {
+            super.addComponent(c);
+            requestRepaint();
+        } catch (IllegalArgumentException e) {
+            components.remove(c);
+            throw e;
+        }
     }
 
     /**
@@ -89,9 +96,19 @@ public class CssLayout extends AbstractLayout implements LayoutClickNotifier {
      *            the component to be added.
      */
     public void addComponentAsFirst(Component c) {
-        super.addComponent(c);
+        // If c is already in this, we must remove it before proceeding
+        // see ticket #7668
+        if(c.getParent() == this) {
+            removeComponent(c);
+        }
         components.addFirst(c);
-        requestRepaint();
+        try {
+            super.addComponent(c);
+            requestRepaint();
+        } catch (IllegalArgumentException e) {
+            components.remove(c);
+            throw e;
+        }
     }
 
     /**
@@ -100,18 +117,27 @@ public class CssLayout extends AbstractLayout implements LayoutClickNotifier {
      * @param c
      *            the component to be added.
      * @param index
-     *            the Index of the component position. The components currently
+     *            the index of the component position. The components currently
      *            in and after the position are shifted forwards.
      */
     public void addComponent(Component c, int index) {
-        // if this already contains c, super.addComponent() will remove it
-        // so everything after c's original position shifts one place down
-        if(this == c.getParent() && index > components.indexOf(c)) {
-            index--;
+        // If c is already in this, we must remove it before proceeding
+        // see ticket #7668
+        if(c.getParent() == this) {
+            // When c is removed, all components after it are shifted down
+            if(index > components.indexOf(c)) {
+                index--;
+            }
+            removeComponent(c);
         }
-        super.addComponent(c);
         components.add(index, c);
-        requestRepaint();
+        try {
+            super.addComponent(c);
+            requestRepaint();
+        } catch (IllegalArgumentException e) {
+            components.remove(c);
+            throw e;
+        }
     }
 
     /**