View | Details | Raw Unified | Return to bug 46006
Collapse All | Expand All

(-)src/ooxml/java/org/apache/poi/xssf/usermodel/extensions/XSSFCellBorder.java (-48 / +80 lines)
Lines 17-30 Link Here
17
package org.apache.poi.xssf.usermodel.extensions;
17
package org.apache.poi.xssf.usermodel.extensions;
18
18
19
19
20
import java.util.LinkedList;
20
import org.apache.poi.xssf.usermodel.BorderStyle;
21
22
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorder;
21
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorder;
23
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorderPr;
22
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorderPr;
24
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STBorderStyle;
23
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STBorderStyle;
25
import org.apache.poi.xssf.usermodel.BorderStyle;
26
24
27
25
/**
26
 * This element contains border formatting information, specifying border definition formats (left, right, top, bottom, diagonal) 
27
 * for cells in the workbook.
28
 * Color is optional.
29
 */
28
public class XSSFCellBorder {
30
public class XSSFCellBorder {
29
31
30
    private CTBorder border;
32
    private CTBorder border;
Lines 33-117 Link Here
33
     * Creates a Cell Border from the supplied XML definition
35
     * Creates a Cell Border from the supplied XML definition
34
     */
36
     */
35
    public XSSFCellBorder(CTBorder border) {
37
    public XSSFCellBorder(CTBorder border) {
36
        this.border = border;
38
	this.border = border;
37
    }
39
    }
38
    /**
40
    /**
39
     * Creates a new, empty Cell Border, on the
41
     * Creates a new, empty Cell Border, on the
40
     *  given Styles Table
42
     *  given Styles Table
41
     */
43
     */
42
    public XSSFCellBorder() {
44
    public XSSFCellBorder() {
43
        border = CTBorder.Factory.newInstance();
45
	border = CTBorder.Factory.newInstance();
44
    }
46
    }
45
47
    
48
/**
49
 * The enumeration value indicating the side being used for a cell border.
50
 *
51
 */
46
    public static enum BorderSide {
52
    public static enum BorderSide {
47
        TOP, RIGHT, BOTTOM, LEFT
53
	TOP, RIGHT, BOTTOM, LEFT
48
    }
54
    }
49
55
56
    /**
57
     * Returns the underlying XML bean.
58
     * @return CTBorder 
59
     */
50
    public CTBorder getCTBorder() {
60
    public CTBorder getCTBorder() {
51
        return border;
61
	return border;
52
    }
62
    }
53
63
64
    /**
65
     * Get the type of border to use for the selected border
66
     * @param side -  - where to apply the color definition 
67
     * @see BorderStyle
68
     * @return borderstyle - the type of border to use. default value is NONE if border style is not set.
69
     */
54
    public BorderStyle getBorderStyle(BorderSide side) {
70
    public BorderStyle getBorderStyle(BorderSide side) {
55
        CTBorderPr ctBorder = getBorder(side);
71
	CTBorderPr ctBorder = getBorder(side);
56
        STBorderStyle.Enum border = ctBorder == null ? STBorderStyle.NONE : ctBorder.getStyle();
72
	STBorderStyle.Enum border = ctBorder == null ? STBorderStyle.NONE : ctBorder.getStyle();
57
        return BorderStyle.values()[border.intValue() - 1];
73
	return BorderStyle.values()[border.intValue() - 1];
58
    }
74
    }
59
75
76
    /**
77
     * Set the type of border to use for the selected border
78
     * @param side -  - where to apply the color definition
79
     * @param style - border style
80
     * @see BorderStyle
81
     */
60
    public void setBorderStyle(BorderSide side, BorderStyle style) {
82
    public void setBorderStyle(BorderSide side, BorderStyle style) {
61
        getBorder(side, true).setStyle(STBorderStyle.Enum.forInt(style.ordinal() + 1));
83
	getBorder(side, true).setStyle(STBorderStyle.Enum.forInt(style.ordinal() + 1));
62
    }
84
    }
63
85
    /**
86
     * Get the color to use for the selected border 
87
     * @param side - where to apply the color definition
88
     * @return color - color to use as XSSFColor. null if color is not set
89
     */
64
    public XSSFColor getBorderColor(BorderSide side) {
90
    public XSSFColor getBorderColor(BorderSide side) {
65
        CTBorderPr borderPr = getBorder(side);
91
	CTBorderPr borderPr = getBorder(side);
66
        return borderPr != null && borderPr.isSetColor() ?
92
	return borderPr != null && borderPr.isSetColor() ?
67
                new XSSFColor(borderPr.getColor()) : null;
93
		new XSSFColor(borderPr.getColor()) : null;
68
    }
94
    }
69
95
    /**
96
     * Set the color to use for the selected border
97
     *
98
     * @param side - where to apply the color definition
99
     * @param color - the color to use
100
     */
70
    public void setBorderColor(BorderSide side, XSSFColor color) {
101
    public void setBorderColor(BorderSide side, XSSFColor color) {
71
        CTBorderPr borderPr = getBorder(side, true);
102
	CTBorderPr borderPr = getBorder(side, true);
72
        if(color == null) borderPr.unsetColor();
103
	if(color == null) borderPr.unsetColor();
73
        else borderPr.setColor(color.getCTColor());
104
	else borderPr.setColor(color.getCTColor());
74
    }
105
    }
75
106
76
    private CTBorderPr getBorder(BorderSide side) {
107
    private CTBorderPr getBorder(BorderSide side) {
77
        return getBorder(side, false);
108
	return getBorder(side, false);
78
    }
109
    }
79
110
111
    
80
    private CTBorderPr getBorder(BorderSide side, boolean ensure) {
112
    private CTBorderPr getBorder(BorderSide side, boolean ensure) {
81
        CTBorderPr borderPr;
113
	CTBorderPr borderPr;
82
        switch (side) {
114
	switch (side) {
83
            case TOP:
115
	case TOP:
84
                borderPr = border.getTop();
116
	    borderPr = border.getTop();
85
                if(ensure && borderPr == null) borderPr = border.addNewTop();
117
	    if(ensure && borderPr == null) borderPr = border.addNewTop();
86
                break;
118
	    break;
87
            case RIGHT:
119
	case RIGHT:
88
                borderPr = border.getRight();
120
	    borderPr = border.getRight();
89
                if(ensure && borderPr == null) borderPr = border.addNewRight();
121
	    if(ensure && borderPr == null) borderPr = border.addNewRight();
90
                break;
122
	    break;
91
            case BOTTOM:
123
	case BOTTOM:
92
                borderPr = border.getBottom();
124
	    borderPr = border.getBottom();
93
                if(ensure && borderPr == null) borderPr = border.addNewBottom();
125
	    if(ensure && borderPr == null) borderPr = border.addNewBottom();
94
                break;
126
	    break;
95
            case LEFT:
127
	case LEFT:
96
                borderPr = border.getLeft();
128
	    borderPr = border.getLeft();
97
                if(ensure && borderPr == null) borderPr = border.addNewLeft();
129
	    if(ensure && borderPr == null) borderPr = border.addNewLeft();
98
                break;
130
	    break;
99
            default:
131
	default:
100
                throw new IllegalArgumentException("No suitable side specified for the border");
132
	    throw new IllegalArgumentException("No suitable side specified for the border");
101
        }
133
	}
102
        return borderPr;
134
	return borderPr;
103
    }
135
    }
104
136
105
137
106
    public int hashCode(){
138
    public int hashCode(){
107
        return border.toString().hashCode();
139
	return border.toString().hashCode();
108
    }
140
    }
109
141
110
    public boolean equals(Object o){
142
    public boolean equals(Object o){
111
        if(!(o instanceof XSSFCellBorder)) return false;
143
	if(!(o instanceof XSSFCellBorder)) return false;
112
144
113
        XSSFCellBorder cf = (XSSFCellBorder)o;
145
	XSSFCellBorder cf = (XSSFCellBorder)o;
114
        return border.toString().equals(cf.getCTBorder().toString());
146
	return border.toString().equals(cf.getCTBorder().toString());
115
    }
147
    }
116
148
117
}
149
}
(-)src/ooxml/java/org/apache/poi/xssf/usermodel/extensions/XSSFCellAlignment.java (-24 / +88 lines)
Lines 30-89 Link Here
30
30
31
    private CTCellAlignment cellAlignement;
