Search in sources :

Example 26 with UpdateDataCommand

use of org.eclipse.nebula.widgets.nattable.edit.command.UpdateDataCommand 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 27 with UpdateDataCommand

use of org.eclipse.nebula.widgets.nattable.edit.command.UpdateDataCommand in project nebula.widgets.nattable by eclipse.

the class DataChangeLayer method discardDataChanges.

/**
 * Discards the tracked data changes. In case temporary data storage is
 * disabled, the applied changes are undone by restoring the previous values
 * via dedicated {@link UpdateDataCommand}s.
 */
public void discardDataChanges() {
    if (!this.temporaryDataStorage) {
        // avoid handling of DataUpdateEvents that are caused by restoring
        // the previous data states
        this.handleDataUpdateEvents = false;
        // old values
        for (Map.Entry<Object, UpdateDataCommand> entry : this.dataChanges.entrySet()) {
            getUnderlyingLayer().doCommand(getUpdateDataCommand(entry.getKey(), entry.getValue()));
        }
        this.handleDataUpdateEvents = true;
    }
    clearDataChanges();
    fireLayerEvent(new VisualRefreshEvent(this));
}
Also used : VisualRefreshEvent(org.eclipse.nebula.widgets.nattable.layer.event.VisualRefreshEvent) UpdateDataCommand(org.eclipse.nebula.widgets.nattable.edit.command.UpdateDataCommand) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 28 with UpdateDataCommand

use of org.eclipse.nebula.widgets.nattable.edit.command.UpdateDataCommand in project nebula.widgets.nattable by eclipse.

the class DataChangeLayer method saveDataChanges.

/**
 * Saves the tracked data changes. In case temporary data storage is enabled
 * this means the underlying data model is updated. Otherwise the stored
 * data changes are simply cleared.
 * <p>
 * <b>Note:</b> In case temporary data storage is disabled and a custom save
 * operation should be performed on save, a custom
 * {@link SaveDataChangesCommandHandler} should be registered that first
 * performs a custom action and afterwards calls this method to ensure a
 * clear state in this layer.
 * </p>
 */
public void saveDataChanges() {
    if (this.temporaryDataStorage) {
        for (Map.Entry<Object, UpdateDataCommand> entry : this.dataChanges.entrySet()) {
            getUnderlyingLayer().doCommand(getUpdateDataCommand(entry.getKey(), entry.getValue()));
        }
    }
    clearDataChanges();
    fireLayerEvent(new VisualRefreshEvent(this));
}
Also used : VisualRefreshEvent(org.eclipse.nebula.widgets.nattable.layer.event.VisualRefreshEvent) UpdateDataCommand(org.eclipse.nebula.widgets.nattable.edit.command.UpdateDataCommand) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 29 with UpdateDataCommand

use of org.eclipse.nebula.widgets.nattable.edit.command.UpdateDataCommand in project nebula.widgets.nattable by eclipse.

the class InternalPasteDataCommandHandler method doCommand.

@Override
protected boolean doCommand(PasteDataCommand command) {
    if (this.clipboard.getCopiedCells() != null) {
        preInternalPaste();
        PositionCoordinate coord = this.selectionLayer.getSelectionAnchor();
        int pasteColumn = coord.getColumnPosition();
        int pasteRow = coord.getRowPosition();
        for (ILayerCell[] cells : this.clipboard.getCopiedCells()) {
            for (ILayerCell cell : cells) {
                if (EditUtils.isCellEditable(this.selectionLayer, command.configRegistry, new PositionCoordinate(this.selectionLayer, pasteColumn, pasteRow))) {
                    this.selectionLayer.doCommand(new UpdateDataCommand(this.selectionLayer, pasteColumn, pasteRow, getPasteValue(cell, pasteColumn, pasteRow)));
                }
                pasteColumn++;
                if (pasteColumn >= this.selectionLayer.getColumnCount()) {
                    break;
                }
            }
            pasteRow++;
            pasteColumn = coord.getColumnPosition();
        }
        postInternalPaste();
    }
    return true;
}
Also used : PositionCoordinate(org.eclipse.nebula.widgets.nattable.coordinate.PositionCoordinate) UpdateDataCommand(org.eclipse.nebula.widgets.nattable.edit.command.UpdateDataCommand) ILayerCell(org.eclipse.nebula.widgets.nattable.layer.cell.ILayerCell)

Example 30 with UpdateDataCommand

use of org.eclipse.nebula.widgets.nattable.edit.command.UpdateDataCommand in project nebula.widgets.nattable by eclipse.

the class ToggleCheckBoxColumnAction method run.

@Override
public void run(NatTable natTable, MouseEvent event) {
    int sourceColumnPosition = natTable.getColumnPositionByX(event.x);
    int columnPosition = LayerUtil.convertColumnPosition(natTable, sourceColumnPosition, this.bodyDataLayer);
    int checkedCellsCount = this.columnHeaderCheckBoxPainter.getCheckedCellsCount(columnPosition, natTable.getConfigRegistry());
    boolean targetState = checkedCellsCount < this.bodyDataLayer.getRowCount();
    for (int rowPosition = 0; rowPosition < this.bodyDataLayer.getRowCount(); rowPosition++) {
        this.bodyDataLayer.doCommand(new UpdateDataCommand(this.bodyDataLayer, columnPosition, rowPosition, targetState));
    }
}
Also used : UpdateDataCommand(org.eclipse.nebula.widgets.nattable.edit.command.UpdateDataCommand)

Aggregations

UpdateDataCommand (org.eclipse.nebula.widgets.nattable.edit.command.UpdateDataCommand)63 Test (org.junit.Test)52 RowDeleteEvent (org.eclipse.nebula.widgets.nattable.layer.event.RowDeleteEvent)10 DiscardDataChangesCommand (org.eclipse.nebula.widgets.nattable.datachange.command.DiscardDataChangesCommand)7 SaveDataChangesCommand (org.eclipse.nebula.widgets.nattable.datachange.command.SaveDataChangesCommand)6 Person (org.eclipse.nebula.widgets.nattable.dataset.person.Person)6 ILayerCell (org.eclipse.nebula.widgets.nattable.layer.cell.ILayerCell)5 Date (java.util.Date)4 Range (org.eclipse.nebula.widgets.nattable.coordinate.Range)4 ColumnDeleteEvent (org.eclipse.nebula.widgets.nattable.layer.event.ColumnDeleteEvent)4 RowInsertEvent (org.eclipse.nebula.widgets.nattable.layer.event.RowInsertEvent)4 StructuralRefreshCommand (org.eclipse.nebula.widgets.nattable.command.StructuralRefreshCommand)3 Map (java.util.Map)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 IConfigRegistry (org.eclipse.nebula.widgets.nattable.config.IConfigRegistry)2 PositionCoordinate (org.eclipse.nebula.widgets.nattable.coordinate.PositionCoordinate)2 DataUpdateEvent (org.eclipse.nebula.widgets.nattable.edit.event.DataUpdateEvent)2 ClearAllFiltersCommand (org.eclipse.nebula.widgets.nattable.filterrow.command.ClearAllFiltersCommand)2 ClearFilterCommand (org.eclipse.nebula.widgets.nattable.filterrow.command.ClearFilterCommand)2 CellVisualChangeEvent (org.eclipse.nebula.widgets.nattable.layer.event.CellVisualChangeEvent)2