PDA

View Full Version : Question about TreeStore update



cnestor
10 Dec 2009, 9:15 PM
I have the following issue. When I insert a new node in my treePanel, the tree is updated in the UI, (see code snippet below)




private TreeStore<ModelData> store = new TreeStore<ModelData>();
private TreePanel<ModelData> aoiTree;
.....
insert.addSelectionListener(new SelectionListener<MenuEvent>() {
public void componentSelected(MenuEvent ce) {

ModelData folder = aoiTree.getSelectionModel().getSelectedItem();
if (folder != null && !(folder instanceof Folder)) {
Category child = new Category("Add Child" + count++);
store.add(folder, child, false);
store.update(folder);
aoiTree.setExpanded(folder, true);
}
}
});

public class Folder extends BaseTreeModel
....

but when I retrieve all the nodes from the TreeStore, it doesn't contain the new relationship related to the insert or drag and drop action. It still refers to the 'old' store.



List<ModelData> itemlist = aoiTree.getStore().getAllItems();
for (ModelData modelData : itemlist) {
System.out.println("Modeldata: " + modelData.get("name"));
if (modelData instanceof Folder) {
Folder folder = (Folder) modelData;
System.out.println("ModeldataParent: " + folder.getParent().get("name"));
}
}


In the API doc for TreeStore, I can see:
The parent child relationships are handled internally by the store. It is important to note that the store does not use the the parent and children of any TreeModel instances added to the store.

So my question is: How do I update the treestore or how do I retrieve the relationship (parent/child) for the new node? I need this information to update the store on the server side.

Thanks a lot.
C

HerrB
11 Dec 2009, 5:10 AM
Err, just a question: Are you sure, that it should be !(folder instanceof Folder) in:

if (folder != null && !(folder instanceof Folder)) {
Category child = new Category("Add Child" + count++);
store.add(folder, child, false);
store.update(folder);
aoiTree.setExpanded(folder, true);
}

So, you are adding a child to a child - not a folder. If this should make the parent child to become a folder, then the object has to be changed, I think.

But maybe I'm completely wrong on what you are trying to do.

Regards,

HerrB

cnestor
11 Dec 2009, 6:03 AM
Thanks HerrB.
Poor naming convention on my part, the folder is really an item, therefore I am checking if it's a child or a parent. In my tree, Folder.class is a child, and Category is a parent. I can only create a new category under an existing category.


||Category
||_ Folder
||_ Folder



aoiTree = new TreePanel<ModelData>(store){
@Override
public boolean hasChildren(ModelData parent) {
if(parent instanceof Category){
return true;
}
return super.hasChildren(parent);
}
};


Apparently when I insert a new category, the wrapper around the treestore is updated since I can see the new node on the screen, but the real store is not. How do I tie these two together?

HerrB
11 Dec 2009, 7:10 AM
Ok, thanks for the clarification. I think you can ask the store:


List<ModelData> itemlist = aoiTree.getStore().getAllItems();
for (ModelData modelData : itemlist) {
System.out.println("Modeldata: " + modelData.get("name"));
if (modelData instanceof Folder) {
Folder folder = (Folder) modelData;
System.out.println("ModeldataParent: " + aoiTree.getStore().getParent(folder).get("name"));
}
}

Regards,

HerrB

cnestor
11 Dec 2009, 7:40 AM
Thanks HerrB, it worked!

But I am definitely missing something in term of basic understanding.
I don't understand why when I use:
aoiTree.getStore().getParent(folder).get("name"));
it returns the correct parent (the target directory where the item has been inserted or dragged and dropped).

but when I use the following snippet:


List<ModelData> itemlist = aoiTree.getStore().getAllItems();
for (ModelData modelData : itemlist) {
System.out.println("Modeldata: " + modelData.get("name"));
if (modelData instanceof Folder) {
Folder folder = (Folder) modelData;
System.out.println("ModeldataParent: " + folder.getParent().get("name"));

The same item will return the parent before the drag & drop.
Ex:
ParentA - FileA
ParentB
action: Move FileA to ParentB
ParentA
ParentB - FileA

aoiTree.getStore().getParent(folder).get("name"));=> ParentB // correct.
List<ModelData> itemlist = aoiTree.getStore().getAllItems()... => ParentA for fileA //Incorrect.
Why is that?

Thanks a lot
C

HerrB
24 Dec 2009, 5:26 PM
Actually, I think, you said it by yourself:

The parent child relationships are handled internally by the store. It is important to note that the store does not use the the parent and children of any TreeModel instances added to the store.

aoiTree.getStore().getAllItems(); gets you all objects. They do have a getParent method (as they are TreeModel objects), but the information is just "nice" and is ignored by the store. Say, you generate an object Z with Z.setParent(A). You can add this object to every parent - Z.getParent gets you always A, asking the Store with store.getParent(Z) will get you the real parent.

Maybe this will be changed in the future... ;)

Regards,

HerrB