Search in sources :

Example 1 with UpdateModelEvent

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

the class AbstractMergableGridWidgetSortingTest method testOnSortData.

@Test
public void testOnSortData() {
    final Coordinate origin = new Coordinate(0, 0);
    final DynamicData data = new DynamicData();
    data.addRow();
    data.addRow();
    final List<CellValue<? extends Comparable<?>>> columnData = new ArrayList<>();
    final CellValue<String> cellOne = new CellValue<>("one");
    final CellValue<String> cellTwo = new CellValue<>("two");
    columnData.add(cellOne);
    columnData.add(cellTwo);
    data.addColumn(0, columnData, true);
    widget.setData(data);
    final SortConfiguration sortConfiguration = new SortConfiguration();
    sortConfiguration.setColumnIndex(0);
    sortConfiguration.setSortDirection(SortDirection.DESCENDING);
    final SortDataEvent sortEvent = new SortDataEvent(Collections.singletonList(sortConfiguration));
    widget.onSortData(sortEvent);
    verify(eventBus).fireEvent(updateModelEventCaptor.capture());
    final UpdateModelEvent updateModelEvent = updateModelEventCaptor.getValue();
    final Map<Coordinate, List<List<CellValue<? extends Comparable<?>>>>> updates = updateModelEvent.getUpdates();
    assertNotNull(updates);
    assertEquals(1, updates.size());
    assertTrue(updates.containsKey(origin));
    final List<List<CellValue<? extends Comparable<?>>>> updates00 = updates.get(origin);
    assertEquals(2, updates00.size());
    assertEquals(1, updates00.get(0).size());
    assertEquals(1, updates00.get(1).size());
    assertEquals(cellTwo, updates00.get(0).get(0));
    assertEquals(cellOne, updates00.get(1).get(0));
}
Also used : ArrayList(java.util.ArrayList) SortDataEvent(org.kie.workbench.common.widgets.decoratedgrid.client.widget.events.SortDataEvent) 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) DynamicData(org.kie.workbench.common.widgets.decoratedgrid.client.widget.data.DynamicData) Test(org.junit.Test)

Example 2 with UpdateModelEvent

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

the class AbstractMergableGridWidgetSortingTest method testOnSortDataAfterFirstColumnSortedNoChangesNeeded.

@Test
public void testOnSortDataAfterFirstColumnSortedNoChangesNeeded() {
    final Coordinate origin = new Coordinate(0, 0);
    final DynamicData data = new DynamicData();
    data.addRow();
    data.addRow();
    data.addRow();
    final List<CellValue<? extends Comparable<?>>> columnOneData = new ArrayList<>();
    final CellValue<String> cell_00 = new CellValue<>("a");
    final CellValue<String> cell_01 = new CellValue<>("a");
    final CellValue<String> cell_02 = new CellValue<>("b");
    columnOneData.add(cell_00);
    columnOneData.add(cell_01);
    columnOneData.add(cell_02);
    data.addColumn(0, columnOneData, true);
    final List<CellValue<? extends Comparable<?>>> columnTwoData = new ArrayList<>();
    final CellValue<String> cell_10 = new CellValue<>("y");
    final CellValue<String> cell_11 = new CellValue<>("y");
    final CellValue<String> cell_12 = new CellValue<>("x");
    columnTwoData.add(cell_10);
    columnTwoData.add(cell_11);
    columnTwoData.add(cell_12);
    data.addColumn(1, columnTwoData, true);
    widget.setData(data);
    final SortConfiguration columnOneSortConfiguration = new SortConfiguration();
    columnOneSortConfiguration.setColumnIndex(0);
    columnOneSortConfiguration.setSortIndex(0);
    columnOneSortConfiguration.setSortDirection(SortDirection.DESCENDING);
    final SortConfiguration columnTwoSortConfiguration = new SortConfiguration();
    columnTwoSortConfiguration.setColumnIndex(1);
    columnTwoSortConfiguration.setSortIndex(1);
    columnTwoSortConfiguration.setSortDirection(SortDirection.ASCENDING);
    final SortDataEvent sortEvent = new SortDataEvent(Arrays.asList(columnOneSortConfiguration, columnTwoSortConfiguration));
    widget.onSortData(sortEvent);
    verify(eventBus).fireEvent(updateModelEventCaptor.capture());
    final UpdateModelEvent updateModelEvent = updateModelEventCaptor.getValue();
    final Map<Coordinate, List<List<CellValue<? extends Comparable<?>>>>> updates = updateModelEvent.getUpdates();
    assertNotNull(updates);
    assertEquals(1, updates.size());
    assertTrue(updates.containsKey(origin));
    final List<List<CellValue<? extends Comparable<?>>>> updates00 = updates.get(origin);
    final List<CellValue<? extends Comparable<?>>> sortedRow0 = new Lists.Builder().add(cell_02).add(cell_12).build();
    final List<CellValue<? extends Comparable<?>>> sortedRow1 = new Lists.Builder().add(cell_00).add(cell_10).build();
    final List<CellValue<? extends Comparable<?>>> sortedRow2 = new Lists.Builder().add(cell_01).add(cell_11).build();
    assertEquals(3, updates00.size());
    assertEquals(sortedRow0, updates00.get(0));
    assertEquals(sortedRow1, updates00.get(1));
    assertEquals(sortedRow2, updates00.get(2));
}
Also used : ArrayList(java.util.ArrayList) SortDataEvent(org.kie.workbench.common.widgets.decoratedgrid.client.widget.events.SortDataEvent) UpdateModelEvent(org.kie.workbench.common.widgets.decoratedgrid.client.widget.events.UpdateModelEvent) Coordinate(org.kie.workbench.common.widgets.decoratedgrid.client.widget.data.Coordinate) Lists(org.kie.soup.commons.util.Lists) ArrayList(java.util.ArrayList) List(java.util.List) DynamicData(org.kie.workbench.common.widgets.decoratedgrid.client.widget.data.DynamicData) Test(org.junit.Test)

