You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

XSLFTableRow.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * ====================================================================
  3. * Licensed to the Apache Software Foundation (ASF) under one or more
  4. * contributor license agreements. See the NOTICE file distributed with
  5. * this work for additional information regarding copyright ownership.
  6. * The ASF licenses this file to You under the Apache License, Version 2.0
  7. * (the "License"); you may not use this file except in compliance with
  8. * the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. * ====================================================================
  18. */
  19. package org.apache.poi.xslf.usermodel;
  20. import java.util.ArrayList;
  21. import java.util.Collections;
  22. import java.util.Iterator;
  23. import java.util.List;
  24. import org.apache.poi.util.Units;
  25. import org.openxmlformats.schemas.drawingml.x2006.main.CTTableCell;
  26. import org.openxmlformats.schemas.drawingml.x2006.main.CTTableRow;
  27. /**
  28. * Represents a table in a .pptx presentation
  29. */
  30. public class XSLFTableRow implements Iterable<XSLFTableCell> {
  31. private final CTTableRow _row;
  32. private final List<XSLFTableCell> _cells;
  33. private final XSLFTable _table;
  34. /*package*/ XSLFTableRow(CTTableRow row, XSLFTable table){
  35. _row = row;
  36. _table = table;
  37. CTTableCell[] tcArray = _row.getTcArray();
  38. _cells = new ArrayList<XSLFTableCell>(tcArray.length);
  39. for(CTTableCell cell : tcArray) {
  40. _cells.add(new XSLFTableCell(cell, table));
  41. }
  42. }
  43. public CTTableRow getXmlObject(){
  44. return _row;
  45. }
  46. public Iterator<XSLFTableCell> iterator(){
  47. return _cells.iterator();
  48. }
  49. public List<XSLFTableCell> getCells(){
  50. return Collections.unmodifiableList(_cells);
  51. }
  52. public double getHeight(){
  53. return Units.toPoints(_row.getH());
  54. }
  55. public void setHeight(double height){
  56. _row.setH(Units.toEMU(height));
  57. }
  58. public XSLFTableCell addCell(){
  59. CTTableCell c = _row.addNewTc();
  60. c.set(XSLFTableCell.prototype());
  61. XSLFTableCell cell = new XSLFTableCell(c, _table);
  62. _cells.add(cell);
  63. if(_table.getNumberOfColumns() < _row.sizeOfTcArray()) {
  64. _table.getCTTable().getTblGrid().addNewGridCol().setW(Units.toEMU(100.0));
  65. }
  66. _table.updateRowColIndexes();
  67. return cell;
  68. }
  69. /**
  70. * Merge cells of a table row, inclusive.
  71. * Indices are 0-based.
  72. *
  73. * @param firstCol 0-based index of first column to merge, inclusive
  74. * @param lastCol 0-based index of last column to merge, inclusive
  75. */
  76. public void mergeCells(int firstCol, int lastCol)
  77. {
  78. if (firstCol >= lastCol) {
  79. throw new IllegalArgumentException(
  80. "Cannot merge, first column >= last column : "
  81. + firstCol + " >= " + lastCol
  82. );
  83. }
  84. final int colSpan = (lastCol - firstCol) + 1;
  85. _cells.get(firstCol).setGridSpan(colSpan);
  86. for (final XSLFTableCell cell : _cells.subList(firstCol+1, lastCol+1)) {
  87. cell.setHMerge(true);
  88. }
  89. }
  90. }