diff options
author | Marc Englund <marc.englund@itmill.com> | 2008-02-01 11:34:50 +0000 |
---|---|---|
committer | Marc Englund <marc.englund@itmill.com> | 2008-02-01 11:34:50 +0000 |
commit | 8a30d9ec793b343e48ff4ec05391be0fca1cd9d0 (patch) | |
tree | 6a5369d2ff95d623b78cdb7ed561a37b32fd189a /src/com/itmill/toolkit/ui/AbstractComponentContainer.java | |
parent | 4585aecfabe3ba603c8354611ebd84d90d695b53 (diff) | |
download | vaadin-framework-8a30d9ec793b343e48ff4ec05391be0fca1cd9d0.tar.gz vaadin-framework-8a30d9ec793b343e48ff4ec05391be0fca1cd9d0.zip |
AbstractComponent.setParent(parent) cannot be called if component already has parent, unless the new parent is null (unsetting parent).
AbstractComponentContainer.addComponent(component) removes the component from it's previous ComponentContainer if needed (moves component), or throws if the component can't be removed.
Fixes #1137
All components that contain other components should implement ComponentContainer for this to be perfect (e.g CustomComponent, Table)
svn changeset:3698/svn branch:trunk
Diffstat (limited to 'src/com/itmill/toolkit/ui/AbstractComponentContainer.java')
-rw-r--r-- | src/com/itmill/toolkit/ui/AbstractComponentContainer.java | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/src/com/itmill/toolkit/ui/AbstractComponentContainer.java b/src/com/itmill/toolkit/ui/AbstractComponentContainer.java index 5251e99438..d0169535b7 100644 --- a/src/com/itmill/toolkit/ui/AbstractComponentContainer.java +++ b/src/com/itmill/toolkit/ui/AbstractComponentContainer.java @@ -169,6 +169,30 @@ public abstract class AbstractComponentContainer extends AbstractComponent * @see com.itmill.toolkit.ui.ComponentContainer#addComponent(Component) */ public void addComponent(Component c) { + if (c instanceof ComponentContainer) { + // Make sure we're not adding the component inside it's own content + for (Component parent = this; parent != null; parent = parent + .getParent()) { + if (parent == c) { + throw new IllegalArgumentException( + "Component cannot be added inside it's own content"); + } + } + } + + if (c.getParent() != null) { + // If the component already has a parent, try to remove it + try { + ComponentContainer oldParent = (ComponentContainer) c + .getParent(); + oldParent.removeComponent(c); + } catch (Exception e) { + throw new IllegalStateException( + "Component could be removed from it's old parent."); + } + + } + c.setParent(this); fireComponentAttachEvent(c); } |