PDA

View Full Version : Adding a new root to a TreeStore



cyChop
17 Sep 2010, 3:06 AM
Hello there,

I'm having a hard time with a TreePanel. Somehow, the objective is alike to the one of the ContextMenuTree (http://www.sencha.com/examples/#contextmenutree): adding new elements from an automated method in a context menu.

It works. Almost...
If I try to add a new child, there is no problem at all. Except that the method adds a new root and fills it. And I don't know why the root isn't added (and therefore its children aren't added either).

I tried adding the root to a parent, and it works just fine. Why can I add an element as a node but not as a root? Missed something important? Thanks in advance!

sven
17 Sep 2010, 3:07 AM
We cannot know if you are missing something, as you have not posted any code. Can you try to post some small testcase that shows exactly what you are doing and is runnable for others?

cyChop
17 Sep 2010, 3:56 AM
I'm sorry, I'm quite under pressure today, so here's the most understandable I can manage at this time:

(panel is the TreePanel, its store is in another class, created through a RpcProxy)

Basically, we have root-level nodes for documents and 1st-child-level nodes for each page. The method should split a document, so create a new root-node, and move the concerned pages to the new document, so here's some legible code...


// split document
MenuItem splitDoc =
new MenuItem("Split Document", icons.cutSmall(),
new SelectionListener<MenuEvent>() {
private int counter = 0;

@Override
public void componentSelected(MenuEvent ce) {
ModelData slctd = panel.getSelectionModel().getSelectedItem();
if (slctd instanceof ScanDocumentModel) {
bus.displayBoxInfo(labels.info_action_invalid(), labels
.info_split_document_method(), null);
} else {
TreeStore<ModelData> store = panel.getStore();

ModelData parent = store.getParent(slctd);

ModelData newDoc =
// creating a new model with the required info

// FIXME cannot insert panel as a root node
store.add(newDoc, true);
List<ModelData> pages = store.getChildren(parent);

boolean foundIt = false;
for (ModelData page : pages) {
if (foundIt || page.equals(slctd)) {
foundIt = true;

// remove from previous parent
store.remove(parent, page);

// adding to new parent
store.add(newDoc, page, true);
}
}
}
}
});
contextMenu.add(splitDoc);

// done
panel.setContextMenu(contextMenu);

This doesn't work, but if I just change :

// FIXME cannot insert panel as a root node
store.add(newDoc, true);
to


// FIXME cannot insert panel as a root node
store.add(parent, newDoc, true);
the new node is created with the correct pages, but obviously not at root level.


Just for intel, I actually tried something more like that first:

store.insert(newDoc, store.indexOf(parent) + 1, true);

sven, you amaze by your ability to answer so fast and efficiently to most problems about GXT. Thanks!

sven
17 Sep 2010, 4:08 AM
I just did a testcase for
store.add(newDoc, true); and it works fine for me. I will need some runnable testcase so i can debug it for myself.

Which version of GXT are you using? I tested this against 2.2

sven
17 Sep 2010, 4:21 AM
I changed the ContextMenuTreePanelExample to the following:
MenuItem insert = new MenuItem();
insert.setText("Insert Item");
insert.setIcon(Resources.ICONS.add());
insert.addSelectionListener(new SelectionListener<MenuEvent>() {
public void componentSelected(MenuEvent ce) {
store.add(TestData.getTreeModel(), true);
}
});
contextMenu.add(insert);

It is adding a node called "root" (TestData.getTreeModel() returns this) with all its children as root node.

cyChop
17 Sep 2010, 4:58 AM
OK, now you're worrying me. I'd be glad to show more code, but this would imply to get more classes and make it complex with few hope of understanding. I'll go on and find what's wrong (I hope) or use a workaround that's been on my mind, though less ergonomic.


Which version of GXT are you using? I tested this against 2.2
We're using GXT 2.1.1. Began some weeks ago with the latest on Maven, and now is the rush, so we got no time to upgrade. I still hope we can go to 2.2 during the testing period, but nothing's for sure.

sven
17 Sep 2010, 5:02 AM
You should defintly think about updating to GXT 2.2 as soon as possible.