Skip to content

Commit 9dd9e81

Browse files
committed
fixed #8 The maximum number of Cell Styles was exceeded
the named cell styles are reused as much as possible
1 parent dc1c7e4 commit 9dd9e81

File tree

7 files changed

+168
-20
lines changed

7 files changed

+168
-20
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ plugins {
1212
id 'org.asciidoctor.gradle.asciidoctor' version '1.5.1'
1313
}
1414

15-
String currentVersion = '0.1.11'
15+
String currentVersion = '0.1.12'
1616

1717
version = currentVersion
1818

spreadsheet-builder-api/src/main/java/org/modelcatalogue/builder/spreadsheet/api/HasStyle.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,27 @@
66
import groovy.transform.stc.FromString;
77

88
public interface HasStyle {
9+
10+
/**
11+
* Applies a customized named style to the current element.
12+
*
13+
* @param name the name of the style
14+
* @param styleDefinition the definition of the style customizing the predefined style
15+
*/
16+
void style(String name, @DelegatesTo(CellStyle.class) @ClosureParams(value=FromString.class, options = "org.modelcatalogue.builder.spreadsheet.api.CellStyle") Closure styleDefinition);
17+
18+
/**
19+
* Applies the style defined by the closure to the current element.
20+
* @param styleDefinition the definition of the style
21+
*/
922
void style(@DelegatesTo(CellStyle.class) @ClosureParams(value=FromString.class, options = "org.modelcatalogue.builder.spreadsheet.api.CellStyle") Closure styleDefinition);
23+
24+
/**
25+
* Applies the names style to the current element.
26+
*
27+
* The style can be changed no longer.
28+
*
29+
* @param name the name of the style
30+
*/
1031
void style(String name);
1132
}

