Search in sources :

Example 1 with CellStateChangedEvent

use of org.kie.workbench.common.widgets.decoratedgrid.client.widget.events.CellStateChangedEvent in project kie-wb-common by kiegroup.

the class AbstractMergableGridWidget method onCellStateChanged.

@SuppressWarnings("rawtypes")
public void onCellStateChanged(CellStateChangedEvent event) {
    Set<CellStateChangedEvent.CellStateOperation> states = event.getStates();
    boolean bUngroupCells = false;
    Coordinate selection = selections.first().getCoordinate();
    List<List<CellValue<? extends Comparable<?>>>> changedData = new ArrayList<List<CellValue<? extends Comparable<?>>>>();
    // If selections span multiple cells, any of which are grouped we should ungroup them
    if (selections.size() > 1) {
        for (CellValue<? extends Comparable<?>> cell : selections) {
            if (cell instanceof CellValue.GroupedCellValue) {
                bUngroupCells = true;
                break;
            }
        }
    }
    // Update underlying data (update before ungrouping as selections would need to be expanded too)
    for (CellValue<? extends Comparable<?>> cell : selections) {
        Coordinate c = cell.getCoordinate();
        if (!columns.get(c.getCol()).isSystemControlled()) {
            CellValue<? extends Comparable<?>> cv = data.get(c);
            for (CellStateChangedEvent.CellStateOperation state : states) {
                switch(state.getOperation()) {
                    case ADD:
                        cv.addState(state.getState());
                        if (state.getState() == CellValue.CellState.OTHERWISE) {
                            cv.setValue(null);
                        }
                        break;
                    case REMOVE:
                        cv.removeState(state.getState());
                        break;
                }
            }
            // 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++) {
                    List<CellValue<? extends Comparable<?>>> changedRow = new ArrayList<CellValue<? extends Comparable<?>>>();
                    changedRow.add(data.get(c));
                    changedData.add(changedRow);
                }
            } else {
                List<CellValue<? extends Comparable<?>>> changedRow = new ArrayList<CellValue<? extends Comparable<?>>>();
                changedRow.add(data.get(c));
                changedData.add(changedRow);
            }
        }
    }
    // 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 {
        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);
    }
    // Re-select applicable cells, following change to merge
    startSelecting(selection);
    // Raise event for underlying model to update itself
    UpdateModelEvent dce = new UpdateModelEvent(selections.first().getCoordinate(), changedData);
    eventBus.fireEvent(dce);
}
Also used : ArrayList(java.util.ArrayList) UpdateModelEvent(org.kie.workbench.common.widgets.decoratedgrid.client.widget.events.UpdateModelEvent) CellStateChangedEvent(org.kie.workbench.common.widgets.decoratedgrid.client.widget.events.CellStateChangedEvent) Coordinate(org.kie.workbench.common.widgets.decoratedgrid.client.widget.data.Coordinate) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

ArrayList (java.util.ArrayList)1 List (java.util.List)1 Coordinate (org.kie.workbench.common.widgets.decoratedgrid.client.widget.data.Coordinate)1 CellStateChangedEvent (org.kie.workbench.common.widgets.decoratedgrid.client.widget.events.CellStateChangedEvent)1 UpdateModelEvent (org.kie.workbench.common.widgets.decoratedgrid.client.widget.events.UpdateModelEvent)1