From ade40dca806566c73986c907fb00d5d09330fe73 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Johannes=20Dahlstr=C3=B6m?= Date: Thu, 24 Nov 2011 13:48:31 +0000 Subject: [PATCH] Fixed batch for #7668 - addComponent methods *must* add the component to the components list before calling super.addComponent() because AttachListeners expect it to be there svn changeset:22122/svn branch:6.7 --- src/com/vaadin/ui/AbstractOrderedLayout.java | 48 +++++++++++++++----- src/com/vaadin/ui/CssLayout.java | 48 +++++++++++++++----- 2 files changed, 74 insertions(+), 22 deletions(-) diff --git a/src/com/vaadin/ui/AbstractOrderedLayout.java b/src/com/vaadin/ui/AbstractOrderedLayout.java index fb232abe3c..8898dbad3f 100644 --- a/src/com/vaadin/ui/AbstractOrderedLayout.java +++ b/src/com/vaadin/ui/AbstractOrderedLayout.java @@ -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; + } } /** diff --git a/src/com/vaadin/ui/CssLayout.java b/src/com/vaadin/ui/CssLayout.java index bdc4db8080..52b0e6dc27 100644 --- a/src/com/vaadin/ui/CssLayout.java +++ b/src/com/vaadin/ui/CssLayout.java @@ -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; + } } /** -- 2.39.5