Search in sources :

Example 26 with StructuralDiff

use of org.eclipse.nebula.widgets.nattable.layer.event.StructuralDiff in project nebula.widgets.nattable by eclipse.

the class RowReorderLayer method handleLayerEvent.

@Override
public void handleLayerEvent(ILayerEvent event) {
    if (event instanceof IStructuralChangeEvent) {
        IStructuralChangeEvent structuralChangeEvent = (IStructuralChangeEvent) event;
        if (structuralChangeEvent.isVerticalStructureChanged()) {
            Collection<StructuralDiff> structuralDiffs = structuralChangeEvent.getRowDiffs();
            if (structuralDiffs == null) {
                // Assume everything changed
                populateIndexOrder();
            } else {
                // only react on ADD or DELETE and not on CHANGE
                StructuralChangeEventHelper.handleRowDelete(structuralDiffs, this.underlyingLayer, this.rowIndexOrder, true);
                StructuralChangeEventHelper.handleRowInsert(structuralDiffs, this.underlyingLayer, this.rowIndexOrder, true);
                // update index-position mapping
                refreshIndexPositionMapping();
            }
            invalidateCache();
        }
    }
    super.handleLayerEvent(event);
}
Also used : IStructuralChangeEvent(org.eclipse.nebula.widgets.nattable.layer.event.IStructuralChangeEvent) StructuralDiff(org.eclipse.nebula.widgets.nattable.layer.event.StructuralDiff)

Example 27 with StructuralDiff

use of org.eclipse.nebula.widgets.nattable.layer.event.StructuralDiff in project nebula.widgets.nattable by eclipse.

the class RowReorderEvent method getRowDiffs.

@Override
public Collection<StructuralDiff> getRowDiffs() {
    Collection<StructuralDiff> rowDiffs = new ArrayList<StructuralDiff>();
    Collection<Range> beforeFromRowPositionRanges = getBeforeFromRowPositionRanges();
    final int beforeToRowPosition = (this.reorderToTopEdge) ? this.beforeToRowPosition : (this.beforeToRowPosition + 1);
    int afterAddRowPosition = beforeToRowPosition;
    for (Range beforeFromRowPositionRange : beforeFromRowPositionRanges) {
        if (beforeFromRowPositionRange.start < beforeToRowPosition) {
            afterAddRowPosition -= Math.min(beforeFromRowPositionRange.end, beforeToRowPosition) - beforeFromRowPositionRange.start;
        } else {
            break;
        }
    }
    int cumulativeAddSize = 0;
    for (Range beforeFromRowPositionRange : beforeFromRowPositionRanges) {
        cumulativeAddSize += beforeFromRowPositionRange.size();
    }
    int offset = 0;
    for (Range beforeFromRowPositionRange : beforeFromRowPositionRanges) {
        int afterDeleteRowPosition = beforeFromRowPositionRange.start - offset;
        if (afterAddRowPosition < afterDeleteRowPosition) {
            afterDeleteRowPosition += cumulativeAddSize;
        }
        rowDiffs.add(new StructuralDiff(DiffTypeEnum.DELETE, beforeFromRowPositionRange, new Range(afterDeleteRowPosition, afterDeleteRowPosition)));
        offset += beforeFromRowPositionRange.size();
    }
    Range beforeAddRange = new Range(beforeToRowPosition, beforeToRowPosition);
    offset = 0;
    for (Range beforeFromRowPositionRange : beforeFromRowPositionRanges) {
        int size = beforeFromRowPositionRange.size();
        rowDiffs.add(new StructuralDiff(DiffTypeEnum.ADD, beforeAddRange, new Range(afterAddRowPosition + offset, afterAddRowPosition + offset + size)));
        offset += size;
    }
    return rowDiffs;
}
Also used : StructuralDiff(org.eclipse.nebula.widgets.nattable.layer.event.StructuralDiff) ArrayList(java.util.ArrayList) Range(org.eclipse.nebula.widgets.nattable.coordinate.Range)

