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.PdfPCell; 57 import com.lowagie.text.pdf.PdfPCellEvent; 58 import com.lowagie.text.pdf.PdfPTable; 59 60 64 public class SimpleCell extends Rectangle implements PdfPCellEvent, TextElementArray { 65 66 68 public static final boolean ROW = true; 69 70 public static final boolean CELL = false; 71 72 74 private ArrayList content = new ArrayList (); 75 76 private float width = 0f; 77 78 private float widthpercentage = 0f; 79 80 private float spacing_left = Float.NaN; 81 82 private float spacing_right = Float.NaN; 83 84 private float spacing_top = Float.NaN; 85 86 private float spacing_bottom = Float.NaN; 87 88 private float padding_left = Float.NaN; 89 90 private float padding_right = Float.NaN; 91 92 private float padding_top = Float.NaN; 93 94 private float padding_bottom = Float.NaN; 95 96 private int colspan = 1; 97 98 private int horizontalAlignment = Element.ALIGN_UNDEFINED; 99 100 private int verticalAlignment = Element.ALIGN_UNDEFINED; 101 102 private boolean cellgroup = false; 103 106 protected boolean useAscender = false; 107 110 protected boolean useDescender = false; 111 115 protected boolean useBorderPadding; 116 117 122 public SimpleCell(boolean row) { 123 super(0f, 0f, 0f, 0f); 124 cellgroup = row; 125 setBorder(BOX); 126 } 127 128 133 public void addElement(Element element) throws BadElementException { 134 if (cellgroup) { 135 if (element instanceof SimpleCell) { 136 if(((SimpleCell)element).isCellgroup()) { 137 throw new BadElementException("You can't add one row to another row."); 138 } 139 content.add(element); 140 return; 141 } 142 else { 143 throw new BadElementException("You can only add cells to rows, no objects of type " + element.getClass().getName()); 144 } 145 } 146 if (element.type() == Element.PARAGRAPH 147 || element.type() == Element.PHRASE 148 || element.type() == Element.ANCHOR 149 || element.type() == Element.CHUNK 150 || element.type() == Element.LIST 151 || element.type() == Element.MARKED 152 || element.type() == Element.JPEG 153 || element.type() == Element.IMGRAW 154 || element.type() == Element.IMGTEMPLATE) { 155 content.add(element); 156 } 157 else { 158 throw new BadElementException("You can't add an element of type " + element.getClass().getName() + " to a SimpleCell."); 159 } 160 } 161 162 168 public Cell createCell(SimpleCell rowAttributes) throws BadElementException { 169 Cell cell = new Cell(); 170 cell.cloneNonPositionParameters(rowAttributes); 171 cell.softCloneNonPositionParameters(this); 172 cell.setColspan(colspan); 173 cell.setHorizontalAlignment(horizontalAlignment); 174 cell.setVerticalAlignment(verticalAlignment); 175 cell.setUseAscender(useAscender); 176 cell.setUseBorderPadding(useBorderPadding); 177 cell.setUseDescender(useDescender); 178 Element element; 179 for (Iterator i = content.iterator(); i.hasNext(); ) { 180 element = (Element)i.next(); 181 cell.addElement(element); 182 } 183 return cell; 184 } 185 186 191 public PdfPCell createPdfPCell(SimpleCell rowAttributes) { 192 PdfPCell cell = new PdfPCell(); 193 cell.setBorder(NO_BORDER); 194 SimpleCell tmp = new SimpleCell(CELL); 195 tmp.setSpacing_left(spacing_left); 196 tmp.setSpacing_right(spacing_right); 197 tmp.setSpacing_top(spacing_top); 198 tmp.setSpacing_bottom(spacing_bottom); 199 tmp.cloneNonPositionParameters(rowAttributes); 200 tmp.softCloneNonPositionParameters(this); 201 cell.setCellEvent(tmp); 202 cell.setHorizontalAlignment(rowAttributes.horizontalAlignment); 203 cell.setVerticalAlignment(rowAttributes.verticalAlignment); 204 cell.setUseAscender(rowAttributes.useAscender); 205 cell.setUseBorderPadding(rowAttributes.useBorderPadding); 206 cell.setUseDescender(rowAttributes.useDescender); 207 cell.setColspan(colspan); 208 if (horizontalAlignment != Element.ALIGN_UNDEFINED) 209 cell.setHorizontalAlignment(horizontalAlignment); 210 if (verticalAlignment != Element.ALIGN_UNDEFINED) 211 cell.setVerticalAlignment(verticalAlignment); 212 if (useAscender) 213 cell.setUseAscender(useAscender); 214 if (useBorderPadding) 215 cell.setUseBorderPadding(useBorderPadding); 216 if (useDescender) 217 cell.setUseDescender(useDescender); 218 float p; 219 float sp_left = spacing_left; 220 if (Float.isNaN(sp_left)) sp_left = 0f; 221 float sp_right = spacing_right; 222 if (Float.isNaN(sp_right)) sp_right = 0f; 223 float sp_top = spacing_top; 224 if (Float.isNaN(sp_top)) sp_top = 0f; 225 float sp_bottom = spacing_bottom; 226 if (Float.isNaN(sp_bottom)) sp_bottom = 0f; 227 p = padding_left; 228 if (Float.isNaN(p)) p = 0f; 229 cell.setPaddingLeft(p + sp_left); 230 p = padding_right; 231 if (Float.isNaN(p)) p = 0f; 232 cell.setPaddingRight(p + sp_right); 233 p = padding_top; 234 if (Float.isNaN(p)) p = 0f; 235 cell.setPaddingTop(p + sp_top); 236 p = padding_bottom; 237 if (Float.isNaN(p)) p = 0f; 238 cell.setPaddingBottom(p + sp_bottom); 239 Element element; 240 for (Iterator i = content.iterator(); i.hasNext(); ) { 241 element = (Element)i.next(); 242 cell.addElement(element); 243 } 244 return cell; 245 } 246 247 252 public static SimpleCell getDimensionlessInstance(Rectangle rectangle, float spacing) { 253 SimpleCell event = new SimpleCell(CELL); 254 event.cloneNonPositionParameters(rectangle); 255 event.setSpacing(spacing * 2f); 256 return event; 257 } 258 259 262 public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) { 263 float sp_left = spacing_left; 264 if (Float.isNaN(sp_left)) sp_left = 0f; 265 float sp_right = spacing_right; 266 if (Float.isNaN(sp_right)) sp_right = 0f; 267 float sp_top = spacing_top; 268 if (Float.isNaN(sp_top)) sp_top = 0f; 269 float sp_bottom = spacing_bottom; 270 if (Float.isNaN(sp_bottom)) sp_bottom = 0f; 271 Rectangle rect = new Rectangle(position.getLeft(sp_left), position.getBottom(sp_bottom), position.getRight(sp_right), position.getTop(sp_top)); 272 rect.cloneNonPositionParameters(this); 273 canvases[PdfPTable.BACKGROUNDCANVAS].rectangle(rect); 274 rect.setBackgroundColor(null); 275 canvases[PdfPTable.LINECANVAS].rectangle(rect); 276 } 277 278 280 public void setPadding(float padding) { 281 if (Float.isNaN(padding_right)) { 282 setPadding_right(padding); 283 } 284 if (Float.isNaN(padding_left)) { 285 setPadding_left(padding); 286 } 287 if (Float.isNaN(padding_top)) { 288 setPadding_top(padding); 289 } 290 if (Float.isNaN(padding_bottom)) { 291 setPadding_bottom(padding); 292 } 293 } 294 295 298 public int getColspan() { 299 return colspan; 300 } 301 304 public void setColspan(int colspan) { 305 if (colspan > 0) this.colspan = colspan; 306 } 307 310 public float getPadding_bottom() { 311 return padding_bottom; 312 } 313 316 public void setPadding_bottom(float padding_bottom) { 317 this.padding_bottom = padding_bottom; 318 } 319 322 public float getPadding_left() { 323 return padding_left; 324 } 325 328 public void setPadding_left(float padding_left) { 329 this.padding_left = padding_left; 330 } 331 334 public float getPadding_right() { 335 return padding_right; 336 } 337 340 public void setPadding_right(float padding_right) { 341 this.padding_right = padding_right; 342 } 343 346 public float getPadding_top() { 347 return padding_top; 348 } 349 352 public void setPadding_top(float padding_top) { 353 this.padding_top = padding_top; 354 } 355 358 public float getSpacing_left() { 359 return spacing_left; 360 } 361 364 public float getSpacing_right() { 365 return spacing_right; 366 } 367 370 public float getSpacing_top() { 371 return spacing_top; 372 } 373 376 public float getSpacing_bottom() { 377 return spacing_bottom; 378 } 379 380 383 public void setSpacing(float spacing) { 384 this.spacing_left = spacing; 385 this.spacing_right = spacing; 386 this.spacing_top = spacing; 387 this.spacing_bottom = spacing; 388 } 389 390 393 public void setSpacing_left(float spacing) { 394 this.spacing_left = spacing; 395 } 396 397 400 public void setSpacing_right(float spacing) { 401 this.spacing_right = spacing; 402 } 403 404 407 public void setSpacing_top(float spacing) { 408 this.spacing_top = spacing; 409 } 410 411 414 public void setSpacing_bottom(float spacing) { 415 this.spacing_bottom = spacing; 416 } 417 418 421 public boolean isCellgroup() { 422 return cellgroup; 423 } 424 427 public void setCellgroup(boolean cellgroup) { 428 this.cellgroup = cellgroup; 429 } 430 433 public int getHorizontalAlignment() { 434 return horizontalAlignment; 435 } 436 439 public void setHorizontalAlignment(int horizontalAlignment) { 440 this.horizontalAlignment = horizontalAlignment; 441 } 442 445 public int getVerticalAlignment() { 446 return verticalAlignment; 447 } 448 451 public void setVerticalAlignment(int verticalAlignment) { 452 this.verticalAlignment = verticalAlignment; 453 } 454 457 public float getWidth() { 458 return width; 459 } 460 463 public void setWidth(float width) { 464 this.width = width; 465 } 466 469 public float getWidthpercentage() { 470 return widthpercentage; 471 } 472 475 public void setWidthpercentage(float widthpercentage) { 476 this.widthpercentage = widthpercentage; 477 } 478 481 public boolean isUseAscender() { 482 return useAscender; 483 } 484 487 public void setUseAscender(boolean useAscender) { 488 this.useAscender = useAscender; 489 } 490 493 public boolean isUseBorderPadding() { 494 return useBorderPadding; 495 } 496 499 public void setUseBorderPadding(boolean useBorderPadding) { 500 this.useBorderPadding = useBorderPadding; 501 } 502 505 public boolean isUseDescender() { 506 return useDescender; 507 } 508 511 public void setUseDescender(boolean useDescender) { 512 this.useDescender = useDescender; 513 } 514 515 518 ArrayList getContent() { 519 return content; 520 } 521 522 525 public boolean add(Object o) { 526 try { 527 addElement((Element)o); 528 return true; 529 } 530 catch(ClassCastException e) { 531 return false; 532 } 533 catch(BadElementException e) { 534 throw new ExceptionConverter(e); 535 } 536 } 537 540 public int type() { 541 return Element.CELL; 542 } 543 } | Popular Tags |