use of io.jmix.reports.entity.wizard.OrderableEntity in project jmix by jmix-framework.
the class ItemOrderableAction method moveFewItems.
/**
* Move few items up or down by recalculating their indexes.
* Then we sorting table and normalize indexes
*/
protected void moveFewItems() {
// System.out.println("swap-------------");
List<E> allItems = getItems();
Set<E> selectedItems = target.getSelected();
// U can use 10 for easier debug
int spreadKoef = target.getSelected().size();
long idx = 0;
long lastIdxInGrp = Long.MAX_VALUE;
long idxInGrp = 0;
// for detection new orderNum values we must to store information about size of selected groups of items
Map<OrderableEntity, Long> itemAndIndexInSelectedGroup = Collections.emptyMap();
for (OrderableEntity item : allItems) {
++idx;
// spread item indexes
item.setOrderNum(item.getOrderNum() * spreadKoef);
if (selectedItems.contains(item)) {
if (itemAndIndexInSelectedGroup.isEmpty()) {
// start to store selected items with them index changing values to that map
itemAndIndexInSelectedGroup = new LinkedHashMap<>();
}
if (!itemAndIndexInSelectedGroup.isEmpty() || lastIdxInGrp == Long.MAX_VALUE) {
// check that we are still in group of sequential selected items. sequence can contain one element
// we are enter inside group of selected item(s) now
idxInGrp++;
itemAndIndexInSelectedGroup.put(item, idxInGrp);
}
lastIdxInGrp = idx;
} else {
// we left group of sequential selected items. Now lets calc new orderNum values
if (!itemAndIndexInSelectedGroup.isEmpty()) {
// System.out.print("*midGrp size" + idxInGrp + "*");
updateItemOrderNums(idxInGrp, itemAndIndexInSelectedGroup, spreadKoef);
itemAndIndexInSelectedGroup = Collections.emptyMap();
}
// reset counter values for re-use
idxInGrp = 0;
lastIdxInGrp = Long.MAX_VALUE;
}
// System.out.print("before " + item.getOrderNum() + " |");
// System.out.print("after " + item.getOrderNum() + " |\n");
}
// we left group of sequential selected items. Last item was selected in list. Now lets calc new orderNum values
if (!itemAndIndexInSelectedGroup.isEmpty()) {
// System.out.print("*endGrp size" + idxInGrp + "*");
updateItemOrderNums(idxInGrp, itemAndIndexInSelectedGroup, spreadKoef);
}
// lets sort by the new values
sortTableDsByItemsOrderNum();
// lets normalize recalculated indexes like 1,2,3...
normalizeEntityOrderNum();
}
use of io.jmix.reports.entity.wizard.OrderableEntity in project jmix by jmix-framework.
the class ItemOrderableAction method updateItemOrderNums.
private void updateItemOrderNums(long grpSize, Map<OrderableEntity, Long> itemAndIndexInGroup, int spreadKoef) {
for (Map.Entry<OrderableEntity, Long> entityLongEntry : itemAndIndexInGroup.entrySet()) {
OrderableEntity itemToChange = entityLongEntry.getKey();
// System.out.print("*** before " + itemToChange.getOrderNum() + " |");
long newValue = itemToChange.getOrderNum();
long itemIndexInGrp = entityLongEntry.getValue();
switch(direction) {
case UP:
newValue = itemToChange.getOrderNum() + (grpSize - itemIndexInGrp) * spreadKoef - (grpSize - itemIndexInGrp + 1) - grpSize * spreadKoef;
break;
case DOWN:
newValue = itemToChange.getOrderNum() - (itemIndexInGrp - 1) * spreadKoef + itemIndexInGrp + grpSize * spreadKoef;
break;
}
itemToChange.setOrderNum(newValue);
// System.out.print("after " + itemToChange.getOrderNum() + " |\n");
}
}
use of io.jmix.reports.entity.wizard.OrderableEntity in project jmix by jmix-framework.
the class ItemOrderableAction method sortTableDsByItemsOrderNum.
protected void sortTableDsByItemsOrderNum() {
DataUnit dataUnit = target.getItems();
if (dataUnit instanceof ContainerDataUnit) {
ContainerDataUnit containerDataUnit = (ContainerDataUnit) dataUnit;
CollectionContainer<? extends OrderableEntity> collectionContainer = containerDataUnit.getContainer();
List sortedItems = collectionContainer.getItems().stream().sorted(Comparator.comparingLong(OrderableEntity::getOrderNum)).collect(Collectors.toList());
collectionContainer.setItems(sortedItems);
}
}
use of io.jmix.reports.entity.wizard.OrderableEntity in project jmix by jmix-framework.
the class ItemOrderableAction method normalizeEntityOrderNum.
/**
* Iterate over items and set orderNum value from 1 to size()+1;
*/
protected void normalizeEntityOrderNum() {
long normalizedIdx = 0;
List<OrderableEntity> allItems = getItems();
for (OrderableEntity item : allItems) {
// first must to be 1
item.setOrderNum(++normalizedIdx);
}
}
use of io.jmix.reports.entity.wizard.OrderableEntity in project jmix by jmix-framework.
the class ItemOrderableAction method swapSingleItemWithNeighbour.
/**
* Swap items is simple
*/
protected void swapSingleItemWithNeighbour() {
OrderableEntity selectedItem = (OrderableEntity) target.getSingleSelected();
OrderableEntity neighbourItem = null;
List<OrderableEntity> allItems = getItems();
for (ListIterator<OrderableEntity> iterator = allItems.listIterator(); iterator.hasNext(); ) {
if (iterator.next().equals(target.getSingleSelected())) {
neighbourItem = getItemNeighbour(iterator);
break;
}
}
if (neighbourItem != null) {
switch(direction) {
case UP:
neighbourItem.setOrderNum(neighbourItem.getOrderNum() + 1);
selectedItem.setOrderNum(selectedItem.getOrderNum() - 1);
break;
case DOWN:
neighbourItem.setOrderNum(neighbourItem.getOrderNum() - 1);
selectedItem.setOrderNum(selectedItem.getOrderNum() + 1);
break;
}
sortTableDsByItemsOrderNum();
}
}
Aggregations