1 50 package com.lowagie.text; 51 52 import java.util.ArrayList ; 53 import java.util.Iterator ; 54 55 import com.lowagie.text.pdf.PdfContentByte; 56 import com.lowagie.text.pdf.PdfPTable; 57 import com.lowagie.text.pdf.PdfPTableEvent; 58 59 63 public class SimpleTable extends Rectangle implements PdfPTableEvent, TextElementArray { 64 65 66 private ArrayList content = new ArrayList (); 67 68 private float width = 0f; 69 70 private float widthpercentage = 0f; 71 72 private float cellspacing; 73 74 private float cellpadding; 75 76 private int alignment; 77 78 82 public SimpleTable() { 83 super(0f, 0f, 0f, 0f); 84 setBorder(BOX); 85 setBorderWidth(2f); 86 } 87 88 93 public void addElement(SimpleCell element) throws BadElementException { 94 if(!element.isCellgroup()) { 95 throw new BadElementException("You can't add cells to a table directly, add them to a row first."); 96 } 97 content.add(element); 98 } 99 100 105 public Table createTable() throws BadElementException { 106 if (content.isEmpty()) throw new BadElementException("Trying to create a table without rows."); 107 SimpleCell row = (SimpleCell)content.get(0); 108 SimpleCell cell; 109 int columns = 0; 110 for (Iterator i = row.getContent().iterator(); i.hasNext(); ) { 111 cell = (SimpleCell)i.next(); 112 columns += cell.getColspan(); 113 } 114 float[] widths = new float[columns]; 115 float[] widthpercentages = new float[columns]; 116 Table table = new Table(columns); 117 table.setAlignment(alignment); 118 table.setSpacing(cellspacing); 119 table.setPadding(cellpadding); 120 table.cloneNonPositionParameters(this); 121 int pos; 122 for (Iterator rows = content.iterator(); rows.hasNext(); ) { 123 row = (SimpleCell)rows.next(); 124 pos = 0; 125 for (Iterator cells = row.getContent().iterator(); cells.hasNext(); ) { 126 cell = (SimpleCell)cells.next(); 127 table.addCell(cell.createCell(row)); 128 if (cell.getColspan() == 1) { 129 if (cell.getWidth() > 0) widths[pos] = cell.getWidth(); 130 if (cell.getWidthpercentage() > 0) widthpercentages[pos] = cell.getWidthpercentage(); 131 } 132 pos += cell.getColspan(); 133 } 134 } 135 float sumWidths = 0f; 136 for(int i = 0; i < columns; i++) { 137 if (widths[i] == 0) { 138 sumWidths = 0; 139 break; 140 } 141 sumWidths += widths[i]; 142 } 143 if (sumWidths > 0) { 144 table.setWidth(sumWidths); 145 table.setLocked(true); 146 table.setWidths(widths); 147 } 148 else { 149 for(int i = 0; i < columns; i++) { 150 if (widthpercentages[i] == 0) { 151 sumWidths = 0; 152 break; 153 } 154 sumWidths += widthpercentages[i]; 155 } 156 if (sumWidths > 0) { 157 table.setWidths(widthpercentages); 158 } 159 } 160 if (width > 0) { 161 table.setWidth(width); 162 table.setLocked(true); 163 } 164 else if (widthpercentage > 0) { 165 table.setWidth(widthpercentage); 166 } 167 return table; 168 } 169 170 175 public PdfPTable createPdfPTable() throws DocumentException { 176 if (content.isEmpty()) throw new BadElementException("Trying to create a table without rows."); 177 SimpleCell row = (SimpleCell)content.get(0); 178 SimpleCell cell; 179 int columns = 0; 180 for (Iterator i = row.getContent().iterator(); i.hasNext(); ) { 181 cell = (SimpleCell)i.next(); 182 columns += cell.getColspan(); 183 } 184 float[] widths = new float[columns]; 185 float[] widthpercentages = new float[columns]; 186 PdfPTable table = new PdfPTable(columns); 187 table.setTableEvent(this); 188 table.setHorizontalAlignment(alignment); 189 int pos; 190 for (Iterator rows = content.iterator(); rows.hasNext(); ) { 191 row = (SimpleCell)rows.next(); 192 pos = 0; 193 for (Iterator cells = row.getContent().iterator(); cells.hasNext(); ) { 194 cell = (SimpleCell)cells.next(); 195 if (Float.isNaN(cell.getSpacing_left())) { 196 cell.setSpacing_left(cellspacing / 2f); 197 } 198 if (Float.isNaN(cell.getSpacing_right())) { 199 cell.setSpacing_right(cellspacing / 2f); 200 } 201 if (Float.isNaN(cell.getSpacing_top())) { 202 cell.setSpacing_top(cellspacing / 2f); 203 } 204 if (Float.isNaN(cell.getSpacing_bottom())) { 205 cell.setSpacing_bottom(cellspacing / 2f); 206 } 207 cell.setPadding(cellpadding); 208 table.addCell(cell.createPdfPCell(row)); 209 if (cell.getColspan() == 1) { 210 if (cell.getWidth() > 0) widths[pos] = cell.getWidth(); 211 if (cell.getWidthpercentage() > 0) widthpercentages[pos] = cell.getWidthpercentage(); 212 } 213 pos += cell.getColspan(); 214 } 215 } 216 float sumWidths = 0f; 217 for(int i = 0; i < columns; i++) { 218 if (widths[i] == 0) { 219 sumWidths = 0; 220 break; 221 } 222 sumWidths += widths[i]; 223 } 224 if (sumWidths > 0) { 225 table.setTotalWidth(sumWidths); 226 table.setWidths(widths); 227 } 228 else { 229 for(int i = 0; i < columns; i++) { 230 if (widthpercentages[i] == 0) { 231 sumWidths = 0; 232 break; 233 } 234 sumWidths += widthpercentages[i]; 235 } 236 if (sumWidths > 0) { 237 table.setWidths(widthpercentages); 238 } 239 } 240 if (width > 0) { 241 table.setTotalWidth(width); 242 } 243 if (widthpercentage > 0) { 244 table.setWidthPercentage(widthpercentage); 245 } 246 return table; 247 } 248 249 254 public static SimpleTable getDimensionlessInstance(Rectangle rectangle, float spacing) { 255 SimpleTable event = new SimpleTable(); 256 event.cloneNonPositionParameters(rectangle); 257 event.setCellspacing(spacing); 258 return event; 259 } 260 261 264 public void tableLayout(PdfPTable table, float[][] widths, float[] heights, int headerRows, int rowStart, PdfContentByte[] canvases) { 265 float[] width = widths[0]; 266 Rectangle rect = new Rectangle(width[0], heights[heights.length - 1], width[width.length - 1], heights[0]); 267 rect.cloneNonPositionParameters(this); 268 int bd = rect.getBorder(); 269 rect.setBorder(Rectangle.NO_BORDER); 270 canvases[PdfPTable.BACKGROUNDCANVAS].rectangle(rect); 271 rect.setBorder(bd); 272 rect.setBackgroundColor(null); 273 canvases[PdfPTable.LINECANVAS].rectangle(rect); 274 } 275 276 279 public float getCellpadding() { 280 return cellpadding; 281 } 282 285 public void setCellpadding(float cellpadding) { 286 this.cellpadding = cellpadding; 287 } 288 291 public float getCellspacing() { 292 return cellspacing; 293 } 294 297 public void setCellspacing(float cellspacing) { 298 this.cellspacing = cellspacing; 299 } 300 301 304 public int getAlignment() { 305 return alignment; 306 } 307 310 public void setAlignment(int alignment) { 311 this.alignment = alignment; 312 } 313 316 public float getWidth() { 317 return width; 318 } 319 322 public void setWidth(float width) { 323 this.width = width; 324 } 325 328 public float getWidthpercentage() { 329 return widthpercentage; 330 } 331 334 public void setWidthpercentage(float widthpercentage) { 335 this.widthpercentage = widthpercentage; 336 } 337 340 public int type() { 341 return Element.TABLE; 342 } 343 344 347 public boolean add(Object o) { 348 try { 349 addElement((SimpleCell)o); 350 return true; 351 } 352 catch(ClassCastException e) { 353 return false; 354 } 355 catch(BadElementException e) { 356 throw new ExceptionConverter(e); 357 } 358 } 359 } | Popular Tags |