PDA

View Full Version : TreePanel drag and drop element into element without any children



MickeyMiner
30 Apr 2010, 6:42 AM
Hi,

I have a simple TreePanel where all nodes/elements are based on the same class called "Person". Within this simple TreePanel I try to drag and drop one element into an element that has no children yet. How do I do it?

If a node has some children, it is no problem to drop inside other persons. However one can not drop an element into an "empty" person. How do I drop "Son 1" to "Father 1"?


+- Root person
|
+- Father 1
|
+- Father 2
|
+- Son 1
|
+- Son 2

public class Person extends BaseTreeModel {

public Person() {

}

public Person(String id, String name) {
setIdentNr(id);
setSortName(name);
}

public String getId() {
return (String) get("id");
}

public void setId(String id) {
set("id", id);
}

public String getName() {
return (String) get("name");
}

public void setName(String name) {
set("name", name);
}

}
public class FamilyTree extends ContentPanel {

FamilyTree() {

setHeading("Family");
setLayout(new FitLayout());

Person r = new Person("0", "Root person");
Person f1 = new Person("1", "Father 1");
Person f2 = new Person("2", "Father 2");
Person s1 = new Person("3", "Son 1");
Person s2 = new Person("4", "Son 2");

f2.add(s1);
f2.add(s2);
r.add(f1);
r.add(f2);

TreeStore<Person> store = new TreeStore<Person>();
store.add(r, true);

TreePanel<Person> tree = new TreePanel<Person>(store);
tree.setDisplayProperty("name");
tree.expandAll();
tree.setContextMenu(contextMenu);

DNDListener listener = new DNDListener() {
@SuppressWarnings("unchecked")
@Override
public void dragStart(DNDEvent e) {
TreePanel tree = ((TreePanel) e.getComponent());
Person sel = (Person) tree.getSelectionModel().getSelectedItem();
if (sel != null && tree.getStore().getParent(sel) == null) {
e.setCancelled(true);
e.getStatus().setStatus(false);
return;
}
super.dragStart(e);
}
};
TreePanelDragSource source = new TreePanelDragSource(tree);
source.addDNDListener(listener);

TreePanelDropTarget target = new TreePanelDropTarget(tree);
target.setAllowSelfAsSource(true);
target.setFeedback(Feedback.BOTH);

add(tree);

}
}
Thank you for your help.

mm

joseblas
13 Feb 2014, 11:54 PM
Hi,

I have the same problem, did you solve it?