PDA

View Full Version : BaseTreeModel - setParent - suggest



fother
25 Aug 2009, 4:14 AM
actually in gxt if you set parent dont added the son at the parent..

code example..


final BaseTreeModel father = new BaseTreeModel();
final BaseTreeModel son = new BaseTreeModel();
son.setParent(father);

System.out.println(father.getChildren());

always will return a empty list...

my suggest is that if the model have the parent automatic should be have the son..
code example


final BaseTreeModel father = new BaseTreeModel();
final BaseTreeModel son = new BaseTreeModel() {
@Override
public void setParent(final TreeModel parent) {

super.setParent(parent);

parent.getChildren().add(this);
}
};
son.setParent(father);

System.out.println(father.getChildren());

will return the childrens...

Arno.Nyhm
25 Aug 2009, 8:09 AM
but there are other problem if you set the parent:

1) moving item

what is if this child is already attached to an other parent?
then is should be removing this child from the old one.
maybe a adopt before the children.add is better?

2)
and what if the items are connected to a tree? then the tee should informed via a change event

3) setParent is also used internaly by BaseTreeModel. so if you overwrite it, then can be more problems with your solution for example adding it twice if you insert a children via BaseTreeModel#add(ModelData child)