| /*=============================================================================# |
| # Copyright (c) 2012, 2021 Original NatTable authors and others. |
| # |
| # This program and the accompanying materials are made available under the |
| # terms of the Eclipse Public License 2.0 which is available at |
| # https://www.eclipse.org/legal/epl-2.0. |
| # |
| # SPDX-License-Identifier: EPL-2.0 |
| # |
| # Contributors: |
| # Original NatTable authors and others - initial API and implementation |
| #=============================================================================*/ |
| |
| package org.eclipse.statet.ecommons.waltable.painter.layer; |
| |
| import static org.eclipse.statet.ecommons.waltable.coordinate.Orientation.HORIZONTAL; |
| import static org.eclipse.statet.ecommons.waltable.coordinate.Orientation.VERTICAL; |
| import static org.eclipse.statet.ecommons.waltable.painter.cell.GraphicsUtils.safe; |
| |
| import org.eclipse.swt.graphics.Color; |
| import org.eclipse.swt.graphics.GC; |
| |
| import org.eclipse.statet.ecommons.waltable.NatTable; |
| import org.eclipse.statet.ecommons.waltable.config.IConfigRegistry; |
| import org.eclipse.statet.ecommons.waltable.layer.ILayer; |
| import org.eclipse.statet.ecommons.waltable.layer.ILayerDim; |
| import org.eclipse.statet.ecommons.waltable.util.GUIHelper; |
| |
| |
| public class NatGridLayerPainter extends NatLayerPainter { |
| |
| |
| private final Color gridColor; |
| |
| |
| public NatGridLayerPainter(final NatTable natTable) { |
| this(natTable, GUIHelper.COLOR_GRAY); |
| } |
| |
| public NatGridLayerPainter(final NatTable natTable, final Color gridColor) { |
| super(natTable); |
| this.gridColor= gridColor; |
| } |
| |
| |
| @Override |
| protected void paintBackground(final ILayer natLayer, final GC gc, final long xOffset, final long yOffset, final org.eclipse.swt.graphics.Rectangle rectangle, final IConfigRegistry configRegistry) { |
| super.paintBackground(natLayer, gc, xOffset, yOffset, rectangle, configRegistry); |
| |
| gc.setForeground(this.gridColor); |
| drawHorizontalLines(natLayer, gc, rectangle); |
| drawVerticalLines(natLayer, gc, rectangle); |
| } |
| |
| private void drawHorizontalLines(final ILayer natLayer, final GC gc, final org.eclipse.swt.graphics.Rectangle rectangle) { |
| final int startX= rectangle.x; |
| final int endX= rectangle.x + rectangle.width; |
| |
| final ILayerDim dim= natLayer.getDim(VERTICAL); |
| final long endPosition= CellLayerPainter.getEndPosition(dim, rectangle.y + rectangle.height); |
| for (long position= dim.getPositionByPixel(rectangle.y); position < endPosition; position++) { |
| final int size= dim.getPositionSize(position); |
| if (size > 0) { |
| final int y= safe(dim.getPositionStart(position) + size - 1); |
| gc.drawLine(startX, y, endX, y); |
| } |
| } |
| } |
| |
| private void drawVerticalLines(final ILayer natLayer, final GC gc, final org.eclipse.swt.graphics.Rectangle rectangle) { |
| final int startY= rectangle.y; |
| final int endY= rectangle.y + rectangle.height; |
| |
| final ILayerDim dim= natLayer.getDim(HORIZONTAL); |
| final long endPosition= CellLayerPainter.getEndPosition(dim, rectangle.x + rectangle.width); |
| for (long position= dim.getPositionByPixel(rectangle.x); position < endPosition; position++) { |
| final int size= dim.getPositionSize(position); |
| if (size > 0) { |
| final int x= safe(dim.getPositionStart(position) + size - 1); |
| gc.drawLine(x, startY, x, endY); |
| } |
| } |
| } |
| |
| } |