Example 28 with StructuralDiff

use of org.eclipse.nebula.widgets.nattable.layer.event.StructuralDiff in project nebula.widgets.nattable by eclipse.

the class PreserveSelectionModel method handleLayerEvent.

@Override
public void handleLayerEvent(IStructuralChangeEvent event) {
    // handling for deleting columns
    if (event.isHorizontalStructureChanged()) {
        Collection<StructuralDiff> diffs = event.getColumnDiffs();
        if (diffs != null) {
            // confusing indexes
            for (StructuralDiff columnDiff : diffs) {
                if (columnDiff.getDiffType() != null && columnDiff.getDiffType().equals(DiffTypeEnum.DELETE)) {
                    Range beforePositionRange = columnDiff.getBeforePositionRange();
                    for (int i = beforePositionRange.start; i < beforePositionRange.end; i++) {
                        this.selections.deselectColumn(i);
                        // ask for further column selections that need to be
                        // modified
                        this.selections.updateColumnsForRemoval(i);
                    }
                }
            }
            for (StructuralDiff columnDiff : diffs) {
                if (columnDiff.getDiffType() != null && columnDiff.getDiffType().equals(DiffTypeEnum.ADD)) {
                    Range afterPositionRange = columnDiff.getAfterPositionRange();
                    for (int i = afterPositionRange.start; i < afterPositionRange.end; i++) {
                        // ask for column selections that need to be
                        // modified
                        this.selections.updateColumnsForAddition(i);
                    }
                }
            }
        }
    }
    // handling for deleting rows
    if (event.isVerticalStructureChanged()) {
        // the change is already done and we don't know about indexes, so we
        // need to check if the selected objects still exist
        Collection<Serializable> keysToRemove = new ArrayList<Serializable>();
        for (Selections.Row<T> row : this.selections.getRows()) {
            int rowIndex = this.rowDataProvider.indexOfRowObject(row.getRowObject());
            if (rowIndex == -1) {
                keysToRemove.add(row.getId());
            }
        }
        for (Serializable toRemove : keysToRemove) {
            this.selections.deselectRow(toRemove);
        }
    }
}
Also used : Serializable(java.io.Serializable) StructuralDiff(org.eclipse.nebula.widgets.nattable.layer.event.StructuralDiff) ArrayList(java.util.ArrayList) Range(org.eclipse.nebula.widgets.nattable.coordinate.Range) Point(org.eclipse.swt.graphics.Point)

Aggregations

StructuralDiff (org.eclipse.nebula.widgets.nattable.layer.event.StructuralDiff)28 Range (org.eclipse.nebula.widgets.nattable.coordinate.Range)23 Test (org.junit.Test)12 ArrayList (java.util.ArrayList)9 IStructuralChangeEvent (org.eclipse.nebula.widgets.nattable.layer.event.IStructuralChangeEvent)5 ColumnReorderEvent (org.eclipse.nebula.widgets.nattable.reorder.event.ColumnReorderEvent)4 Serializable (java.io.Serializable)2 NatTable (org.eclipse.nebula.widgets.nattable.NatTable)2 IUniqueIndexLayer (org.eclipse.nebula.widgets.nattable.layer.IUniqueIndexLayer)2 NatTableFixture (org.eclipse.nebula.widgets.nattable.test.fixture.NatTableFixture)2 LayerListenerFixture (org.eclipse.nebula.widgets.nattable.test.fixture.layer.LayerListenerFixture)2 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 PositionCoordinate (org.eclipse.nebula.widgets.nattable.coordinate.PositionCoordinate)1 UpdateDataCommand (org.eclipse.nebula.widgets.nattable.edit.command.UpdateDataCommand)1 DataUpdateEvent (org.eclipse.nebula.widgets.nattable.edit.event.DataUpdateEvent)1 ColumnHideCommand (org.eclipse.nebula.widgets.nattable.hideshow.command.ColumnHideCommand)1