use of com.vaadin.event.dd.acceptcriteria.AcceptCriterion 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");
}
}
});
}
use of com.vaadin.event.dd.acceptcriteria.AcceptCriterion 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);
}
});
}
Aggregations