use of com.vaadin.ui.components.grid.TreeGridDropTarget in project cuba by cuba-platform.
the class AccessGroupCompanion method initDragAndDrop.
@Override
public void initDragAndDrop(Table<User> usersTable, Tree<Group> groupsTree, Consumer<UserGroupChangedEvent> userGroupChangedHandler, Consumer<GroupChangeEvent> groupChangeEventHandler) {
usersTable.withUnwrapped(CubaTable.class, CubaTableDragSourceExtension::new);
groupsTree.withUnwrapped(CubaTree.class, vTree -> {
// tree as drag source
// noinspection unchecked
TreeGridDragSource<Group> treeGridDragSource = new TreeGridDragSource<>(vTree.getCompositionRoot());
treeGridDragSource.setDragDataGenerator(TRANSFER_DATA_TYPE, group -> group.getId().toString());
// tree as drop target
// noinspection unchecked
TreeGridDropTarget<Group> treeGridDropTarget = new TreeGridDropTarget<>(vTree.getCompositionRoot(), DropMode.ON_TOP);
treeGridDropTarget.addTreeGridDropListener(event -> {
// if we drop users from table
if (event.getDragSourceExtension().isPresent() && event.getDragSourceExtension().get() instanceof CubaTableDragSourceExtension) {
// return if we drop user between rows
if (event.getDropLocation() == DropLocation.BELOW) {
return;
}
// noinspection unchecked
CubaTableDragSourceExtension<CubaTable> sourceExtension = (CubaTableDragSourceExtension<CubaTable>) event.getDragSourceExtension().get();
List<Object> itemIds = sourceExtension.getLastDraggedItemIds();
TableItems<User> tableItems = usersTable.getItems();
List<User> users = new ArrayList<>();
for (Object id : itemIds) {
users.add(tableItems.getItem(id));
}
if (event.getDropTargetRow().isPresent()) {
Group group = event.getDropTargetRow().get();
userGroupChangedHandler.accept(new UserGroupChangedEvent(groupsTree, users, group));
}
// if we reorder groups inside tree
} else {
String draggedItemId = event.getDataTransferData().get(TEXT_PLAIN_DATA_TYPE);
if (isEdgeOrIE() && draggedItemId == null) {
draggedItemId = event.getDataTransferText();
}
if (draggedItemId == null) {
return;
}
String[] draggedItemIds = draggedItemId.split("\\r?\\n");
for (String itemId : draggedItemIds) {
Group draggedGroup = groupsTree.getItems().getItem(UUID.fromString(itemId));
if (event.getDropTargetRow().isPresent()) {
Group targetGroup = event.getDropTargetRow().get();
// if we drop to itself
if (targetGroup.getId().equals(draggedGroup.getId())) {
continue;
}
// noinspection unchecked
if (isParentDroppedToChild(draggedGroup, targetGroup, vTree)) {
continue;
}
// if we drop child to the same parent
if (draggedGroup.getParent() != null && (draggedGroup.getParent().getId().equals(targetGroup.getId()))) {
continue;
}
groupChangeEventHandler.accept(new GroupChangeEvent(groupsTree, draggedGroup.getId(), targetGroup.getId()));
// if we drop group to empty space make it root
} else if (event.getDropLocation() == DropLocation.EMPTY) {
groupChangeEventHandler.accept(new GroupChangeEvent(groupsTree, draggedGroup.getId(), null));
}
}
}
});
});
}
use of com.vaadin.ui.components.grid.TreeGridDropTarget in project cuba by cuba-platform.
the class WebFilterHelper method initConditionsDragAndDrop.
@SuppressWarnings("unchecked")
@Override
public void initConditionsDragAndDrop(final Tree tree, final ConditionsTree conditions) {
CubaTree vTree = tree.unwrapOrNull(CubaTree.class);
if (vTree == null) {
return;
}
TreeGridDragSource<AbstractCondition> treeGridDragSource = new TreeGridDragSource<>(vTree.getCompositionRoot());
treeGridDragSource.setDragDataGenerator(TREE_DRAGGED_ITEM_ID, item -> item.getId().toString());
TreeGridDropTarget<AbstractCondition> treeGridDropTarget = new TreeGridDropTarget<>(vTree.getCompositionRoot(), DropMode.ON_TOP_OR_BETWEEN);
treeGridDropTarget.addTreeGridDropListener(event -> {
if (!event.getDragSourceComponent().isPresent() || event.getDragSourceComponent().get() != vTree.getCompositionRoot()) {
return;
}
String sourceId = event.getDataTransferData(TREE_DRAGGED_ITEM_ID).isPresent() ? event.getDataTransferData(TREE_DRAGGED_ITEM_ID).get() : null;
if (sourceId == null) {
return;
}
Object sourceItemId = UUID.fromString(sourceId);
Object targetItemId = event.getDropTargetRow().isPresent() ? event.getDropTargetRow().get().getId() : null;
if (targetItemId == null) {
return;
}
// if we drop to itself
if (targetItemId.equals(sourceItemId)) {
return;
}
AbstractCondition sourceCondition = (AbstractCondition) tree.getItems().getItem(sourceItemId);
AbstractCondition targetCondition = (AbstractCondition) tree.getItems().getItem(targetItemId);
Node<AbstractCondition> sourceNode = conditions.getNode(sourceCondition);
Node<AbstractCondition> targetNode = conditions.getNode(targetCondition);
// if we drop parent to its child
if (isAncestorOf(targetNode, sourceNode)) {
return;
}
boolean moveToTheSameParent = Objects.equals(sourceNode.getParent(), targetNode.getParent());
DropLocation location = event.getDropLocation();
if (location == DropLocation.ON_TOP) {
// prevent drop to not group condition
if (!(targetCondition instanceof GroupCondition)) {
return;
}
if (sourceNode.getParent() == null) {
conditions.getRootNodes().remove(sourceNode);
} else {
sourceNode.getParent().getChildren().remove(sourceNode);
}
targetNode.addChild(sourceNode);
refreshConditionsDs(tree, conditions);
tree.expand(targetCondition);
} else {
List<Node<AbstractCondition>> siblings;
if (targetNode.getParent() == null)
siblings = conditions.getRootNodes();
else
siblings = targetNode.getParent().getChildren();
int targetIndex = siblings.indexOf(targetNode);
if (location == DropLocation.BELOW)
targetIndex++;
int sourceNodeIndex;
if (sourceNode.getParent() == null) {
sourceNodeIndex = conditions.getRootNodes().indexOf(sourceNode);
conditions.getRootNodes().remove(sourceNode);
} else {
sourceNodeIndex = sourceNode.getParent().getChildren().indexOf(sourceNode);
sourceNode.getParent().getChildren().remove(sourceNode);
}
// decrease drop position index if dragging from top to bottom inside the same parent node
if (moveToTheSameParent && (sourceNodeIndex < targetIndex))
targetIndex--;
// if we drop source accurate below expanded target
if (tree.isExpanded(targetItemId) && location == DropLocation.BELOW) {
targetNode.insertChildAt(0, sourceNode);
} else if (targetNode.getParent() == null) {
sourceNode.parent = null;
conditions.getRootNodes().add(targetIndex, sourceNode);
} else {
targetNode.getParent().insertChildAt(targetIndex, sourceNode);
}
refreshConditionsDs(tree, conditions);
}
});
}
Aggregations