Search in sources :

Example 1 with DataUpdateEvent

use of org.eclipse.nebula.widgets.nattable.edit.event.DataUpdateEvent 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)

Example 2 with DataUpdateEvent

use of org.eclipse.nebula.widgets.nattable.edit.event.DataUpdateEvent in project nebula.widgets.nattable by eclipse.

the class DataChangeLayerTempStorageTest method shouldSaveChanges.

@Test
public void shouldSaveChanges() {
    assertEquals("Simpson", this.dataLayer.getDataValue(1, 1));
    this.dataChangeLayer.doCommand(new UpdateDataCommand(this.dataChangeLayer, 1, 1, "Lovejoy"));
    assertEquals("Simpson", this.dataLayer.getDataValue(1, 1));
    assertEquals("Lovejoy", this.dataChangeLayer.getDataValueByPosition(1, 1));
    this.dataChangeLayer.doCommand(new SaveDataChangesCommand());
    assertEquals("Lovejoy", this.dataLayer.getDataValue(1, 1));
    assertEquals("Lovejoy", this.dataChangeLayer.getDataValueByPosition(1, 1));
    assertFalse("Dirty label not set", this.dataChangeLayer.getConfigLabelsByPosition(1, 1).hasLabel(DataChangeLayer.DIRTY));
    assertFalse("Column 1 is not dirty", this.dataChangeLayer.isColumnDirty(1));
    assertFalse("Row 1 is not dirty", this.dataChangeLayer.isRowDirty(1));
    assertFalse("Cell is not dirty", this.dataChangeLayer.isCellDirty(1, 1));
    assertTrue("changed columns are not empty", this.dataChangeLayer.changedColumns.isEmpty());
    assertTrue("changed rows are not empty", this.dataChangeLayer.changedRows.isEmpty());
    assertTrue("changes are not empty", this.dataChangeLayer.dataChanges.isEmpty());
    assertEquals(1, this.listener.getReceivedEvents().size());
    assertTrue(this.listener.getReceivedEvents().get(0) instanceof DataUpdateEvent);
}
Also used : SaveDataChangesCommand(org.eclipse.nebula.widgets.nattable.datachange.command.SaveDataChangesCommand) UpdateDataCommand(org.eclipse.nebula.widgets.nattable.edit.command.UpdateDataCommand) DataUpdateEvent(org.eclipse.nebula.widgets.nattable.edit.event.DataUpdateEvent) Test(org.junit.Test)

Example 3 with DataUpdateEvent

use of org.eclipse.nebula.widgets.nattable.edit.event.DataUpdateEvent in project nebula.widgets.nattable by eclipse.

the class UpdateDataCommandHandler method doCommand.

@Override
protected boolean doCommand(UpdateDataCommand command) {
    try {
        int columnPosition = command.getColumnPosition();
        int rowPosition = command.getRowPosition();
        Object currentValue = this.dataLayer.getDataValueByPosition(columnPosition, rowPosition);
        if ((currentValue == null && command.getNewValue() != null) || (command.getNewValue() == null && currentValue != null) || (currentValue != null && command.getNewValue() != null && !currentValue.equals(command.getNewValue()))) {
            this.dataLayer.setDataValueByPosition(columnPosition, rowPosition, command.getNewValue());
            this.dataLayer.fireLayerEvent(new DataUpdateEvent(this.dataLayer, columnPosition, rowPosition, currentValue, command.getNewValue()));
        }
        return true;
    } catch (Exception e) {
        // $NON-NLS-1$
        LOG.error("Failed to update value to: " + command.getNewValue(), e);
        return false;
    }
}
Also used : DataUpdateEvent(org.eclipse.nebula.widgets.nattable.edit.event.DataUpdateEvent)

Aggregations

DataUpdateEvent (org.eclipse.nebula.widgets.nattable.edit.event.DataUpdateEvent)3 UpdateDataCommand (org.eclipse.nebula.widgets.nattable.edit.command.UpdateDataCommand)2 Collection (java.util.Collection)1 SaveDataChangesCommand (org.eclipse.nebula.widgets.nattable.datachange.command.SaveDataChangesCommand)1 IStructuralChangeEvent (org.eclipse.nebula.widgets.nattable.layer.event.IStructuralChangeEvent)1 StructuralDiff (org.eclipse.nebula.widgets.nattable.layer.event.StructuralDiff)1 Test (org.junit.Test)1