Search in sources :

Example 96 with Range

use of org.eclipse.nebula.widgets.nattable.coordinate.Range in project nebula.widgets.nattable by eclipse.

the class ShowRowPositionsEvent method getRowDiffs.

@Override
public Collection<StructuralDiff> getRowDiffs() {
    Collection<StructuralDiff> rowDiffs = new ArrayList<StructuralDiff>(getRowPositionRanges().size());
    int offset = 0;
    for (Range range : getRowPositionRanges()) {
        rowDiffs.add(new StructuralDiff(DiffTypeEnum.ADD, new Range(range.start - offset, range.start - offset), range));
        offset += range.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 97 with Range

use of org.eclipse.nebula.widgets.nattable.coordinate.Range in project nebula.widgets.nattable by eclipse.

the class RowStructuralChangeEvent method getChangedPositionRectangles.

@Override
public Collection<Rectangle> getChangedPositionRectangles() {
    Collection<Rectangle> changedPositionRectangles = new ArrayList<Rectangle>(getRowPositionRanges().size());
    int columnCount = getLayer().getColumnCount();
    int rowCount = getLayer().getRowCount();
    for (Range range : getRowPositionRanges()) {
        changedPositionRectangles.add(new Rectangle(0, range.start, columnCount, rowCount - range.start));
    }
    return changedPositionRectangles;
}
Also used : Rectangle(org.eclipse.swt.graphics.Rectangle) ArrayList(java.util.ArrayList) Range(org.eclipse.nebula.widgets.nattable.coordinate.Range)

Example 98 with Range

use of org.eclipse.nebula.widgets.nattable.coordinate.Range in project nebula.widgets.nattable by eclipse.

the class RowVisualChangeEvent method getChangedPositionRectangles.

@Override
public Collection<Rectangle> getChangedPositionRectangles() {
    Collection<Rectangle> changedPositionRectangles = new ArrayList<Rectangle>(this.rowPositionRanges.size());
    int columnCount = this.layer.getColumnCount();
    for (Range range : this.rowPositionRanges) {
        changedPositionRectangles.add(new Rectangle(0, range.start, columnCount, range.end - range.start));
    }
    return changedPositionRectangles;
}
Also used : Rectangle(org.eclipse.swt.graphics.Rectangle) ArrayList(java.util.ArrayList) Range(org.eclipse.nebula.widgets.nattable.coordinate.Range)

Example 99 with Range

use of org.eclipse.nebula.widgets.nattable.coordinate.Range in project nebula.widgets.nattable by eclipse.

the class StructuralChangeEventHelper method handleRowDelete.

/**
 * Will check for events that indicate that rows have been deleted. In that
 * case the given cached dataChanges need to be updated because the index of
 * the rows might have changed. E.g. cell with row at index 3 is changed in
 * the given layer, deleting row at index 1 will cause the row at index 3 to
 * be moved to index 2. Without transforming the index regarding the delete
 * event, the wrong cell at the incorrect row would be shown as changed.
 *
 * @param rowDiffs
 *            The collection of {@link StructuralDiff}s to handle.
 * @param dataChanges
 *            The map that contains the data changes identified by a key
 *            that should be updated.
 * @param keyHandler
 *            the {@link CellKeyHandler} that needs to be used to operate on
 *            the dataChanges correctly.
 *
 * @since 1.6
 */
public static <K, T> void handleRowDelete(Collection<StructuralDiff> rowDiffs, Map<K, T> dataChanges, CellKeyHandler<K> keyHandler) {
    // for correct calculation the diffs need to be processed from lowest
    // position to highest
    List<StructuralDiff> diffs = new ArrayList<StructuralDiff>(rowDiffs);
    Collections.sort(diffs, new Comparator<StructuralDiff>() {

        @Override
        public int compare(StructuralDiff o1, StructuralDiff o2) {
            return o1.getBeforePositionRange().start - o2.getBeforePositionRange().start;
        }
    });
    List<Integer> toRemove = new ArrayList<Integer>();
    for (StructuralDiff rowDiff : diffs) {
        if (rowDiff.getDiffType() != null && rowDiff.getDiffType().equals(DiffTypeEnum.DELETE)) {
            Range beforePositionRange = rowDiff.getBeforePositionRange();
            for (int i = beforePositionRange.start; i < beforePositionRange.end; i++) {
                int index = i;
                if (index >= 0) {
                    toRemove.add(index);
                }
            }
        }
    }
    // modify row indexes regarding the deleted rows
    Map<K, T> modifiedRows = new HashMap<K, T>();
    for (Map.Entry<K, T> entry : dataChanges.entrySet()) {
        int columnIndex = keyHandler.getColumnIndex(entry.getKey());
        int rowIndex = keyHandler.getRowIndex(entry.getKey());
        if (!toRemove.contains(rowIndex)) {
            // check number of removed indexes that are lower than the
            // current one
            int deletedBefore = 0;
            for (Integer removed : toRemove) {
                if (removed < rowIndex) {
                    deletedBefore++;
                }
            }
            int modRow = rowIndex - deletedBefore;
            if (modRow >= 0) {
                modifiedRows.put(keyHandler.getKey(columnIndex, modRow), entry.getValue());
            }
        }
    }
    dataChanges.clear();
    dataChanges.putAll(modifiedRows);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Range(org.eclipse.nebula.widgets.nattable.coordinate.Range) Map(java.util.Map) HashMap(java.util.HashMap)

Example 100 with Range

use of org.eclipse.nebula.widgets.nattable.coordinate.Range in project nebula.widgets.nattable by eclipse.

the class StructuralChangeEventHelper method handleRowInsert.

/**
 * Will check for events that indicate that rows are added. In that case the
 * given cached indexes need to be updated because the index of the rows
 * might have changed. E.g. Row with index 3 is hidden in the given layer,
 * adding a row at index 1 will cause the row at index 3 to be moved to
 * index 4. Without transforming the index regarding the add event, the
 * wrong row would be hidden.
 *
 * @param rowDiffs
 *            The collection of {@link StructuralDiff}s to handle
 * @param underlyingLayer
 *            The underlying layer of the layer who caches the indexes.
 *            Needed to translate the transported row positions to indexes,
 *            because the conversion to the layer who caches the index is
 *            done before it is fired further in the layer stack
 * @param cachedRowIndexes
 *            The collection of indexes that is cached by the layer that
 *            needs transformation
 * @param addToCache
 *            Flag to configure if the added value should be added to the
 *            cache or not. This is necessary to differ whether
 *            cachedRowIndexes are a collection of all indexes that need to
 *            be updated (e.g. row reordering) or just a collection of
 *            indexes that are applied for a specific state (e.g. row hide
 *            state)
 */
public static void handleRowInsert(Collection<StructuralDiff> rowDiffs, ILayer underlyingLayer, Collection<Integer> cachedRowIndexes, boolean addToCache) {
    for (StructuralDiff rowDiff : rowDiffs) {
        if (rowDiff.getDiffType() != null && rowDiff.getDiffType().equals(DiffTypeEnum.ADD)) {
            Range beforePositionRange = rowDiff.getBeforePositionRange();
            List<Integer> modifiedRows = new ArrayList<Integer>();
            int beforeIndex = underlyingLayer.getRowIndexByPosition(beforePositionRange.start);
            for (Integer row : cachedRowIndexes) {
                if (row >= beforeIndex) {
                    modifiedRows.add(row + 1);
                } else {
                    modifiedRows.add(row);
                }
            }
            if (addToCache) {
                modifiedRows.add(beforeIndex, beforePositionRange.start);
            }
            cachedRowIndexes.clear();
            cachedRowIndexes.addAll(modifiedRows);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) Range(org.eclipse.nebula.widgets.nattable.coordinate.Range)

Aggregations

Range (org.eclipse.nebula.widgets.nattable.coordinate.Range)115 Test (org.junit.Test)54 ArrayList (java.util.ArrayList)40 StructuralDiff (org.eclipse.nebula.widgets.nattable.layer.event.StructuralDiff)23 HashSet (java.util.HashSet)12 Rectangle (org.eclipse.swt.graphics.Rectangle)12 RowDeleteEvent (org.eclipse.nebula.widgets.nattable.layer.event.RowDeleteEvent)9 RowSelectionEvent (org.eclipse.nebula.widgets.nattable.selection.event.RowSelectionEvent)9 NatTableFixture (org.eclipse.nebula.widgets.nattable.test.fixture.NatTableFixture)7 HashMap (java.util.HashMap)6 Map (java.util.Map)6 NatTable (org.eclipse.nebula.widgets.nattable.NatTable)6 RenameColumnHeaderCommand (org.eclipse.nebula.widgets.nattable.columnRename.RenameColumnHeaderCommand)6 HideRowPositionsEvent (org.eclipse.nebula.widgets.nattable.hideshow.event.HideRowPositionsEvent)6 ColumnResizeEvent (org.eclipse.nebula.widgets.nattable.resize.event.ColumnResizeEvent)6 ShowRowPositionsEvent (org.eclipse.nebula.widgets.nattable.hideshow.event.ShowRowPositionsEvent)5 ColumnReorderEvent (org.eclipse.nebula.widgets.nattable.reorder.event.ColumnReorderEvent)5 SelectRowsCommand (org.eclipse.nebula.widgets.nattable.selection.command.SelectRowsCommand)5 Point (org.eclipse.swt.graphics.Point)5 ListDataProvider (org.eclipse.nebula.widgets.nattable.data.ListDataProvider)4