Search in sources :

Example 81 with PositionCoordinate

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

the class CellSelectionTest method onlyOneCellSelectedAtAnyTime.

@Test
public void onlyOneCellSelectedAtAnyTime() {
    this.selectionLayer.getSelectionModel().setMultipleSelectionAllowed(false);
    this.selectionLayer.clear();
    this.selectionLayer.doCommand(new SelectCellCommand(this.selectionLayer, 1, 0, false, true));
    Collection<PositionCoordinate> cells = ArrayUtil.asCollection(this.selectionLayer.getSelectedCellPositions());
    Assert.assertEquals(1, cells.size());
    Assert.assertTrue(cells.contains(new PositionCoordinate(this.selectionLayer, 1, 0)));
    // select another cell with control mask
    this.selectionLayer.doCommand(new SelectCellCommand(this.selectionLayer, 2, 0, false, true));
    cells = ArrayUtil.asCollection(this.selectionLayer.getSelectedCellPositions());
    Assert.assertEquals(1, cells.size());
    Assert.assertTrue(cells.contains(new PositionCoordinate(this.selectionLayer, 2, 0)));
    // select additional cells with shift mask
    // only the first cell should be selected afterwards
    this.selectionLayer.doCommand(new SelectCellCommand(this.selectionLayer, 2, 10, true, false));
    cells = ArrayUtil.asCollection(this.selectionLayer.getSelectedCellPositions());
    Assert.assertEquals(1, cells.size());
    Assert.assertTrue(cells.contains(new PositionCoordinate(this.selectionLayer, 2, 0)));
    // select additional cells with shift mask
    // only the first cell should be selected afterwards
    this.selectionLayer.doCommand(new SelectCellCommand(this.selectionLayer, 10, 0, true, false));
    cells = ArrayUtil.asCollection(this.selectionLayer.getSelectedCellPositions());
    Assert.assertEquals(1, cells.size());
    Assert.assertTrue(cells.contains(new PositionCoordinate(this.selectionLayer, 2, 0)));
}
Also used : SelectCellCommand(org.eclipse.nebula.widgets.nattable.selection.command.SelectCellCommand) PositionCoordinate(org.eclipse.nebula.widgets.nattable.coordinate.PositionCoordinate) Test(org.junit.Test)

Example 82 with PositionCoordinate

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

the class CellSelectionTest method shouldReturnSixCells.

@Test
public void shouldReturnSixCells() {
    this.selectionLayer.clear();
    this.selectionLayer.doCommand(new SelectCellCommand(this.selectionLayer, 1, 0, false, true));
    this.selectionLayer.doCommand(new SelectCellCommand(this.selectionLayer, 2, 0, false, true));
    this.selectionLayer.doCommand(new SelectCellCommand(this.selectionLayer, 1, 1, false, true));
    this.selectionLayer.doCommand(new SelectCellCommand(this.selectionLayer, 2, 1, false, true));
    this.selectionLayer.doCommand(new SelectCellCommand(this.selectionLayer, 1, 2, false, true));
    this.selectionLayer.doCommand(new SelectCellCommand(this.selectionLayer, 2, 2, false, true));
    Collection<PositionCoordinate> cells = ArrayUtil.asCollection(this.selectionLayer.getSelectedCellPositions());
    Assert.assertEquals(6, cells.size());
    // (1, 0)
    Assert.assertTrue(cells.contains(new PositionCoordinate(this.selectionLayer, 1, 0)));
    // (1, 1)
    Assert.assertTrue(cells.contains(new PositionCoordinate(this.selectionLayer, 1, 1)));
    // (1, 2)
    Assert.assertTrue(cells.contains(new PositionCoordinate(this.selectionLayer, 1, 2)));
    // (2, 0)
    Assert.assertTrue(cells.contains(new PositionCoordinate(this.selectionLayer, 2, 0)));
    // (2, 1)
    Assert.assertTrue(cells.contains(new PositionCoordinate(this.selectionLayer, 2, 1)));
    // (2, 2)
    Assert.assertTrue(cells.contains(new PositionCoordinate(this.selectionLayer, 2, 2)));
}
Also used : SelectCellCommand(org.eclipse.nebula.widgets.nattable.selection.command.SelectCellCommand) PositionCoordinate(org.eclipse.nebula.widgets.nattable.coordinate.PositionCoordinate) Test(org.junit.Test)

