KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > calipso > reportgenerator > userinterface > PrintPivoteTable


1 package com.calipso.reportgenerator.userinterface;
2
3 import javax.swing.*;
4 import javax.swing.table.TableColumnModel JavaDoc;
5 import javax.swing.table.TableColumn JavaDoc;
6 import java.awt.print.*;
7 import java.awt.*;
8 import java.util.Vector JavaDoc;
9 import java.util.Properties JavaDoc;
10
11 /**
12  * User: pgore
13  * Date: May 19, 2003
14  * Time: 1:03:01 PM
15  */

16 public class PrintPivoteTable extends JComponent implements Printable, Pageable {
17
18   private PivotTable pivotTable;
19   private Vector JavaDoc pageInfoVector;
20   private double pageWidth;
21   private double pageHeigth;
22   private Toolkit toolkit;
23   private Properties JavaDoc printprefs;
24   private PrintJob job;
25
26
27   public PrintPivoteTable(PivotTable pivotTable) {
28       this.pivotTable = pivotTable;
29       pageInfoVector = new Vector JavaDoc();
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 JavaDoc leftSideColumnModel = leftSideTable.getColumnModel();
72     for(int i = 0 ; i < leftSideColumnModel.getColumnCount() ; i++) {
73         TableColumn JavaDoc 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 JavaDoc rigthSideColumnModel = rightSideTable.getColumnModel();
86     for(int i = 0 ; i < rigthSideColumnModel.getColumnCount() ; i++) {
87         TableColumn JavaDoc 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 JavaDoc imageableX = new Float JavaDoc(pf.getPaper().getImageableX());
107     Float JavaDoc imageableY = new Float JavaDoc(pf.getPaper().getImageableY());
108 // pivotTable.getMainSplit().print(g2);
109
return Printable.PAGE_EXISTS;
110
111 /** g2.setBackground(Color.WHITE);
112     Color color;
113     color = pivotTable.getRowHeaderjTable().getBackground();
114
115     graphics = pivotTable.getColumnHeaderjTable().getGraphics();
116     pivotTable.getRowHeaderjTable().setForeground(Color.WHITE);
117 // pivotTable.getLeftCenterPanel().paint(g2);
118 // pivotTable.getCenterSplitNorthPanel().paint(g2);
119     //pivotTable.getFrame().setSize();
120     pivotTable.getRowHeaderjTable().setForeground(color);
121 // pivotTable.getRowHeaderjTable().get
122
123
124     g2.translate(pivotTable.getMainSplit().getLeftComponent().getWidth(), 0);
125     color = pivotTable.getColumnHeaderjTable().getBackground();
126     pivotTable.getColumnHeaderjTable().setForeground(Color.WHITE);
127  // pivotTable.getColumnHeaderjTable().paint(g2);
128     pivotTable.revalidate();
129     pivotTable.getColumnHeaderjTable().setForeground(color);
130     pivotTable.revalidate();*/

131   }
132
133   public int getNumberOfPages() {
134     //return UNKNOWN_NUMBER_OF_PAGES;
135
return 1;
136   }
137
138   public PageFormat getPageFormat(int pageIndex)
139       throws IndexOutOfBoundsException JavaDoc {
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 JavaDoc {
151     return this;
152   }
153
154 }
155
Popular Tags