-
10 Dec 2009 9:15 PM #1
Question about TreeStore update
Question about TreeStore update
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)
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.Code: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 ....
In the API doc for TreeStore, I can see:Code: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 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
-
11 Dec 2009 5:10 AM #2
Err, just a question: Are you sure, that it should be !(folder instanceof Folder) in:
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.Code: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); }
But maybe I'm completely wrong on what you are trying to do.
Regards,
HerrB
-
11 Dec 2009 6:03 AM #3
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.
Code:||Category ||_ Folder ||_ FolderApparently 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?Code:aoiTree = new TreePanel<ModelData>(store){ @Override public boolean hasChildren(ModelData parent) { if(parent instanceof Category){ return true; } return super.hasChildren(parent); } };
-
11 Dec 2009 7:10 AM #4
Ok, thanks for the clarification. I think you can ask the store:
Regards,Code: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")); } }
HerrB
-
11 Dec 2009 7:40 AM #5
Solved
Solved
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:
The same item will return the parent before the drag & drop.Code: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"));
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
-
24 Dec 2009 5:26 PM #6
Actually, I think, you said it by yourself:
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.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.
Maybe this will be changed in the future...
Regards,
HerrB


Reply With Quote