Search in sources :

Example 91 with PositionCoordinate

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

the class CellDisplayValueSearchUtil method findCell.

/**
 * Finds the first matching cell in a list of cells.
 *
 * @param layer
 * @param configRegistry
 * @param cellsToSearch
 * @param valueToMatch
 * @param comparator
 * @param caseSensitive
 * @param wholeWord
 * @param regex
 * @param includeCollapsed
 *            TODO currently ignored
 * @return
 * @throws PatternSyntaxException
 */
static PositionCoordinate findCell(final ILayer layer, final IConfigRegistry configRegistry, final PositionCoordinate[] cellsToSearch, final Object valueToMatch, final Comparator<String> comparator, final boolean caseSensitive, final boolean wholeWord, final boolean regex, final boolean includeCollapsed) throws PatternSyntaxException {
    String stringValue = caseSensitive ? valueToMatch.toString() : valueToMatch.toString().toLowerCase();
    Pattern pattern = regex ? Pattern.compile(stringValue) : null;
    for (int cellIndex = 0; cellIndex < cellsToSearch.length; cellIndex++) {
        final PositionCoordinate cellCoordinate = cellsToSearch[cellIndex];
        if (compare(layer, configRegistry, pattern, stringValue, comparator, caseSensitive, wholeWord, regex, cellCoordinate.columnPosition, cellCoordinate.rowPosition)) {
            return cellCoordinate;
        }
    }
    return null;
}
Also used : Pattern(java.util.regex.Pattern) PositionCoordinate(org.eclipse.nebula.widgets.nattable.coordinate.PositionCoordinate)

Example 92 with PositionCoordinate

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

the class SelectionSearchStrategy method getSelectedCells.

protected PositionCoordinate[] getSelectedCells(SelectionLayer selectionLayer) {
    PositionCoordinate[] selectedCells = null;
    PositionCoordinate selectionAnchor = selectionLayer.getSelectionAnchor();
    if ((selectionAnchor.columnPosition == SelectionLayer.NO_SELECTION && selectionAnchor.rowPosition == SelectionLayer.NO_SELECTION) && !ISearchDirection.SEARCH_BACKWARDS.equals(this.searchDirection)) {
        selectedCells = selectionLayer.getSelectedCellPositions();
    } else {
        List<PositionCoordinate> coordinates = Arrays.asList(selectionLayer.getSelectedCellPositions());
        if (this.searchDirection.equals(ISearchDirection.SEARCH_BACKWARDS)) {
            Collections.reverse(coordinates);
        }
        // find the selection anchor index in the collection
        int index = coordinates.indexOf(selectionAnchor);
        // reorder to make the selection anchor the first element in the
        // list
        List<PositionCoordinate> reordered = new ArrayList<PositionCoordinate>(coordinates.subList(index + 1, coordinates.size()));
        if (this.wrapSearch) {
            reordered.addAll(coordinates.subList(0, index + 1));
        }
        selectedCells = reordered.toArray(new PositionCoordinate[0]);
    }
    return selectedCells;
}
Also used : PositionCoordinate(org.eclipse.nebula.widgets.nattable.coordinate.PositionCoordinate) ArrayList(java.util.ArrayList)

Example 93 with PositionCoordinate

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

the class SelectRegionCommandHandler method selectRegionWithCtrlKey.

/**
 * Selects a region with CTRL modifier enabled. That means the current
 * selection is extended by the given region.
 *
 * @param region
 *            The region to be selected.
 * @param anchorColumn
 *            The column position to which the selection anchor should be
 *            moved to or -1 if the calculated anchor column position should
 *            be used.
 * @param anchorRow
 *            The row position to which the selection anchor should be moved
 *            to or -1 if the calculated anchor row position should be used.
 * @return The row positions that have gained selection.
 */
