001: package com.calipso.reportgenerator.userinterface;
002:
003: import javax.swing.*;
004: import javax.swing.table.TableColumnModel;
005: import javax.swing.table.TableColumn;
006: import java.awt.print.*;
007: import java.awt.*;
008: import java.util.Vector;
009: import java.util.Properties;
010:
011: /**
012: * User: pgore
013: * Date: May 19, 2003
014: * Time: 1:03:01 PM
015: */
016: public class PrintPivoteTable extends JComponent implements Printable,
017: Pageable {
018:
019: private PivotTable pivotTable;
020: private Vector pageInfoVector;
021: private double pageWidth;
022: private double pageHeigth;
023: private Toolkit toolkit;
024: private Properties printprefs;
025: private PrintJob job;
026:
027: public PrintPivoteTable(PivotTable pivotTable) {
028: this .pivotTable = pivotTable;
029: pageInfoVector = new Vector();
030: setPageWidth();
031: setPageHeight();
032:
033: setInfoPerPage();
034: }
035:
036: private void setPageWidth() {
037: pageWidth = PrinterJob.getPrinterJob().defaultPage().getWidth();
038: }
039:
040: private void setPageHeight() {
041: pageHeigth = PrinterJob.getPrinterJob().defaultPage()
042: .getHeight();
043: }
044:
045: private void setInfoPerPage() {
046: PageInfo pageInfo = new PageInfo();
047: getHeightPerPage(0, pageInfo);
048: getWidthPerPage(0, pageInfo);
049: pageInfoVector.add(pageInfo);
050: }
051:
052: private void getHeightPerPage(double startAtHeight,
053: PageInfo pageInfo) {
054: int endAtHeight = 0;
055:
056: JTable header = pivotTable.getColumnHeaderjTable();
057: JTable rowsTable = pivotTable.getDatajTable();
058: for (int i = 0; i < rowsTable.getRowCount(); i++) {
059: int rowHeight = rowsTable.getRowHeight(i);
060: endAtHeight += rowHeight;
061: if (rowHeight >= pageHeigth - rowsTable.getHeight()) {
062: endAtHeight = endAtHeight - rowHeight;
063: pageInfo.setRowFrom(startAtHeight);
064: pageInfo.setRowTo(endAtHeight);
065: }
066: }
067: }
068:
069: private void getWidthPerPage(double startAtWidth, PageInfo pageInfo) {
070: int endAtWidth = 0;
071:
072: JTable leftSideTable = pivotTable.getRowHeaderjTable();
073: TableColumnModel leftSideColumnModel = leftSideTable
074: .getColumnModel();
075: for (int i = 0; i < leftSideColumnModel.getColumnCount(); i++) {
076: TableColumn leftSideColumn = leftSideColumnModel
077: .getColumn(i);
078: int leftSideColumnWidth = leftSideColumn.getWidth();
079: endAtWidth = +leftSideColumnWidth;
080: if (endAtWidth >= pageWidth) {
081: endAtWidth = endAtWidth - leftSideColumnWidth;
082: pageInfo.setColumnFrom(startAtWidth);
083: pageInfo.setColumnTo(endAtWidth);
084: return;
085: }
086: }
087:
088: JTable rightSideTable = pivotTable.getRowHeaderjTable();
089: TableColumnModel rigthSideColumnModel = rightSideTable
090: .getColumnModel();
091: for (int i = 0; i < rigthSideColumnModel.getColumnCount(); i++) {
092: TableColumn rightSideColumn = rigthSideColumnModel
093: .getColumn(i);
094: int rightSideColumnWidth = rightSideColumn.getWidth();
095: endAtWidth = +rightSideColumnWidth;
096: if (endAtWidth >= pageWidth) {
097: endAtWidth = endAtWidth - rightSideColumnWidth;
098: pageInfo.setColumnFrom(startAtWidth);
099: pageInfo.setColumnTo(endAtWidth);
100: }
101: }
102:
103: }
104:
105: public int print(Graphics graphics, PageFormat pf, int pageIndex)
106: throws PrinterException {
107: Graphics2D g2 = (Graphics2D) graphics;
108:
109: g2.translate(pf.getPaper().getImageableX(), pf.getPaper()
110: .getImageableY());
111: g2.drawString("Prueba", 0, 0);
112: g2.translate(0, 60);
113: Float imageableX = new Float(pf.getPaper().getImageableX());
114: Float imageableY = new Float(pf.getPaper().getImageableY());
115: // pivotTable.getMainSplit().print(g2);
116: return Printable.PAGE_EXISTS;
117:
118: /** g2.setBackground(Color.WHITE);
119: Color color;
120: color = pivotTable.getRowHeaderjTable().getBackground();
121:
122: graphics = pivotTable.getColumnHeaderjTable().getGraphics();
123: pivotTable.getRowHeaderjTable().setForeground(Color.WHITE);
124: // pivotTable.getLeftCenterPanel().paint(g2);
125: // pivotTable.getCenterSplitNorthPanel().paint(g2);
126: //pivotTable.getFrame().setSize();
127: pivotTable.getRowHeaderjTable().setForeground(color);
128: // pivotTable.getRowHeaderjTable().get
129:
130:
131: g2.translate(pivotTable.getMainSplit().getLeftComponent().getWidth(), 0);
132: color = pivotTable.getColumnHeaderjTable().getBackground();
133: pivotTable.getColumnHeaderjTable().setForeground(Color.WHITE);
134: // pivotTable.getColumnHeaderjTable().paint(g2);
135: pivotTable.revalidate();
136: pivotTable.getColumnHeaderjTable().setForeground(color);
137: pivotTable.revalidate();*/
138: }
139:
140: public int getNumberOfPages() {
141: //return UNKNOWN_NUMBER_OF_PAGES;
142: return 1;
143: }
144:
145: public PageFormat getPageFormat(int pageIndex)
146: throws IndexOutOfBoundsException {
147: PageFormat pf = new PageFormat();
148: Paper paper = pf.getPaper();
149: double widthA4 = 210 * 1.0 / 25.4 * 72;
150: double HeightA4 = 210 * 1.0 / 25.4 * 72;
151: paper.setSize(widthA4, HeightA4);
152: pf.setPaper(paper);
153: return pf;
154: }
155:
156: public Printable getPrintable(int pageIndex)
157: throws IndexOutOfBoundsException {
158: return this;
159: }
160:
161: }
|