Example 3 with UpdateModelEvent

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

the class AbstractMergableGridWidgetSortingTest method testOnSortDataTwoColumns.

@Test
public void testOnSortDataTwoColumns() {
    final Coordinate origin = new Coordinate(0, 0);
    final DynamicData data = new DynamicData();
    data.addRow();
    data.addRow();
    data.addRow();
    final List<CellValue<? extends Comparable<?>>> columnOneData = new ArrayList<>();
    final CellValue<String> cell_00 = new CellValue<>("a");
    final CellValue<String> cell_01 = new CellValue<>("b");
    final CellValue<String> cell_02 = new CellValue<>("c");
    columnOneData.add(cell_00);
    columnOneData.add(cell_01);
    columnOneData.add(cell_02);
    data.addColumn(0, columnOneData, true);
    final List<CellValue<? extends Comparable<?>>> columnTwoData = new ArrayList<>();
    final CellValue<String> cell_10 = new CellValue<>("x");
    final CellValue<String> cell_11 = new CellValue<>("y");
    final CellValue<String> cell_12 = new CellValue<>("x");
    columnTwoData.add(cell_10);
    columnTwoData.add(cell_11);
    columnTwoData.add(cell_12);
    data.addColumn(1, columnTwoData, true);
    widget.setData(data);
    final SortConfiguration columnOneSortConfiguration = new SortConfiguration();
    columnOneSortConfiguration.setColumnIndex(0);
    columnOneSortConfiguration.setSortIndex(0);
    columnOneSortConfiguration.setSortDirection(SortDirection.DESCENDING);
    final SortConfiguration columnTwoSortConfiguration = new SortConfiguration();
    columnTwoSortConfiguration.setColumnIndex(1);
    columnTwoSortConfiguration.setSortIndex(1);
    columnTwoSortConfiguration.setSortDirection(SortDirection.ASCENDING);
    final SortDataEvent sortEvent = new SortDataEvent(Arrays.asList(columnOneSortConfiguration, columnTwoSortConfiguration));
    widget.onSortData(sortEvent);
    verify(eventBus).fireEvent(updateModelEventCaptor.capture());
    final UpdateModelEvent updateModelEvent = updateModelEventCaptor.getValue();
    final Map<Coordinate, List<List<CellValue<? extends Comparable<?>>>>> updates = updateModelEvent.getUpdates();
    assertNotNull(updates);
    assertEquals(1, updates.size());
    assertTrue(updates.containsKey(origin));
    final List<List<CellValue<? extends Comparable<?>>>> updates00 = updates.get(origin);
    final List<CellValue<? extends Comparable<?>>> sortedRow0 = new Lists.Builder().add(cell_02).add(cell_12).build();
    final List<CellValue<? extends Comparable<?>>> sortedRow1 = new Lists.Builder().add(cell_01).add(cell_11).build();
    final List<CellValue<? extends Comparable<?>>> sortedRow2 = new Lists.Builder().add(cell_00).add(cell_10).build();
    assertEquals(3, updates00.size());
    assertEquals(sortedRow0, updates00.get(0));
    assertEquals(sortedRow1, updates00.get(1));
    assertEquals(sortedRow2, updates00.get(2));
}
Also used : ArrayList(java.util.ArrayList) SortDataEvent(org.kie.workbench.common.widgets.decoratedgrid.client.widget.events.SortDataEvent) UpdateModelEvent(org.kie.workbench.common.widgets.decoratedgrid.client.widget.events.UpdateModelEvent) Coordinate(org.kie.workbench.common.widgets.decoratedgrid.client.widget.data.Coordinate) Lists(org.kie.soup.commons.util.Lists) ArrayList(java.util.ArrayList) List(java.util.List) DynamicData(org.kie.workbench.common.widgets.decoratedgrid.client.widget.data.DynamicData) Test(org.junit.Test)