31
    private CTCellAlignment cellAlignement;
32
32
33
    /**
34
     * Creates a Cell Alignment from the supplied XML definition
35
     * @param cellAlignment
36
     */
33
    public XSSFCellAlignment(CTCellAlignment cellAlignment) {
37
    public XSSFCellAlignment(CTCellAlignment cellAlignment) {
34
        this.cellAlignement = cellAlignment;
38
	this.cellAlignement = cellAlignment;
35
    }
39
    }
36
40
41
    /**
42
     * Get the type of vertical alignment for the cell
43
     * @see VerticalAlignment
44
     * @return the type of aligment
45
     */
37
    public VerticalAlignment getVertical() {
46
    public VerticalAlignment getVertical() {
38
        STVerticalAlignment.Enum align = cellAlignement.getVertical();
47
	STVerticalAlignment.Enum align = cellAlignement.getVertical();
39
        if(align == null) align = STVerticalAlignment.BOTTOM;
48
	if(align == null) align = STVerticalAlignment.BOTTOM;
40
49
41
        return VerticalAlignment.values()[align.intValue() - 1];
50
	return VerticalAlignment.values()[align.intValue() - 1];
42
    }
51
    }
43
52
    /**
53
     * Set the type of vertical alignment for the cell
54
     * @see VerticalAlignment
55
     * @param align - the type of alignment
56
     */
