2011-08-17 06:18:31 -04:00
|
|
|
/*
|
|
|
|
* ====================================================================
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership.
|
|
|
|
* The ASF licenses this file to You under the Apache License, Version 2.0
|
|
|
|
* (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
* ====================================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.apache.poi.xslf.usermodel;
|
|
|
|
|
2012-02-14 02:02:54 -05:00
|
|
|
import java.awt.Color;
|
|
|
|
|
2015-08-24 19:15:14 -04:00
|
|
|
import org.apache.poi.sl.usermodel.TableCell;
|
2015-02-21 05:56:03 -05:00
|
|
|
import org.apache.poi.sl.usermodel.VerticalAlignment;
|
2011-10-28 10:25:53 -04:00
|
|
|
import org.apache.poi.util.Units;
|
2015-08-24 19:15:14 -04:00
|
|
|
import org.openxmlformats.schemas.drawingml.x2006.main.CTLineEndProperties;
|
|
|
|
import org.openxmlformats.schemas.drawingml.x2006.main.CTLineProperties;
|
|
|
|
import org.openxmlformats.schemas.drawingml.x2006.main.CTSRgbColor;
|
|
|
|
import org.openxmlformats.schemas.drawingml.x2006.main.CTSolidColorFillProperties;
|
|
|
|
import org.openxmlformats.schemas.drawingml.x2006.main.CTTableCell;
|
|
|
|
import org.openxmlformats.schemas.drawingml.x2006.main.CTTableCellProperties;
|
|
|
|
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextBody;
|
|
|
|
import org.openxmlformats.schemas.drawingml.x2006.main.STCompoundLine;
|
|
|
|
import org.openxmlformats.schemas.drawingml.x2006.main.STLineCap;
|
|
|
|
import org.openxmlformats.schemas.drawingml.x2006.main.STLineEndLength;
|
|
|
|
import org.openxmlformats.schemas.drawingml.x2006.main.STLineEndType;
|
|
|
|
import org.openxmlformats.schemas.drawingml.x2006.main.STLineEndWidth;
|
|
|
|
import org.openxmlformats.schemas.drawingml.x2006.main.STPenAlignment;
|
|
|
|
import org.openxmlformats.schemas.drawingml.x2006.main.STPresetLineDashVal;
|
|
|
|
import org.openxmlformats.schemas.drawingml.x2006.main.STTextAnchoringType;
|
2011-08-17 06:18:31 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents a cell of a table in a .pptx presentation
|
|
|
|
*/
|
2015-08-24 19:15:14 -04:00
|
|
|
public class XSLFTableCell extends XSLFTextShape implements TableCell<XSLFShape,XSLFTextParagraph> {
|
2011-08-17 06:18:31 -04:00
|
|
|
static double defaultBorderWidth = 1.0;
|
2015-03-07 18:35:40 -05:00
|
|
|
private CTTableCellProperties _tcPr = null;
|
2011-08-17 06:18:31 -04:00
|
|
|
|
|
|
|
/*package*/ XSLFTableCell(CTTableCell cell, XSLFSheet sheet){
|
|
|
|
super(cell, sheet);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected CTTextBody getTextBody(boolean create){
|
2015-03-07 18:35:40 -05:00
|
|
|
CTTableCell cell = (CTTableCell)getXmlObject();
|
2011-08-17 06:18:31 -04:00
|
|
|
CTTextBody txBody = cell.getTxBody();
|
|
|
|
if (txBody == null && create) {
|
|
|
|
txBody = cell.addNewTxBody();
|
|
|
|
txBody.addNewBodyPr();
|
|
|
|
txBody.addNewLstStyle();
|
|
|
|
}
|
|
|
|
return txBody;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CTTableCell prototype() {
|
|
|
|
CTTableCell cell = CTTableCell.Factory.newInstance();
|
|
|
|
CTTableCellProperties pr = cell.addNewTcPr();
|
|
|
|
pr.addNewLnL().addNewNoFill();
|
|
|
|
pr.addNewLnR().addNewNoFill();
|
|
|
|
pr.addNewLnT().addNewNoFill();
|
|
|
|
pr.addNewLnB().addNewNoFill();
|
|
|
|
return cell;
|
|
|
|
}
|
|
|
|
|
2015-03-07 18:35:40 -05:00
|
|
|
protected CTTableCellProperties getCellProperties(boolean create) {
|
|
|
|
if (_tcPr == null) {
|
|
|
|
CTTableCell cell = (CTTableCell)getXmlObject();
|
|
|
|
_tcPr = cell.getTcPr();
|
|
|
|
if (_tcPr == null && create) {
|
|
|
|
_tcPr = cell.addNewTcPr();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return _tcPr;
|
|
|
|
}
|
|
|
|
|
2011-08-17 06:18:31 -04:00
|
|
|
@Override
|
2011-11-07 04:12:16 -05:00
|
|
|
public void setLeftInset(double margin){
|
2015-03-07 18:35:40 -05:00
|
|
|
CTTableCellProperties pr = getCellProperties(true);
|
2011-08-17 06:18:31 -04:00
|
|
|
pr.setMarL(Units.toEMU(margin));
|
|
|
|
}
|
2012-02-14 02:02:54 -05:00
|
|
|
|
2011-08-17 06:18:31 -04:00
|
|
|
@Override
|
2011-11-07 04:12:16 -05:00
|
|
|
public void setRightInset(double margin){
|
2015-03-07 18:35:40 -05:00
|
|
|
CTTableCellProperties pr = getCellProperties(true);
|
2011-08-17 06:18:31 -04:00
|
|
|
pr.setMarR(Units.toEMU(margin));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2011-11-07 04:12:16 -05:00
|
|
|
public void setTopInset(double margin){
|
2015-03-07 18:35:40 -05:00
|
|
|
CTTableCellProperties pr = getCellProperties(true);
|
2011-08-17 06:18:31 -04:00
|
|
|
pr.setMarT(Units.toEMU(margin));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2011-11-07 04:12:16 -05:00
|
|
|
public void setBottomInset(double margin){
|
2015-03-07 18:35:40 -05:00
|
|
|
CTTableCellProperties pr = getCellProperties(true);
|
2011-08-17 06:18:31 -04:00
|
|
|
pr.setMarB(Units.toEMU(margin));
|
|
|
|
}
|
|
|
|
|
2015-03-07 18:35:40 -05:00
|
|
|
private CTLineProperties getCTLine(char bltr, boolean create) {
|
|
|
|
CTTableCellProperties pr = getCellProperties(create);
|
|
|
|
if (pr == null) return null;
|
|
|
|
|
|
|
|
switch (bltr) {
|
|
|
|
case 'b':
|
|
|
|
return (pr.isSetLnB()) ? pr.getLnB() : (create ? pr.addNewLnB() : null);
|
|
|
|
case 'l':
|
|
|
|
return (pr.isSetLnL()) ? pr.getLnL() : (create ? pr.addNewLnL() : null);
|
|
|
|
case 't':
|
|
|
|
return (pr.isSetLnT()) ? pr.getLnT() : (create ? pr.addNewLnT() : null);
|
|
|
|
case 'r':
|
|
|
|
return (pr.isSetLnR()) ? pr.getLnR() : (create ? pr.addNewLnR() : null);
|
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
}
|
2011-08-17 06:18:31 -04:00
|
|
|
}
|
2015-03-07 18:35:40 -05:00
|
|
|
|
|
|
|
private void setBorderWidth(char bltr, double width) {
|
|
|
|
CTLineProperties ln = getCTLine(bltr, true);
|
2011-08-17 06:18:31 -04:00
|
|
|
ln.setW(Units.toEMU(width));
|
|
|
|
}
|
|
|
|
|
2015-03-07 18:35:40 -05:00
|
|
|
private double getBorderWidth(char bltr) {
|
|
|
|
CTLineProperties ln = getCTLine(bltr, false);
|
2015-07-14 20:30:21 -04:00
|
|
|
return (ln == null || !ln.isSetW()) ? defaultBorderWidth : Units.toPoints(ln.getW());
|
2011-08-17 06:18:31 -04:00
|
|
|
}
|
|
|
|
|
2015-03-07 18:35:40 -05:00
|
|
|
private void setBorderColor(char bltr, Color color) {
|
|
|
|
CTLineProperties ln = getCTLine(bltr, true);
|
2011-08-17 06:18:31 -04:00
|
|
|
|
|
|
|
if(color == null){
|
|
|
|
ln.addNewNoFill();
|
|
|
|
if(ln.isSetSolidFill()) ln.unsetSolidFill();
|
|
|
|
} else {
|
|
|
|
if(ln.isSetNoFill()) ln.unsetNoFill();
|
|
|
|
|
|
|
|
if(!ln.isSetPrstDash()) ln.addNewPrstDash().setVal(STPresetLineDashVal.SOLID);
|
|
|
|
ln.setCmpd(STCompoundLine.SNG);
|
|
|
|
ln.setAlgn(STPenAlignment.CTR);
|
|
|
|
ln.setCap(STLineCap.FLAT);
|
|
|
|
ln.addNewRound();
|
|
|
|
|
|
|
|
CTLineEndProperties hd = ln.addNewHeadEnd();
|
|
|
|
hd.setType(STLineEndType.NONE);
|
|
|
|
hd.setW(STLineEndWidth.MED);
|
|
|
|
hd.setLen(STLineEndLength.MED);
|
|
|
|
|
|
|
|
CTLineEndProperties tl = ln.addNewTailEnd();
|
|
|
|
tl.setType(STLineEndType.NONE);
|
|
|
|
tl.setW(STLineEndWidth.MED);
|
|
|
|
tl.setLen(STLineEndLength.MED);
|
|
|
|
|
|
|
|
CTSRgbColor rgb = CTSRgbColor.Factory.newInstance();
|
|
|
|
rgb.setVal(new byte[]{(byte)color.getRed(), (byte)color.getGreen(), (byte)color.getBlue()});
|
|
|
|
ln.addNewSolidFill().setSrgbClr(rgb);
|
|
|
|
}
|
2015-03-07 18:35:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
private Color getBorderColor(char bltr) {
|
|
|
|
CTLineProperties ln = getCTLine(bltr,false);
|
|
|
|
if (ln == null || ln.isSetNoFill() || !ln.isSetSolidFill()) return null;
|
2011-08-17 06:18:31 -04:00
|
|
|
|
|
|
|
CTSolidColorFillProperties fill = ln.getSolidFill();
|
2015-03-07 18:35:40 -05:00
|
|
|
if (!fill.isSetSrgbClr()) {
|
2011-08-17 06:18:31 -04:00
|
|
|
// TODO for now return null for all colors except explicit RGB
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
byte[] val = fill.getSrgbClr().getVal();
|
|
|
|
return new Color(0xFF & val[0], 0xFF & val[1], 0xFF & val[2]);
|
2015-03-07 18:35:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setBorderLeft(double width) {
|
|
|
|
setBorderWidth('l', width);
|
2011-08-17 06:18:31 -04:00
|
|
|
}
|
2015-03-07 18:35:40 -05:00
|
|
|
|
|
|
|
public double getBorderLeft() {
|
|
|
|
return getBorderWidth('l');
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setBorderLeftColor(Color color) {
|
|
|
|
setBorderColor('l', color);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Color getBorderLeftColor() {
|
|
|
|
return getBorderColor('l');
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setBorderRight(double width) {
|
|
|
|
setBorderWidth('r', width);
|
|
|
|
}
|
|
|
|
|
|
|
|
public double getBorderRight() {
|
|
|
|
return getBorderWidth('r');
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setBorderRightColor(Color color) {
|
|
|
|
setBorderColor('r', color);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Color getBorderRightColor() {
|
|
|
|
return getBorderColor('r');
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setBorderTop(double width) {
|
|
|
|
setBorderWidth('t', width);
|
|
|
|
}
|
|
|
|
|
|
|
|
public double getBorderTop() {
|
|
|
|
return getBorderWidth('t');
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setBorderTopColor(Color color) {
|
|
|
|
setBorderColor('t', color);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Color getBorderTopColor() {
|
|
|
|
return getBorderColor('t');
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setBorderBottom(double width) {
|
|
|
|
setBorderWidth('b', width);
|
|
|
|
}
|
|
|
|
|
|
|
|
public double getBorderBottom() {
|
|
|
|
return getBorderWidth('b');
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setBorderBottomColor(Color color) {
|
|
|
|
setBorderColor('b', color);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Color getBorderBottomColor(){
|
|
|
|
return getBorderColor('b');
|
|
|
|
}
|
|
|
|
|
2011-08-17 06:18:31 -04:00
|
|
|
/**
|
|
|
|
* Specifies a solid color fill. The shape is filled entirely with the specified color.
|
|
|
|
*
|
|
|
|
* @param color the solid color fill.
|
|
|
|
* The value of <code>null</code> unsets the solidFIll attribute from the underlying xml
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public void setFillColor(Color color) {
|
2015-03-07 18:35:40 -05:00
|
|
|
CTTableCellProperties spPr = getCellProperties(true);
|
2011-08-17 06:18:31 -04:00
|
|
|
if (color == null) {
|
|
|
|
if(spPr.isSetSolidFill()) spPr.unsetSolidFill();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
CTSolidColorFillProperties fill = spPr.isSetSolidFill() ? spPr.getSolidFill() : spPr.addNewSolidFill();
|
|
|
|
|
|
|
|
CTSRgbColor rgb = CTSRgbColor.Factory.newInstance();
|
|
|
|
rgb.setVal(new byte[]{(byte) color.getRed(), (byte) color.getGreen(), (byte) color.getBlue()});
|
|
|
|
|
|
|
|
fill.setSrgbClr(rgb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @return solid fill color of null if not set
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public Color getFillColor(){
|
2015-03-07 18:35:40 -05:00
|
|
|
CTTableCellProperties spPr = getCellProperties(false);
|
|
|
|
if (spPr == null || !spPr.isSetSolidFill()) return null;
|
2011-08-17 06:18:31 -04:00
|
|
|
|
|
|
|
CTSolidColorFillProperties fill = spPr.getSolidFill();
|
2015-03-07 18:35:40 -05:00
|
|
|
if (!fill.isSetSrgbClr()) {
|
2011-08-17 06:18:31 -04:00
|
|
|
// TODO for now return null for all colors except explicit RGB
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
byte[] val = fill.getSrgbClr().getVal();
|
|
|
|
return new Color(0xFF & val[0], 0xFF & val[1], 0xFF & val[2]);
|
|
|
|
}
|
|
|
|
|
2012-02-14 02:02:54 -05:00
|
|
|
void setGridSpan(int gridSpan_) {
|
2015-03-07 18:35:40 -05:00
|
|
|
((CTTableCell)getXmlObject()).setGridSpan(gridSpan_);
|
2012-02-14 02:02:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void setRowSpan(int rowSpan_) {
|
2015-03-07 18:35:40 -05:00
|
|
|
((CTTableCell)getXmlObject()).setRowSpan(rowSpan_);
|
2012-02-14 02:02:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void setHMerge(boolean merge_) {
|
2015-03-07 18:35:40 -05:00
|
|
|
((CTTableCell)getXmlObject()).setHMerge(merge_);
|
2012-02-14 02:02:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void setVMerge(boolean merge_) {
|
2015-03-07 18:35:40 -05:00
|
|
|
((CTTableCell)getXmlObject()).setVMerge(merge_);
|
2012-02-14 02:02:54 -05:00
|
|
|
}
|
2012-02-21 07:39:23 -05:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setVerticalAlignment(VerticalAlignment anchor){
|
2015-03-07 18:35:40 -05:00
|
|
|
CTTableCellProperties cellProps = getCellProperties(true);
|
|
|
|
if(anchor == null) {
|
|
|
|
if(cellProps.isSetAnchor()) {
|
|
|
|
cellProps.unsetAnchor();
|
2012-02-21 07:39:23 -05:00
|
|
|
}
|
2015-03-07 18:35:40 -05:00
|
|
|
} else {
|
|
|
|
cellProps.setAnchor(STTextAnchoringType.Enum.forInt(anchor.ordinal() + 1));
|
|
|
|
}
|
2012-02-21 07:39:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public VerticalAlignment getVerticalAlignment(){
|
2015-03-07 18:35:40 -05:00
|
|
|
CTTableCellProperties cellProps = getCellProperties(false);
|
2012-02-21 07:39:23 -05:00
|
|
|
|
|
|
|
VerticalAlignment align = VerticalAlignment.TOP;
|
|
|
|
if(cellProps != null && cellProps.isSetAnchor()) {
|
|
|
|
int ival = cellProps.getAnchor().intValue();
|
|
|
|
align = VerticalAlignment.values()[ival - 1];
|
|
|
|
}
|
|
|
|
return align;
|
|
|
|
}
|
|
|
|
|
2011-08-17 06:18:31 -04:00
|
|
|
}
|