Skip to content

Commit 2b2fc0b

Browse files
authored
Merge pull request #360 from JakKowalCzyk/add-column-style
#344 Added possibility to define style for entire column.
2 parents b7c6f6e + c06e671 commit 2b2fc0b

File tree

6 files changed

+827
-491
lines changed

6 files changed

+827
-491
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package org.dhatim.fastexcel;
2+
3+
import java.util.Map;
4+
import java.util.Objects;
5+
6+
/**
7+
* Definition of a column.
8+
*/
9+
class Column {
10+
11+
/**
12+
* Worksheet where this column is defined.
13+
*/
14+
private final Worksheet worksheet;
15+
/**
16+
* Position of the column
17+
*/
18+
private final int colNumber;
19+
20+
private int style;
21+
22+
/**
23+
* Constructor
24+
* @param worksheet Worksheet where this column is defined.
25+
* @param colNumber Position of the column
26+
*/
27+
Column(Worksheet worksheet, int colNumber) {
28+
this.worksheet = Objects.requireNonNull(worksheet);
29+
this.colNumber = Objects.requireNonNull(colNumber);
30+
this.style = 0;
31+
}
32+
33+
static Column noStyle(Worksheet worksheet, int c) {
34+
return new Column(worksheet, c);
35+
}
36+
37+
/**
38+
* Get parent worksheet.
39+
*
40+
* @return Parent worksheet.
41+
*/
42+
public Worksheet getWorksheet() {
43+
return worksheet;
44+
}
45+
46+
/**
47+
* Get column number.
48+
*
49+
* @return Column number.
50+
*/
51+
public int getColNumber() {
52+
return colNumber;
53+
}
54+
55+
/**
56+
* Get a new style setter for this column.
57+
*
58+
* @return Newly created style setter.
59+
*/
60+
public ColumnStyleSetter style() {
61+
return new ColumnStyleSetter(this);
62+
}
63+
64+
@Override
65+
public boolean equals(Object o) {
66+
if (this == o) return true;
67+
if (o == null || getClass() != o.getClass()) return false;
68+
Column column = (Column) o;
69+
return colNumber == column.colNumber && Objects.equals(worksheet, column.worksheet);
70+
}
71+
72+
@Override
73+
public int hashCode() {
74+
return Objects.hash(worksheet, colNumber);
75+
}
76+
77+
/**
78+
* Return the style assigned to this column.
79+
*
80+
* @return style.
81+
*/
82+
Integer getStyle() {
83+
return style;
84+
}
85+
86+
/**
87+
* Apply new (merged) style to this column.
88+
*
89+
* @param stylesMap new styles map
90+
*/
91+
void applyStyle(Map<Integer, Integer> stylesMap) {
92+
this.style = stylesMap.get(this.style);
93+
}
94+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright 2016 Dhatim.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.dhatim.fastexcel;
17+
18+
import java.util.Collections;
19+
import java.util.HashSet;
20+
21+
import static org.dhatim.fastexcel.Worksheet.MAX_ROWS;
22+
23+
/**
24+
* Helper class to set style elements on a column. This class
25+
* implements the builder pattern to easily modify a bunch of attributes.<p>
26+
* For example:
27+
* <blockquote><pre>
28+
* Worksheet ws = ...
29+
* ws.range(1, 1, 1, 10).style().borderStyle("thin").bold().fillColor(Color.GRAY4).horizontalAlignment("center").set();
30+
* </pre></blockquote>
31+
*/
32+
public class ColumnStyleSetter extends GenericStyleSetter<ColumnStyleSetter> {
33+
34+
/**
35+
* Column where the style is applied.
36+
*/
37+
private final Column column;
38+
39+
/**
40+
* Constructor.
41+
*
42+
* @param column Column where style is modified.
43+
*/
44+
ColumnStyleSetter(Column column) {
45+
super(column.getWorksheet());
46+
this.column = column;
47+
}
48+
49+
/**
50+
* Apply style elements. <b>Do not forget to call this method when you are
51+
* done otherwise style changes are lost!</b>
52+
*/
53+
public void set() {
54+
super.setStyle(false, new HashSet<>(Collections.singletonList(column.getStyle())), column::applyStyle);
55+
}
56+
57+
@Override
58+
protected Range getRange() {
59+
int colNumber = column.getColNumber();
60+
return column.getWorksheet().range(0, colNumber, MAX_ROWS - 1, colNumber);
61+
}
62+
63+
@Override
64+
protected ColumnStyleSetter getThis() {
65+
return this;
66+
}
67+
}

0 commit comments

Comments
 (0)