use of org.apache.poi.sl.usermodel.TableCell.BorderEdge in project poi by apache.
the class DrawTableShape method draw.
public void draw(Graphics2D graphics) {
Drawable d = getGroupShape(graphics);
if (d != null) {
d.draw(graphics);
return;
}
TableShape<?, ?> ts = getShape();
DrawPaint drawPaint = DrawFactory.getInstance(graphics).getPaint(ts);
final int rows = ts.getNumberOfRows();
final int cols = ts.getNumberOfColumns();
// draw background boxes
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
TableCell<?, ?> tc = ts.getCell(row, col);
if (tc == null || tc.isMerged()) {
continue;
}
Paint fillPaint = drawPaint.getPaint(graphics, tc.getFillStyle().getPaint());
graphics.setPaint(fillPaint);
Rectangle2D cellAnc = tc.getAnchor();
graphics.fill(cellAnc);
for (BorderEdge edge : BorderEdge.values()) {
StrokeStyle stroke = tc.getBorderStyle(edge);
if (stroke == null) {
continue;
}
graphics.setStroke(getStroke(stroke));
Paint linePaint = drawPaint.getPaint(graphics, stroke.getPaint());
graphics.setPaint(linePaint);
double x = cellAnc.getX(), y = cellAnc.getY(), w = cellAnc.getWidth(), h = cellAnc.getHeight();
Line2D line;
switch(edge) {
default:
case bottom:
line = new Line2D.Double(x - borderSize, y + h, x + w + borderSize, y + h);
break;
case left:
line = new Line2D.Double(x, y, x, y + h + borderSize);
break;
case right:
line = new Line2D.Double(x + w, y, x + w, y + h + borderSize);
break;
case top:
line = new Line2D.Double(x - borderSize, y, x + w + borderSize, y);
break;
}
graphics.draw(line);
}
}
}
// draw text
drawContent(graphics);
}
use of org.apache.poi.sl.usermodel.TableCell.BorderEdge in project poi by apache.
the class DrawTableShape method setInsideBorders.
/**
* Format the inside border using the specified Line object
* An empty args parameter removes the affected border.
*
* @param args a varargs array possible containing {@link Double} (width),
* {@link LineCompound}, {@link Color}, {@link LineDash}
*/
public void setInsideBorders(Object... args) {
if (args.length == 0)
return;
TableShape<?, ?> table = getShape();
final int rows = table.getNumberOfRows();
final int cols = table.getNumberOfColumns();
BorderEdge[] edges = new BorderEdge[2];
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
edges[0] = (col > 0 && col < cols - 1) ? BorderEdge.right : null;
edges[1] = (row > 0 && row < rows - 1) ? BorderEdge.bottom : null;
setEdges(table.getCell(row, col), edges, args);
}
}
}
use of org.apache.poi.sl.usermodel.TableCell.BorderEdge in project poi by apache.
the class DrawTableShape method setOutsideBorders.
/**
* Format the outside border using the specified Line object
* An empty args parameter removes the affected border.
*
* @param args a varargs array possible containing {@link Double} (width),
* {@link LineCompound}, {@link Color}, {@link LineDash}
*/
public void setOutsideBorders(Object... args) {
if (args.length == 0)
return;
TableShape<?, ?> table = getShape();
final int rows = table.getNumberOfRows();
final int cols = table.getNumberOfColumns();
BorderEdge[] edges = new BorderEdge[4];
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
edges[0] = (col == 0) ? BorderEdge.left : null;
edges[1] = (col == cols - 1) ? BorderEdge.right : null;
edges[2] = (row == 0) ? BorderEdge.top : null;
edges[3] = (row == rows - 1) ? BorderEdge.bottom : null;
setEdges(table.getCell(row, col), edges, args);
}
}
}
use of org.apache.poi.sl.usermodel.TableCell.BorderEdge in project poi by apache.
the class DrawTableShape method setAllBorders.
/**
* Format the table and apply the specified Line to all cell boundaries,
* both outside and inside.
* An empty args parameter removes the affected border.
*
* @param args a varargs array possible containing {@link Double} (width),
* {@link LineCompound}, {@link Color}, {@link LineDash}
*/
public void setAllBorders(Object... args) {
TableShape<?, ?> table = getShape();
final int rows = table.getNumberOfRows();
final int cols = table.getNumberOfColumns();
BorderEdge[] edges = { BorderEdge.top, BorderEdge.left, null, null };
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
edges[2] = (col == cols - 1) ? BorderEdge.right : null;
edges[3] = (row == rows - 1) ? BorderEdge.bottom : null;
setEdges(table.getCell(row, col), edges, args);
}
}
}
use of org.apache.poi.sl.usermodel.TableCell.BorderEdge in project poi by apache.
the class TestXSLFTable method testCreate.
@Test
public void testCreate() throws IOException {
XMLSlideShow ppt1 = new XMLSlideShow();
XSLFSlide slide = ppt1.createSlide();
XSLFTable tbl = slide.createTable();
assertNotNull(tbl.getCTTable());
assertNotNull(tbl.getCTTable().getTblGrid());
assertNotNull(tbl.getCTTable().getTblPr());
assertTrue(tbl.getXmlObject() instanceof CTGraphicalObjectFrame);
assertEquals("Table 1", tbl.getShapeName());
assertEquals(2, tbl.getShapeId());
assertEquals(0, tbl.getRows().size());
assertEquals(0, tbl.getCTTable().sizeOfTrArray());
assertEquals(0, tbl.getCTTable().getTblGrid().sizeOfGridColArray());
assertEquals(0, tbl.getNumberOfColumns());
assertEquals(0, tbl.getNumberOfRows());
XSLFTableRow row0 = tbl.addRow();
assertNotNull(row0.getXmlObject());
assertEquals(1, tbl.getNumberOfRows());
assertSame(row0, tbl.getRows().get(0));
assertEquals(20.0, row0.getHeight(), 0);
row0.setHeight(30.0);
assertEquals(30.0, row0.getHeight(), 0);
assertEquals(0, row0.getCells().size());
XSLFTableCell cell0 = row0.addCell();
assertNotNull(cell0.getXmlObject());
// by default table cell has no borders
CTTableCell tc = (CTTableCell) cell0.getXmlObject();
assertTrue(tc.getTcPr().getLnB().isSetNoFill());
assertTrue(tc.getTcPr().getLnT().isSetNoFill());
assertTrue(tc.getTcPr().getLnL().isSetNoFill());
assertTrue(tc.getTcPr().getLnR().isSetNoFill());
assertSame(cell0, row0.getCells().get(0));
assertEquals(1, tbl.getNumberOfColumns());
assertEquals(100.0, tbl.getColumnWidth(0), 0);
cell0.addNewTextParagraph().addNewTextRun().setText("POI");
assertEquals("POI", cell0.getText());
XSLFTableCell cell1 = row0.addCell();
assertSame(cell1, row0.getCells().get(1));
assertEquals(2, tbl.getNumberOfColumns());
assertEquals(100.0, tbl.getColumnWidth(1), 0);
cell1.addNewTextParagraph().addNewTextRun().setText("Apache");
assertEquals("Apache", cell1.getText());
for (BorderEdge edge : BorderEdge.values()) {
assertNull(cell1.getBorderWidth(edge));
cell1.setBorderWidth(edge, 2.0);
assertEquals(2.0, cell1.getBorderWidth(edge), 0);
assertNull(cell1.getBorderColor(edge));
cell1.setBorderColor(edge, Color.yellow);
assertEquals(Color.yellow, cell1.getBorderColor(edge));
}
assertEquals(VerticalAlignment.TOP, cell1.getVerticalAlignment());
cell1.setVerticalAlignment(VerticalAlignment.MIDDLE);
assertEquals(VerticalAlignment.MIDDLE, cell1.getVerticalAlignment());
cell1.setVerticalAlignment(null);
assertEquals(VerticalAlignment.TOP, cell1.getVerticalAlignment());
XMLSlideShow ppt2 = XSLFTestDataSamples.writeOutAndReadBack(ppt1);
ppt1.close();
slide = ppt2.getSlides().get(0);
tbl = (XSLFTable) slide.getShapes().get(0);
assertEquals(2, tbl.getNumberOfColumns());
assertEquals(1, tbl.getNumberOfRows());
assertEquals("POI", tbl.getCell(0, 0).getText());
assertEquals("Apache", tbl.getCell(0, 1).getText());
ppt2.close();
}
Aggregations