Search in sources :

Example 36 with Range

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

the class StructuralChangeEventHelper method handleColumnDelete.

/**
 * Will check for events that indicate that columns has been deleted. In
 * that case the given cached indexes for the given layer need to be updated
 * because the index of the columns might have changed. E.g. Column with
 * index 3 is hidden in the given layer, deleting column at index 1 will
 * cause the column at index 3 to be moved at index 2. Without transforming
 * the index regarding the delete event, the wrong column would be hidden.
 *
 * @param columnDiffs
 *            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 column 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 cachedColumnIndexes
 *            The collection of indexes that is cached by the layer that
 *            needs transformation
 * @param handleNotFound
 *            flag to tell whether the not found column indexes should be
 *            taken into account or not. Needed for last column checks
 */
public static void handleColumnDelete(Collection<StructuralDiff> columnDiffs, ILayer underlyingLayer, Collection<Integer> cachedColumnIndexes, boolean handleNotFound) {
    // the number of all deleted columns that don't have a corresponding
    // index anymore (last column cases)
    int numberOfNoIndex = 0;
    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++) {
                // underlyingLayer.getColumnIndexByPosition(i);
                int index = i;
                if (index >= 0) {
                    toRemove.add(index);
                } else {
                    numberOfNoIndex++;
                }
            }
        }
    }
    // remove the column indexes that are deleted
    cachedColumnIndexes.removeAll(toRemove);
    // modify column indexes regarding the deleted columns
    List<Integer> modifiedColumns = new ArrayList<Integer>();
    for (Integer column : cachedColumnIndexes) {
        // check number of removed indexes that are lower than the current
        // one
        int deletedBefore = handleNotFound ? numberOfNoIndex : 0;
        for (Integer removed : toRemove) {
            if (removed < column) {
                deletedBefore++;
            }
        }
        int modColumn = column - deletedBefore;
        if (modColumn >= 0) {
            modifiedColumns.add(modColumn);
        }
    }
    cachedColumnIndexes.clear();
    cachedColumnIndexes.addAll(modifiedColumns);
}
Also used : ArrayList(java.util.ArrayList) Range(org.eclipse.nebula.widgets.nattable.coordinate.Range)

Example 37 with Range

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

the class StructuralChangeEventHelper method handleColumnInsert.