protected Range selectRegionWithCtrlKey(Rectangle region, int anchorColumn, int anchorRow) {
    if (this.selectionLayer.allCellsSelectedInRegion(region)) {
        // clear if all cells in the region are selected
        this.selectionLayer.clearSelection(region);
        this.selectionLayer.setLastSelectedRegion(null);
        // update anchor
        PositionCoordinate[] selectedCells = this.selectionLayer.getSelectedCellPositions();
        if (selectedCells.length > 0 && this.selectionLayer.getSelectionAnchor().columnPosition == SelectionLayer.NO_SELECTION && this.selectionLayer.getSelectionAnchor().rowPosition == SelectionLayer.NO_SELECTION) {
            // determine column to move the anchor to
            // for this we sort the coordinates in a deterministic way
            Arrays.sort(selectedCells, new PositionCoordinateComparator());
            // if another cell in the region.x column is selected, only
            // search for a new anchor in that column
            PositionCoordinate toPos = null;
            if (this.selectionLayer.isColumnPositionSelected(region.x)) {
                for (int i = 0; i < selectedCells.length; i++) {
                    if (selectedCells[i].rowPosition < region.y && selectedCells[i].columnPosition == region.x) {
                        toPos = selectedCells[i];
                    } else {
                        break;
                    }
                }
            }
            if (toPos == null && this.selectionLayer.isRowPositionSelected(region.y)) {
                for (int i = 0; i < selectedCells.length; i++) {
                    if (selectedCells[i].rowPosition == region.y && selectedCells[i].columnPosition < region.x) {
                        toPos = selectedCells[i];
                    } else {
                        break;
                    }
                }
            }
            // none in the same column
            if (toPos == null) {
                toPos = selectedCells[0];
                for (int i = 0; i < selectedCells.length; i++) {
                    if (selectedCells[i].rowPosition < region.y || selectedCells[i].columnPosition < region.x) {
                        toPos = selectedCells[i];
                    } else {
                        break;
                    }
                }
            }
            this.selectionLayer.moveSelectionAnchor(anchorColumn < 0 ? toPos.columnPosition : anchorColumn, anchorRow < 0 ? toPos.rowPosition : anchorRow);
        }
    } else {
        // if none or at least one cell in the region is already
        // selected, simply add
        this.selectionLayer.selectRegion(region.x, region.y, region.width, region.height);
        this.selectionLayer.moveSelectionAnchor(anchorColumn < 0 ? region.x : anchorColumn, anchorRow < 0 ? region.y : anchorRow);
    }
    return new Range(region.y, (region.height < Integer.MAX_VALUE) ? region.y + region.height : this.selectionLayer.getRowCount() - region.y);
}
Also used : PositionCoordinate(org.eclipse.nebula.widgets.nattable.coordinate.PositionCoordinate) Range(org.eclipse.nebula.widgets.nattable.coordinate.Range)

Example 94 with PositionCoordinate

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

the class SelectRegionCommandHandler method selectRegionWithShiftKey.

/**
 * Selects a region with SHIFT modifier enabled. That means the selection
 * range is calculated based on the current selection anchor and the corner
 * of the given region that is most away from the anchor.
 *
 * @param region
 *            The region to be selected.
 * @param anchorColumn
 *            The column position to which the selection anchor should be
 *            moved to or -1 if the calculated anchor column position should
 *            be used.
 * @param anchorRow
 *            The row position to which the selection anchor should be moved
 *            to or -1 if the calculated anchor row position should be used.
 * @return The row positions that have gained selection.
 */
