You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
215 lines
7.5 KiB
215 lines
7.5 KiB
/* |
|
* Copyright(c) 2001-2010, FineReport Inc, All Rights Reserved. |
|
*/ |
|
package com.fr.design.cell.clipboard; |
|
|
|
import com.fr.base.TableData; |
|
import com.fr.design.base.clipboard.ClipboardHelper; |
|
import com.fr.design.data.tabledata.paste.TableDataFollowingPasteUtils; |
|
import com.fr.design.file.HistoryTemplateListCache; |
|
import com.fr.design.mainframe.JTemplate; |
|
import com.fr.design.mainframe.theme.utils.CellElementStylePaster; |
|
import com.fr.grid.selection.CellSelection; |
|
import com.fr.log.FineLoggerFactory; |
|
import com.fr.report.cell.CellElement; |
|
import com.fr.report.cell.CellElementComparator; |
|
import com.fr.report.cell.TemplateCellElement; |
|
import com.fr.report.elementcase.TemplateElementCase; |
|
import com.fr.stable.StringUtils; |
|
import com.fr.stable.unit.FU; |
|
|
|
import java.util.ArrayList; |
|
import java.util.Arrays; |
|
import java.util.Iterator; |
|
import java.util.List; |
|
import java.util.Map; |
|
import java.util.Objects; |
|
|
|
/** |
|
* The clip of CellElement. |
|
*/ |
|
public class CellElementsClip implements Cloneable, java.io.Serializable { |
|
private String copyFromTemplatePath; |
|
private int columnSpan = 0; |
|
private int rowSpan = 0; |
|
private FU[] columnWidth; |
|
private FU[] rowHeight; |
|
private TemplateCellElement[] clips; |
|
private Map<String, TableData> elementUsedTableDatas; |
|
|
|
public CellElementsClip(int columnSpan, int rowSpan, FU[] columnWidth, FU[] rowHeight, TemplateCellElement[] clips) { |
|
this.columnSpan = columnSpan; |
|
this.rowSpan = rowSpan; |
|
this.columnWidth = columnWidth; |
|
this.rowHeight = rowHeight; |
|
this.clips = clips; |
|
this.elementUsedTableDatas = TableDataFollowingPasteUtils.transferProvider2TableDataMap(clips); |
|
this.copyFromTemplatePath = this.getCurrentTemplatePath(); |
|
} |
|
|
|
public CellElementsClip(int columnSpan, int rowSpan, TemplateCellElement[] clips) { |
|
this.columnSpan = columnSpan; |
|
this.rowSpan = rowSpan; |
|
this.clips = clips; |
|
this.elementUsedTableDatas = TableDataFollowingPasteUtils.transferProvider2TableDataMap(clips); |
|
this.copyFromTemplatePath = this.getCurrentTemplatePath(); |
|
} |
|
|
|
public int getColumnSpan() { |
|
return columnSpan; |
|
} |
|
|
|
public void setColumnSpan(int columnSpan) { |
|
this.columnSpan = columnSpan; |
|
} |
|
|
|
public int getRowSpan() { |
|
return rowSpan; |
|
} |
|
|
|
public void setRowSpan(int rowSpan) { |
|
this.rowSpan = rowSpan; |
|
} |
|
|
|
public FU[] getColumnWidth() { |
|
return columnWidth; |
|
} |
|
|
|
public void setColumnWidth(FU[] columnWidth) { |
|
this.columnWidth = columnWidth; |
|
} |
|
|
|
public FU[] getRowHeight() { |
|
return rowHeight; |
|
} |
|
|
|
public void setRowHeight(FU[] rowHeight) { |
|
this.rowHeight = rowHeight; |
|
} |
|
|
|
public TemplateCellElement[] getClips() { |
|
return clips; |
|
} |
|
|
|
public void setClips(TemplateCellElement[] clips) { |
|
this.clips = clips; |
|
} |
|
|
|
public String compateExcelPaste() { |
|
Arrays.sort(this.clips, CellElementComparator.getRowFirstComparator()); |
|
|
|
// 排序 |
|
|
|
List<List<Object>> table = new ArrayList<>(); |
|
int startRow = -1; |
|
int currentRow = -1; |
|
for (int i = 0; i < clips.length; i++) { |
|
CellElement cellElement = clips[i]; |
|
if (currentRow == -1) {// 初始化当前行. |
|
currentRow = cellElement.getRow(); |
|
startRow = currentRow; |
|
table.add(new ArrayList<>()); |
|
} |
|
|
|
while (currentRow < cellElement.getRow()) { |
|
table.add(new ArrayList<>()); |
|
currentRow++; |
|
} |
|
|
|
Object cellValue = cellElement.getValue() == null ? StringUtils.EMPTY : cellElement.getValue(); |
|
table.get(currentRow - startRow).add(cellValue); |
|
} |
|
|
|
return ClipboardHelper.formatExcelString(table); |
|
} |
|
|
|
|
|
public CellSelection pasteAt(TemplateElementCase ec, int column, int row) { |
|
|
|
Iterator cells = ec.intersect(column, row, columnSpan, rowSpan); |
|
while (cells.hasNext()) { |
|
TemplateCellElement cellElement = (TemplateCellElement) cells.next(); |
|
ec.removeCellElement(cellElement); |
|
} |
|
for (int i = 0; i < clips.length; i++) { |
|
TemplateCellElement cellElement; |
|
try { |
|
cellElement = (TemplateCellElement) clips[i].clone(); |
|
} catch (CloneNotSupportedException e) { |
|
FineLoggerFactory.getLogger().error(e.getMessage(), e); |
|
return null; |
|
} |
|
|
|
String pasteToTemplatePath = this.getCurrentTemplatePath(); |
|
boolean pastingToDifferentTemplate = copyFromTemplatePath != null && pasteToTemplatePath != null && !Objects.equals(copyFromTemplatePath, pasteToTemplatePath); |
|
if (pastingToDifferentTemplate) { |
|
cellElement = CellElementStylePaster.convertStyleAndColor(cellElement); |
|
} |
|
|
|
// peter:因为前面已经将这个位置的元素删除了,所以不需要override了. |
|
ec.addCellElement((TemplateCellElement) cellElement.deriveCellElement( |
|
column + cellElement.getColumn(), row + cellElement.getRow() |
|
), false); |
|
} |
|
//设置单元格的宽高 |
|
if (this.columnWidth != null && this.rowHeight != null) { |
|
pasteWidthAndHeight(ec, column, row, columnSpan, rowSpan); |
|
} |
|
// 粘贴数据集 |
|
TableDataFollowingPasteUtils.paste(elementUsedTableDatas); |
|
return new CellSelection(column, row, columnSpan, rowSpan); |
|
} |
|
|
|
public void pasteWidthAndHeight(TemplateElementCase ec, int column, int row, int columnSpan, int rowSpan) { |
|
for (int i = 0; i < columnSpan; i++) { |
|
ec.setColumnWidth(column + i, columnWidth[i]); |
|
} |
|
for (int j = 0; j < rowSpan; j++) { |
|
ec.setRowHeight(row + j, rowHeight[j]); |
|
} |
|
} |
|
|
|
public void pasteAtRegion(TemplateElementCase ec, |
|
int startColumn, int startRow, |
|
int column, int row, |
|
int columnSpan, int rowSpan) { |
|
for (int i = 0; i < clips.length; i++) { |
|
TemplateCellElement cellElement = clips[i]; |
|
|
|
cellElement = (TemplateCellElement) cellElement.deriveCellElement(startColumn + cellElement.getColumn(), startRow + cellElement.getRow()); |
|
//peter:检查是否越界,越界就不做了. |
|
if (cellElement.getColumn() >= column + columnSpan || cellElement.getRow() >= row + rowSpan || cellElement.getColumn() < column |
|
|| cellElement.getRow() < row) { |
|
continue; |
|
} |
|
|
|
ec.addCellElement(cellElement); |
|
// 跟随粘贴数据集 |
|
TableDataFollowingPasteUtils.paste(elementUsedTableDatas); |
|
} |
|
} |
|
|
|
private String getCurrentTemplatePath() { |
|
JTemplate<?,?> currentTemplate = HistoryTemplateListCache.getInstance().getCurrentEditingTemplate(); |
|
return currentTemplate != null ? currentTemplate.getPath() : null; |
|
} |
|
|
|
/** |
|
* Clone. |
|
*/ |
|
@Override |
|
public Object clone() throws CloneNotSupportedException { |
|
CellElementsClip cloned = (CellElementsClip) super.clone(); |
|
|
|
cloned.copyFromTemplatePath = this.copyFromTemplatePath; |
|
|
|
if (this.clips != null) { |
|
cloned.clips = new TemplateCellElement[this.clips.length]; |
|
for (int i = 0; i < this.clips.length; i++) { |
|
cloned.clips[i] = (TemplateCellElement) this.clips[i].clone(); |
|
} |
|
} |
|
|
|
return cloned; |
|
} |
|
}
|
|
|