This is an automated email from the ASF dual-hosted git repository.
ahuber pushed a commit to branch v4
in repository https://gitbox.apache.org/repos/asf/causeway.git
The following commit(s) were added to refs/heads/v4 by this push:
new df52beb11d4 CAUSEWAY-3902: adds a few pre-def. background colors for
XLSX Export
df52beb11d4 is described below
commit df52beb11d466bbe32367a70b5f55bd939ca2c7a
Author: a.huber <[email protected]>
AuthorDate: Tue Sep 16 08:58:31 2025 +0200
CAUSEWAY-3902: adds a few pre-def. background colors for XLSX Export
---
.../tabular/excel/exporter/CellStyleProvider.java | 60 +++++++++++-----------
.../tabular/excel/exporter/ExcelFileWriter.java | 18 ++++---
2 files changed, 41 insertions(+), 37 deletions(-)
diff --git
a/extensions/vw/tabular/excel/src/main/java/org/apache/causeway/extensions/tabular/excel/exporter/CellStyleProvider.java
b/extensions/vw/tabular/excel/src/main/java/org/apache/causeway/extensions/tabular/excel/exporter/CellStyleProvider.java
index aaf78a287d6..caac1af50c2 100644
---
a/extensions/vw/tabular/excel/src/main/java/org/apache/causeway/extensions/tabular/excel/exporter/CellStyleProvider.java
+++
b/extensions/vw/tabular/excel/src/main/java/org/apache/causeway/extensions/tabular/excel/exporter/CellStyleProvider.java
@@ -19,6 +19,8 @@
package org.apache.causeway.extensions.tabular.excel.exporter;
import java.awt.Color;
+import java.util.List;
+import java.util.stream.Stream;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.Cell;
@@ -28,26 +30,37 @@
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.DefaultIndexedColorMap;
import org.apache.poi.xssf.usermodel.XSSFColor;
-
import org.jspecify.annotations.Nullable;
import org.apache.causeway.commons.tabular.TabularModel.TabularRow;
import
org.apache.causeway.extensions.tabular.excel.exporter.ExcelFileWriter.Options.CustomCellStyle;
-import org.jspecify.annotations.NonNull;
-
record CellStyleProvider(
- @NonNull Workbook workbook,
- @NonNull CellStyle primaryHeaderStyle,
- @NonNull CellStyle secondaryHeaderStyle,
- @NonNull CellStyle dateStyle,
- @NonNull CellStyle multilineStyle,
- @NonNull CellStyle[] blueStyles,
- @NonNull CellStyle[] greenStyles,
- @NonNull CellStyle[] indigoStyles,
- @NonNull CellStyle[] warningStyles,
- @NonNull CellStyle[] dangerStyles,
- ExcelFileWriter.@NonNull Options options) {
+ Workbook workbook,
+ CellStyle primaryHeaderStyle,
+ CellStyle secondaryHeaderStyle,
+ CellStyle dateStyle,
+ CellStyle multilineStyle,
+ ColorPalette colorPalette,
+ ExcelFileWriter.Options options) {
+
+ private record ColorPalette(
+ List<CellStyle[]> listOfStyles) {
+
+ static ColorPalette create(final Workbook wb, final StyleIndexList
styleIndexList) {
+ return new ColorPalette(Stream.of(CustomCellStyle.values())
+ .skip(1) // skip DEFAULT style
+ .mapToInt(style->style.rgb)
+ .mapToObj(Color::new)
+ .map(color->createColoredStyles(wb, styleIndexList, color))
+ .toList());
+ }
+
+ CellStyle[] styles(final CustomCellStyle customCellStyle) {
+ return listOfStyles.get(customCellStyle.ordinal() - 1); // skip
DEFAULT style
+ }
+
+ }
static CellStyleProvider create(final Workbook wb, final
ExcelFileWriter.@Nullable Options options) {
var styleIndexList = new StyleIndexList();
@@ -56,11 +69,7 @@ static CellStyleProvider create(final Workbook wb, final
ExcelFileWriter.@Nullab
createSecondaryHeaderRowStyle(wb),
createDateFormatCellStyle(wb, styleIndexList, "yyyy-mm-dd"),
createMultilineCellStyle(wb, styleIndexList),
- createColoredStyles(wb, styleIndexList, new Color(0x6ea8fe)), //
blue-300 (6ea8fe)
- createColoredStyles(wb, styleIndexList, new Color(0x75b798)), //
green-300 (75b798)
- createColoredStyles(wb, styleIndexList, new Color(0xa370f7)), //
indigo-300 (a370f7)
- createColoredStyles(wb, styleIndexList, new Color(0xffcd39)), //
warning yellow-400 (ffcd39)
- createColoredStyles(wb, styleIndexList, new Color(0xe35d6a)), //
danger red-400 (e35d6a)
+ ColorPalette.create(wb, styleIndexList),
options!=null
? options
: ExcelFileWriter.Options.builder().build());
@@ -130,17 +139,8 @@ private void applyCustomCellStyle(final Cell cell, final
CustomCellStyle customC
if(customCellStyle==null
|| customCellStyle.isDefault()) return;
- final int ordinal = valueKindOf(cell).ordinal();
-
- cell.setCellStyle(
- switch (customCellStyle) {
- case BLUE -> blueStyles()[ordinal];
- case GREEN -> greenStyles()[ordinal];
- case INDIGO -> indigoStyles()[ordinal];
- case WARNING -> warningStyles()[ordinal];
- case DANGER -> dangerStyles()[ordinal];
- case DEFAULT -> throw new
UnsupportedOperationException("unexpected code reach");
- });
+ cell.setCellStyle(colorPalette
+ .styles(customCellStyle)[valueKindOf(cell).ordinal()]);
}
private enum ValueKind {
diff --git
a/extensions/vw/tabular/excel/src/main/java/org/apache/causeway/extensions/tabular/excel/exporter/ExcelFileWriter.java
b/extensions/vw/tabular/excel/src/main/java/org/apache/causeway/extensions/tabular/excel/exporter/ExcelFileWriter.java
index e45977c5b76..4bf80fdc544 100644
---
a/extensions/vw/tabular/excel/src/main/java/org/apache/causeway/extensions/tabular/excel/exporter/ExcelFileWriter.java
+++
b/extensions/vw/tabular/excel/src/main/java/org/apache/causeway/extensions/tabular/excel/exporter/ExcelFileWriter.java
@@ -67,13 +67,18 @@ public record Options(
*/
@Nullable Function<TabularRow, CustomCellStyle> rowStyleFunction) {
+ @RequiredArgsConstructor
public enum CustomCellStyle {
- DEFAULT,
- BLUE,
- GREEN,
- INDIGO,
- WARNING,
- DANGER;
+ DEFAULT(-1),
+ BLUE(0xa0c4ff), // light-blue
+ GREEN(0x75b798), // green-300
+ INDIGO(0xa370f7), // indigo-300
+ MINT(0xb2e0d9), // pale-mint
+ PINK(0xffb2d1), // light-pink
+ GRAY(0xe0e0e0), // light-gray
+ WARNING(0xffcd39), // warning yellow-400
+ DANGER(0xe35d6a); // danger red-400
+ public final int rgb;
public boolean isDefault() { return this ==
CustomCellStyle.DEFAULT; }
static CustomCellStyle nullToDefault(final @Nullable
CustomCellStyle customCellStyle) {
return customCellStyle!=null
@@ -81,7 +86,6 @@ static CustomCellStyle nullToDefault(final @Nullable
CustomCellStyle customCellS
: DEFAULT;
}
}
-
}
@SneakyThrows