public class TreeDragStart extends TestBase {
@Override
protected void setup() {
final Tree tree = new Tree("Inventory");
CheckBox checkBox = new CheckBox("Enabled");
checkBox.setImmediate(true);
checkBox.setValue(true);
checkBox.addListener(new ValueChangeListener() {
@Override
public void valueChange(ValueChangeEvent event) {
tree.setEnabled(!tree.isEnabled());
}
});
addComponent(checkBox);
checkBox = new CheckBox("Drag start");
checkBox.setImmediate(true);
checkBox.setValue(true);
checkBox.addListener(new ValueChangeListener() {
@Override
public void valueChange(ValueChangeEvent event) {
if (((CheckBox) event.getProperty()).getValue()) {
tree.setDragMode(TreeDragMode.NODE);
} else {
tree.setDragMode(TreeDragMode.NONE);
}
}
});
addComponent(checkBox);
tree.setContainerDataSource(createTreeContent());
tree.setItemCaptionMode(Tree.ITEM_CAPTION_MODE_EXPLICIT_DEFAULTS_ID);
for (Object item : tree.getItemIds().toArray()) {
tree.setItemCaption(item, (String) ((BeanItem<?>) item)
.getItemProperty("name").getValue());
}
getLayout().addComponent(tree);
// Expand all items
for (Iterator<?> it = tree.rootItemIds().iterator(); it.hasNext();) {
tree.expandItemsRecursively(it.next());
}
// Set the tree in drag source mode
tree.setDragMode(TreeDragMode.NODE);
// Allow the tree to receive drag drops and handle them
tree.setDropHandler(new DropHandler() {
@Override
public AcceptCriterion getAcceptCriterion() {
// Accept drops in the middle of container items
// and below and above all items.
return new Or(Tree.TargetItemAllowsChildren.get(), new Not(
VerticalLocationIs.MIDDLE));
}
@Override
public void drop(DragAndDropEvent event) {
// Wrapper for the object that is dragged
DataBoundTransferable t = (DataBoundTransferable) event
.getTransferable();
TreeTargetDetails target = (TreeTargetDetails) event
.getTargetDetails();
// Get ids of the dragged item and the target item
Object sourceItemId = t.getData("itemId");
Object targetItemId = target.getItemIdOver();
// On which side of the target the item was dropped
VerticalDropLocation location = target.getDropLocation();
HierarchicalContainer container = (HierarchicalContainer) tree
.getContainerDataSource();
BeanItem<?> beanItem = null;
if (sourceItemId instanceof BeanItem<?>) {
beanItem = (BeanItem<?>) sourceItemId;
} else if (sourceItemId instanceof InventoryObject) {
beanItem = new BeanItem<InventoryObject>(
(InventoryObject) sourceItemId);
}
// Remove the item from the source container and
// add it to the tree's container
Container sourceContainer = t.getSourceContainer();
sourceContainer.removeItem(sourceItemId);
tree.addItem(beanItem);
InventoryObject bean = (InventoryObject) beanItem.getBean();
tree.setChildrenAllowed(beanItem, bean.isContainer());
// Drop right on an item -> make it a child
if (location == VerticalDropLocation.MIDDLE) {
tree.setParent(beanItem, targetItemId);
} else if (location == VerticalDropLocation.TOP) {
Object parentId = container.getParent(targetItemId);
tree.setParent(beanItem, parentId);
container.moveAfterSibling(beanItem, targetItemId);
container.moveAfterSibling(targetItemId, beanItem);
}
// Drop below another item -> make it next
else if (location == VerticalDropLocation.BOTTOM) {
Object parentId = container.getParent(targetItemId);
tree.setParent(beanItem, parentId);
container.moveAfterSibling(beanItem, targetItemId);
}
tree.setItemCaption(beanItem, bean.getName());
}
});
// Have a table that allows dragging from
final Table table = new Table("Inventory List");