Skip to content

Commit 6d8e5e4

Browse files
committed
Implement Vertical text centering feature
Requested in issue #31
1 parent 1ac2e81 commit 6d8e5e4

File tree

5 files changed

+66
-10
lines changed

5 files changed

+66
-10
lines changed

resources/CAS.ods

748 Bytes
Binary file not shown.

src/com/github/miachm/sods/OdsReader.java

+13
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,19 @@ private Style readCellStyleEntry(XmlReaderInstance reader) {
169169
style.setBackgroundColor(new Color(backgroundColor));
170170
}
171171
catch (IllegalArgumentException e) { System.err.println(e.getMessage());}
172+
173+
String verticalAlign = instance.getAttribValue("style:vertical-align");
174+
if (verticalAlign != null) {
175+
Style.VERTICAL_TEXT_ALIGMENT pos = null;
176+
if (verticalAlign.equalsIgnoreCase("middle")) {
177+
pos = Style.VERTICAL_TEXT_ALIGMENT.Middle;
178+
} else if (verticalAlign.equalsIgnoreCase("top")) {
179+
pos = Style.VERTICAL_TEXT_ALIGMENT.Top;
180+
} else if (verticalAlign.equalsIgnoreCase("bottom")) {
181+
pos = Style.VERTICAL_TEXT_ALIGMENT.Bottom;
182+
}
183+
style.setVerticalTextAligment(pos);
184+
}
172185
}
173186

