Search in sources :

Example 51 with Range

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

the class RowSelectionModel method getSelectedRowPositions.

@Override
public Set<Range> getSelectedRowPositions() {
    Set<Range> selectedRowRanges = new HashSet<Range>();
    this.selectionsLock.readLock().lock();
    try {
        for (Serializable rowId : this.selectedRows.keySet()) {
            int rowPosition = getRowPositionById(rowId);
            selectedRowRanges.add(new Range(rowPosition, rowPosition + 1));
        }
    } finally {
        this.selectionsLock.readLock().unlock();
    }
    return selectedRowRanges;
}
Also used : Serializable(java.io.Serializable) Range(org.eclipse.nebula.widgets.nattable.coordinate.Range) HashSet(java.util.HashSet)

Example 52 with Range

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

the class RowSelectionModel method handleLayerEvent.

@Override
public void handleLayerEvent(IStructuralChangeEvent event) {
    // handling for deleting rows
    if (event.isVerticalStructureChanged()) {
        // the change is already done and we don't know about indexes, so we
        // need to check if the selected objects still exist
        Collection<Serializable> keysToRemove = new ArrayList<Serializable>();
        for (Map.Entry<Serializable, R> entry : this.selectedRows.entrySet()) {
            int rowIndex = this.rowDataProvider.indexOfRowObject(entry.getValue());
            if (rowIndex == -1) {
                keysToRemove.add(entry.getKey());
            }
        }
        this.selectionsLock.readLock().lock();
        try {
            for (Serializable toRemove : keysToRemove) {
                this.selectedRows.remove(toRemove);
            }
        } finally {
            this.selectionsLock.readLock().unlock();
        }
        // change for all deleted rows
        if (!keysToRemove.isEmpty()) {
            Collection<Integer> rowPositions = new HashSet<Integer>();
            Collection<StructuralDiff> diffs = event.getRowDiffs();
            if (diffs != null) {
                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++) {
                            rowPositions.add(i);
                        }
                    }
                }
            }
            // if there is no diff in the event we assume everything has
            // changed, in such a case we are not able to fire an
            // appropriate event the layer stack upwards since it will be
            // stopped while converting it to the target layer
            // for the RowSelectionProvider this is sufficient because it
            // registers itself as a listener to the SelectionLayer and
            // therefore gets informed about the selection change
            this.selectionLayer.fireLayerEvent(new RowSelectionEvent(this.selectionLayer, rowPositions, -1, false, false));
        }
    }
}
Also used : Serializable(java.io.Serializable) ArrayList(java.util.ArrayList) Range(org.eclipse.nebula.widgets.nattable.coordinate.Range) RowSelectionEvent(org.eclipse.nebula.widgets.nattable.selection.event.RowSelectionEvent) StructuralDiff(org.eclipse.nebula.widgets.nattable.layer.event.StructuralDiff) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 53 with Range

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

the class SelectionUtils method getSelectedRowObjects.

/**
 * Inspects the current selection on the given {@link SelectionLayer} and
 * returns a list of the corresponding list item objects. Uses the
 * {@link IRowDataProvider} to be able to determine the row objects per
 * selected row position.
 *
 * @param selectionLayer
 *            The {@link SelectionLayer} to retrieve the selected row
 *            indexes from.
 * @param rowDataProvider
 *            The {@link IRowDataProvider} to retrieve the object for the
 *            row index.
 * @param fullySelectedRowsOnly
 *            Flag to determine if only fully selected rows should be taken
 *            into account.
 * @return The list of all objects that are currently marked as selected.
 *         Never <code>null</code>.
 *
 * @since 1.4
 */
public static <T> List<T> getSelectedRowObjects(SelectionLayer selectionLayer, IRowDataProvider<T> rowDataProvider, boolean fullySelectedRowsOnly) {
    List<RowObjectIndexHolder<T>> rows = new ArrayList<RowObjectIndexHolder<T>>();
    if (selectionLayer != null) {
        if (fullySelectedRowsOnly) {
            for (int rowPosition : selectionLayer.getFullySelectedRowPositions()) {
                addToSelection(rows, rowPosition, selectionLayer, rowDataProvider);
            }
        } else {
            Set<Range> rowRanges = selectionLayer.getSelectedRowPositions();
            for (Range rowRange : rowRanges) {
                for (int rowPosition = rowRange.start; rowPosition < rowRange.end; rowPosition++) {
                    addToSelection(rows, rowPosition, selectionLayer, rowDataProvider);
                }
            }
        }
    }
    Collections.sort(rows);
    List<T> rowObjects = new ArrayList<T>(rows.size());
    for (RowObjectIndexHolder<T> holder : rows) {
        rowObjects.add(holder.getRow());
    }
    return rowObjects;
}
Also used : ArrayList(java.util.ArrayList) Range(org.eclipse.nebula.widgets.nattable.coordinate.Range)

Example 54 with Range

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

the class PreserveSelectionModel method getSelectedRowPositions.

@Override
public Set<Range> getSelectedRowPositions() {
    HashSet<Range> visiblySelectedRowPositions = new HashSet<Range>();
    this.selectionsLock.readLock().lock();
    try {
        for (Selections.Row<T> row : this.selections.getRows()) {
            int rowPosition = getRowPositionByRowObject(row.getRowObject());
            if (isRowVisible(rowPosition)) {
                visiblySelectedRowPositions.add(new Range(rowPosition, rowPosition + 1));
            }
        }
    } finally {
        this.selectionsLock.readLock().unlock();
    }
    return visiblySelectedRowPositions;
}
Also used : Range(org.eclipse.nebula.widgets.nattable.coordinate.Range) Point(org.eclipse.swt.graphics.Point) HashSet(java.util.HashSet)

Example 55 with Range

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

the class ViewportLayer method processColumnSelection.

/**
 * Handle {@link ColumnSelectionEvent}
 *
 * @param selectionEvent
 *            The event to handle
 */
private void processColumnSelection(ColumnSelectionEvent selectionEvent) {
    for (Range columnPositionRange : selectionEvent.getColumnPositionRanges()) {
        moveColumnPositionIntoViewport(columnPositionRange.end - 1);
        adjustHorizontalScrollBar();
    }
}
Also used : 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