Browse Source

REPORT-40684 绘制表格辅助线时提前适配合并单元格,去除合并之后使用背景覆盖辅助线的方式

bugfix/10.0
vito 4 years ago
parent
commit
1454cc104f
  1. 331
      designer-realize/src/main/java/com/fr/grid/GridUI.java

331
designer-realize/src/main/java/com/fr/grid/GridUI.java

@ -49,6 +49,7 @@ import com.fr.stable.script.CalculatorUtils;
import com.fr.stable.unit.FU; import com.fr.stable.unit.FU;
import com.fr.stable.AssistUtils; import com.fr.stable.AssistUtils;
import com.fr.third.antlr.ANTLRException; import com.fr.third.antlr.ANTLRException;
import com.fr.third.guava.collect.HashMultimap;
import javax.swing.JComponent; import javax.swing.JComponent;
import javax.swing.UIManager; import javax.swing.UIManager;
@ -73,6 +74,7 @@ import java.awt.geom.Rectangle2D;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Objects;
public class GridUI extends ComponentUI { public class GridUI extends ComponentUI {
@ -91,6 +93,7 @@ public class GridUI extends ComponentUI {
protected List paintCellElementList = new ArrayList(); protected List paintCellElementList = new ArrayList();
protected List paintCellElementRectangleList = new ArrayList(); protected List paintCellElementRectangleList = new ArrayList();
protected List paginateLineList = new ArrayList(); // 分页线 protected List paginateLineList = new ArrayList(); // 分页线
protected HashMultimap<CellPosition, Integer> mergeCellElementTable = HashMultimap.create();
// 为了画白色的背景. // 为了画白色的背景.
protected static Background WHITE_Backgorund = ColorBackground.getInstance(Color.WHITE); protected static Background WHITE_Backgorund = ColorBackground.getInstance(Color.WHITE);
// CellElementPainter // CellElementPainter
@ -247,7 +250,7 @@ public class GridUI extends ComponentUI {
- reportSettings.getFooterHeight().toPixD(resolution); - reportSettings.getFooterHeight().toPixD(resolution);
} }
private void paintGridLine(Graphics g, Grid grid, double realWidth, double realHeight, private void paintGridLine(Graphics g, Grid grid, TemplateElementCase report, double realWidth, double realHeight,
int resolution) { int resolution) {
Graphics2D g2d = (Graphics2D) g; Graphics2D g2d = (Graphics2D) g;
@ -257,22 +260,51 @@ public class GridUI extends ComponentUI {
// 分页线 // 分页线
paginateLineList.clear(); paginateLineList.clear();
// 合并单元格
mergeCellElementTable.clear();
boolean isShowVerticalPaginateLine = grid.getPaginateLineShowType() == Grid.MULTIPLE_PAGINATE_LINE; boolean isShowVerticalPaginateLine = grid.getPaginateLineShowType() == Grid.MULTIPLE_PAGINATE_LINE;
boolean isShowHorizontalPaginateLine = grid.getPaginateLineShowType() != Grid.NO_PAGINATE_LINE; boolean isShowHorizontalPaginateLine = grid.getPaginateLineShowType() != Grid.NO_PAGINATE_LINE;
new DrawVerticalLineHelper(grid.getVerticalBeginValue(), verticalEndValue, Iterator cells = report.intersect(grid.getHorizontalBeginValue(), grid.getVerticalBeginValue(),
grid.isShowGridLine(), isShowVerticalPaginateLine, rowHeightList, paperPaintHeight, horizontalEndValue - grid.getHorizontalBeginValue(), verticalEndValue - grid.getVerticalBeginValue());
paginateLineList, realWidth, resolution).iterateStart2End(g2d); while (cells.hasNext()) {
TemplateCellElement cellElement = (TemplateCellElement) cells.next();
if (cellElement == null) {
continue;
}
int columnSpan = cellElement.getColumnSpan();
int rowSpan = cellElement.getRowSpan();
if (columnSpan > 1 || rowSpan > 1) {
for (int c = 0; c < columnSpan - 1; c++) {
for (int r = 0; r < rowSpan; r++) {
mergeCellElementTable.put(CellPosition.value(cellElement.getRow() + r, cellElement.getColumn() + c), CellPosition.RIGHT);
}
}
for (int r = 0; r < rowSpan - 1; r++) {
for (int c = 0; c < columnSpan; c++) {
mergeCellElementTable.put(CellPosition.value(cellElement.getRow() + r, cellElement.getColumn() + c), CellPosition.BOTTOM);
}
}
}
}
GridRange gridRange = GridRange.range(grid.getHorizontalBeginValue(), horizontalEndValue, grid.getVerticalBeginValue(), verticalEndValue)
.rangeList(rowHeightList, columnWidthList)
.realSize(realWidth, realHeight);
new DrawHorizontalLineHelper(grid.getHorizontalBeginValue(), horizontalEndValue, new DrawVerticalLineHelper(gridRange, grid.isShowGridLine(),
grid.isShowGridLine(), isShowHorizontalPaginateLine, columnWidthList, paperPaintWidth, isShowVerticalPaginateLine, paperPaintHeight,
paginateLineList, realHeight, resolution).iterateStart2End(g2d); paginateLineList, resolution).iterateStart2End(g2d);
new DrawHorizontalLineHelper(gridRange, grid.isShowGridLine(),
isShowHorizontalPaginateLine, paperPaintWidth,
paginateLineList, resolution).iterateStart2End(g2d);
} }
/** /**
* 最后处理 * 最后处理
*/ */
@Override
public void finalize() { public void finalize() {
try { try {
super.finalize(); super.finalize();
@ -285,80 +317,34 @@ public class GridUI extends ComponentUI {
} }
private static abstract class DrawLineHelper { private static abstract class DrawLineHelper {
private int startIndex; protected GridRange gridRange;
private int endIndex;
private boolean showGridLine; protected boolean showGridLine;
private boolean showPaginateLine; protected boolean showPaginateLine;
private DynamicUnitList sizeList; protected double paperPaintSize;
private double paperPaintSize;
private List paginateLineList; protected List paginateLineList;
Line2D tmpLine2D = new Line2D.Double(0, 0, 0, 0); Line2D tmpLine2D = new Line2D.Double(0, 0, 0, 0);
private int resolution; protected int resolution;
private static final double THRESHOLD = 1.0E-4D; protected static final double THRESHOLD = 1.0E-4D;
DrawLineHelper(int startIndex, int endIndex, boolean showGridLine, DrawLineHelper(GridRange gridRange, boolean showGridLine,
boolean showPaginateLine, DynamicUnitList sizeList, double paperPaintSize, boolean showPaginateLine, double paperPaintSize,
List paginateLineList, int resolution) { List paginateLineList, int resolution) {
this.startIndex = startIndex; this.gridRange = gridRange;
this.endIndex = endIndex;
this.showGridLine = showGridLine; this.showGridLine = showGridLine;
this.showPaginateLine = showPaginateLine; this.showPaginateLine = showPaginateLine;
this.sizeList = sizeList;
this.paperPaintSize = paperPaintSize; this.paperPaintSize = paperPaintSize;
this.paginateLineList = paginateLineList; this.paginateLineList = paginateLineList;
this.resolution = resolution; this.resolution = resolution;
} }
protected void iterateStart2End(Graphics2D g2d) { protected abstract void iterateStart2End(Graphics2D g2d);
float tmpSize = 0, paperSumSize = 0, sumSize = 0;
for (int i = 0; i <= endIndex; i++) {
// denny: 开始
if (i == 0) {
i = startIndex;
// denny: 增加从0到Grid左边被hide的列宽
for (int k = 0; k < startIndex; k++) {
tmpSize = sizeList.get(k).toPixF(resolution);
paperSumSize += tmpSize;
if (paperSumSize >= paperPaintSize) {
paperSumSize = tmpSize;
}
}
}
// adjust height.
tmpSize = sizeList.get(i).toPixF(resolution);
paperSumSize += tmpSize;
if (showGridLine) {// paint line.
setLine2D((int) sumSize);
g2d.draw(tmpLine2D);
}
// paint paper margin line.
if (showPaginateLine && paperSumSize - paperPaintSize > THRESHOLD) {
paginateLineList.add(getPaginateLine2D((int) sumSize));
paperSumSize = tmpSize;
}
sumSize += tmpSize;
}
// paint 最后一个横线..
if (showGridLine) {
drawLastLine(g2d, (int) sumSize);
}
}
protected abstract void setLine2D(int sumSize);
protected abstract Line2D.Double getPaginateLine2D(int sumSize); protected abstract Line2D.Double getPaginateLine2D(int sumSize);
@ -366,14 +352,12 @@ public class GridUI extends ComponentUI {
} }
private class DrawVerticalLineHelper extends DrawLineHelper { private class DrawVerticalLineHelper extends DrawLineHelper {
private double realWidth;
DrawVerticalLineHelper(int startIndex, int endIndex, boolean showGridLine, DrawVerticalLineHelper(GridRange gridRange, boolean showGridLine,
boolean showPaginateLine, DynamicUnitList unitSizeList, double paperPaintSize, boolean showPaginateLine, double paperPaintSize,
List paginateLineList, double realWidth, int resolution) { List paginateLineList, int resolution) {
super(startIndex, endIndex, showGridLine, showPaginateLine, unitSizeList, super(gridRange, showGridLine, showPaginateLine,
paperPaintSize, paginateLineList, resolution); paperPaintSize, paginateLineList, resolution);
this.realWidth = realWidth;
} }
@Override @Override
@ -382,25 +366,58 @@ public class GridUI extends ComponentUI {
} }
@Override @Override
protected void setLine2D(int sumHeight) { protected void drawLastLine(Graphics2D g2d, int sumHeight) {
tmpLine2D.setLine(0, sumHeight, realWidth, sumHeight); GraphHelper.drawLine(g2d, 0, sumHeight, gridRange.realWidth, sumHeight);
} }
@Override @Override
protected void drawLastLine(Graphics2D g2d, int sumHeight) { protected void iterateStart2End(Graphics2D g2d) {
GraphHelper.drawLine(g2d, 0, sumHeight, realWidth, sumHeight); float rowHeight, paperYAxisSumSize = 0, yAxisSumSize = 0;
for (int i = 0; i <= gridRange.yEndIndex; i++) {
if (i == 0) {
i = gridRange.yBeginIndex;
for (int k = 0; k < gridRange.yBeginIndex; k++) {
rowHeight = gridRange.rowHeightList.get(k).toPixF(resolution);
paperYAxisSumSize += rowHeight;
if (paperYAxisSumSize >= paperPaintSize) {
paperYAxisSumSize = rowHeight;
}
}
}
rowHeight = gridRange.rowHeightList.get(i).toPixF(resolution);
paperYAxisSumSize += rowHeight;
if (showGridLine) {
float columnWidth, xAxisSumSize = 0;
for (int j = gridRange.xBeginIndex; j <= gridRange.xEndIndex; j++) {
columnWidth = gridRange.columnWidthList.get(j).toPixF(resolution);
if (!mergeCellElementTable.get(CellPosition.value(i - 1, j)).contains(CellPosition.BOTTOM)) {
tmpLine2D.setLine(xAxisSumSize, yAxisSumSize, xAxisSumSize + columnWidth, yAxisSumSize);
g2d.draw(tmpLine2D);
}
xAxisSumSize += columnWidth;
}
}
if (showPaginateLine && paperYAxisSumSize - paperPaintSize > THRESHOLD) {
paginateLineList.add(getPaginateLine2D((int) yAxisSumSize));
paperYAxisSumSize = rowHeight;
}
yAxisSumSize += rowHeight;
}
// paint 最后一个横线..
if (showGridLine) {
drawLastLine(g2d, (int) yAxisSumSize);
}
} }
} }
private class DrawHorizontalLineHelper extends DrawLineHelper { private class DrawHorizontalLineHelper extends DrawLineHelper {
private double realHeight;
DrawHorizontalLineHelper(int startIndex, int endIndex, boolean showGridLine, DrawHorizontalLineHelper(GridRange gridRange, boolean showGridLine,
boolean showPaginateLine, DynamicUnitList unitSizeList, double paperPaintSize, boolean showPaginateLine, double paperPaintSize,
List paginateLineList, double realHeight, int resolution) { List paginateLineList, int resolution) {
super(startIndex, endIndex, showGridLine, showPaginateLine, unitSizeList, super(gridRange, showGridLine, showPaginateLine,
paperPaintSize, paginateLineList, resolution); paperPaintSize, paginateLineList, resolution);
this.realHeight = realHeight;
} }
@Override @Override
@ -409,13 +426,47 @@ public class GridUI extends ComponentUI {
} }
@Override @Override
protected void setLine2D(int sumWidth) { protected void drawLastLine(Graphics2D g2d, int sumWidth) {
tmpLine2D.setLine(sumWidth, 0, sumWidth, realHeight); GraphHelper.drawLine(g2d, sumWidth, 0, sumWidth, gridRange.realHeight);
} }
@Override @Override
protected void drawLastLine(Graphics2D g2d, int sumWidth) { protected void iterateStart2End(Graphics2D g2d) {
GraphHelper.drawLine(g2d, sumWidth, 0, sumWidth, realHeight); float columnWidth, paperXAxisSumSize = 0, xAxisSumSize = 0;
for (int i = 0; i <= gridRange.xEndIndex; i++) {
if (i == 0) {
i = gridRange.xBeginIndex;
for (int k = 0; k < gridRange.xBeginIndex; k++) {
columnWidth = gridRange.columnWidthList.get(k).toPixF(resolution);
paperXAxisSumSize += columnWidth;
if (paperXAxisSumSize >= paperPaintSize) {
paperXAxisSumSize = columnWidth;
}
}
}
columnWidth = gridRange.columnWidthList.get(i).toPixF(resolution);
paperXAxisSumSize += columnWidth;
if (showGridLine) {
float rowHeight, yAxisSumSize = 0;
for (int j = gridRange.yBeginIndex; j <= gridRange.yEndIndex; j++) {
rowHeight = gridRange.rowHeightList.get(j).toPixF(resolution);
if (!mergeCellElementTable.get(CellPosition.value(j, i - 1)).contains(CellPosition.RIGHT)) {
tmpLine2D.setLine(xAxisSumSize, yAxisSumSize, xAxisSumSize, yAxisSumSize + rowHeight);
g2d.draw(tmpLine2D);
}
yAxisSumSize += rowHeight;
}
}
if (showPaginateLine && paperXAxisSumSize - paperPaintSize > THRESHOLD) {
paginateLineList.add(getPaginateLine2D((int) xAxisSumSize));
paperXAxisSumSize = columnWidth;
}
xAxisSumSize += columnWidth;
}
// paint 最后一个横线..
if (showGridLine) {
drawLastLine(g2d, (int) xAxisSumSize);
}
} }
} }
@ -482,18 +533,6 @@ public class GridUI extends ComponentUI {
// peter:tmpRectangle2D_3只是一个临时的Rectangle2D,由于后面不少地方需要用到这个矩形 // peter:tmpRectangle2D_3只是一个临时的Rectangle2D,由于后面不少地方需要用到这个矩形
this.cell_back_rect.setRect(0, 0, this.tmpRectangle.getWidth() - 1, this.cell_back_rect.setRect(0, 0, this.tmpRectangle.getWidth() - 1,
this.tmpRectangle.getHeight() - 1.5); this.tmpRectangle.getHeight() - 1.5);
// peter:对于合并的单元格,需要先白色的背景来清除背景.
if (tmpCellElement.getColumnSpan() > 1 || tmpCellElement.getRowSpan() > 1) {
// REPORT-23492 要看下是否设置了纸张背景 如果设置了按照背景来画
ReportSettingsProvider reportSettings = getReportSettings(report);
Background currentBackground = reportSettings.getBackground();
if (currentBackground != null) {
currentBackground.paint(g2d, this.cell_back_rect);
} else {
WHITE_Backgorund.paint(g2d, this.cell_back_rect);
}
//daniel:上面这里就有问题了啊....报表的背景在这个之前画的 会覆盖报表背景....不过只是设计器中看到预览浏览没问题
}
// peter:将这个元素添加到需要paint的元素列表当中去,留着画边框线.. // peter:将这个元素添加到需要paint的元素列表当中去,留着画边框线..
paintCellElementList.add(tmpCellElement); paintCellElementList.add(tmpCellElement);
paintCellElementRectangleList.add(this.tmpRectangle.clone()); paintCellElementRectangleList.add(this.tmpRectangle.clone());
@ -1093,8 +1132,7 @@ public class GridUI extends ComponentUI {
this.paintBackground(g2d, grid, elementCase, resolution); this.paintBackground(g2d, grid, elementCase, resolution);
// 画Grid Line // 画Grid Line
this.paintGridLine(g2d, grid, realWidth, realHeight, resolution); this.paintGridLine(g2d, grid, elementCase, realWidth, realHeight, resolution);
// peter:添上线程的支持,有时候,paint元素的时候,可能会有元素被删除了. // peter:添上线程的支持,有时候,paint元素的时候,可能会有元素被删除了.
// 需要先清除画Border需要的元素. // 需要先清除画Border需要的元素.
paintCellElementList.clear(); paintCellElementList.clear();
@ -1277,4 +1315,101 @@ public class GridUI extends ComponentUI {
paginateLineList.add(new Line2D.Double(0, sumHeight, width, sumHeight)); paginateLineList.add(new Line2D.Double(0, sumHeight, width, sumHeight));
} }
} }
/**
* 描述单元格位置的POJO
*/
private static class CellPosition {
public final static int TOP = 0;
public static final int LEFT = 1;
public final static int BOTTOM = 2;
public static final int RIGHT = 4;
public int x;
public int y;
public CellPosition(int x, int y) {
this.x = x;
this.y = y;
}
public static CellPosition value(int x, int y) {
return new CellPosition(x, y);
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
CellPosition that = (CellPosition) o;
return x == that.x && y == that.y;
}
@Override
public int hashCode() {
return Objects.hash(x, y);
}
}
/**
* 描述表格范围的POJO
* x轴为水平方向对应的宽度表为列宽列表
* y轴为垂直方向对应的宽度表为行高列表
*/
private static class GridRange {
public int xBeginIndex;
public int xEndIndex;
public int yBeginIndex;
public int yEndIndex;
double realWidth;
double realHeight;
/**
* 行高列表对应y方向线段
*/
public DynamicUnitList rowHeightList;
/**
* 列宽列表对应x方向线段
*/
public DynamicUnitList columnWidthList;
public GridRange(int xBeginIndex, int xEndIndex, int yBeginIndex, int yEndIndex) {
this.xBeginIndex = xBeginIndex;
this.xEndIndex = xEndIndex;
this.yBeginIndex = yBeginIndex;
this.yEndIndex = yEndIndex;
}
/**
* 设置行高列宽列表
*
* @param rowHeightList 行高列表
* @param columnWidthList 列宽列表
*/
public GridRange rangeList(DynamicUnitList rowHeightList, DynamicUnitList columnWidthList) {
this.rowHeightList = rowHeightList;
this.columnWidthList = columnWidthList;
return this;
}
/**
* 设置表格真实宽高
*
* @param realWidth 宽度
* @param realHeight 高度
*/
public GridRange realSize(double realWidth, double realHeight) {
this.realWidth = realWidth;
this.realHeight = realHeight;
return this;
}
public static GridRange range(int xBeginIndex, int xEndIndex, int yBeginIndex, int yEndIndex) {
return new GridRange(xBeginIndex, xEndIndex, yBeginIndex, yEndIndex);
}
}
} }

Loading…
Cancel
Save