174187
if(instance.getTag().equals("style:paragraph-properties")) {

src/com/github/miachm/sods/OdsWritter.java

+4
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,10 @@ private void writeCellStyle(XMLStreamWriter out, Style style) throws XMLStreamEx
364364
if (style.isWrap()) {
365365
out.writeAttribute("fo:wrap-option", "wrap");
366366
}
367+
368+
if (style.getVerticalTextAligment() != null) {
369+
out.writeAttribute("style:vertical-align", style.getVerticalTextAligment().toString().toLowerCase());
370+
}
367371

368372
if(style.hasBorders()) {
369373
writeBorderStyle(out, style);

src/com/github/miachm/sods/Style.java

+30-10
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.HashMap;
44
import java.util.Map;
5+
import java.util.Objects;
56

67
/**
78
* This a class which represents the formatting of a cell (background color, font size, font style, etc...)
@@ -16,7 +17,8 @@ public final class Style implements Cloneable {
1617
private int fontSize = -1;
1718
private Borders borders = null;
1819
private boolean wrap = false;
19-
private TEXT_ALIGMENT alignment = null;
20+
private TEXT_ALIGMENT horizontal_alignment = null;
21+
private VERTICAL_TEXT_ALIGMENT vertical_alignment = null;
2022
private boolean isDate = false;
2123

2224
/** Defines the text position of a Cell
@@ -25,6 +27,10 @@ public enum TEXT_ALIGMENT {
2527
Left, Center, Right
2628
}
2729

30+
public enum VERTICAL_TEXT_ALIGMENT {
31+
Top, Middle, Bottom
32+
}
33+
2834
/**
2935
* Constructs an empty-default Style.
3036
*/
@@ -219,7 +225,7 @@ public void setWrap(boolean wrap) {
219225
* @return true if the style has table cell properties, false otherwise.
220226
*/
221227
public boolean hasTableCellProperties() {
222-
return backgroundColor != null || hasBorders() || wrap;
228+
return backgroundColor != null || hasBorders() || wrap || vertical_alignment != null;
223229
}
224230

225231
/**
@@ -236,17 +242,24 @@ public boolean hasBorders() {
236242
* @param p {@link TEXT_ALIGMENT} Left, Center, Right
237243
*/
238244
public void setTextAligment (TEXT_ALIGMENT p) {
239-
alignment = p;
245+
horizontal_alignment = p;
240246
}
241247

242248
/**
243249
* Get text aligment of the cell
244250
* @return p {@link TEXT_ALIGMENT} Left, Center, Right
245251
*/
246252
public TEXT_ALIGMENT getTextAligment() {
247-
return alignment;
253+
return horizontal_alignment;
254+
}
255+
256+
257+
public void setVerticalTextAligment (VERTICAL_TEXT_ALIGMENT p) {
258+
vertical_alignment = p;
248259
}
249260

261+
public VERTICAL_TEXT_ALIGMENT getVerticalTextAligment() { return vertical_alignment;}
262+
250263
public Object clone() throws CloneNotSupportedException {
251264
return super.clone();
252265
}
@@ -272,14 +285,17 @@ public boolean equals(Object o) {
272285
if (italic != style.italic) return false;
273286
if (underline != style.underline) return false;
274287
if (fontSize != style.fontSize) return false;
275-
if (borders != null ? !borders.equals(style.borders) : style.borders != null) return false;
288+
if (!Objects.equals(borders, style.borders)) return false;
276289
if (wrap != style.wrap) return false;
277-
if (fontColor != null ? !fontColor.equals(style.fontColor) : style.fontColor != null) return false;
278-
if (backgroundColor != null ? !backgroundColor.equals(style.backgroundColor) : style.backgroundColor != null)
290+
if (!Objects.equals(fontColor, style.fontColor)) return false;
291+
if (!Objects.equals(backgroundColor, style.backgroundColor))
279292
return false;
280293
if (isDate != style.isDate)
281294
return false;
282-
return alignment == style.alignment;
295+
if (horizontal_alignment != style.horizontal_alignment)
296+
return false;
297+
298+
return vertical_alignment == style.vertical_alignment;
283299
}
284300

285301
@Override
@@ -293,7 +309,8 @@ public int hashCode() {
293309
result = 31 * result + (borders != null ? borders.hashCode() : 0);
294310
result = 31 * result + (wrap ? 1 : 0);
295311
result = 31 * result + (isDate ? 1 : 0);
296-
result = 31 * result + (alignment != null ? alignment.hashCode() : 0);
312+
result = 31 * result + (horizontal_alignment != null ? horizontal_alignment.hashCode() : 0);
313+
result = 31 * result + (vertical_alignment != null ? vertical_alignment.hashCode() : 0);
297314
return result;
298315
}
299316

@@ -341,9 +358,12 @@ public Map<String, String> getCssStyles()
341358
result.put("white-space", "normal");
342359
}
343360

344-
if(alignment != null)
361+
if(horizontal_alignment != null)
345362
result.put("text-align", getTextAligment().toString());
346363

364+
if(vertical_alignment != null)
365+
result.put("vertical-align", getVerticalTextAligment().toString().toLowerCase());
366+
347367
return result;
348368
}
349369

tests/com/github/miachm/sods/CellCustomStyles.java

+19
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,23 @@ public void testCustomFontSize() throws IOException {
6060
SpreadSheet spreadSheet = new SpreadSheet(new File("resources/floatfontsize.ods"));
6161
assertEquals(spreadSheet.getSheet(0).getDataRange().getStyle().getFontSize(), 4);
6262
}
63+
64+
65+
@Test
66+
public void testVerticalsAligment()
67+
{
68+
for (Style.VERTICAL_TEXT_ALIGMENT aligment : Style.VERTICAL_TEXT_ALIGMENT.values()) {
69+
Style style = new Style();
70+
assertNull(style.getVerticalTextAligment());
71+
style.setVerticalTextAligment(aligment);
72+
assertEquals(style.getVerticalTextAligment(), aligment);
73+
74+
Sheet sheet = new Sheet("A", 2, 2);
75+
Range range = sheet.getRange(0, 0);
76+
range.setStyle(style);
77+
78+
sheet = saveAndLoad(sheet);
79+
assertEquals(sheet.getRange(0, 0).getStyle().getVerticalTextAligment(), aligment);
80+
}
81+
}
6382
}

0 commit comments

Comments
 (0)