}
@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");
table.setDragMode(TableDragMode.ROW);
// Initialize the table container
ArrayList<InventoryObject> collection = new ArrayList<InventoryObject>();
collection.add(new InventoryObject("Dummy Item", 0.0, false));
final BeanItemContainer<InventoryObject> tableContainer = new BeanItemContainer<InventoryObject>(
collection);
table.setContainerDataSource(tableContainer);
table.setVisibleColumns(new String[] { "name", "weight" });
table.removeAllItems();
// Allow the table to receive drops and handle them
table.setDropHandler(new DropHandler() {
@Override
public AcceptCriterion getAcceptCriterion() {
return new Not(VerticalLocationIs.MIDDLE);
}
@Override
public void drop(DragAndDropEvent event) {
// Wrapper for the object that is dragged
DataBoundTransferable t = (DataBoundTransferable) event
.getTransferable();
// Make sure the drag source is the same tree
if (t.getSourceComponent() != tree
&& t.getSourceComponent() != table) {
return;
}
AbstractSelectTargetDetails target = (AbstractSelectTargetDetails) event
.getTargetDetails();
// Get ids of the dragged item and the target item
Object sourceItemId = t.getData("itemId");
Object targetItemId = target.getItemIdOver();
// Do not allow drop on the item itself
if (sourceItemId.equals(targetItemId)) {
return;
}
InventoryObject bean = null;
if (sourceItemId instanceof BeanItem<?>) {
bean = (InventoryObject) ((BeanItem<?>) sourceItemId)
.getBean();
} else if (sourceItemId instanceof InventoryObject) {
bean = (InventoryObject) sourceItemId;
}
// Remove the item from the source container
t.getSourceContainer().removeItem(sourceItemId);
// On which side of the target the item was dropped
VerticalDropLocation location = target.getDropLocation();
// The table was empty or otherwise not on an item