1 12 package org.displaytag.render; 13 14 import java.awt.Color ; 15 import java.util.Iterator ; 16 17 import org.apache.commons.lang.ObjectUtils; 18 import org.apache.commons.lang.StringUtils; 19 import org.displaytag.decorator.TableDecorator; 20 import org.displaytag.exception.DecoratorException; 21 import org.displaytag.exception.ObjectLookupException; 22 import org.displaytag.model.Column; 23 import org.displaytag.model.HeaderCell; 24 import org.displaytag.model.TableModel; 25 26 import com.lowagie.text.BadElementException; 27 import com.lowagie.text.Cell; 28 import com.lowagie.text.Chunk; 29 import com.lowagie.text.Document; 30 import com.lowagie.text.DocumentException; 31 import com.lowagie.text.Element; 32 import com.lowagie.text.Font; 33 import com.lowagie.text.FontFactory; 34 import com.lowagie.text.Paragraph; 35 import com.lowagie.text.Rectangle; 36 import com.lowagie.text.Table; 37 38 39 45 public class ItextTableWriter extends TableWriterAdapter 46 { 47 48 51 private Table table; 52 53 56 private Document document; 57 58 61 private Font defaultFont; 62 63 68 public ItextTableWriter(Table table, Document document) 69 { 70 this.table = table; 71 this.document = document; 72 } 73 74 79 protected void writeTableOpener(TableModel model) 80 { 81 this.table.setDefaultVerticalAlignment(Element.ALIGN_TOP); 82 this.table.setCellsFitPage(true); 83 this.table.setWidth(100); 84 this.table.setPadding(2); 85 this.table.setSpacing(0); 86 this.table.setBorder(Rectangle.NO_BORDER); 87 this.defaultFont = this.getTableFont(); 88 } 89 90 94 protected Font getTableFont() 95 { 96 return FontFactory.getFont(FontFactory.HELVETICA, 10, Font.NORMAL, new Color (0x00, 0x00, 0x00)); 97 } 98 99 103 protected void writeCaption(TableModel model) throws Exception 104 { 105 this.decorateCaption(model); 106 } 107 108 113 private void decorateCaption(TableModel model) throws DocumentException 114 { 115 Paragraph caption = new Paragraph(new Chunk(model.getCaption(), this.getCaptionFont())); 116 caption.setAlignment(this.getCaptionHorizontalAlignment()); 117 this.document.add(caption); 118 } 119 120 124 protected Font getCaptionFont() 125 { 126 return FontFactory.getFont(FontFactory.HELVETICA, 17, Font.BOLD, new Color (0x00, 0x00, 0x00)); 127 } 128 129 133 protected int getCaptionHorizontalAlignment() 134 { 135 return Element.ALIGN_CENTER; 136 } 137 138 143 protected void writeTableHeader(TableModel model) throws BadElementException 144 { 145 Iterator iterator = model.getHeaderCellList().iterator(); 146 147 float[] widths = new float[model.getNumberOfColumns()]; 148 for (int i = 0; iterator.hasNext(); i++) 149 { 150 HeaderCell headerCell = (HeaderCell) iterator.next(); 151 widths[i] = this.getCellWidth(headerCell); 152 153 String columnHeader = headerCell.getTitle(); 154 155 if (columnHeader == null) 156 { 157 columnHeader = StringUtils.capitalize(headerCell.getBeanPropertyName()); 158 } 159 160 Cell hdrCell = this.getHeaderCell(columnHeader); 161 this.table.addCell(hdrCell); 162 } 163 this.table.setWidths(widths); 164 this.table.endHeaders(); 165 } 166 167 172 private float getCellWidth(HeaderCell headerCell) 173 { 174 int maxWidth = headerCell.getMaxLength(); 175 return (maxWidth > 0) ? maxWidth : headerCell.getTitle().length(); 176 } 177 178 182 protected void writePostBodyFooter(TableModel model) throws DocumentException 183 { 184 Chunk cellContent = new Chunk(model.getFooter(), this.getFooterFont()); 185 this.setFooterFontStyle(cellContent); 186 Cell cell = new Cell(cellContent); 187 cell.setLeading(8); 188 cell.setBackgroundColor(this.getFooterBackgroundColor()); 189 cell.setHorizontalAlignment(this.getFooterHorizontalAlignment()); 190 cell.setColspan(model.getNumberOfColumns()); 191 table.addCell(cell); 192 } 193 194 198 protected Color getFooterBackgroundColor() 199 { 200 return new Color (0xce, 0xcf, 0xce); 201 } 202 203 207 protected int getFooterHorizontalAlignment() 208 { 209 return Element.ALIGN_LEFT; 210 } 211 212 216 protected void setFooterFontStyle(Chunk cellContent) 217 { 218 this.setBoldStyle(cellContent, this.getFooterFontColor()); 219 } 220 221 225 protected Color getFooterFontColor() 226 { 227 return new Color (0x00, 0x00, 0x00); 228 } 229 230 234 protected Font getFooterFont() 235 { 236 return FontFactory.getFont(FontFactory.HELVETICA, 10); 237 } 238 239 242 protected void writeDecoratedRowStart(TableModel model) 243 { 244 ItextDecorator decorator = (ItextDecorator) model.getTableDecorator(); 245 decorator.setTable(this.table); 246 decorator.setFont(this.defaultFont); 247 ((TableDecorator) decorator).startRow(); 248 } 249 250 253 protected void writeDecoratedRowFinish(TableModel model) throws Exception 254 { 255 model.getTableDecorator().finishRow(); 256 } 257 258 262 protected void writeColumnOpener(Column column) throws ObjectLookupException, DecoratorException 263 { 264 column.initialize(); } 266 267 271 protected void writeColumnValue(Object value, Column column) throws BadElementException 272 { 273 this.table.addCell(getCell(value)); 274 } 275 276 279 protected void writeDecoratedTableFinish(TableModel model) 280 { 281 model.getTableDecorator().finish(); 282 } 283 284 290 private Cell getCell(Object value) throws BadElementException 291 { 292 Cell cell = new Cell(new Chunk(StringUtils.trimToEmpty(ObjectUtils.toString(value)), this.defaultFont)); 293 cell.setVerticalAlignment(Element.ALIGN_TOP); 294 cell.setLeading(8); 295 return cell; 296 } 297 298 304 private Cell getHeaderCell(String value) throws BadElementException 305 { 306 Chunk cellContent = new Chunk(value, this.getHeaderFont()); 307 setHeaderFontStyle(cellContent); 308 Cell cell = new Cell(cellContent); 309 cell.setLeading(8); 310 cell.setHeader(true); 311 cell.setHorizontalAlignment(this.getHeaderHorizontalAlignment()); 312 cell.setBackgroundColor(this.getHeaderBackgroundColor()); 313 return cell; 314 } 315 316 320 protected Font getHeaderFont() 321 { 322 return this.defaultFont; 323 } 324 325 330 protected Color getHeaderBackgroundColor() 331 { 332 return new Color (0xee, 0xee, 0xee); 333 } 334 335 339 protected void setHeaderFontStyle(Chunk cellContent) 340 { 341 setBoldStyle(cellContent, this.getHeaderFontColor()); 342 } 343 344 348 protected Color getHeaderFontColor() 349 { 350 return new Color (0x00, 0x00, 0x00); 351 } 352 353 358 protected int getHeaderHorizontalAlignment() 359 { 360 return Element.ALIGN_CENTER; 361 } 362 363 368 private void setBoldStyle(Chunk chunk, Color color) 369 { 370 Font font = chunk.font(); 371 chunk.setFont(FontFactory.getFont(font.getFamilyname(), font.size(), Font.BOLD, color)); 372 } 373 374 379 public interface ItextDecorator 380 { 381 382 386 void setTable(Table table); 387 388 392 void setFont(Font font); 393 } 394 395 } 396 | Popular Tags |