Search in sources :

Example 6 with StructuralDiff

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

the class ResizeEventDiffTest method testConvertToLocal.

@Test
public void testConvertToLocal() {
    this.event.convertToLocal(this.viewportLayer);
    Collection<StructuralDiff> columnDiffs = this.event.getColumnDiffs();
    Assert.assertNotNull(columnDiffs);
    Assert.assertEquals(1, columnDiffs.size());
    Assert.assertEquals(new StructuralDiff(DiffTypeEnum.CHANGE, new Range(0, 1), new Range(0, 1)), columnDiffs.iterator().next());
}
Also used : StructuralDiff(org.eclipse.nebula.widgets.nattable.layer.event.StructuralDiff) Range(org.eclipse.nebula.widgets.nattable.coordinate.Range) Test(org.junit.Test)

Example 7 with StructuralDiff

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

the class MultiColumnReorderEventDiffTest method testReorderLeftColumnDiffs.

/**
 * + - - - before: 0 1 2 3 4 5 6 7 8 9 10 after: 0 1 7 8 9 2 3 4 5 6 10 + +
 * + -
 */
@Test
public void testReorderLeftColumnDiffs() {
    this.event = new ColumnReorderEvent(this.dataLayer, Arrays.asList(new Integer[] { 7, 8, 9 }), 2, true);
    Collection<StructuralDiff> columnDiffs = this.event.getColumnDiffs();
    Assert.assertNotNull(columnDiffs);
    Assert.assertEquals(2, columnDiffs.size());
    Iterator<StructuralDiff> iterator = columnDiffs.iterator();
    Assert.assertEquals(new StructuralDiff(DiffTypeEnum.DELETE, new Range(7, 10), new Range(10, 10)), iterator.next());
    Assert.assertEquals(new StructuralDiff(DiffTypeEnum.ADD, new Range(2, 2), new Range(2, 5)), iterator.next());
}
Also used : StructuralDiff(org.eclipse.nebula.widgets.nattable.layer.event.StructuralDiff) ColumnReorderEvent(org.eclipse.nebula.widgets.nattable.reorder.event.ColumnReorderEvent) Range(org.eclipse.nebula.widgets.nattable.coordinate.Range) Test(org.junit.Test)

Example 8 with StructuralDiff

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

the class MultiColumnReorderEventDiffTest method testReorderRightColumnDiffs.

/**
 * - - - + before: 0 1 2 3 4 5 6 7 8 after: 0 1 5 6 2 3 4 7 8 - + + +
 */
@Test
public void testReorderRightColumnDiffs() {
    this.event = new ColumnReorderEvent(this.dataLayer, Arrays.asList(new Integer[] { 2, 3, 4 }), 7, true);
    Collection<StructuralDiff> columnDiffs = this.event.getColumnDiffs();
    Assert.assertNotNull(columnDiffs);
    Assert.assertEquals(2, columnDiffs.size());
    Iterator<StructuralDiff> iterator = columnDiffs.iterator();
    Assert.assertEquals(new StructuralDiff(DiffTypeEnum.DELETE, new Range(2, 5), new Range(2, 2)), iterator.next());
    Assert.assertEquals(new StructuralDiff(DiffTypeEnum.ADD, new Range(7, 7), new Range(4, 7)), iterator.next());
}
Also used : StructuralDiff(org.eclipse.nebula.widgets.nattable.layer.event.StructuralDiff) ColumnReorderEvent(org.eclipse.nebula.widgets.nattable.reorder.event.ColumnReorderEvent) Range(org.eclipse.nebula.widgets.nattable.coordinate.Range) Test(org.junit.Test)

Example 9 with StructuralDiff

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

the class RenameColumnHelper method handleStructuralChanges.

/**
 * Handle the given collection of {@link StructuralDiff} objects to update
 * the indexes of the renamed column labels.
 *
 * @param columnDiffs
 *            The {@link StructuralDiff}s to handle
 * @since 1.4
 */