/**
 * Will check for events that indicate that columns are added. In that case
 * the given cached dataChanges need to be updated because the index of the
 * columns might have changed. E.g. column with index 3 is hidden in the
 * given layer, adding a column at index 1 will cause the column at index 3
 * to be moved to index 4. Without transforming the index regarding the add
 * event, the wrong column would be hidden.
 *
 * @param columnDiffs
 *            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 handleColumnInsert(Collection<StructuralDiff> columnDiffs, Map<K, T> dataChanges, CellKeyHandler<K> keyHandler) {
    // for correct calculation the diffs need to be processed from highest
    // position to lowest
    List<StructuralDiff> diffs = new ArrayList<StructuralDiff>(columnDiffs);
    Collections.sort(diffs, new Comparator<StructuralDiff>() {

        @Override
        public int compare(StructuralDiff o1, StructuralDiff o2) {
            return o2.getBeforePositionRange().start - o1.getBeforePositionRange().start;
        }
    });
    for (StructuralDiff columnDiff : diffs) {
        if (columnDiff.getDiffType() != null && columnDiff.getDiffType().equals(DiffTypeEnum.ADD)) {
            Range beforePositionRange = columnDiff.getBeforePositionRange();
            // modify column indexes regarding the inserted columns
            Map<K, T> modifiedColumns = 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 (columnIndex >= beforePositionRange.start) {
                    modifiedColumns.put(keyHandler.getKey(columnIndex + 1, rowIndex), entry.getValue());
                } else {
                    modifiedColumns.put(keyHandler.getKey(columnIndex, rowIndex), entry.getValue());
                }
            }
            dataChanges.clear();
            dataChanges.putAll(modifiedColumns);
        }
    }
}
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 38 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 dataChanges 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 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 handleRowInsert(Collection<StructuralDiff> rowDiffs, Map<K, T> dataChanges, CellKeyHandler<K> keyHandler) {
    // for correct calculation the diffs need to be processed from highest
    // position to lowest
    List<StructuralDiff> diffs = new ArrayList<StructuralDiff>(rowDiffs);
    Collections.sort(diffs, new Comparator<StructuralDiff>() {

        @Override
        public int compare(StructuralDiff o1, StructuralDiff o2) {
            return o2.getBeforePositionRange().start - o1.getBeforePositionRange().start;
        }
    });
    for (StructuralDiff rowDiff : diffs) {
        if (rowDiff.getDiffType() != null && rowDiff.getDiffType().equals(DiffTypeEnum.ADD)) {
            Range beforePositionRange = rowDiff.getBeforePositionRange();
            // modify row indexes regarding the inserted 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 (rowIndex >= beforePositionRange.start) {
                    modifiedRows.put(keyHandler.getKey(columnIndex, rowIndex + 1), entry.getValue());
                } else {
                    modifiedRows.put(keyHandler.getKey(columnIndex, rowIndex), 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 39 with Range

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

the class StructuralChangeEventHelper method handleColumnDelete.

/**
 * Will check for events that indicate that columns have been deleted. In
 * that case the given cached dataChanges need to be updated because the
 * index of the columns might have changed. E.g. cell with column at index 3
 * is changed in the given layer, deleting column at index 1 will cause the
 * column at index 3 to be moved to index 2. Without transforming the index
 * regarding the delete event, the wrong cell at the incorrect column would
 * be shown as changed.
 *
 * @param columnDiffs
 *            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 handleColumnDelete(Collection<StructuralDiff> columnDiffs, 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>(columnDiffs);
    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 columnDiff : diffs) {
        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);
                }
            }
        }
    }
    // modify column indexes regarding the deleted column
    Map<K, T> modifiedColumns = 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(columnIndex)) {
            // check number of removed indexes that are lower than the
            // current one
            int deletedBefore = 0;
            for (Integer removed : toRemove) {
                if (removed < columnIndex) {
                    deletedBefore++;
                }
            }
            int modColumn = columnIndex - deletedBefore;
            if (modColumn >= 0) {
                modifiedColumns.put(keyHandler.getKey(modColumn, rowIndex), entry.getValue());
            }
        }
    }
    dataChanges.clear();
    dataChanges.putAll(modifiedColumns);
}
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 40 with Range

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

the class ColumnReorderEvent method getColumnDiffs.

@Override
public Collection<StructuralDiff> getColumnDiffs() {
    Collection<StructuralDiff> columnDiffs = new ArrayList<StructuralDiff>();
    Collection<Range> beforeFromColumnPositionRanges = getBeforeFromColumnPositionRanges();
    final int beforeToColumnPosition = (this.reorderToLeftEdge) ? this.beforeToColumnPosition : (this.beforeToColumnPosition + 1);
    int afterAddColumnPosition = beforeToColumnPosition;
    for (Range beforeFromColumnPositionRange : beforeFromColumnPositionRanges) {
        if (beforeFromColumnPositionRange.start < beforeToColumnPosition) {
            afterAddColumnPosition -= Math.min(beforeFromColumnPositionRange.end, beforeToColumnPosition) - beforeFromColumnPositionRange.start;
        } else {
            break;
        }
    }
    int cumulativeAddSize = 0;
    for (Range beforeFromColumnPositionRange : beforeFromColumnPositionRanges) {
        cumulativeAddSize += beforeFromColumnPositionRange.size();
    }
    int offset = 0;
    for (Range beforeFromColumnPositionRange : beforeFromColumnPositionRanges) {
        int afterDeleteColumnPosition = beforeFromColumnPositionRange.start - offset;
        if (afterAddColumnPosition < afterDeleteColumnPosition) {
            afterDeleteColumnPosition += cumulativeAddSize;
        }
        columnDiffs.add(new StructuralDiff(DiffTypeEnum.DELETE, beforeFromColumnPositionRange, new Range(afterDeleteColumnPosition, afterDeleteColumnPosition)));
        offset += beforeFromColumnPositionRange.size();
    }
    Range beforeAddRange = new Range(beforeToColumnPosition, beforeToColumnPosition);
    offset = 0;
    for (Range beforeFromColumnPositionRange : beforeFromColumnPositionRanges) {
        int size = beforeFromColumnPositionRange.size();
        columnDiffs.add(new StructuralDiff(DiffTypeEnum.ADD, beforeAddRange, new Range(afterAddColumnPosition + offset, afterAddColumnPosition + offset + size)));
        offset += size;
    }
    return columnDiffs;
}
Also used : StructuralDiff(org.eclipse.nebula.widgets.nattable.layer.event.StructuralDiff) 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