1 package com.calipso.reportgenerator.userinterface; 2 3 import javax.swing.*; 4 import javax.swing.table.TableColumnModel ; 5 import javax.swing.table.TableColumn ; 6 import java.awt.print.*; 7 import java.awt.*; 8 import java.util.Vector ; 9 import java.util.Properties ; 10 11 16 public class PrintPivoteTable extends JComponent implements Printable, Pageable { 17 18 private PivotTable pivotTable; 19 private Vector pageInfoVector; 20 private double pageWidth; 21 private double pageHeigth; 22 private Toolkit toolkit; 23 private Properties printprefs; 24 private PrintJob job; 25 26 27 public PrintPivoteTable(PivotTable pivotTable) { 28 this.pivotTable = pivotTable; 29 pageInfoVector = new Vector (); 30 setPageWidth(); 31 setPageHeight(); 32 33 setInfoPerPage(); 34 } 35 36 private void setPageWidth() { 37 pageWidth = PrinterJob.getPrinterJob().defaultPage().getWidth(); 38 } 39 40 private void setPageHeight(){ 41 pageHeigth = PrinterJob.getPrinterJob().defaultPage().getHeight(); 42 } 43 44 private void setInfoPerPage() { 45 PageInfo pageInfo = new PageInfo(); 46 getHeightPerPage(0, pageInfo); 47 getWidthPerPage(0, pageInfo); 48 pageInfoVector.add(pageInfo); 49 } 50 51 private void getHeightPerPage(double startAtHeight, PageInfo pageInfo) { 52 int endAtHeight=0; 53 54 JTable header = pivotTable.getColumnHeaderjTable(); 55 JTable rowsTable = pivotTable.getDatajTable(); 56 for(int i=0 ; i < rowsTable.getRowCount() ; i++) { 57 int rowHeight = rowsTable.getRowHeight(i); 58 endAtHeight += rowHeight; 59 if(rowHeight >= pageHeigth - rowsTable.getHeight()) { 60 endAtHeight = endAtHeight - rowHeight; 61 pageInfo.setRowFrom(startAtHeight); 62 pageInfo.setRowTo(endAtHeight); 63 } 64 } 65 } 66 67 private void getWidthPerPage(double startAtWidth, PageInfo pageInfo){ 68 int endAtWidth=0; 69 70 JTable leftSideTable = pivotTable.getRowHeaderjTable(); 71 TableColumnModel leftSideColumnModel = leftSideTable.getColumnModel(); 72 for(int i = 0 ; i < leftSideColumnModel.getColumnCount() ; i++) { 73 TableColumn leftSideColumn = leftSideColumnModel.getColumn(i); 74 int leftSideColumnWidth = leftSideColumn.getWidth(); 75 endAtWidth =+ leftSideColumnWidth; 76 if(endAtWidth >= pageWidth) { 77 endAtWidth = endAtWidth - leftSideColumnWidth; 78 pageInfo.setColumnFrom(startAtWidth); 79 pageInfo.setColumnTo(endAtWidth); 80 return; 81 } 82 } 83 84 JTable rightSideTable = pivotTable.getRowHeaderjTable(); 85 TableColumnModel rigthSideColumnModel = rightSideTable.getColumnModel(); 86 for(int i = 0 ; i < rigthSideColumnModel.getColumnCount() ; i++) { 87 TableColumn rightSideColumn = rigthSideColumnModel.getColumn(i); 88 int rightSideColumnWidth = rightSideColumn.getWidth(); 89 endAtWidth =+ rightSideColumnWidth; 90 if(endAtWidth >= pageWidth) { 91 endAtWidth = endAtWidth - rightSideColumnWidth; 92 pageInfo.setColumnFrom(startAtWidth); 93 pageInfo.setColumnTo(endAtWidth); 94 } 95 } 96 97 } 98 99 public int print(Graphics graphics, PageFormat pf, int pageIndex) 100 throws PrinterException { 101 Graphics2D g2 = (Graphics2D) graphics; 102 103 g2.translate(pf.getPaper().getImageableX(), pf.getPaper().getImageableY()); 104 g2.drawString("Prueba",0,0); 105 g2.translate(0, 60); 106 Float imageableX = new Float (pf.getPaper().getImageableX()); 107 Float imageableY = new Float (pf.getPaper().getImageableY()); 108 return Printable.PAGE_EXISTS; 110 111 131 } 132 133 public int getNumberOfPages() { 134 return 1; 136 } 137 138 public PageFormat getPageFormat(int pageIndex) 139 throws IndexOutOfBoundsException { 140 PageFormat pf = new PageFormat(); 141 Paper paper = pf.getPaper(); 142 double widthA4 = 210 * 1.0/25.4*72; 143 double HeightA4 = 210 * 1.0/25.4*72; 144 paper.setSize(widthA4 , HeightA4); 145 pf.setPaper(paper); 146 return pf; 147 } 148 149 public Printable getPrintable(int pageIndex) 150 throws IndexOutOfBoundsException { 151 return this; 152 } 153 154 } 155 | Popular Tags |