PDA

View Full Version : [FIXED] Cannot move TreeItem from one Tree to another



goodwrench
8 May 2008, 11:31 AM
I'm trying to move selected items from one tree to another. Items are disappearing from the source tree, but not getting added to the destination tree. Looks like once the tree item is rendered, it is not getting added to the new TreeItemUI element anymore, even if it was previously removed from another tree.
This is the sample code. It does not look really pretty, but illustrates the issue.


public void onModuleLoad() {
Viewport viewport = new Viewport();
viewport.setLayout(new FitLayout());
HorizontalPanel mainPanel = new HorizontalPanel();

final Tree<SingleTreeSelectionModel> source = new Tree<SingleTreeSelectionModel>();
TreeItem item = new TreeItem("Move this item");
item.setId("id0");
source.getRootItem().add(item);
source.setSize(200, 200);
mainPanel.add(source);

Button move = new Button("Move");

mainPanel.add(move,
new TableData(Style.HorizontalAlignment.CENTER, Style.VerticalAlignment.MIDDLE));

final Tree<SingleTreeSelectionModel> dest = new Tree<SingleTreeSelectionModel>();
dest.setItemIconStyle("icon-music");
dest.setSize(200, 200);
mainPanel.add(dest);

move.addListener(Events.Select, new Listener<ComponentEvent>(){

public void handleEvent(ComponentEvent be) {
TreeItem item = source.getItemById("id0");
source.remove(item);
dest.getRootItem().add(item);
}

});

viewport.add(mainPanel);
viewport.layout(true);
RootPanel.get().add(viewport);
}

kcaseye
9 May 2008, 10:05 AM
I haven't tried do anything like that myself, but you may want to try "moving" the underlying data that is displayed by the tree item by using that data to create a new (identical) tree item in the second tree.

So...
1. Move event is triggered somehow
2. Get data of item being moved
3. Remove item from first tree
4. Create new tree item with data
5. Add new tree item to second tree

I don't know if there is an easier solution, but I think this would work.

darrellmeyer
12 May 2008, 11:59 AM
Thanks for the code sample. There was a bug in TreeItem when adding rendered children. The fix is in SVN.