Search in sources :

Example 41 with ILayerCell

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

the class MaxCellBoundsHelper method getPreferredRowHeight.

private static int getPreferredRowHeight(ILayer layer, int rowPosition, IConfigRegistry configRegistry, GC gc) {
    int maxHeight = 0;
    ICellPainter painter;
    ILayerCell cell;
    for (int columnPosition = 0; columnPosition < layer.getColumnCount(); columnPosition++) {
        cell = layer.getCellByPosition(columnPosition, rowPosition);
        if (cell != null) {
            boolean atEndOfCellSpan = cell.getOriginRowPosition() + cell.getRowSpan() - 1 == rowPosition;
            if (atEndOfCellSpan) {
                painter = layer.getCellPainter(cell.getColumnPosition(), cell.getRowPosition(), cell, configRegistry);
                if (painter != null) {
                    int preferredHeight = painter.getPreferredHeight(cell, gc, configRegistry);
                    // Adjust height
                    Rectangle bounds = cell.getBounds();
                    bounds.height = preferredHeight;
                    Rectangle adjustedCellBounds = cell.getLayer().getLayerPainter().adjustCellBounds(columnPosition, rowPosition, bounds);
                    preferredHeight += preferredHeight - adjustedCellBounds.height;
                    if (cell.getColumnSpan() > 1) {
                        int rowStartY = layer.getStartYOfRowPosition(rowPosition);
                        int cellStartY = layer.getStartYOfRowPosition(cell.getOriginRowPosition());
                        preferredHeight = Math.max(0, preferredHeight - (rowStartY - cellStartY));
                    }
                    maxHeight = (preferredHeight > maxHeight) ? preferredHeight : maxHeight;
                }
            }
        }
    }
    return maxHeight;
}
Also used : Rectangle(org.eclipse.swt.graphics.Rectangle) ILayerCell(org.eclipse.nebula.widgets.nattable.layer.cell.ILayerCell) ICellPainter(org.eclipse.nebula.widgets.nattable.painter.cell.ICellPainter)

Example 42 with ILayerCell

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

the class LayerPrinter method getPageCount.

/**
 * Calculates number of horizontal and vertical pages needed to print the
 * entire layer of the given print target.
 *
 * @param target
 *            The print target to print.
 * @param printer
 *            The printer that will be used.
 * @param available
 *            The remaining available space in pixel on a page after the
 *            target is printed in case the print targets should be glued,
 *            -1 otherwise.
 * @param prevScaleFactor
 *            The scale factor of the previous table in case the tables
 *            should be joined. Needed to calculate the available space
 *            correctly
 * @return The number of horizontal and vertical pages that are needed to
 *         print the layer of the given print target and the remaining space
 *         on a page if the print targets should be glued.
 */
private int[] getPageCount(PrintTarget target, Printer printer, int available, float[] prevScaleFactor) {
    Rectangle printArea = computePrintArea(printer);
    float[] scaleFactor = computeLayerScaleFactor(target.layer, printer);
    Integer[] gridLineWidth = getGridLineWidth(target.configRegistry);
    // calculate pages based on non cut off columns/rows
    int numOfHorizontalPages = 0;
    int pageWidth = Math.round(Float.valueOf(printArea.width) / scaleFactor[0]);
    int endX = 0;
    while (endX < target.layer.getWidth()) {
        endX += pageWidth;
        int colPos = target.layer.getColumnPositionByX(endX);
        if (colPos >= 0) {
            ILayerCell cell = findColumnCellForBounds(target.layer, colPos);
            if (cell != null) {
                Rectangle cellBounds = cell.getBounds();
                if (cellBounds.x < endX) {
                    endX -= (endX - cellBounds.x);
                }
            }
        } else {
            endX = target.layer.getWidth();
        }
        numOfHorizontalPages++;
    }
    int numOfVerticalPages = 0;
    // if we are ourself the print target that should be repeated, we don't
    // need to consider the repeat print target height
    int repeatPrintTargetHeightInDpi = target.repeat ? 0 : Math.round(Float.valueOf(getRepeatPrintTargetHeight()) * getRepeatPrintTargetScaleFactor(printer)[1]);
    int headerHeightInDpi = (target.repeatHeaderLayer != null) ? Math.round(Float.valueOf(target.repeatHeaderLayer.getHeight()) * scaleFactor[1]) : 0;
    int pageHeight = Math.round(Float.valueOf((printArea.height - repeatPrintTargetHeightInDpi - headerHeightInDpi - getFooterHeightInPrinterDPI()) / scaleFactor[1]));
    int firstPageHeight = (available < 0) ? Math.round(Float.valueOf((printArea.height - repeatPrintTargetHeightInDpi - getFooterHeightInPrinterDPI()) / scaleFactor[1])) : Math.round(Float.valueOf(available) * prevScaleFactor[1] / scaleFactor[1]);
    int endY = 0;
    int added = 0;
    int remaining = -1;
    while (endY < target.layer.getHeight()) {
        // on the first page we don't need to take care of the repeat
        // header height
        added = (numOfVerticalPages == 0) ? firstPageHeight : pageHeight;
        endY += added;
        int rowPos = target.layer.getRowPositionByY(endY);
        if (rowPos >= 0) {
            ILayerCell cell = findRowCellForBounds(target.layer, rowPos);
            if (cell != null) {
                Rectangle cellBounds = cell.getBounds();
                if (cellBounds.y < endY) {
                    endY -= (endY - cellBounds.y);
                }
            }
        } else {
            // calculate the remaining space for the following print target
            if (this.join) {
                remaining = ((numOfVerticalPages == 0) ? firstPageHeight : pageHeight) - (target.layer.getHeight() - (endY - added));
            }
            endY = target.layer.getHeight();
        }
        numOfVerticalPages++;
    }
    if (gridLineWidth[0] == null) {
        target.configRegistry.unregisterConfigAttribute(CellConfigAttributes.GRID_LINE_WIDTH);
    }
    return new int[] { numOfHorizontalPages, numOfVerticalPages, remaining };
}
Also used : Rectangle(org.eclipse.swt.graphics.Rectangle) ILayerCell(org.eclipse.nebula.widgets.nattable.layer.cell.ILayerCell) Point(org.eclipse.swt.graphics.Point)