protected Range selectRegionWithShiftKey(Rectangle region, int anchorColumn, int anchorRow) {
    int startCol = region.x;
    int startRow = region.y;
    int noCol = region.width;
    int noRow = region.height;
    // This method selects the range based on the selection anchor and the
    // clicked position. Therefore the selection prior adding the newly
    // calculated selection needs to be cleared in advance.
    Rectangle lastSelectedRegion = this.selectionLayer.getLastSelectedRegion();
    if (lastSelectedRegion != null) {
        this.selectionLayer.getSelectionModel().clearSelection(lastSelectedRegion);
    } else {
        this.selectionLayer.getSelectionModel().clearSelection();
    }
    PositionCoordinate anchor = this.selectionLayer.getSelectionAnchor();
    if (anchor.columnPosition != SelectionLayer.NO_SELECTION && anchor.rowPosition != SelectionLayer.NO_SELECTION) {
        // if the region.width is Integer.MAX_VALUE we do not calculate
        if (region.width < Integer.MAX_VALUE) {
            if (startCol < anchor.columnPosition) {
                noCol = Math.abs(anchor.columnPosition - startCol) + 1;
            } else {
                startCol = anchor.columnPosition;
                noCol = (region.x + region.width) - anchor.columnPosition;
            }
        }
        // if the region.height is Integer.MAX_VALUE we do not calculate
        if (region.height < Integer.MAX_VALUE) {
            if (startRow < anchor.rowPosition) {
                noRow = Math.abs(anchor.rowPosition - startRow) + 1;
            } else {
                startRow = anchor.rowPosition;
                noRow = (region.y + region.height) - anchor.rowPosition;
            }
        }
    } else {
        // if there is no last selected region we need to set the anchor
        // for correct behavior on further actions
        this.selectionLayer.moveSelectionAnchor(anchorColumn < 0 ? startCol : anchorColumn, anchorRow < 0 ? startRow : anchorRow);
    }
    this.selectionLayer.selectRegion(startCol, startRow, noCol, noRow);
    return new Range(startRow, (noRow < Integer.MAX_VALUE) ? startRow + noRow : this.selectionLayer.getRowCount() - startRow);
}
Also used : Rectangle(org.eclipse.swt.graphics.Rectangle) PositionCoordinate(org.eclipse.nebula.widgets.nattable.coordinate.PositionCoordinate) Range(org.eclipse.nebula.widgets.nattable.coordinate.Range)

Example 95 with PositionCoordinate

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

the class SelectionLayer method getSelectedCells.

/**
 * Retrieves the ILayerCells out of the SelectionLayer that are currently
 * marked as selected in the SelectionModel. Takes spanning into account.
 *
 * @return The selected ILayerCells
 */
public Collection<ILayerCell> getSelectedCells() {
    Set<ILayerCell> selectedCells = new HashSet<ILayerCell>();
    PositionCoordinate[] selectedCoords = getSelectedCellPositions();
    for (PositionCoordinate coord : selectedCoords) {
        selectedCells.add(getCellByPosition(coord.columnPosition, coord.rowPosition));
    }
    return selectedCells;
}
Also used : PositionCoordinate(org.eclipse.nebula.widgets.nattable.coordinate.PositionCoordinate) ILayerCell(org.eclipse.nebula.widgets.nattable.layer.cell.ILayerCell) HashSet(java.util.HashSet)

Aggregations

PositionCoordinate (org.eclipse.nebula.widgets.nattable.coordinate.PositionCoordinate)97 Test (org.junit.Test)55 SelectCellCommand (org.eclipse.nebula.widgets.nattable.selection.command.SelectCellCommand)15 ILayerCell (org.eclipse.nebula.widgets.nattable.layer.cell.ILayerCell)10 Rectangle (org.eclipse.swt.graphics.Rectangle)9 SelectRegionCommand (org.eclipse.nebula.widgets.nattable.selection.command.SelectRegionCommand)7 ArrayList (java.util.ArrayList)5 ILayer (org.eclipse.nebula.widgets.nattable.layer.ILayer)5 SelectionLayer (org.eclipse.nebula.widgets.nattable.selection.SelectionLayer)5 Range (org.eclipse.nebula.widgets.nattable.coordinate.Range)4 ViewportSelectColumnGroupCommand (org.eclipse.nebula.widgets.nattable.group.command.ViewportSelectColumnGroupCommand)4 SearchEvent (org.eclipse.nebula.widgets.nattable.search.event.SearchEvent)4 SelectAllCommand (org.eclipse.nebula.widgets.nattable.selection.command.SelectAllCommand)4 ILayerListener (org.eclipse.nebula.widgets.nattable.layer.ILayerListener)3 HashSet (java.util.HashSet)2 Pattern (java.util.regex.Pattern)2 IConfigRegistry (org.eclipse.nebula.widgets.nattable.config.IConfigRegistry)2 ColumnPositionCoordinate (org.eclipse.nebula.widgets.nattable.coordinate.ColumnPositionCoordinate)2 RowPositionCoordinate (org.eclipse.nebula.widgets.nattable.coordinate.RowPositionCoordinate)2 UpdateDataCommand (org.eclipse.nebula.widgets.nattable.edit.command.UpdateDataCommand)2