Example 83 with PositionCoordinate

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

the class LayerCommandUtil method convertPositionToTargetContext.

public static PositionCoordinate convertPositionToTargetContext(PositionCoordinate positionCoordinate, ILayer targetLayer) {
    ILayer layer = positionCoordinate.getLayer();
    if (layer == targetLayer) {
        return positionCoordinate;
    }
    int columnPosition = positionCoordinate.getColumnPosition();
    int underlyingColumnPosition = layer.localToUnderlyingColumnPosition(columnPosition);
    if (underlyingColumnPosition < 0) {
        return null;
    }
    int rowPosition = positionCoordinate.getRowPosition();
    int underlyingRowPosition = layer.localToUnderlyingRowPosition(rowPosition);
    if (underlyingRowPosition < 0) {
        return null;
    }
    ILayer underlyingLayer = layer.getUnderlyingLayerByPosition(columnPosition, rowPosition);
    if (underlyingLayer == null) {
        return null;
    }
    return convertPositionToTargetContext(new PositionCoordinate(underlyingLayer, underlyingColumnPosition, underlyingRowPosition), targetLayer);
}
Also used : ILayer(org.eclipse.nebula.widgets.nattable.layer.ILayer) RowPositionCoordinate(org.eclipse.nebula.widgets.nattable.coordinate.RowPositionCoordinate) ColumnPositionCoordinate(org.eclipse.nebula.widgets.nattable.coordinate.ColumnPositionCoordinate) PositionCoordinate(org.eclipse.nebula.widgets.nattable.coordinate.PositionCoordinate)

Example 84 with PositionCoordinate

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

the class EditSelectionCommandHandler method doCommand.

@Override
public boolean doCommand(EditSelectionCommand command) {
    Composite parent = command.getParent();
    IConfigRegistry configRegistry = command.getConfigRegistry();
    Character initialValue = command.getCharacter();
    if (EditUtils.allCellsEditable(this.selectionLayer, configRegistry) && EditUtils.isEditorSame(this.selectionLayer, configRegistry) && EditUtils.isConverterSame(this.selectionLayer, configRegistry) && EditUtils.activateLastSelectedCellEditor(this.selectionLayer, configRegistry, command.isByTraversal())) {
        // check how many cells are selected
        Collection<ILayerCell> selectedCells = EditUtils.getSelectedCellsForEditing(this.selectionLayer);
        if (selectedCells.size() == 1) {
            // editing is triggered by key for a single cell
            // we need to fire the InlineCellEditEvent here because we
            // don't know the correct bounds of the cell to edit inline
            // corresponding to the NatTable.
            // On firing the event, a translation process is triggered,
            // converting the information to the correct values
            // needed for inline editing
            ILayerCell cell = selectedCells.iterator().next();
            this.selectionLayer.fireLayerEvent(new InlineCellEditEvent(this.selectionLayer, new PositionCoordinate(this.selectionLayer, cell.getOriginColumnPosition(), cell.getOriginRowPosition()), parent, configRegistry, (initialValue != null ? initialValue : cell.getDataValue())));
        } else if (selectedCells.size() > 1) {
            // determine the initial value
            Object initialEditValue = initialValue;
            if (initialValue == null && EditUtils.isValueSame(this.selectionLayer)) {
                ILayerCell cell = selectedCells.iterator().next();
                initialEditValue = this.selectionLayer.getDataValueByPosition(cell.getColumnPosition(), cell.getRowPosition());
            }
            EditController.editCells(selectedCells, parent, initialEditValue, configRegistry);
        }
    }
    // successful or not
    return true;
}
Also used : Composite(org.eclipse.swt.widgets.Composite) IConfigRegistry(org.eclipse.nebula.widgets.nattable.config.IConfigRegistry) PositionCoordinate(org.eclipse.nebula.widgets.nattable.coordinate.PositionCoordinate) InlineCellEditEvent(org.eclipse.nebula.widgets.nattable.edit.event.InlineCellEditEvent) ILayerCell(org.eclipse.nebula.widgets.nattable.layer.cell.ILayerCell)

