Search in sources :

Example 1 with CubaTableDragSourceExtension

use of com.haulmont.cuba.web.widgets.CubaTableDragSourceExtension 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));
                    }
                }
            }
        });
    });
}
Also used : TreeGridDragSource(com.vaadin.ui.components.grid.TreeGridDragSource) Group(com.haulmont.cuba.security.entity.Group) User(com.haulmont.cuba.security.entity.User) GroupChangeEvent(com.haulmont.cuba.gui.app.security.group.browse.GroupChangeEvent) ArrayList(java.util.ArrayList) TreeGridDropTarget(com.vaadin.ui.components.grid.TreeGridDropTarget) UserGroupChangedEvent(com.haulmont.cuba.gui.app.security.group.browse.UserGroupChangedEvent) CubaTable(com.haulmont.cuba.web.widgets.CubaTable) CubaTableDragSourceExtension(com.haulmont.cuba.web.widgets.CubaTableDragSourceExtension)

Aggregations

GroupChangeEvent (com.haulmont.cuba.gui.app.security.group.browse.GroupChangeEvent)1 UserGroupChangedEvent (com.haulmont.cuba.gui.app.security.group.browse.UserGroupChangedEvent)1 Group (com.haulmont.cuba.security.entity.Group)1 User (com.haulmont.cuba.security.entity.User)1 CubaTable (com.haulmont.cuba.web.widgets.CubaTable)1 CubaTableDragSourceExtension (com.haulmont.cuba.web.widgets.CubaTableDragSourceExtension)1 TreeGridDragSource (com.vaadin.ui.components.grid.TreeGridDragSource)1 TreeGridDropTarget (com.vaadin.ui.components.grid.TreeGridDropTarget)1 ArrayList (java.util.ArrayList)1