use of org.kie.workbench.common.widgets.decoratedgrid.client.widget.events.UpdateModelEvent in project kie-wb-common by kiegroup.
the class AbstractMergableGridWidget method onUpdateSelectedCells.
@SuppressWarnings("rawtypes")
public void onUpdateSelectedCells(UpdateSelectedCellsEvent event) {
Comparable<?> value = event.getValue();
Map<Coordinate, List<List<CellValue<? extends Comparable<?>>>>> changedData = new HashMap<Coordinate, List<List<CellValue<? extends Comparable<?>>>>>();
List<List<CellValue<? extends Comparable<?>>>> changedBlock;
List<CellValue<? extends Comparable<?>>> changedRow;
Coordinate firstSelection = selections.first().getCoordinate();
// If selections span multiple cells, any of which are grouped we should ungroup them
boolean bUngroupCells = false;
if (selections.size() > 1) {
for (CellValue<? extends Comparable<?>> cell : selections) {
if (cell instanceof CellValue.GroupedCellValue) {
bUngroupCells = true;
break;
}
}
}
// ---Update selected cells (before ungrouping otherwise selections would need to be expanded too)---
changedBlock = new ArrayList<List<CellValue<? extends Comparable<?>>>>();
for (CellValue<? extends Comparable<?>> cell : selections) {
changedRow = new ArrayList<CellValue<? extends Comparable<?>>>();
Coordinate c = cell.getCoordinate();
if (!columns.get(c.getCol()).isSystemControlled()) {
data.set(c, value);
if (value != null) {
cell.removeState(CellValue.CellState.OTHERWISE);
}
// Copy data that is changing for an event to update the underlying model
if (cell instanceof CellValue.GroupedCellValue) {
CellValue.GroupedCellValue gcv = (CellValue.GroupedCellValue) cell;
for (int iChildValueIndex = 0; iChildValueIndex < gcv.getGroupedCells().size(); iChildValueIndex++) {
changedRow.add(data.get(c));
changedBlock.add(changedRow);
}
} else {
changedRow.add(data.get(c));
changedBlock.add(changedRow);
}
}
}
Coordinate originSelected = new Coordinate(rowMapper.mapToAbsoluteRow(firstSelection.getRow()), firstSelection.getCol());
changedData.put(originSelected, changedBlock);
// ---Clear dependent cells' values---
final Context context = new Context(0, firstSelection.getCol(), null);
final Set<Integer> dependentColumnIndexes = this.dropDownManager.getDependentColumnIndexes(context);
for (Integer dependentColumnIndex : dependentColumnIndexes) {
changedBlock = new ArrayList<List<CellValue<? extends Comparable<?>>>>();
for (CellValue<? extends Comparable<?>> cell : selections) {
changedRow = new ArrayList<CellValue<? extends Comparable<?>>>();
Coordinate dc = new Coordinate(cell.getCoordinate().getRow(), dependentColumnIndex);
if (!columns.get(dc.getCol()).isSystemControlled()) {
data.set(dc, null);
if (value != null) {
cell.removeState(CellValue.CellState.OTHERWISE);
}
// Copy data that is changing for an event to update the underlying model
if (cell instanceof CellValue.GroupedCellValue) {
CellValue.GroupedCellValue gcv = (CellValue.GroupedCellValue) cell;
for (int iChildValueIndex = 0; iChildValueIndex < gcv.getGroupedCells().size(); iChildValueIndex++) {
changedRow.add(data.get(dc));
changedBlock.add(changedRow);
}
} else {
changedRow.add(data.get(dc));
changedBlock.add(changedRow);
}
}
}
Coordinate originDependent = new Coordinate(rowMapper.mapToAbsoluteRow(firstSelection.getRow()), dependentColumnIndex);
changedData.put(originDependent, changedBlock);
}
// Ungroup if applicable
if (bUngroupCells) {
for (CellValue<? extends Comparable<?>> cell : selections) {
if (cell instanceof CellValue.GroupedCellValue) {
// Removing merging partially redraws the grid
removeModelGrouping(cell, true);
}
}
} else if (data.isMerged() || selections.size() > 1) {
// If the data is merged changes to the cells' value can cause the need for a greater range of
// rows to be redrawn as a cell's new value could cause the merged span to increase. This is also
// the only mechanism available to update multiple individual cells' values when multiple
// cells are selected.
data.assertModelMerging();
// Partial redraw
int baseRowIndex = selections.first().getCoordinate().getRow();
int minRedrawRow = findMinRedrawRow(baseRowIndex);
int maxRedrawRow = findMaxRedrawRow(baseRowIndex);
// as large as the selection range
if (maxRedrawRow < selections.last().getCoordinate().getRow()) {
maxRedrawRow = selections.last().getCoordinate().getRow();
}
redrawRows(minRedrawRow, maxRedrawRow);
} else {
// Redraw a single row
int baseRowIndex = selections.first().getCoordinate().getRow();
redrawRows(baseRowIndex, baseRowIndex);
}
// Re-select applicable cells, following change to merge
startSelecting(firstSelection);
// Raise event for underlying model to update itself, converting logical row to physical
UpdateModelEvent dce = new UpdateModelEvent(changedData);
eventBus.fireEvent(dce);
}
use of org.kie.workbench.common.widgets.decoratedgrid.client.widget.events.UpdateModelEvent in project kie-wb-common by kiegroup.
the class AbstractMergableGridWidget method onSortData.
public void onSortData(SortDataEvent event) {
// Remove grouping, if applicable
if (data.isGrouped()) {
ToggleMergingEvent tme = new ToggleMergingEvent(false);
eventBus.fireEvent(tme);
}
// Sort data
List<SortConfiguration> sortConfiguration = event.getSortConfiguration();
data.sort(sortConfiguration);
redraw();
// Copy data and raise event for underlying model to update itself
List<List<CellValue<? extends Comparable<?>>>> changedData = new ArrayList<List<CellValue<? extends Comparable<?>>>>();
for (DynamicDataRow row : data) {
List<CellValue<? extends Comparable<?>>> changedRow = new ArrayList<CellValue<? extends Comparable<?>>>();
changedData.add(changedRow);
for (int iCol = 0; iCol < row.size(); iCol++) {
CellValue<? extends Comparable<?>> changedCell = row.get(iCol);
changedRow.add(changedCell);
}
}
UpdateModelEvent dce = new UpdateModelEvent(new Coordinate(0, 0), changedData);
eventBus.fireEvent(dce);
}
Aggregations