Example 85 with PositionCoordinate

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

the class FreezeEventHandler method handleLayerEvent.

@Override
public void handleLayerEvent(IStructuralChangeEvent event) {
    PositionCoordinate topLeftPosition = this.freezeLayer.getTopLeftPosition();
    PositionCoordinate bottomRightPosition = this.freezeLayer.getBottomRightPosition();
    Collection<StructuralDiff> columnDiffs = event.getColumnDiffs();
    if (columnDiffs != null) {
        int leftOffset = 0;
        int rightOffset = 0;
        for (StructuralDiff columnDiff : columnDiffs) {
            switch(columnDiff.getDiffType()) {
                case ADD:
                    Range afterPositionRange = columnDiff.getAfterPositionRange();
                    if (afterPositionRange.start < topLeftPosition.columnPosition) {
                        leftOffset += afterPositionRange.size();
                    }
                    if (afterPositionRange.start <= bottomRightPosition.columnPosition) {
                        rightOffset += afterPositionRange.size();
                    }
                    break;
                case DELETE:
                    Range beforePositionRange = columnDiff.getBeforePositionRange();
                    if (beforePositionRange.start < topLeftPosition.columnPosition) {
                        leftOffset -= Math.min(beforePositionRange.end, topLeftPosition.columnPosition + 1) - beforePositionRange.start;
                    }
                    if (beforePositionRange.start <= bottomRightPosition.columnPosition) {
                        rightOffset -= Math.min(beforePositionRange.end, bottomRightPosition.columnPosition + 1) - beforePositionRange.start;
                    }
                    break;
            }
        }
        topLeftPosition.columnPosition += leftOffset;
        bottomRightPosition.columnPosition += rightOffset;
    }
    Collection<StructuralDiff> rowDiffs = event.getRowDiffs();
    if (rowDiffs != null) {
        int leftOffset = 0;
        int rightOffset = 0;
        for (StructuralDiff rowDiff : rowDiffs) {
            switch(rowDiff.getDiffType()) {
                case ADD:
                    Range afterPositionRange = rowDiff.getAfterPositionRange();
                    if (afterPositionRange.start < topLeftPosition.rowPosition) {
                        leftOffset += afterPositionRange.size();
                    }
                    if (afterPositionRange.start <= bottomRightPosition.rowPosition) {
                        rightOffset += afterPositionRange.size();
                    }
                    break;
                case DELETE:
                    Range beforePositionRange = rowDiff.getBeforePositionRange();
                    if (beforePositionRange.start < topLeftPosition.rowPosition) {
                        leftOffset -= Math.min(beforePositionRange.end, topLeftPosition.rowPosition + 1) - beforePositionRange.start;
                    }
                    if (beforePositionRange.start <= bottomRightPosition.rowPosition) {
                        rightOffset -= Math.min(beforePositionRange.end, bottomRightPosition.rowPosition + 1) - beforePositionRange.start;
                    }
                    break;
            }
        }
        topLeftPosition.rowPosition += leftOffset;
        bottomRightPosition.rowPosition += rightOffset;
    }
}
Also used : PositionCoordinate(org.eclipse.nebula.widgets.nattable.coordinate.PositionCoordinate) StructuralDiff(org.eclipse.nebula.widgets.nattable.layer.event.StructuralDiff) Range(org.eclipse.nebula.widgets.nattable.coordinate.Range)

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