44
    public void setVertical(VerticalAlignment vertical) {
57
    public void setVertical(VerticalAlignment vertical) {
45
        cellAlignement.setVertical(STVerticalAlignment.Enum.forInt(vertical.ordinal() + 1));
58
	cellAlignement.setVertical(STVerticalAlignment.Enum.forInt(vertical.ordinal() + 1));
46
    }
59
    }
47
60
    /**
61
     * Get the type of horizontal alignment for the cell
62
     * @see HorizontalAlignment
63
     * @return the type of aligment
64
     */
48
    public HorizontalAlignment getHorizontal() {
65
    public HorizontalAlignment getHorizontal() {
49
        STHorizontalAlignment.Enum align = cellAlignement.getHorizontal();
66
	STHorizontalAlignment.Enum align = cellAlignement.getHorizontal();
50
        if(align == null) align = STHorizontalAlignment.GENERAL;
67
	if(align == null) align = STHorizontalAlignment.GENERAL;
51
68
52
        return HorizontalAlignment.values()[align.intValue() - 1];
69
	return HorizontalAlignment.values()[align.intValue() - 1];
53
    }
70
    }
54
71
    /**
72
     * Set the type of horizontal alignment for the cell
73
     * @see HorizontalAlignment
74
     * @param align - the type of alignment
75
     */
55
    public void setHorizontal(HorizontalAlignment align) {
76
    public void setHorizontal(HorizontalAlignment align) {
56
        cellAlignement.setHorizontal(STHorizontalAlignment.Enum.forInt(align.ordinal() + 1));
77
	cellAlignement.setHorizontal(STHorizontalAlignment.Enum.forInt(align.ordinal() + 1));
57
    }
78
    }
58
79
    /**
80
     * Get the number of spaces to indent the text in the cell
81
     *
82
     * @return indent - number of spaces
83
     */
59
    public long getIndent() {
84
    public long getIndent() {
60
        return cellAlignement.getIndent();
85
	return cellAlignement.getIndent();
61
    }
86
    }
62
87
    /**
88
     * Set the number of spaces to indent the text in the cell
89
     *
90
     * @param indent - number of spaces
91
     */
63
    public void setIndent(long indent) {
92
    public void setIndent(long indent) {
64
        cellAlignement.setIndent(indent);
93
	cellAlignement.setIndent(indent);
65
    }
94
    }
66
95
    /**
96
     * Get the degree of rotation for the text in the cell
97
     * <p>
98
     * Expressed in degrees. Values range from 0 to 180. The first letter of
99
     * the text is considered the center-point of the arc.
100
     * <br/>
101
     * For 0 - 90, the value represents degrees above horizon. For 91-180 the degrees below the
102
     * horizon is calculated as:
103
     * <br/>
104
     * <code>[degrees below horizon] = 90 - textRotation.</code>
105
     * </p>
106
     *
107
     * @return rotation degrees (between 0 and 180 degrees)
108
     */
67
    public long getTextRotation() {
109
    public long getTextRotation() {
68
        return cellAlignement.getTextRotation();
110
	return cellAlignement.getTextRotation();
69
    }
111
    }
70
112
    /**
113
     * Set the degree of rotation for the text in the cell
114
     * <p>
115
     * Expressed in degrees. Values range from 0 to 180. The first letter of
116
     * the text is considered the center-point of the arc.
117
     * <br/>
118
     * For 0 - 90, the value represents degrees above horizon. For 91-180 the degrees below the
119
     * horizon is calculated as:
120
     * <br/>
121
     * <code>[degrees below horizon] = 90 - textRotation.</code>
122
     * </p>
123
     *
124
     * @param rotation - the rotation degrees (between 0 and 180 degrees)
125
     */
71
    public void setTextRotation(long rotation) {
126
    public void setTextRotation(long rotation) {
72
        cellAlignement.setTextRotation(rotation);
127
	cellAlignement.setTextRotation(rotation);
73
    }
128
    }
74
129
    /**
130
     * Whether the text should be wrapped
131
     *
132
     * @return  a boolean value indicating if the text in a cell should be line-wrapped within the cell.
133
     */
