Check if the component already has a parent. And remove it from the parent first and then add it to yourself.
I don't think we can check if the component already has a parent and then just throw an exception.. Because it could be reusing components/panels and he wants to move if from X to Y
something like this in MarkupContainer:
private final void addedComponent(final Component component)
{
// Check for degenerate case
if (component == this)
{
throw new IllegalArgumentException("Component can't be added to itself");
}
<NEW>
// remove if from another parent.
MarkupContainer mc = component.getParent();
if(mc != null)
{
mc.remove(component);
}
else if(mc == this)
{
// component already added don't do anything
return;
}
</NEW>
// Set child's parent
component.setParent(this);
// Tell the page a component was added
final Page page = findPage();
if (page != null)
{
page.componentAdded(component);
}
}
johan
On 12/22/05, Igor Vaynberg <[EMAIL PROTECTED]> wrote:
what do you think about performance? seems kind of overkill to traverse the entire tree every time you add a component, especially for listview/dataview type stuff. i know it wont be a problem in prod because component-use-check should be turned off, but still in development it might be a pain.
i think maybe this one we call user error and leave it at that?
-IgorOn 12/21/05, Juergen Donnerstag < [EMAIL PROTECTED]> wrote:We do not check that a component (same object instance) is added twice
at different levels in the hierarchy.
This test could be added to MarkupContainer.add() though. Something like
// Check that the same component instance is not
// added more than once.
if (getApplicationSettings().getComponentUseCheck())
{
Page page = findPage();
if (page != null)
{
page.visitChildren(new IVisitor()
{
public Object component(Component component)
{
if (component.equals(child))
{
throw new WicketRuntimeException(
"You can not add the same Component twice: "
+ child.toString());
}
return IVisitor.CONTINUE_TRAVERSAL;
}
});
}
}
It seems to work. Any objections?
Juergen
On 12/21/05, Juergen Donnerstag < [EMAIL PROTECTED]> wrote:
> On 12/21/05, Igor Vaynberg <[EMAIL PROTECTED] > wrote:
> > hi Juergen,
> > could you please take a look at
> >
> > Bugs item #1375584, was opened at 2005-12-07 18:44
> > Message generated for change (Comment added) made by ivaynberg
> > You can respond by visiting:
> > https://sourceforge.net/tracker/?func=detail&atid=684975&aid=1375584&group_id=119783
> >
> > i think this is more your area then mine. this guy added the same component
> > to the page twice. once to a form and once to a feedbackborder which was
> > also added to the form. wicket didnt catch this.
> >
>
> I'll check it.
>
> Juergen
>
-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems? Stop! Download the new AJAX search engine that makes
searching your log files as easy as surfing the web. DOWNLOAD SPLUNK!
http://ads.osdn.com/?ad_idv37&alloc_id865&opclick
_______________________________________________
Wicket-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/wicket-user
