Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Implement colors in custom formatting for vaadin-spreadsheet #7148

Draft
wants to merge 14 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,8 @@
<excludes>
<!-- Do not modify GWT sources / license headers -->
<exclude>**/com/google/gwt/**/*.*</exclude>
<!-- Do not modify Apache POI sources / license headers -->
<exclude>**/org/apache/poi/**/*.*</exclude>
</excludes>
<endWithNewline/>
<removeUnusedImports/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class Cell {
private String value;

private String cellStyle = "cs0";
private String textColor;
private boolean needsMeasure;
private SheetWidget sheetWidget;
private boolean overflowDirty = true;
Expand All @@ -63,6 +64,7 @@ public Cell(SheetWidget sheetWidget, int col, int row, CellData cellData) {
needsMeasure = cellData.needsMeasure;
value = cellData.value;
cellStyle = cellData.cellStyle;
textColor = cellData.textColor;
}
updateCellValues();
updateInnerText();
Expand All @@ -72,12 +74,17 @@ public DivElement getElement() {
return element;
}

public String getTextColor() {
return textColor;
}

public void update(int col, int row, CellData cellData) {
this.col = col;
this.row = row;
cellStyle = cellData == null ? "cs0" : cellData.cellStyle;
value = cellData == null ? null : cellData.value;
needsMeasure = cellData == null ? false : cellData.needsMeasure;
this.cellStyle = cellData == null ? "cs0" : cellData.cellStyle;
this.value = cellData == null ? null : cellData.value;
this.needsMeasure = cellData == null ? false : cellData.needsMeasure;
this.textColor = cellData == null ? null : cellData.textColor;

updateInnerText();
updateCellValues();
Expand All @@ -87,6 +94,7 @@ public void update(int col, int row, CellData cellData) {

private void updateInnerText() {
element.getStyle().setOverflow(Overflow.HIDDEN);
element.getStyle().clearColor();
if (value == null || value.isEmpty()) {
element.setInnerText("");
element.getStyle().clearZIndex();
Expand All @@ -100,6 +108,10 @@ private void updateInnerText() {
}
}

if (textColor != null) {
element.getStyle().setColor(textColor);
}

appendOverlayElements();
}

Expand Down Expand Up @@ -221,12 +233,14 @@ public String getValue() {
return value;
}

public void setValue(String value, String cellStyle, boolean needsMeasure) {
public void setValue(String value, String cellStyle, String textColor,
boolean needsMeasure) {
if (!this.cellStyle.equals(cellStyle)) {
this.cellStyle = cellStyle;
updateClassName();
}
this.needsMeasure = needsMeasure;
this.textColor = textColor;
setValue(value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class CellData implements Serializable {
public String formulaValue;
public String originalValue;
public String cellStyle = "cs0";
public String textColor;
public boolean locked = false;
public boolean needsMeasure;
public boolean isPercentage;
Expand Down Expand Up @@ -56,7 +57,7 @@ public boolean equals(Object obj) {
@Override
public String toString() {
return new StringBuilder().append("r").append(row).append("c")
.append(col).append(cellStyle).append("|").append(value)
.toString();
.append(col).append(cellStyle).append("tc").append(textColor)
.append("|").append(value).toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3139,7 +3139,14 @@ else if (vScrollDiff < 0) {
final ArrayList<Cell> tempCols = new ArrayList<Cell>();
for (Iterator<Cell> cells = row.iterator(); cells.hasNext();) {
Cell cell = cells.next();
if (cell == null) {
// Cell can apparently be null here; scrolling will fail
// unless this is checked for.
continue;
}

int cIndex = cell.getCol();

// scroll right
if (hScrollDiff > 0) {
// move cells from left to right
Expand Down Expand Up @@ -3562,11 +3569,13 @@ public void addMergedRegion(MergedRegion region) {
MergedCell mergedCell = new MergedCell(this, region.col1, region.row1);
String cellStyle = "cs0";
Cell cell = getCell(region.col1, region.row1);
String textColor = null;
if (cell != null) {
cellStyle = cell.getCellStyle();
textColor = cell.getTextColor();
}
mergedCell.setValue(getCellValue(region.col1, region.row1), cellStyle,
false);
textColor, false);
DivElement element = mergedCell.getElement();
element.addClassName(MERGED_CELL_CLASSNAME);
updateMergedRegionRegionSize(region, mergedCell);
Expand Down Expand Up @@ -3724,7 +3733,7 @@ public void removeMergedRegion(MergedRegion region, int ruleIndex) {
Cell originalCell = getCell(region.col1, region.row1);
if (originalCell != null) {
originalCell.setValue(mCell.getValue(), mCell.getCellStyle(),
false);
originalCell.getTextColor(), false);
}
mergedCells.remove(region.id).getElement().removeFromParent();
overflownMergedCells.remove(region);
Expand Down Expand Up @@ -3852,10 +3861,10 @@ private Cell getMergedCell(String key) {
}

private boolean setMergedCellValue(String key, String value,
String cellStyle, boolean needsMeasure) {
String cellStyle, String textColor, boolean needsMeasure) {
Cell cell = getMergedCell(key);
if (cell != null) {
cell.setValue(value, cellStyle, needsMeasure);
cell.setValue(value, cellStyle, textColor, needsMeasure);
return true;
}
return false;
Expand Down Expand Up @@ -4214,12 +4223,12 @@ public void updateTopLeftCellValues(List<CellData> cellData2) {
Iterator<CellData> i = cellData2.iterator();
while (i.hasNext()) {
CellData cd = i.next();
topLeftCells
.get((cd.row - 1) * horizontalSplitPosition + cd.col
- 1)
.setValue(cd.value, cd.cellStyle, cd.needsMeasure);
topLeftCells.get(
(cd.row - 1) * horizontalSplitPosition + cd.col - 1)
.setValue(cd.value, cd.cellStyle, cd.textColor,
cd.needsMeasure);
String key = toKey(cd.col, cd.row);
setMergedCellValue(key, cd.value, cd.cellStyle,
setMergedCellValue(key, cd.value, cd.cellStyle, cd.textColor,
cd.needsMeasure);
if (cd.value == null) {
cachedCellData.remove(key);
Expand Down Expand Up @@ -4266,10 +4275,11 @@ private void updateCellData(int r1, int r2, int c1, int c2,
}
}
row.get(cd.col - c1).setValue(cd.value, cd.cellStyle,
cd.needsMeasure);
cd.textColor, cd.needsMeasure);
}
String key = toKey(cd.col, cd.row);
setMergedCellValue(key, cd.value, cd.cellStyle, cd.needsMeasure);
setMergedCellValue(key, cd.value, cd.cellStyle, cd.textColor,
cd.needsMeasure);
if (cd.value == null) {
cachedCellData.remove(key);
} else {
Expand All @@ -4291,7 +4301,7 @@ public void cellValuesUpdated(ArrayList<CellData> updatedCellData) {
} else {
cachedCellData.put(key, cd);
}
if (!setMergedCellValue(key, cd.value, cd.cellStyle,
if (!setMergedCellValue(key, cd.value, cd.cellStyle, cd.textColor,
cd.needsMeasure)) {
Cell cell = null;
if (isCellRenderedInScrollPane(cd.col, cd.row)) {
Expand All @@ -4302,7 +4312,8 @@ public void cellValuesUpdated(ArrayList<CellData> updatedCellData) {
}

if (cell != null) {
cell.setValue(cd.value, cd.cellStyle, cd.needsMeasure);
cell.setValue(cd.value, cd.cellStyle, cd.textColor,
cd.needsMeasure);
cell.markAsOverflowDirty();
}
int j = verticalSplitPosition > 0 ? 0 : firstColumnIndex;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ public void execute() {
// initial display only used single column width,
// re-calculate with merged width
cell.setValue(cell.getValue(), cell.getCellStyle(),
false);
cell.getTextColor(), false);
}
i++;
}
Expand Down
Binary file not shown.
Loading