1 7 8 package javax.swing; 9 10 import javax.swing.table.*; 11 import java.awt.*; 12 import java.awt.print.*; 13 import java.awt.geom.*; 14 import java.text.MessageFormat ; 15 16 80 class TablePrintable implements Printable { 81 82 83 private JTable table; 84 85 86 private JTableHeader header; 87 88 89 private TableColumnModel colModel; 90 91 92 private int totalColWidth; 93 94 95 private JTable.PrintMode printMode; 96 97 98 private MessageFormat headerFormat; 99 100 101 private MessageFormat footerFormat; 102 103 104 private int last = -1; 105 106 107 private int row = 0; 108 109 110 private int col = 0; 111 112 113 private final Rectangle clip = new Rectangle(0, 0, 0, 0); 114 115 116 private final Rectangle hclip = new Rectangle(0, 0, 0, 0); 117 118 119 private final Rectangle tempRect = new Rectangle(0, 0, 0, 0); 120 121 122 private static final int H_F_SPACE = 8; 123 124 125 private static final float HEADER_FONT_SIZE = 18.0f; 126 127 128 private static final float FOOTER_FONT_SIZE = 12.0f; 129 130 131 private Font headerFont; 132 133 134 private Font footerFont; 135 136 150 public TablePrintable(JTable table, 151 JTable.PrintMode printMode, 152 MessageFormat headerFormat, 153 MessageFormat footerFormat) { 154 155 this.table = table; 156 157 header = table.getTableHeader(); 158 colModel = table.getColumnModel(); 159 totalColWidth = colModel.getTotalColumnWidth(); 160 161 if (header != null) { 162 hclip.height = header.getHeight(); 164 } 165 166 this.printMode = printMode; 167 168 this.headerFormat = headerFormat; 169 this.footerFormat = footerFormat; 170 171 headerFont = table.getFont().deriveFont(Font.BOLD, 173 HEADER_FONT_SIZE); 174 footerFont = table.getFont().deriveFont(Font.PLAIN, 175 FOOTER_FONT_SIZE); 176 } 177 178 189 public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) 190 throws PrinterException { 191 192 final int imgWidth = (int)pageFormat.getImageableWidth(); 194 final int imgHeight = (int)pageFormat.getImageableHeight(); 195 196 if (imgWidth <= 0) { 197 throw new PrinterException("Width of printable area is too small."); 198 } 199 200 Object [] pageNumber = new Object []{new Integer (pageIndex + 1)}; 202 203 String headerText = null; 205 if (headerFormat != null) { 206 headerText = headerFormat.format(pageNumber); 207 } 208 209 String footerText = null; 211 if (footerFormat != null) { 212 footerText = footerFormat.format(pageNumber); 213 } 214 215 Rectangle2D hRect = null; 217 Rectangle2D fRect = null; 218 219 int headerTextSpace = 0; 221 int footerTextSpace = 0; 222 223 int availableSpace = imgHeight; 225 226 if (headerText != null) { 229 graphics.setFont(headerFont); 230 hRect = graphics.getFontMetrics().getStringBounds(headerText, 231 graphics); 232 233 headerTextSpace = (int)Math.ceil(hRect.getHeight()); 234 availableSpace -= headerTextSpace + H_F_SPACE; 235 } 236 237 if (footerText != null) { 240 graphics.setFont(footerFont); 241 fRect = graphics.getFontMetrics().getStringBounds(footerText, 242 graphics); 243 244 footerTextSpace = (int)Math.ceil(fRect.getHeight()); 245 availableSpace -= footerTextSpace + H_F_SPACE; 246 } 247 248 if (availableSpace <= 0) { 249 throw new PrinterException("Height of printable area is too small."); 250 } 251 252 double sf = 1.0D; 255 if (printMode == JTable.PrintMode.FIT_WIDTH && 256 totalColWidth > imgWidth) { 257 258 assert imgWidth > 0; 260 261 assert totalColWidth > 1; 263 264 sf = (double)imgWidth / (double)totalColWidth; 265 } 266 267 assert sf > 0; 269 270 while (last < pageIndex) { 278 if (row >= table.getRowCount() && col == 0) { 280 return NO_SUCH_PAGE; 281 } 282 283 int scaledWidth = (int)(imgWidth / sf); 287 int scaledHeight = (int)((availableSpace - hclip.height) / sf); 288 289 findNextClip(scaledWidth, scaledHeight); 291 292 last++; 293 } 294 295 Graphics2D g2d = (Graphics2D)graphics; 297 g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY()); 298 299 AffineTransform oldTrans; 301 302 if (footerText != null) { 304 oldTrans = g2d.getTransform(); 305 306 g2d.translate(0, imgHeight - footerTextSpace); 307 308 printText(g2d, footerText, fRect, footerFont, imgWidth); 309 310 g2d.setTransform(oldTrans); 311 } 312 313 if (headerText != null) { 316 printText(g2d, headerText, hRect, headerFont, imgWidth); 317 318 g2d.translate(0, headerTextSpace + H_F_SPACE); 319 } 320 321 tempRect.x = 0; 323 tempRect.y = 0; 324 tempRect.width = imgWidth; 325 tempRect.height = availableSpace; 326 g2d.clip(tempRect); 327 328 if (sf != 1.0D) { 331 g2d.scale(sf, sf); 332 333 } else { 336 int diff = (imgWidth - clip.width) / 2; 337 g2d.translate(diff, 0); 338 } 339 340 oldTrans = g2d.getTransform(); 342 Shape oldClip = g2d.getClip(); 343 344 if (header != null) { 347 hclip.x = clip.x; 348 hclip.width = clip.width; 349 350 g2d.translate(-hclip.x, 0); 351 g2d.clip(hclip); 352 header.print(g2d); 353 354 g2d.setTransform(oldTrans); 356 g2d.setClip(oldClip); 357 358 g2d.translate(0, hclip.height); 360 } 361 362 g2d.translate(-clip.x, -clip.y); 364 g2d.clip(clip); 365 table.print(g2d); 366 367 g2d.setTransform(oldTrans); 369 g2d.setClip(oldClip); 370 371 g2d.setColor(Color.BLACK); 373 g2d.drawRect(0, 0, clip.width, hclip.height + clip.height); 374 375 return PAGE_EXISTS; 376 } 377 378 389 private void printText(Graphics2D g2d, 390 String text, 391 Rectangle2D rect, 392 Font font, 393 int imgWidth) { 394 395 int tx; 396 397 if (rect.getWidth() < imgWidth) { 399 tx = (int)((imgWidth - rect.getWidth()) / 2); 400 401 } else if (table.getComponentOrientation().isLeftToRight()) { 404 tx = 0; 405 406 } else { 408 tx = -(int)(Math.ceil(rect.getWidth()) - imgWidth); 409 } 410 411 int ty = (int)Math.ceil(Math.abs(rect.getY())); 412 g2d.setColor(Color.BLACK); 413 g2d.setFont(font); 414 g2d.drawString(text, tx, ty); 415 } 416 417 428 private void findNextClip(int pw, int ph) { 429 final boolean ltr = table.getComponentOrientation().isLeftToRight(); 430 431 if (col == 0) { 433 if (ltr) { 434 clip.x = 0; 436 } else { 437 clip.x = totalColWidth; 439 } 440 441 clip.y += clip.height; 443 444 clip.width = 0; 446 clip.height = 0; 447 448 int rowCount = table.getRowCount(); 450 int rowHeight = table.getRowHeight(row); 451 do { 452 clip.height += rowHeight; 453 454 if (++row >= rowCount) { 455 break; 456 } 457 458 rowHeight = table.getRowHeight(row); 459 } while (clip.height + rowHeight <= ph); 460 } 461 462 if (printMode == JTable.PrintMode.FIT_WIDTH) { 465 clip.x = 0; 466 clip.width = totalColWidth; 467 return; 468 } 469 470 if (ltr) { 471 clip.x += clip.width; 473 } 474 475 clip.width = 0; 477 478 int colCount = table.getColumnCount(); 480 int colWidth = colModel.getColumn(col).getWidth(); 481 do { 482 clip.width += colWidth; 483 if (!ltr) { 484 clip.x -= colWidth; 485 } 486 487 if (++col >= colCount) { 488 col = 0; 490 491 break; 492 } 493 494 colWidth = colModel.getColumn(col).getWidth(); 495 } while (clip.width + colWidth <= pw); 496 497 } 498 499 } 500 | Popular Tags |