Search in sources :

Example 1 with PaintModeEnum

use of org.eclipse.nebula.widgets.nattable.painter.cell.BorderPainter.PaintModeEnum in project nebula.widgets.nattable by eclipse.

the class SelectionLayerPainter 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);
    Rectangle positionRectangle = getPositionRectangleFromPixelRectangle(natLayer, pixelRectangle);
    int columnPositionOffset = positionRectangle.x;
    int rowPositionOffset = positionRectangle.y;
    // nothing to draw, we exit
    if (positionRectangle.width <= 0 || positionRectangle.height <= 0) {
        return;
    }
    BorderCell[][] borderCells;
    boolean atLeastOne = false;
    PaintModeEnum paintMode;
    // tentative way to know that this is a single cell update
    if (positionRectangle.width <= 2 && positionRectangle.height <= 2) {
        // In order to correctly paint the selection borders in case of
        // single cell updates we need to consider also the adjacent cells.
        // Therefore we try to retrieve also cells that are outside the
        // pixelRectangle but still inside our layer.
        // +2 because we are going to read also adjacent cells in the
        // extremities
        borderCells = new BorderCell[positionRectangle.height + 2][positionRectangle.width + 2];
        // we need to repaint only the internal borders of the external
        // cells
        paintMode = PaintModeEnum.NO_EXTERNAL_BORDERS;
        // extremities
        for (int columnPosition = columnPositionOffset - 1, ix = 0; columnPosition < columnPositionOffset + positionRectangle.width + 1; columnPosition++, ix++) {
            for (int rowPosition = rowPositionOffset - 1, iy = 0; rowPosition < rowPositionOffset + positionRectangle.height + 1; rowPosition++, iy++) {
                boolean insideBorder = false;
                Rectangle cellBounds = null;
                ILayerCell currentCell = natLayer.getCellByPosition(columnPosition, rowPosition);
                if (currentCell != null) {
                    cellBounds = currentCell.getBounds();
                    // the cell should be considered only if it is in our
                    // layer
                    boolean toBeConsidered = isInCurrentLayer(ix, iy, xOffset, yOffset, cellBounds, borderCells);
                    if (toBeConsidered && isSelected(currentCell)) {
                        insideBorder = true;
                        atLeastOne = true;
                    }
                }
                Rectangle fixedBounds = fixBoundsInGridLines(cellBounds, xOffset, yOffset);
                BorderCell borderCell = new BorderCell(fixedBounds, insideBorder);
                borderCells[iy][ix] = borderCell;
            }
        }
    } else {
        borderCells = new BorderCell[positionRectangle.height][positionRectangle.width];
        paintMode = PaintModeEnum.ALL;
        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) {
                    // In case of spanned cells the border painter needs to
                    // know the bounds of adjacent cells even if they are
                    // not selected. This is the reason why we get the
                    // bounds also for non selected cells.
                    cellBounds = currentCell.getBounds();
                    if (isSelected(currentCell)) {
                        insideBorder = true;
                        atLeastOne = true;
                    }
                }
                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 borderStyle = getBorderStyle(configRegistry);
        BorderPainter borderPainter = new BorderPainter(borderCells, borderStyle, paintMode);
        borderPainter.paintBorder(gc);
        // Restore original gc settings
        gc.setLineStyle(originalLineStyle);
        gc.setLineWidth(originalLineWidth);
        gc.setForeground(originalForeground);
    }
}
Also used : BorderPainter(org.eclipse.nebula.widgets.nattable.painter.cell.BorderPainter) PaintModeEnum(org.eclipse.nebula.widgets.nattable.painter.cell.BorderPainter.PaintModeEnum) 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)

Aggregations

ILayerCell (org.eclipse.nebula.widgets.nattable.layer.cell.ILayerCell)1 BorderPainter (org.eclipse.nebula.widgets.nattable.painter.cell.BorderPainter)1 BorderCell (org.eclipse.nebula.widgets.nattable.painter.cell.BorderPainter.BorderCell)1 PaintModeEnum (org.eclipse.nebula.widgets.nattable.painter.cell.BorderPainter.PaintModeEnum)1 BorderStyle (org.eclipse.nebula.widgets.nattable.style.BorderStyle)1 Color (org.eclipse.swt.graphics.Color)1 Rectangle (org.eclipse.swt.graphics.Rectangle)1