Search in sources :

Example 1 with AbstractSelectTargetDetails

use of com.vaadin.ui.AbstractSelect.AbstractSelectTargetDetails 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) OptimisticLockException(javax.persistence.OptimisticLockException) ExecutionException(java.util.concurrent.ExecutionException) DropHandler(com.vaadin.event.dd.DropHandler) EntityItem(com.vaadin.addon.jpacontainer.EntityItem) AcceptCriterion(com.vaadin.event.dd.acceptcriteria.AcceptCriterion)

Aggregations

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 DragAndDropEvent (com.vaadin.event.dd.DragAndDropEvent)1 DropHandler (com.vaadin.event.dd.DropHandler)1 AcceptCriterion (com.vaadin.event.dd.acceptcriteria.AcceptCriterion)1 VerticalDropLocation (com.vaadin.shared.ui.dd.VerticalDropLocation)1 AbstractSelectTargetDetails (com.vaadin.ui.AbstractSelect.AbstractSelectTargetDetails)1 ExecutionException (java.util.concurrent.ExecutionException)1 EntityNotFoundException (javax.persistence.EntityNotFoundException)1 OptimisticLockException (javax.persistence.OptimisticLockException)1