75
    public boolean getWrapText() {
134
    public boolean getWrapText() {
76
        return cellAlignement.getWrapText();
135
	return cellAlignement.getWrapText();
77
    }
136
    }
78
137
138
    /**
139
     * Set whether the text should be wrapped
140
     *
141
     * @param wrapped a boolean value indicating if the text in a cell should be line-wrapped within the cell.
142
     */
79
    public void setWrapText(boolean wrapped) {
143
    public void setWrapText(boolean wrapped) {
80
        cellAlignement.setWrapText(wrapped);
144
	cellAlignement.setWrapText(wrapped);
81
    }
145
    }
82
146
83
    /**
147
    /**
84
     * Access to low-level data
148
     * Access to low-level data
85
     */
149
     */
86
    public CTCellAlignment getCTCellAlignment() {
150
    public CTCellAlignment getCTCellAlignment() {
87
        return cellAlignement;
151
	return cellAlignement;
88
    }
152
    }
89
}
153
}
(-)src/ooxml/java/org/apache/poi/xssf/usermodel/extensions/XSSFCellFill.java (-61 / +98 lines)
Lines 16-110 Link Here
16
==================================================================== */
16
==================================================================== */
17
package org.apache.poi.xssf.usermodel.extensions;
17
package org.apache.poi.xssf.usermodel.extensions;
18
18
19
import java.util.List;
20
21
import org.apache.poi.xssf.usermodel.IndexedColors;
22
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor;
19
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor;
23
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFill;
20
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFill;
24
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPatternFill;
21
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPatternFill;
25
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPatternType;
22
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPatternType;
26
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPatternType.Enum;
27
23
24
/**
25
 * This element specifies fill formatting.
26
 * A cell fill consists of a background color, foreground color, and pattern to be applied across the cell.
27
 */
