1 50 51 package com.lowagie.text.pdf; 52 53 import java.util.ArrayList ; 54 import java.util.Iterator ; 55 56 import com.lowagie.text.Cell; 57 import com.lowagie.text.Element; 58 import com.lowagie.text.Rectangle; 59 import com.lowagie.text.Row; 60 import com.lowagie.text.Table; 61 62 70 71 public class PdfTable extends Rectangle { 72 73 75 76 private int columns; 77 78 79 private ArrayList headercells; 80 81 82 private ArrayList cells; 83 84 85 protected Table table; 86 87 88 protected float[] positions; 89 90 92 103 104 PdfTable(Table table, float left, float right, float top, boolean supportUpdateRowAdditions) { 105 super(left, top, right, top); 107 this.table = table; 108 table.complete(); 109 110 cloneNonPositionParameters(table); 112 113 this.columns = table.getColumns(); 114 positions = table.getWidths(left, right - left); 115 116 setLeft(positions[0]); 118 setRight(positions[positions.length - 1]); 119 120 headercells = new ArrayList (); 121 cells = new ArrayList (); 122 123 updateRowAdditionsInternal(); 124 if (supportUpdateRowAdditions) { 125 table.deleteAllRows(); 126 } 127 } 128 129 131 136 137 void updateRowAdditions() { 138 table.complete(); 139 updateRowAdditionsInternal(); 140 table.deleteAllRows(); 141 } 142 143 146 147 private void updateRowAdditionsInternal() { 148 Row row; 150 int prevRows = rows(); 151 int rowNumber = 0; 152 int groupNumber = 0; 153 boolean groupChange; 154 int firstDataRow = table.getLastHeaderRow() + 1; 155 Cell cell; 156 PdfCell currentCell; 157 ArrayList newCells = new ArrayList (); 158 int rows = table.size() + 1; 159 float[] offsets = new float[rows]; 160 for (int i = 0; i < rows; i++) { 161 offsets[i] = getBottom(); 162 } 163 164 for (Iterator rowIterator = table.iterator(); rowIterator.hasNext(); ) { 166 groupChange = false; 167 row = (Row) rowIterator.next(); 168 if (row.isEmpty()) { 169 if (rowNumber < rows - 1 && offsets[rowNumber + 1] > offsets[rowNumber]) offsets[rowNumber + 1] = offsets[rowNumber]; 170 } 171 else { 172 for(int i = 0; i < row.getColumns(); i++) { 173 cell = (Cell) row.getCell(i); 174 if (cell != null) { 175 currentCell = new PdfCell(cell, rowNumber+prevRows, positions[i], positions[i + cell.getColspan()], offsets[rowNumber], cellspacing(), cellpadding()); 176 try { 177 if (offsets[rowNumber] - currentCell.getHeight() - cellpadding() < offsets[rowNumber + currentCell.rowspan()]) { 178 offsets[rowNumber + currentCell.rowspan()] = offsets[rowNumber] - currentCell.getHeight() - cellpadding(); 179 } 180 } 181 catch(ArrayIndexOutOfBoundsException aioobe) { 182 if (offsets[rowNumber] - currentCell.getHeight() < offsets[rows - 1]) { 183 offsets[rows - 1] = offsets[rowNumber] - currentCell.getHeight(); 184 } 185 } 186 if (rowNumber < firstDataRow) { 187 currentCell.setHeader(); 188 headercells.add(currentCell); 189 } 190 currentCell.setGroupNumber(groupNumber); 191 groupChange |= cell.getGroupChange(); 192 newCells.add(currentCell); 193 } 194 } 195 } 196 rowNumber++; 197 if( groupChange ) groupNumber++; 198 } 199 200 int n = newCells.size(); 202 for (int i = 0; i < n; i++) { 203 currentCell = (PdfCell) newCells.get(i); 204 try { 205 currentCell.setBottom(offsets[currentCell.rownumber()-prevRows + currentCell.rowspan()]); 206 } 207 catch(ArrayIndexOutOfBoundsException aioobe) { 208 currentCell.setBottom(offsets[rows - 1]); 209 } 210 } 211 cells.addAll(newCells); 212 setBottom(offsets[rows - 1]); 213 } 214 215 218 219 int rows() { 220 return cells.isEmpty() ? 0 : ((PdfCell)cells.get(cells.size()-1)).rownumber()+1; 221 } 222 223 224 public int type() { 225 return Element.TABLE; 226 } 227 228 233 234 ArrayList getHeaderCells() { 235 return headercells; 236 } 237 238 243 244 boolean hasHeader() { 245 return !headercells.isEmpty(); 246 } 247 248 253 254 ArrayList getCells() { 255 return cells; 256 } 257 258 263 264 int columns() { 265 return columns; 266 } 267 268 273 274 final float cellpadding() { 275 return table.getPadding(); 276 } 277 278 283 284 final float cellspacing() { 285 return table.getSpacing(); 286 } 287 288 293 294 public final boolean hasToFitPageTable() { 295 return table.isTableFitsPage(); 296 } 297 298 303 304 public final boolean hasToFitPageCells() { 305 return table.isCellsFitPage(); 306 } 307 308 313 public float getOffset() { 314 return table.getOffset(); 315 } 316 } 317 | Popular Tags |