Example 4 with UpdateModelEvent

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

the class AbstractMergableGridWidgetSortingTest method testOnSortDataChangesCausedByBothSortingConfigurations.

@Test
public void testOnSortDataChangesCausedByBothSortingConfigurations() {
    final Coordinate origin = new Coordinate(0, 0);
    final DynamicData data = new DynamicData();
    data.addRow();
    data.addRow();
    data.addRow();
    final List<CellValue<? extends Comparable<?>>> columnOneData = new ArrayList<>();
    final CellValue<String> cell_00 = new CellValue<>("a");
    final CellValue<String> cell_01 = new CellValue<>("a");
    final CellValue<String> cell_02 = new CellValue<>("b");
    columnOneData.add(cell_00);
    columnOneData.add(cell_01);
    columnOneData.add(cell_02);
    data.addColumn(0, columnOneData, true);
    final List<CellValue<? extends Comparable<?>>> columnTwoData = new ArrayList<>();
    final CellValue<String> cell_10 = new CellValue<>("y");
    final CellValue<String> cell_11 = new CellValue<>("x");
    final CellValue<String> cell_12 = new CellValue<>("y");
    columnTwoData.add(cell_10);
    columnTwoData.add(cell_11);
    columnTwoData.add(cell_12);
    data.addColumn(1, columnTwoData, true);
    widget.setData(data);
    final SortConfiguration columnOneSortConfiguration = new SortConfiguration();
    columnOneSortConfiguration.setColumnIndex(0);
    columnOneSortConfiguration.setSortIndex(0);
    columnOneSortConfiguration.setSortDirection(SortDirection.DESCENDING);
    final SortConfiguration columnTwoSortConfiguration = new SortConfiguration();
    columnTwoSortConfiguration.setColumnIndex(1);
    columnTwoSortConfiguration.setSortIndex(1);
    columnTwoSortConfiguration.setSortDirection(SortDirection.ASCENDING);
    final SortDataEvent sortEvent = new SortDataEvent(Arrays.asList(columnOneSortConfiguration, columnTwoSortConfiguration));
    widget.onSortData(sortEvent);
    verify(eventBus).fireEvent(updateModelEventCaptor.capture());
    final UpdateModelEvent updateModelEvent = updateModelEventCaptor.getValue();
    final Map<Coordinate, List<List<CellValue<? extends Comparable<?>>>>> updates = updateModelEvent.getUpdates();
    assertNotNull(updates);
    assertEquals(1, updates.size());
    assertTrue(updates.containsKey(origin));
    final List<List<CellValue<? extends Comparable<?>>>> updates00 = updates.get(origin);
    final List<CellValue<? extends Comparable<?>>> sortedRow0 = new Lists.Builder().add(cell_02).add(cell_12).build();
    final List<CellValue<? extends Comparable<?>>> sortedRow1 = new Lists.Builder().add(cell_01).add(cell_11).build();
    final List<CellValue<? extends Comparable<?>>> sortedRow2 = new Lists.Builder().add(cell_00).add(cell_10).build();
    assertEquals(3, updates00.size());
    assertEquals(sortedRow0, updates00.get(0));
    assertEquals(sortedRow1, updates00.get(1));
    assertEquals(sortedRow2, updates00.get(2));
}
Also used : ArrayList(java.util.ArrayList) SortDataEvent(org.kie.workbench.common.widgets.decoratedgrid.client.widget.events.SortDataEvent) UpdateModelEvent(org.kie.workbench.common.widgets.decoratedgrid.client.widget.events.UpdateModelEvent) Coordinate(org.kie.workbench.common.widgets.decoratedgrid.client.widget.data.Coordinate) Lists(org.kie.soup.commons.util.Lists) ArrayList(java.util.ArrayList) List(java.util.List) DynamicData(org.kie.workbench.common.widgets.decoratedgrid.client.widget.data.DynamicData) Test(org.junit.Test)

Example 5 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 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)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