Search in sources :

Example 26 with ILayerCell

use of org.eclipse.nebula.widgets.nattable.layer.cell.ILayerCell in project nebula.widgets.nattable by eclipse.

the class EditController method editCell.

/**
 * Activates the edit mode for the given cell. Will determine whether the
 * editor should be opened inline or in a subdialog.
 *
 * @param cell
 *            The cell that should be put into the edit mode.
 * @param parent
 *            The parent Composite, needed for the creation of the editor
 *            control.
 * @param initialCanonicalValue
 *            The value that should be put to the activated editor control.
 *            Usually this value should be the same as calling
 *            <code>cell.getDataValue()</code>, but for the special case
 *            that an editor should be activated pressing a letter or digit
 *            key on the current selection, the initial value should be the
 *            Character representing that key.
 * @param configRegistry
 *            The {@link IConfigRegistry} containing the configuration of
 *            the current NatTable instance the command should be executed
 *            for. This is necessary because the edit controllers in the
 *            current architecture are not aware of the instance they are
 *            running in.
 */
public static void editCell(final ILayerCell cell, final Composite parent, Object initialCanonicalValue, final IConfigRegistry configRegistry) {
    try {
        // determine the position of the cell to put into edit mode
        Rectangle cellBounds = cell.getBounds();
        ILayer layer = cell.getLayer();
        int columnPosition = cell.getColumnPosition();
        int rowPosition = cell.getRowPosition();
        // read the configuration for the specified cell for
        // - which editor to use for that cell
        final List<String> configLabels = cell.getConfigLabels().getLabels();
        // check which editor to use
        final ICellEditor cellEditor = configRegistry.getConfigAttribute(EditConfigAttributes.CELL_EDITOR, DisplayMode.EDIT, configLabels);
        if (cellEditor.openInline(configRegistry, configLabels)) {
            // edit inline
            ICellEditHandler editHandler = new InlineEditHandler(layer, columnPosition, rowPosition);
            Rectangle editorBounds = layer.getLayerPainter().adjustCellBounds(columnPosition, rowPosition, new Rectangle(cellBounds.x, cellBounds.y, cellBounds.width, cellBounds.height));
            cellEditor.activateCell(parent, initialCanonicalValue, EditModeEnum.INLINE, editHandler, cell, configRegistry);
            final Control editorControl = cellEditor.getEditorControl();
            editorBounds = cellEditor.calculateControlBounds(editorBounds);
            // NatTableBorderOverlayPainter
            if (editorBounds.x == 0) {
                editorBounds.x += 1;
                editorBounds.width -= 1;
            }
            if (editorControl != null && !editorControl.isDisposed()) {
                editorControl.setBounds(editorBounds);
                // We need to add the control listeners after setting the
                // bounds to it because of the strange behaviour on Mac OS
                // where a control loses focus if its bounds are set
                cellEditor.addEditorControlListeners();
                layer.fireLayerEvent(new CellEditorCreatedEvent(cellEditor));
            }
        } else {
            List<ILayerCell> cells = new ArrayList<ILayerCell>();
            cells.add(cell);
            editCells(cells, parent, initialCanonicalValue, configRegistry);
        }
    } catch (Exception e) {
        if (cell == null) {
            // $NON-NLS-1$
            log.error("Cell being edited is no longer available. Initial value: " + initialCanonicalValue, e);
        } else {
            // $NON-NLS-1$ //$NON-NLS-2$
            log.error("Error while editing cell: Cell: " + cell + "; Initial value: " + initialCanonicalValue, e);
        }
    }
}
Also used : ILayer(org.eclipse.nebula.widgets.nattable.layer.ILayer) Rectangle(org.eclipse.swt.graphics.Rectangle) ArrayList(java.util.ArrayList) ILayerCell(org.eclipse.nebula.widgets.nattable.layer.cell.ILayerCell) Control(org.eclipse.swt.widgets.Control) ICellEditor(org.eclipse.nebula.widgets.nattable.edit.editor.ICellEditor)

Example 27 with ILayerCell

use of org.eclipse.nebula.widgets.nattable.layer.cell.ILayerCell in project nebula.widgets.nattable by eclipse.

the class InternalPasteDataCommandHandler method doCommand.

