Search in sources :

Example 1 with DragAndDropEvent

use of com.vaadin.event.dd.DragAndDropEvent in project VaadinUtils by rlsutton1.

the class BaseCrudView method enableDragAndDropOrdering.

/**
 * allows the user to sort the items in the list via drag and drop
 *
 * @param ordinalField
 */
public void enableDragAndDropOrdering(final SingularAttribute<E, Long> ordinalField) {
    dragAndDropOrderingEnabled = true;
    this.ordinalField = ordinalField;
    container.sort(new Object[] { ordinalField.getName() }, new boolean[] { true });
    this.entityTable.setDragMode(TableDragMode.ROW);
    this.entityTable.setDropHandler(new DropHandler() {

        private static final long serialVersionUID = -6024948983201516170L;

        @Override
        public AcceptCriterion getAcceptCriterion() {
            return SourceIsTarget.get();
        }

        @SuppressWarnings("unchecked")
        @Override
        public void drop(DragAndDropEvent event) {
            if (isDirty()) {
                Notification.show("You must save first", Type.WARNING_MESSAGE);
                return;
            }
            Object draggedItemId = event.getTransferable().getData("itemId");
            EntityItem<E> dragged = container.getItem(draggedItemId);
            if (dragged != null) {
                EntityItemProperty draggedOrdinalProp = dragged.getItemProperty(ordinalField.getName());
                if (draggedOrdinalProp != null) {
                    AbstractSelectTargetDetails td = (AbstractSelectTargetDetails) event.getTargetDetails();
                    VerticalDropLocation dl = td.getDropLocation();
                    Object targetId = ((AbstractSelectTargetDetails) event.getTargetDetails()).getItemIdOver();
                    int idx = container.indexOfId(targetId);
                    if (dl == VerticalDropLocation.BOTTOM) {
                        // drop below so move the idx down one
                        idx++;
                    }
                    if (idx > -1) {
                        targetId = container.getIdByIndex(idx);
                    }
                    boolean added = false;
                    Long ctr = 1l;
                    for (Object id : container.getItemIds()) {
                        if (id.equals(targetId)) {
                            draggedOrdinalProp.setValue(ctr++);
                            added = true;
                        }
                        if (!id.equals(draggedItemId)) {
                            container.getItem(id).getItemProperty(ordinalField.getName()).setValue(ctr++);
                        }
                    }
                    if (!added) {
                        draggedOrdinalProp.setValue(ctr++);
                    }
                    container.commit();
                    if (dragAndDropListener != null) {
                        dragAndDropListener.dropped();
                    }
                    // to save.
                    try {
                        invokeTopLevelCrudSave();
                    } catch (Exception e) {
                        ErrorWindow.showErrorWindow(e);
                    }
                } else {
                    logger.error("draggedOrdinalProp is null");
                }
            } else {
                logger.error("dragged is null");
            }
        }
    });
}
Also used : AbstractSelectTargetDetails(com.vaadin.ui.AbstractSelect.AbstractSelectTargetDetails) VerticalDropLocation(com.vaadin.shared.ui.dd.VerticalDropLocation) DragAndDropEvent(com.vaadin.event.dd.DragAndDropEvent) EntityItemProperty(com.vaadin.addon.jpacontainer.EntityItemProperty) CommitException(com.vaadin.data.fieldgroup.FieldGroup.CommitException) EntityNotFoundException(javax.persistence.EntityNotFoundException) InvalidValueException(com.vaadin.data.Validator.InvalidValueException) ExecutionException(java.util.concurrent.ExecutionException) DropHandler(com.vaadin.event.dd.DropHandler) EntityItem(com.vaadin.addon.jpacontainer.EntityItem) AcceptCriterion(com.vaadin.event.dd.acceptcriteria.AcceptCriterion)

Example 2 with DragAndDropEvent

use of com.vaadin.event.dd.DragAndDropEvent in project cuba by cuba-platform.

the class AccessGroupCompanion method initDragAndDrop.