public void handleStructuralChanges(Collection<StructuralDiff> columnDiffs) {
    // the number of all deleted columns that don't have a corresponding
    // index anymore (last column cases)
    List<Integer> toRemove = new ArrayList<Integer>();
    for (StructuralDiff columnDiff : columnDiffs) {
        if (columnDiff.getDiffType() != null && columnDiff.getDiffType().equals(DiffTypeEnum.DELETE)) {
            Range beforePositionRange = columnDiff.getBeforePositionRange();
            for (int i = beforePositionRange.start; i < beforePositionRange.end; i++) {
                int index = i;
                if (index >= 0)
                    toRemove.add(index);
            }
        }
    }
    // remove the column indexes that are deleted
    for (Integer r : toRemove) {
        this.renamedColumnsLabelsByIndex.remove(r);
    }
    // modify column indexes regarding the deleted columns
    List<Integer> indices = new ArrayList<Integer>(this.renamedColumnsLabelsByIndex.keySet());
    Collections.sort(indices);
    Map<Integer, String> modified = new TreeMap<Integer, String>();
    for (Integer column : indices) {
        // check number of removed indexes that are lower than the current
        // one
        int deletedBefore = 0;
        for (Integer removed : toRemove) {
            if (removed < column) {
                deletedBefore++;
            }
        }
        int modColumn = column - deletedBefore;
        if (modColumn >= 0)
            modified.put(modColumn, this.renamedColumnsLabelsByIndex.get(column));
    }
    this.renamedColumnsLabelsByIndex.clear();
    this.renamedColumnsLabelsByIndex.putAll(modified);
    for (StructuralDiff columnDiff : columnDiffs) {
        if (columnDiff.getDiffType() != null && columnDiff.getDiffType().equals(DiffTypeEnum.ADD)) {
            indices = new ArrayList<Integer>(this.renamedColumnsLabelsByIndex.keySet());
            Collections.sort(indices);
            Range beforePositionRange = columnDiff.getBeforePositionRange();
            Range afterPositionRange = columnDiff.getAfterPositionRange();
            Map<Integer, String> modifiedColumns = new TreeMap<Integer, String>();
            int beforeIndex = this.columnHeaderLayer.getColumnIndexByPosition(beforePositionRange.start);
            for (Integer column : indices) {
                if (column >= beforeIndex) {
                    modifiedColumns.put(column + (afterPositionRange.end - afterPositionRange.start), this.renamedColumnsLabelsByIndex.get(column));
                } else {
                    modifiedColumns.put(column, this.renamedColumnsLabelsByIndex.get(column));
                }
            }
            this.renamedColumnsLabelsByIndex.clear();
            this.renamedColumnsLabelsByIndex.putAll(modifiedColumns);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) StructuralDiff(org.eclipse.nebula.widgets.nattable.layer.event.StructuralDiff) Range(org.eclipse.nebula.widgets.nattable.coordinate.Range) TreeMap(java.util.TreeMap)

Example 10 with StructuralDiff

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

the class DataChangeLayer method handleLayerEvent.

@SuppressWarnings("unchecked")
@Override
public void handleLayerEvent(ILayerEvent event) {
    // updated and we remember the modifications via DataUpdateEvent
    if (!this.temporaryDataStorage && this.handleDataUpdateEvents && event instanceof DataUpdateEvent) {
        DataUpdateEvent updateEvent = (DataUpdateEvent) event;
        Object key = this.keyHandler.getKey(updateEvent.getColumnPosition(), updateEvent.getRowPosition());
        synchronized (this.dataChanges) {
            // this ensures that a discard really restores the original
            if (!this.dataChanges.containsKey(key)) {
                this.changedColumns.add(updateEvent.getColumnPosition());
                this.changedRows.add(updateEvent.getRowPosition());
                // store an UpdateDataCommand that can be used to revert the
                // change
                this.dataChanges.put(key, new UpdateDataCommand(this, updateEvent.getColumnPosition(), updateEvent.getRowPosition(), updateEvent.getOldValue()));
            } else if ((this.dataChanges.get(key).getNewValue() != null && this.dataChanges.get(key).getNewValue().equals(updateEvent.getNewValue()) || (this.dataChanges.get(key).getNewValue() == null && updateEvent.getNewValue() == null))) {
                // the value was changed back to the original value in
                // the underlying layer simply remove the local storage
                // to not showing the cell as dirty
                this.dataChanges.remove(this.keyHandler.getKey(updateEvent.getColumnPosition(), updateEvent.getRowPosition()));
                rebuildPositionCollections();
            }
        }
    } else if (event instanceof IStructuralChangeEvent) {
        IStructuralChangeEvent structuralChangeEvent = (IStructuralChangeEvent) event;
        if (structuralChangeEvent.getColumnDiffs() == null && structuralChangeEvent.getRowDiffs() == null && structuralChangeEvent.isHorizontalStructureChanged() && structuralChangeEvent.isVerticalStructureChanged()) {
            // Assume everything changed
            clearDataChanges();
        } else if (structuralChangeEvent.isHorizontalStructureChanged() && structuralChangeEvent.getColumnDiffs() != null) {
            if (this.keyHandler.updateOnHorizontalStructuralChange()) {
                Collection<StructuralDiff> structuralDiffs = structuralChangeEvent.getColumnDiffs();
                StructuralChangeEventHelper.handleColumnDelete(structuralDiffs, this.dataChanges, this.keyHandler);
                StructuralChangeEventHelper.handleColumnInsert(structuralDiffs, this.dataChanges, this.keyHandler);
            } else {
                removeChangesForDeletedObjects();
            }
        } else if (structuralChangeEvent.isVerticalStructureChanged() && structuralChangeEvent.getRowDiffs() != null) {
            if (this.keyHandler.updateOnVerticalStructuralChange()) {
                Collection<StructuralDiff> structuralDiffs = structuralChangeEvent.getRowDiffs();
                StructuralChangeEventHelper.handleRowDelete(structuralDiffs, this.dataChanges, this.keyHandler);
                StructuralChangeEventHelper.handleRowInsert(structuralDiffs, this.dataChanges, this.keyHandler);
            } else {
                removeChangesForDeletedObjects();
            }
        }
        rebuildPositionCollections();
    }
    super.handleLayerEvent(event);
}
Also used : IStructuralChangeEvent(org.eclipse.nebula.widgets.nattable.layer.event.IStructuralChangeEvent) StructuralDiff(org.eclipse.nebula.widgets.nattable.layer.event.StructuralDiff) DataUpdateEvent(org.eclipse.nebula.widgets.nattable.edit.event.DataUpdateEvent) UpdateDataCommand(org.eclipse.nebula.widgets.nattable.edit.command.UpdateDataCommand) Collection(java.util.Collection)

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