Example 43 with ILayerCell

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

the class SelectCellCommandHandler method toggleCell.

/**
 * Toggles the selection state of the given row and column.
 */
protected void toggleCell(int columnPosition, int rowPosition, boolean withShiftMask, boolean withControlMask, boolean forcingEntireCellIntoViewport) {
    boolean selectCell = true;
    if (isControlOnly(withShiftMask, withControlMask)) {
        if (this.selectionLayer.isCellPositionSelected(columnPosition, rowPosition)) {
            ILayerCell cell = this.selectionLayer.getCellByPosition(columnPosition, rowPosition);
            Rectangle cellRect = new Rectangle(cell.getOriginColumnPosition(), cell.getOriginRowPosition(), cell.getColumnSpan(), cell.getRowSpan());
            this.selectionLayer.clearSelection(cellRect);
            selectCell = false;
        }
    }
    if (selectCell) {
        selectCell(columnPosition, rowPosition, withShiftMask, withControlMask);
    }
}
Also used : Rectangle(org.eclipse.swt.graphics.Rectangle) ILayerCell(org.eclipse.nebula.widgets.nattable.layer.cell.ILayerCell)

Example 44 with ILayerCell

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

the class SelectionLayer method getConfigLabelsByPosition.

@Override
public LabelStack getConfigLabelsByPosition(int columnPosition, int rowPosition) {
    LabelStack labelStack = super.getConfigLabelsByPosition(columnPosition, rowPosition);
    ILayerCell cell = getCellByPosition(columnPosition, rowPosition);
    if (cell != null) {
        Rectangle cellRectangle = new Rectangle(cell.getOriginColumnPosition(), cell.getOriginRowPosition(), cell.getColumnSpan(), cell.getRowSpan());
        if (cellRectangle.contains(getSelectionAnchor().columnPosition, getSelectionAnchor().rowPosition)) {
            labelStack.addLabelOnTop(SelectionStyleLabels.SELECTION_ANCHOR_STYLE);
        }
        if (this.bottomRightInSelection != null && cellRectangle.contains(this.bottomRightInSelection.columnPosition, this.bottomRightInSelection.rowPosition)) {
            labelStack.addLabel(SelectionStyleLabels.FILL_HANDLE_CELL);
        }
    }
    if (this.fillHandleRegion != null && this.fillHandleRegion.contains(cell.getColumnIndex(), cell.getRowIndex())) {
        labelStack.addLabel(SelectionStyleLabels.FILL_HANDLE_REGION);
    }
    return labelStack;
}
Also used : LabelStack(org.eclipse.nebula.widgets.nattable.layer.LabelStack) Rectangle(org.eclipse.swt.graphics.Rectangle) ILayerCell(org.eclipse.nebula.widgets.nattable.layer.cell.ILayerCell)

Example 45 with ILayerCell

use of org.eclipse.nebula.widgets.nattable.layer.cell.ILayerCell 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)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