spreadsheet-builder-poi/src/main/groovy/org/modelcatalogue/builder/spreadsheet/poi/PoiCell.groovy

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,17 @@ class PoiCell extends AbstractCell implements Resolvable {
119119

120120
@Override
121121
void style(String name) {
122-
style row.sheet.workbook.getStyle(name)
122+
if (!poiCellStyle) {
123+
poiCellStyle = row.sheet.workbook.getStyle(name)
124+
return
125+
}
126+
poiCellStyle.with row.sheet.workbook.getStyleDefinition(name)
127+
}
128+
129+
@Override
130+
void style(String name, @ClosureParams(value=FromString.class, options = "org.modelcatalogue.builder.spreadsheet.api.CellStyle") Closure styleDefinition) {
131+
style row.sheet.workbook.getStyleDefinition(name)
132+
style styleDefinition
123133
}
124134

125135
@Override

spreadsheet-builder-poi/src/main/groovy/org/modelcatalogue/builder/spreadsheet/poi/PoiCellStyle.groovy

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,28 +26,43 @@ import java.util.regex.Matcher
2626
class PoiCellStyle extends AbstractCellStyle {
2727

2828
private final XSSFCellStyle style
29-
private final PoiCell cell
29+
private final PoiWorkbook workbook
3030

3131
private PoiFont poiFont
3232
private PoiBorder poiBorder
3333

34+
private boolean sealed
35+
3436
PoiCellStyle(PoiCell cell) {
35-
this.cell = cell
3637
if (cell.cell.cellStyle == cell.cell.sheet.workbook.stylesSource.getStyleAt(0)) {
3738
style = cell.cell.sheet.workbook.createCellStyle() as XSSFCellStyle
3839
cell.cell.cellStyle = style
3940
} else {
4041
style = cell.cell.cellStyle as XSSFCellStyle
4142
}
43+
workbook = cell.row.sheet.workbook
44+
}
45+
46+
PoiCellStyle(PoiWorkbook workbook) {
47+
this.style = workbook.workbook.createCellStyle() as XSSFCellStyle
48+
this.workbook = workbook
4249
}
4350

4451
@Override
4552
void base(String stylename) {
46-
with cell.row.sheet.workbook.getStyle(stylename)
53+
checkSealed()
54+
with workbook.getStyleDefinition(stylename)
55+
}
56+
57+
void checkSealed() {
58+
if (sealed) {
59+
throw new IllegalStateException("The cell style is already sealed! You need to create new style.")
60+
}
4761
}
4862

4963
@Override
5064
void background(String hexColor) {
65+
checkSealed()
5166
if (style.fillForegroundColor == IndexedColors.AUTOMATIC.index) {
5267
foreground hexColor
5368
} else {
@@ -57,11 +72,13 @@ class PoiCellStyle extends AbstractCellStyle {
5772

5873
@Override
5974
void background(Color colorPreset) {
75+
checkSealed()
6076
background colorPreset.hex
6177
}
6278

6379
@Override
6480
void foreground(String hexColor) {
81+
checkSealed()
6582
if (style.fillForegroundColor != IndexedColors.AUTOMATIC.index) {
6683
// already set as background color
6784
style.setFillBackgroundColor(style.getFillForegroundXSSFColor())
@@ -74,11 +91,13 @@ class PoiCellStyle extends AbstractCellStyle {
7491

7592
@Override
7693
void foreground(Color colorPreset) {
94+
checkSealed()
7795
foreground colorPreset.hex
7896
}
7997

8098
@Override
8199
void fill(ForegroundFill fill) {
100+
checkSealed()
82101
switch (fill) {
83102
case ForegroundFill.NO_FILL:
84103
style.fillPattern = CellStyle.NO_FILL
@@ -136,43 +155,50 @@ class PoiCellStyle extends AbstractCellStyle {
136155

137156
@Override
138157
void font(@DelegatesTo(Font.class) @ClosureParams(value=FromString.class, options = "org.modelcatalogue.builder.spreadsheet.api.Font") Closure fontConfiguration) {
158+
checkSealed()
139159
if (!poiFont) {
140-
poiFont = new PoiFont(cell.row.sheet.sheet.workbook, style)
160+
poiFont = new PoiFont(workbook.workbook, style)
141161
}
142162
poiFont.with fontConfiguration
143163
}
144164

145165
@Override
146166
void indent(int indent) {
167+
checkSealed()
147168
style.indention = (short) indent
148169
}
149170

150171
Object getLocked() {
172+
checkSealed()
151173
style.locked = true
152174
}
153175

154176
@Override
155177
void wrap(TextKeyword text) {
178+
checkSealed()
156179
style.wrapText = true
157180
}
158181

159182
Object getHidden() {
183+
checkSealed()
160184
style.hidden = true
161185
}
162186

163187
@Override
164188
void rotation(int rotation) {
189+
checkSealed()
165190
style.rotation = (short) rotation
166191
}
167192

168193
@Override
169194
void format(String format) {
170-
XSSFDataFormat dataFormat = cell.row.sheet.sheet.workbook.createDataFormat()
195+
XSSFDataFormat dataFormat = workbook.workbook.createDataFormat()
171196
style.dataFormat = dataFormat.getFormat(format)
172197
}
173198

174199
@Override
175200
HorizontalAlignmentConfigurer align(VerticalAlignment alignment) {
201+
checkSealed()
176202
switch (alignment) {
177203
case PureVerticalAlignment.CENTER:
178204
style.setVerticalAlignment(org.apache.poi.ss.usermodel.VerticalAlignment.CENTER)
@@ -197,6 +223,7 @@ class PoiCellStyle extends AbstractCellStyle {
197223

198224
@Override
199225
void border(@DelegatesTo(Border.class) @ClosureParams(value=FromString.class, options = "org.modelcatalogue.builder.spreadsheet.api.Border") Closure borderConfiguration) {
226+
checkSealed()
200227
PoiBorder poiBorder = findOrCreateBorder()
201228
poiBorder.with borderConfiguration
202229

@@ -207,6 +234,7 @@ class PoiCellStyle extends AbstractCellStyle {
207234

208235
@Override
209236
void border(BorderSide location, @DelegatesTo(Border.class) @ClosureParams(value=FromString.class, options = "org.modelcatalogue.builder.spreadsheet.api.Border") Closure borderConfiguration) {
237+
checkSealed()
210238
PoiBorder poiBorder = findOrCreateBorder()
211239
poiBorder.with borderConfiguration
212240
poiBorder.applyTo(location)
@@ -215,7 +243,7 @@ class PoiCellStyle extends AbstractCellStyle {
215243
@Override
216244
void border(BorderSide first, BorderSide second,
217245
@DelegatesTo(Border.class) @ClosureParams(value=FromString.class, options = "org.modelcatalogue.builder.spreadsheet.api.Border") Closure borderConfiguration) {
218-
246+
checkSealed()
219247
PoiBorder poiBorder = findOrCreateBorder()
220248
poiBorder.with borderConfiguration
221249
poiBorder.applyTo(first)
@@ -226,7 +254,7 @@ class PoiCellStyle extends AbstractCellStyle {
226254
@Override
227255
void border(BorderSide first, BorderSide second, BorderSide third,
228256
@DelegatesTo(Border.class) @ClosureParams(value=FromString.class, options = "org.modelcatalogue.builder.spreadsheet.api.Border") Closure borderConfiguration) {
229-
257+
checkSealed()
230258
PoiBorder poiBorder = findOrCreateBorder()
231259
poiBorder.with borderConfiguration
232260
poiBorder.applyTo(first)
@@ -262,4 +290,8 @@ class PoiCellStyle extends AbstractCellStyle {
262290

263291
new XSSFColor([red, green, blue] as byte[])
264292
}
293+
294+
void seal() {
295+
this.sealed = true
296+
}
265297
}

spreadsheet-builder-poi/src/main/groovy/org/modelcatalogue/builder/spreadsheet/poi/PoiRow.groovy

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class PoiRow implements Row {
1515
private final PoiSheet sheet
1616

1717
private String styleName
18-
private Closure styleDefinition = { return null }
18+
private Closure styleDefinition
1919

2020
private final List<Integer> startPositions = []
2121
private int nextColNumber = 0
@@ -36,9 +36,15 @@ class PoiRow implements Row {
3636

3737
PoiCell poiCell = new PoiCell(this, xssfCell)
3838
if (styleName) {
39-
poiCell.style styleName
39+
if (styleDefinition) {
40+
poiCell.style styleName, styleDefinition
41+
} else {
42+
poiCell.style styleName
43+
}
44+
} else if(styleDefinition) {
45+
poiCell.style styleDefinition
4046
}
41-
poiCell.style styleDefinition
47+
4248
poiCell.value value
4349

4450
poiCell.resolve()
@@ -49,10 +55,17 @@ class PoiRow implements Row {
4955
XSSFCell xssfCell = xssfRow.createCell(nextColNumber)
5056

5157
PoiCell poiCell = new PoiCell(this, xssfCell)
58+
5259
if (styleName) {
53-
poiCell.style styleName
60+
if (styleDefinition) {
61+
poiCell.style styleName, styleDefinition
62+
} else {
63+
poiCell.style styleName
64+
}
65+
} else if (styleDefinition) {
66+
poiCell.style styleDefinition
5467
}
55-
poiCell.style styleDefinition
68+
5669
poiCell.with cellDefinition
5770

5871
nextColNumber += poiCell.colspan
@@ -80,10 +93,17 @@ class PoiRow implements Row {
8093
nextColNumber = column
8194

8295
PoiCell poiCell = new PoiCell(this, xssfCell)
96+
8397
if (styleName) {
84-
poiCell.style styleName
98+
if (styleDefinition) {
99+
poiCell.style styleName, styleDefinition
100+
} else {
101+
poiCell.style styleName
102+
}
103+
} else if(styleDefinition) {
104+
poiCell.style styleDefinition
85105
}
86-
poiCell.style styleDefinition
106+
87107
poiCell.with cellDefinition
88108

89109
handleSpans(xssfCell, poiCell)
@@ -106,6 +126,12 @@ class PoiRow implements Row {
106126
this.styleName = name
107127
}
108128

129+
@Override
130+
void style(String name, @DelegatesTo(CellStyle.class) @ClosureParams(value = FromString.class, options = "org.modelcatalogue.builder.spreadsheet.api.CellStyle") Closure styleDefinition) {
131+
style name
132+
style styleDefinition
133+
}
134+
109135
protected PoiSheet getSheet() {
110136
return sheet
111137
}

spreadsheet-builder-poi/src/main/groovy/org/modelcatalogue/builder/spreadsheet/poi/PoiWorkbook.groovy

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import groovy.transform.stc.ClosureParams
44
import groovy.transform.stc.FromString
55
import org.apache.poi.ss.util.WorkbookUtil
66
import org.apache.poi.xssf.usermodel.XSSFCell
7+
import org.apache.poi.xssf.usermodel.XSSFDataFormat
78
import org.apache.poi.xssf.usermodel.XSSFSheet
89
import org.apache.poi.xssf.usermodel.XSSFWorkbook
910
import org.modelcatalogue.builder.spreadsheet.api.CellStyle
@@ -14,7 +15,9 @@ import org.modelcatalogue.builder.spreadsheet.api.Workbook
1415
class PoiWorkbook implements Workbook {
1516

1617
private final XSSFWorkbook workbook
17-
private final Map<String, Closure> namedStyles = [:]
18+
private final Map<String, Closure> namedStylesDefinition = [:]
19+
private final Map<String, PoiCellStyle> namedStyles = [:]
20+
private final Map<String, XSSFDataFormat> formats = [:]
1821
private final List<Resolvable> toBeResolved = []
1922

2023
PoiWorkbook(XSSFWorkbook workbook) {
@@ -33,7 +36,7 @@ class PoiWorkbook implements Workbook {
3336

3437
@Override
3538
void style(String name, @DelegatesTo(CellStyle.class) @ClosureParams(value=FromString.class, options = "org.modelcatalogue.builder.spreadsheet.api.CellStyle") Closure styleDefinition) {
36-
namedStyles[name] = styleDefinition
39+
namedStylesDefinition[name] = styleDefinition
3740
}
3841

3942
@Override
@@ -46,8 +49,28 @@ class PoiWorkbook implements Workbook {
4649
stylesheet.declareStyles(this)
4750
}
4851

49-
protected Closure getStyle(String name) {
50-
Closure style = namedStyles[name]
52+
XSSFWorkbook getWorkbook() {
53+
return workbook
54+
}
55+
56+
protected PoiCellStyle getStyle(String name) {
57+
PoiCellStyle style = namedStyles[name]
58+
59+
if (style) {
60+
return style
61+
}
62+
63+
style = new PoiCellStyle(this)
64+
style.with getStyleDefinition(name)
65+
style.seal()
66+
67+
namedStyles[name] = style
68+
69+
return style
70+
}
71+
72+
protected Closure getStyleDefinition(String name) {
73+
Closure style = namedStylesDefinition[name]
5174
if (!style) {
5275
throw new IllegalArgumentException("Style '$name' not defined")
5376
}

0 commit comments

Comments
 (0)