Instead of using Objects in the addItem function, I think that its a good idea to use another generic class that have a parent description, so when we add an “father” item, all the children should be added too…
I implemented a simple class like this:
import java.util.ArrayList;
public class Produt {
public String name;
public ArrayList<Produt> children= new ArrayList<Produto>();
public Produt(String name) {
this.name = name;
}
public void addChild(Produt child) {
children.add(child);
}
public boolean hasChild() {
if (children.isEmpty() == true) {
return false;
} else {
return true;
}
}
}
and overrideded addItem like this:
public Item addItem(Object itemId) {
Produt father = (Produto) itemId;
Produt child;
if (father.hasChild()) {
super.addItem(father.name);
for (Iterator it = father.children.iterator(); it.hasNext(); ) {
child = (Produto) it.next();
if (child.hasChildren()) {
addItem(child);
setParent(child.name, father.name);
} else {
super.addItem(child.name);
setParent(child.name, father.nome);
setChildrenAllowed(child.name, false);
}
}
return null;
} else {
return super.addItem(father.name);
}
}