@Override
public void initDragAndDrop(Table<User> usersTable, Tree<Group> groupsTree, Consumer<UserGroupChangedEvent> userGroupChangedHandler) {
    com.vaadin.ui.Table vTable = usersTable.unwrap(com.vaadin.ui.Table.class);
    vTable.setDragMode(com.vaadin.ui.Table.TableDragMode.MULTIROW);
    CubaTree vTree = groupsTree.unwrap(CubaTree.class);
    vTree.setDragMode(com.vaadin.ui.Tree.TreeDragMode.NODE);
    vTree.setDropHandler(new DropHandler() {

        @Override
        public void drop(DragAndDropEvent dropEvent) {
            DataBoundTransferable transferable = (DataBoundTransferable) dropEvent.getTransferable();
            AbstractSelect.AbstractSelectTargetDetails dropData = ((AbstractSelect.AbstractSelectTargetDetails) dropEvent.getTargetDetails());
            Component sourceComponent = transferable.getSourceComponent();
            List<User> users = null;
            if (sourceComponent instanceof com.vaadin.ui.Table) {
                users = new ArrayList<>(usersTable.getSelected());
            }
            if (users == null) {
                return;
            }
            if (users.isEmpty()) {
                User user = convertToEntity(vTable.getItem(transferable.getItemId()), User.class);
                users.add(user);
            }
            final Object targetItemId = dropData.getItemIdOver();
            if (targetItemId == null) {
                return;
            }
            Group group = convertToEntity(vTree.getItem(targetItemId), Group.class);
            if (group == null) {
                return;
            }
            userGroupChangedHandler.accept(new UserGroupChangedEvent(groupsTree, users, group));
        }

        @Override
        public AcceptCriterion getAcceptCriterion() {
            return new And(AbstractSelect.AcceptItem.ALL);
        }
    });
}
Also used : Group(com.haulmont.cuba.security.entity.Group) User(com.haulmont.cuba.security.entity.User) DragAndDropEvent(com.vaadin.event.dd.DragAndDropEvent) ArrayList(java.util.ArrayList) CubaTree(com.haulmont.cuba.web.toolkit.ui.CubaTree) DataBoundTransferable(com.vaadin.event.DataBoundTransferable) UserGroupChangedEvent(com.haulmont.cuba.gui.app.security.group.browse.UserGroupChangedEvent) And(com.vaadin.event.dd.acceptcriteria.And) ArrayList(java.util.ArrayList) List(java.util.List) DropHandler(com.vaadin.event.dd.DropHandler) Component(com.vaadin.ui.Component) AcceptCriterion(com.vaadin.event.dd.acceptcriteria.AcceptCriterion)

Example 3 with DragAndDropEvent

use of com.vaadin.event.dd.DragAndDropEvent in project cuba by cuba-platform.

the class WebFilterHelper method initConditionsDragAndDrop.