28
public final class XSSFCellFill {
28
public final class XSSFCellFill {
29
	
30
	private CTFill _fill;
31
	
32
	public XSSFCellFill(CTFill fill) {
33
		_fill = fill;
34
	}
35
	
36
	public XSSFCellFill() {
37
		_fill = CTFill.Factory.newInstance();
38
	}
39
	
40
	public XSSFColor getFillBackgroundColor() {
41
        CTPatternFill ptrn = _fill.getPatternFill();
42
        if(ptrn == null) return null;
43
29
44
        CTColor ctColor = ptrn.getBgColor();
30
    private CTFill _fill;
45
		return ctColor == null ? null : new XSSFColor(ctColor);
46
	}
47
31
32
    /**
33
     * Creates a CellFill from the supplied parts
34
     * @param CTFill - fill
35
     */
36
    public XSSFCellFill(CTFill fill) {
37
	_fill = fill;
38
    }
39
40
    /**
41
     * Creates an empty CellFill
42
     */
43
    public XSSFCellFill() {
44
	_fill = CTFill.Factory.newInstance();
45
    }
46
47
    /**
48
     * Get the background fill color.
49
     * 
50
     * @return fill color, null if color is not set
51
     */
52
    public XSSFColor getFillBackgroundColor() {
53
	CTPatternFill ptrn = _fill.getPatternFill();
54
	if(ptrn == null) return null;
55
56
	CTColor ctColor = ptrn.getBgColor();
57
	return ctColor == null ? null : new XSSFColor(ctColor);
58
    }
59
    /**
60
     * Set the background fill color represented as a indexed color value.
61
     * @param index
62
     */
48
    public void setFillBackgroundColor(int index) {
63
    public void setFillBackgroundColor(int index) {
49
        CTPatternFill ptrn = ensureCTPatternFill();
64
	CTPatternFill ptrn = ensureCTPatternFill();
50
        CTColor ctColor = ptrn.isSetBgColor() ? ptrn.getBgColor() : ptrn.addNewBgColor();
65
	CTColor ctColor = ptrn.isSetBgColor() ? ptrn.getBgColor() : ptrn.addNewBgColor();
51
        ctColor.setIndexed(index);
66
	ctColor.setIndexed(index);
52
    }
67
    }
53
68
    /**
69
     * Set the background fill color represented as a {@link XSSFColor} value.
70
     * @param color
71
     */
54
    public void setFillBackgroundColor(XSSFColor color) {
72
    public void setFillBackgroundColor(XSSFColor color) {
55
        CTPatternFill ptrn = ensureCTPatternFill();
73
	CTPatternFill ptrn = ensureCTPatternFill();
56
        ptrn.setBgColor(color.getCTColor());
74
	ptrn.setBgColor(color.getCTColor());
57
    }
75
    }
58
76
    /**
77
     * Get the foreground fill color.
78
     * @return XSSFColor - foreground color. null if color is not set
79
     */
59
    public XSSFColor getFillForegroundColor() {
80
    public XSSFColor getFillForegroundColor() {
60
        CTPatternFill ptrn = _fill.getPatternFill();
81
	CTPatternFill ptrn = _fill.getPatternFill();
61
        if(ptrn == null) return null;
82
	if(ptrn == null) return null;
62
83
63
        CTColor ctColor = ptrn.getFgColor();
84
	CTColor ctColor = ptrn.getFgColor();
64
        return ctColor == null ? null : new XSSFColor(ctColor);
85
	return ctColor == null ? null : new XSSFColor(ctColor);
65
    }
86
    }
66
87
    /**
88
     * Set the foreground fill color as a indexed color value
89
     * @param index - the color to use
90
     */
67
    public void setFillForegroundColor(int index) {
91
    public void setFillForegroundColor(int index) {
68
        CTPatternFill ptrn = ensureCTPatternFill();
92
	CTPatternFill ptrn = ensureCTPatternFill();
69
        CTColor ctColor = ptrn.isSetFgColor() ? ptrn.getFgColor() : ptrn.addNewFgColor();
93
	CTColor ctColor = ptrn.isSetFgColor() ? ptrn.getFgColor() : ptrn.addNewFgColor();
70
        ctColor.setIndexed(index);
94
	ctColor.setIndexed(index);
71
    }
95
    }
72
96
    /**
97
     * Set the foreground fill color represented as a {@link XSSFColor} value.
98
     * @param color - the color to use
99
     */
73
    public void setFillForegroundColor(XSSFColor color) {
100
    public void setFillForegroundColor(XSSFColor color) {
74
        CTPatternFill ptrn = ensureCTPatternFill();
101
	CTPatternFill ptrn = ensureCTPatternFill();
75
        ptrn.setFgColor(color.getCTColor());
102
	ptrn.setFgColor(color.getCTColor());
76
    }
103
    }
77
104
    /**
78
	public STPatternType.Enum getPatternType() {
105
     * get the fill pattern
79
        CTPatternFill ptrn = _fill.getPatternFill();
106
     * @return fill pattern type. null if fill pattern is not set
80
		return ptrn == null ? null : ptrn.getPatternType();
107
     */
81
	}
108
    public STPatternType.Enum getPatternType() {
82
109
	CTPatternFill ptrn = _fill.getPatternFill();
110
	return ptrn == null ? null : ptrn.getPatternType();
111
    }
112
    /**
113
     * set the fill pattern
114
     * @param patternType fill pattern to use
115
     */
83
    public void setPatternType(STPatternType.Enum patternType) {
116
    public void setPatternType(STPatternType.Enum patternType) {
84
        CTPatternFill ptrn = ensureCTPatternFill();
117
	CTPatternFill ptrn = ensureCTPatternFill();
85
        ptrn.setPatternType(patternType);
118
	ptrn.setPatternType(patternType);
86
    }
119
    }
87
120
88
	private CTPatternFill ensureCTPatternFill() {
121
    private CTPatternFill ensureCTPatternFill() {
89
		CTPatternFill patternFill = _fill.getPatternFill();
122
	CTPatternFill patternFill = _fill.getPatternFill();
90
		if (patternFill == null) {
123
	if (patternFill == null) {
91
			patternFill = _fill.addNewPatternFill();
124
	    patternFill = _fill.addNewPatternFill();
92
		}
93
		return patternFill;
94
	}
125
	}
126
	return patternFill;
127
    }
128
    /**
129
     * Returns the underlying XML bean.
130
     * @return CTFill 
131
     */
132
    public CTFill getCTFill() {
133
	return _fill;
134
    }
95
135
96
	public CTFill getCTFill() {
136
97
		return _fill;
98
	}
99
	
100
    public int hashCode(){
137
    public int hashCode(){
101
        return _fill.toString().hashCode();
138
	return _fill.toString().hashCode();
102
    }
139
    }
103
140
104
    public boolean equals(Object o){
141
    public boolean equals(Object o){
105
        if(!(o instanceof XSSFCellFill)) return false;
142
	if(!(o instanceof XSSFCellFill)) return false;
106
143
107
        XSSFCellFill cf = (XSSFCellFill)o;
144
	XSSFCellFill cf = (XSSFCellFill)o;
108
        return _fill.toString().equals(cf.getCTFill().toString());
145
	return _fill.toString().equals(cf.getCTFill().toString());
109
    }
146
    }
110
}
147
}

Return to bug 46006