@Override
protected boolean doCommand(PasteDataCommand command) {
    if (this.clipboard.getCopiedCells() != null) {
        preInternalPaste();
        PositionCoordinate coord = this.selectionLayer.getSelectionAnchor();
        int pasteColumn = coord.getColumnPosition();
        int pasteRow = coord.getRowPosition();
        for (ILayerCell[] cells : this.clipboard.getCopiedCells()) {
            for (ILayerCell cell : cells) {
                if (EditUtils.isCellEditable(this.selectionLayer, command.configRegistry, new PositionCoordinate(this.selectionLayer, pasteColumn, pasteRow))) {
                    this.selectionLayer.doCommand(new UpdateDataCommand(this.selectionLayer, pasteColumn, pasteRow, getPasteValue(cell, pasteColumn, pasteRow)));
                }
                pasteColumn++;
                if (pasteColumn >= this.selectionLayer.getColumnCount()) {
                    break;
                }
            }
            pasteRow++;
            pasteColumn = coord.getColumnPosition();
        }
        postInternalPaste();
    }
    return true;
}
Also used : PositionCoordinate(org.eclipse.nebula.widgets.nattable.coordinate.PositionCoordinate) UpdateDataCommand(org.eclipse.nebula.widgets.nattable.edit.command.UpdateDataCommand) ILayerCell(org.eclipse.nebula.widgets.nattable.layer.cell.ILayerCell)

Example 28 with ILayerCell

use of org.eclipse.nebula.widgets.nattable.layer.cell.ILayerCell in project nebula.widgets.nattable by eclipse.

the class FillHandleLayerPainter method paintLayer.

@Override
public void paintLayer(ILayer natLayer, GC gc, int xOffset, int yOffset, Rectangle pixelRectangle, IConfigRegistry configRegistry) {
    Rectangle positionRectangle = getPositionRectangleFromPixelRectangle(natLayer, pixelRectangle);
    int columnPositionOffset = positionRectangle.x;
    int rowPositionOffset = positionRectangle.y;
    super.paintLayer(natLayer, gc, xOffset, yOffset, pixelRectangle, configRegistry);
    ILayerCell fillHandleCell = null;
    BorderCell[][] borderCells = new BorderCell[positionRectangle.height][positionRectangle.width];
    boolean atLeastOne = false;
    for (int columnPosition = columnPositionOffset, ix = 0; columnPosition < columnPositionOffset + positionRectangle.width; columnPosition++, ix++) {
        for (int rowPosition = rowPositionOffset, iy = 0; rowPosition < rowPositionOffset + positionRectangle.height; rowPosition++, iy++) {
            boolean insideBorder = false;
            Rectangle cellBounds = null;
            ILayerCell currentCell = natLayer.getCellByPosition(columnPosition, rowPosition);
            if (currentCell != null) {
                cellBounds = currentCell.getBounds();
                if (isFillHandleRegion(currentCell)) {
                    insideBorder = true;
                    atLeastOne = true;
                }
                if (fillHandleCell == null && isFillHandleCell(currentCell)) {
                    fillHandleCell = currentCell;
                }
            }
            Rectangle fixedBounds = fixBoundsInGridLines(cellBounds, xOffset, yOffset);
            BorderCell borderCell = new BorderCell(fixedBounds, insideBorder);
            borderCells[iy][ix] = borderCell;
        }
    }
    if (atLeastOne) {
        // Save gc settings
        int originalLineStyle = gc.getLineStyle();
        int originalLineWidth = gc.getLineWidth();
        Color originalForeground = gc.getForeground();
        BorderStyle fillHandleRegionBorderStyle = getHandleRegionBorderStyle(configRegistry);
        BorderPainter borderPainter = new BorderPainter(borderCells, fillHandleRegionBorderStyle);
        borderPainter.paintBorder(gc);
        // Restore original gc settings
        gc.setLineStyle(originalLineStyle);
        gc.setLineWidth(originalLineWidth);
        gc.setForeground(originalForeground);
    }
    // paint the border around the copied cells if a clipboard is set
    if (this.clipboard != null && this.clipboard.getCopiedCells() != null) {
        paintCopyBorder(natLayer, gc, xOffset, yOffset, pixelRectangle, configRegistry);
    }
    // to find it and eventually repaint it
    if (fillHandleCell == null && positionRectangle.width <= 2 && positionRectangle.height <= 2) {
        for (int columnPosition = columnPositionOffset - 1; columnPosition < columnPositionOffset + positionRectangle.width + 1 && fillHandleCell == null; columnPosition++) {
            for (int rowPosition = rowPositionOffset - 1; rowPosition < rowPositionOffset + positionRectangle.height + 1 && fillHandleCell == null; rowPosition++) {
                ILayerCell currentCell = natLayer.getCellByPosition(columnPosition, rowPosition);
                if (currentCell != null) {
                    if (isFillHandleCell(currentCell)) {
                        fillHandleCell = currentCell;
                    }
                }
            }
        }
    }
    if (fillHandleCell != null) {
        paintFillHandle(fillHandleCell, gc, xOffset, yOffset, configRegistry);
    } else {
        // set the local stored bounds to null as no handle is rendered and
        // therefore event matchers shouldn't react anymore
        this.handleBounds = null;
    }
}
Also used : BorderPainter(org.eclipse.nebula.widgets.nattable.painter.cell.BorderPainter) BorderStyle(org.eclipse.nebula.widgets.nattable.style.BorderStyle) Color(org.eclipse.swt.graphics.Color) Rectangle(org.eclipse.swt.graphics.Rectangle) ILayerCell(org.eclipse.nebula.widgets.nattable.layer.cell.ILayerCell) BorderCell(org.eclipse.nebula.widgets.nattable.painter.cell.BorderPainter.BorderCell)

