*/
public void setParent(Component parent) {
- // If the parent is not changed, dont do nothing
+ // If the parent is not changed, don't do anything
if (parent == this.parent) {
return;
}
+ if (parent != null && this.parent != null) {
+ throw new IllegalStateException("Component already has a parent.");
+ }
+
// Send detach event if the component have been connected to a window
if (getApplication() != null) {
detach();
- this.parent = null;
}
// Connect to new parent
* @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);
}