Search in sources :

Example 6 with UpdateModelEvent

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);
}
Also used : Context(com.google.gwt.cell.client.Cell.Context) HashMap(java.util.HashMap) UpdateModelEvent(org.kie.workbench.common.widgets.decoratedgrid.client.widget.events.UpdateModelEvent) Coordinate(org.kie.workbench.common.widgets.decoratedgrid.client.widget.data.Coordinate) ArrayList(java.util.ArrayList) List(java.util.List)

Example 7 with UpdateModelEvent

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);
}
Also used : ArrayList(java.util.ArrayList) ToggleMergingEvent(org.kie.workbench.common.widgets.decoratedgrid.client.widget.events.ToggleMergingEvent) DynamicDataRow(org.kie.workbench.common.widgets.decoratedgrid.client.widget.data.DynamicDataRow) GroupedDynamicDataRow(org.kie.workbench.common.widgets.decoratedgrid.client.widget.data.GroupedDynamicDataRow) UpdateModelEvent(org.kie.workbench.common.widgets.decoratedgrid.client.widget.events.UpdateModelEvent) Coordinate(org.kie.workbench.common.widgets.decoratedgrid.client.widget.data.Coordinate) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

ArrayList (java.util.ArrayList)7 List (java.util.List)7 Coordinate (org.kie.workbench.common.widgets.decoratedgrid.client.widget.data.Coordinate)7 UpdateModelEvent (org.kie.workbench.common.widgets.decoratedgrid.client.widget.events.UpdateModelEvent)7 Test (org.junit.Test)4 DynamicData (org.kie.workbench.common.widgets.decoratedgrid.client.widget.data.DynamicData)4 SortDataEvent (org.kie.workbench.common.widgets.decoratedgrid.client.widget.events.SortDataEvent)4 Lists (org.kie.soup.commons.util.Lists)3 Context (com.google.gwt.cell.client.Cell.Context)1 HashMap (java.util.HashMap)1 DynamicDataRow (org.kie.workbench.common.widgets.decoratedgrid.client.widget.data.DynamicDataRow)1 GroupedDynamicDataRow (org.kie.workbench.common.widgets.decoratedgrid.client.widget.data.GroupedDynamicDataRow)1 CellStateChangedEvent (org.kie.workbench.common.widgets.decoratedgrid.client.widget.events.CellStateChangedEvent)1 ToggleMergingEvent (org.kie.workbench.common.widgets.decoratedgrid.client.widget.events.ToggleMergingEvent)1