Example 29 with ILayerCell

use of org.eclipse.nebula.widgets.nattable.layer.cell.ILayerCell in project nebula.widgets.nattable by eclipse.

the class CopySelectionLayerPainter method paintLayer.

@Override
public void paintLayer(ILayer natLayer, GC gc, int xOffset, int yOffset, Rectangle pixelRectangle, IConfigRegistry configRegistry) {
    super.paintLayer(natLayer, gc, xOffset, yOffset, pixelRectangle, configRegistry);
    if (this.clipboard.getCopiedCells() != null) {
        Rectangle positionRectangle = getPositionRectangleFromPixelRectangle(natLayer, pixelRectangle);
        int columnPositionOffset = positionRectangle.x;
        int rowPositionOffset = positionRectangle.y;
        // Save gc settings
        int originalLineStyle = gc.getLineStyle();
        Color originalForeground = gc.getForeground();
        applyCopyBorderStyle(gc, configRegistry);
        int x0 = 0;
        int x1 = 0;
        int y0 = 0;
        int y1 = 0;
        boolean isFirst = true;
        for (ILayerCell[] cells : this.clipboard.getCopiedCells()) {
            for (ILayerCell cell : cells) {
                if (isFirst) {
                    x0 = cell.getBounds().x;
                    x1 = cell.getBounds().x + cell.getBounds().width;
                    y0 = cell.getBounds().y;
                    y1 = cell.getBounds().y + cell.getBounds().height;
                    isFirst = false;
                } else {
                    x0 = Math.min(x0, cell.getBounds().x);
                    x1 = Math.max(x1, cell.getBounds().x + cell.getBounds().width);
                    y0 = Math.min(y0, cell.getBounds().y);
                    y1 = Math.max(y1, cell.getBounds().y + cell.getBounds().height);
                }
            }
        }
        x0 += xOffset - columnPositionOffset;
        x1 += xOffset - columnPositionOffset;
        y0 += yOffset - rowPositionOffset;
        y1 += yOffset - rowPositionOffset;
        gc.drawLine(x0, y0, x0, y1);
        gc.drawLine(x0, y0, x1, y0);
        gc.drawLine(x0, y1, x1, y1);
        gc.drawLine(x1, y0, x1, y1);
        // Restore original gc settings
        gc.setLineStyle(originalLineStyle);
        gc.setForeground(originalForeground);
    }
}
Also used : Color(org.eclipse.swt.graphics.Color) Rectangle(org.eclipse.swt.graphics.Rectangle) ILayerCell(org.eclipse.nebula.widgets.nattable.layer.cell.ILayerCell)

Example 30 with ILayerCell

use of org.eclipse.nebula.widgets.nattable.layer.cell.ILayerCell in project nebula.widgets.nattable by eclipse.

the class FillHandlePasteCommandHandler method doCommand.