@Override
public void initConditionsDragAndDrop(final Tree tree, final ConditionsTree conditions) {
    final com.vaadin.ui.Tree vTree = tree.unwrap(com.vaadin.ui.Tree.class);
    vTree.setDragMode(com.vaadin.ui.Tree.TreeDragMode.NODE);
    vTree.setDropHandler(new DropHandler() {

        @Override
        public void drop(DragAndDropEvent event) {
            Transferable t = event.getTransferable();
            if (t.getSourceComponent() != vTree)
                return;
            com.vaadin.ui.Tree.TreeTargetDetails target = (com.vaadin.ui.Tree.TreeTargetDetails) event.getTargetDetails();
            VerticalDropLocation location = target.getDropLocation();
            Object sourceItemId = t.getData("itemId");
            Object targetItemId = target.getItemIdOver();
            if (targetItemId == null)
                return;
            CollectionDatasource datasource = tree.getDatasource();
            AbstractCondition sourceCondition = (AbstractCondition) datasource.getItem(sourceItemId);
            AbstractCondition targetCondition = (AbstractCondition) datasource.getItem(targetItemId);
            Node<AbstractCondition> sourceNode = conditions.getNode(sourceCondition);
            Node<AbstractCondition> targetNode = conditions.getNode(targetCondition);
            if (isAncestorOf(targetNode, sourceNode))
                return;
            boolean moveToTheSameParent = Objects.equals(sourceNode.getParent(), targetNode.getParent());
            if (location == VerticalDropLocation.MIDDLE) {
                if (sourceNode.getParent() == null) {
                    conditions.getRootNodes().remove(sourceNode);
                } else {
                    sourceNode.getParent().getChildren().remove(sourceNode);
                }
                targetNode.addChild(sourceNode);
                refreshConditionsDs();
                tree.expand(targetCondition.getId());
            } else {
                List<Node<AbstractCondition>> siblings;
                if (targetNode.getParent() == null)
                    siblings = conditions.getRootNodes();
                else
                    siblings = targetNode.getParent().getChildren();
                int targetIndex = siblings.indexOf(targetNode);
                if (location == VerticalDropLocation.BOTTOM)
                    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 (targetNode.getParent() == null) {
                    sourceNode.parent = null;
                    conditions.getRootNodes().add(targetIndex, sourceNode);
                } else {
                    targetNode.getParent().insertChildAt(targetIndex, sourceNode);
                }
                refreshConditionsDs();
            }
        }

        protected boolean isAncestorOf(Node childNode, Node possibleParentNode) {
            while (childNode.getParent() != null) {
                if (childNode.getParent().equals(possibleParentNode))
                    return true;
                childNode = childNode.getParent();
            }
            return false;
        }

        protected void refreshConditionsDs() {
            tree.getDatasource().refresh(Collections.singletonMap("conditions", conditions));
        }

        @Override
        public AcceptCriterion getAcceptCriterion() {
            return new Or(new AbstractSelect.TargetItemIs(vTree, getGroupConditionIds().toArray()), new Not(AbstractSelect.VerticalLocationIs.MIDDLE));
        }

        protected List<UUID> getGroupConditionIds() {
            List<UUID> groupConditions = new ArrayList<>();
            List<AbstractCondition> list = conditions.toConditionsList();
            for (AbstractCondition condition : list) {
                if (condition instanceof GroupCondition)
                    groupConditions.add(condition.getId());
            }
            return groupConditions;
        }
    });
}
Also used : Or(com.vaadin.event.dd.acceptcriteria.Or) VerticalDropLocation(com.vaadin.shared.ui.dd.VerticalDropLocation) DragAndDropEvent(com.vaadin.event.dd.DragAndDropEvent) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) Node(com.haulmont.bali.datastruct.Node) Transferable(com.vaadin.event.Transferable) Not(com.vaadin.event.dd.acceptcriteria.Not) ConditionsTree(com.haulmont.cuba.gui.components.filter.ConditionsTree) GroupCondition(com.haulmont.cuba.gui.components.filter.condition.GroupCondition) DropHandler(com.vaadin.event.dd.DropHandler) AbstractCondition(com.haulmont.cuba.gui.components.filter.condition.AbstractCondition) AcceptCriterion(com.vaadin.event.dd.acceptcriteria.AcceptCriterion)

Aggregations

DragAndDropEvent (com.vaadin.event.dd.DragAndDropEvent)3 DropHandler (com.vaadin.event.dd.DropHandler)3 AcceptCriterion (com.vaadin.event.dd.acceptcriteria.AcceptCriterion)3 VerticalDropLocation (com.vaadin.shared.ui.dd.VerticalDropLocation)2 Node (com.haulmont.bali.datastruct.Node)1 UserGroupChangedEvent (com.haulmont.cuba.gui.app.security.group.browse.UserGroupChangedEvent)1 ConditionsTree (com.haulmont.cuba.gui.components.filter.ConditionsTree)1 AbstractCondition (com.haulmont.cuba.gui.components.filter.condition.AbstractCondition)1 GroupCondition (com.haulmont.cuba.gui.components.filter.condition.GroupCondition)1 CollectionDatasource (com.haulmont.cuba.gui.data.CollectionDatasource)1 Group (com.haulmont.cuba.security.entity.Group)1 User (com.haulmont.cuba.security.entity.User)1 CubaTree (com.haulmont.cuba.web.toolkit.ui.CubaTree)1 EntityItem (com.vaadin.addon.jpacontainer.EntityItem)1 EntityItemProperty (com.vaadin.addon.jpacontainer.EntityItemProperty)1 InvalidValueException (com.vaadin.data.Validator.InvalidValueException)1 CommitException (com.vaadin.data.fieldgroup.FieldGroup.CommitException)1 DataBoundTransferable (com.vaadin.event.DataBoundTransferable)1 Transferable (com.vaadin.event.Transferable)1 And (com.vaadin.event.dd.acceptcriteria.And)1