@Override
public boolean doCommand(ILayer targetLayer, FillHandlePasteCommand command) {
    if (this.clipboard.getCopiedCells() != null) {
        int pasteColumn = -1;
        int pasteRow = -1;
        int pasteWidth = this.clipboard.getCopiedCells().length;
        int pasteHeight = this.clipboard.getCopiedCells()[0].length;
        Rectangle handleRegion = this.selectionLayer.getFillHandleRegion();
        if (handleRegion != null) {
            pasteColumn = handleRegion.x;
            pasteRow = handleRegion.y;
            pasteWidth = handleRegion.width;
            pasteHeight = handleRegion.height;
        } else {
            PositionCoordinate coord = this.selectionLayer.getSelectionAnchor();
            pasteColumn = coord.getColumnPosition();
            pasteRow = coord.getRowPosition();
        }
        int pasteStartColumn = pasteColumn;
        int rowStartAdjustment = 0;
        if (command.direction == MoveDirectionEnum.UP) {
            rowStartAdjustment = pasteHeight % this.clipboard.getCopiedCells().length;
        }
        int columnStartAdjustment = 0;
        if (command.direction == MoveDirectionEnum.LEFT) {
            columnStartAdjustment = pasteWidth % this.clipboard.getCopiedCells()[0].length;
        }
        for (int i = 0; i < pasteHeight; i++) {
            ILayerCell[] cells = this.clipboard.getCopiedCells()[(i + rowStartAdjustment) % this.clipboard.getCopiedCells().length];
            for (int j = 0; j < pasteWidth; j++) {
                ILayerCell cell = cells[(j + columnStartAdjustment) % this.clipboard.getCopiedCells()[0].length];
                Object cellValue = getPasteValue(cell, command, pasteColumn, pasteRow);
                if (EditUtils.isCellEditable(this.selectionLayer, command.configRegistry, new PositionCoordinate(this.selectionLayer, pasteColumn, pasteRow))) {
                    this.selectionLayer.doCommand(new UpdateDataCommand(this.selectionLayer, pasteColumn, pasteRow, cellValue));
                }
                pasteColumn++;
                if (pasteColumn >= this.selectionLayer.getColumnCount()) {
                    break;
                }
            }
            pasteRow++;
            pasteColumn = pasteStartColumn;
        }
    }
    return true;
}
Also used : Rectangle(org.eclipse.swt.graphics.Rectangle) PositionCoordinate(org.eclipse.nebula.widgets.nattable.coordinate.PositionCoordinate) UpdateDataCommand(org.eclipse.nebula.widgets.nattable.edit.command.UpdateDataCommand) ILayerCell(org.eclipse.nebula.widgets.nattable.layer.cell.ILayerCell)

Aggregations

ILayerCell (org.eclipse.nebula.widgets.nattable.layer.cell.ILayerCell)118 Test (org.junit.Test)45 Rectangle (org.eclipse.swt.graphics.Rectangle)23 DataLayer (org.eclipse.nebula.widgets.nattable.layer.DataLayer)14 SelectCellCommand (org.eclipse.nebula.widgets.nattable.selection.command.SelectCellCommand)14 IConfigRegistry (org.eclipse.nebula.widgets.nattable.config.IConfigRegistry)11 PositionCoordinate (org.eclipse.nebula.widgets.nattable.coordinate.PositionCoordinate)10 LabelStack (org.eclipse.nebula.widgets.nattable.layer.LabelStack)10 Color (org.eclipse.swt.graphics.Color)10 EditCellCommand (org.eclipse.nebula.widgets.nattable.edit.command.EditCellCommand)9 ICellPainter (org.eclipse.nebula.widgets.nattable.painter.cell.ICellPainter)9 DataProviderFixture (org.eclipse.nebula.widgets.nattable.test.fixture.data.DataProviderFixture)9 ILayer (org.eclipse.nebula.widgets.nattable.layer.ILayer)8 UpdateDataCommand (org.eclipse.nebula.widgets.nattable.edit.command.UpdateDataCommand)5 Point (org.eclipse.swt.graphics.Point)5 HashSet (java.util.HashSet)4 NatTable (org.eclipse.nebula.widgets.nattable.NatTable)4 DefaultNatTableStyleConfiguration (org.eclipse.nebula.widgets.nattable.config.DefaultNatTableStyleConfiguration)4 IEditableRule (org.eclipse.nebula.widgets.nattable.config.IEditableRule)4 ICellEditor (org.eclipse.nebula.widgets.nattable.edit.editor.ICellEditor)4