/*
 * Created on Feb 20, 2004
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
import java.io.FileOutputStream;
import java.io.OutputStream;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.PageSize;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPRow;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;

/**
 * @author SDKNBJ
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
public class CrossTab {

	private static float _heights[] = null;
	private static float _columnLabelWidth = 50f;
	private static float _rowLabelWidth = 50f;
	
	public static void main(String[] args) throws Exception {
		System.out.println("Writing document...");
		OutputStream outputStream = new FileOutputStream("olap.pdf");
		
		Document document = new Document(PageSize.A4, 50, 50, 50, 50);
		PdfWriter writer = PdfWriter.getInstance(document, outputStream);
		document.open();

		PdfPTable pdfTable = new PdfPTable(new float[] {2 * _rowLabelWidth, 4 * _columnLabelWidth});
		PdfPTable labels = createLabels();
		PdfPTable columnHeaders = createColumnHeaders();
		PdfPTable rowHeaders = createRowHeaders();
		PdfPTable rowHeaderCells = createRowHeaderCells();
		PdfPTable rowLabels = createRowLabels();
		PdfPTable cells = createCells();
		
		pdfTable.addCell(newCell(labels));
		pdfTable.addCell(newCell(columnHeaders));
		pdfTable.addCell(newCell(rowHeaders));
		pdfTable.addCell(newCell(rowHeaderCells));
		pdfTable.addCell(newCell(rowLabels));
		pdfTable.addCell(newCell(cells));
		
		document.add(pdfTable);
		
		document.close();
		System.out.println("Done!");
	}

	private static PdfPCell newCell(String text, float height) {
		PdfPCell cell = new PdfPCell(new Phrase(text));
		cell.setBorderWidth(0.5f); 
		cell.setFixedHeight(height);
		return cell;
	}

	private static PdfPCell newCell(String text) {
		PdfPCell cell = new PdfPCell(new Phrase(text));
		cell.setBorderWidth(0.5f); 
		return cell;
	}
	
	private static PdfPCell newCell(PdfPTable table) {
		PdfPCell cell = new PdfPCell(table);
		cell.setBorderWidth(0.5f); 
		return cell;
	}
	
	
	/**
	 * @return
	 */
	private static PdfPTable createRowLabels() {
		PdfPTable table = new PdfPTable(new float[] {_rowLabelWidth, _rowLabelWidth});
		table.setTotalWidth(2 * _rowLabelWidth);
		PdfPTable table1 = new PdfPTable(new float[] {_rowLabelWidth});
		table1.setTotalWidth(_rowLabelWidth);
		PdfPTable table1_2 = new PdfPTable(new float[] {_rowLabelWidth});
		table1_2.setTotalWidth(_rowLabelWidth);
		
		PdfPCell label1 = newCell("2000");
		table1.addCell(label1);
		
		PdfPCell labels1_2[] = new PdfPCell[] {
				newCell("Collectibles"),
				newCell("Gardening"),
				newCell("Pets"),
				newCell("Software"),
				newCell("Sports"),
				newCell("Toys")};
		
		_heights = new float[labels1_2.length];
		
		for (int i = 0; i < labels1_2.length; i++) {
			PdfPRow row = new PdfPRow(new PdfPCell[] {labels1_2[i]});
			row.setWidths(new float[] {_rowLabelWidth});
			_heights[i] = row.calculateHeights();
		}

		for (int i = 0; i < labels1_2.length; i++) {
			table1_2.addCell(labels1_2[i]);
		}

		float totalHeight = 0;
		for (int i = 0; i < labels1_2.length; i++) {
			_heights[i] = table1_2.getRowHeight(i);
			System.out.println(_heights[i]);
			totalHeight += _heights[i];
		}
		
		System.out.println(totalHeight);
		label1.setFixedHeight(totalHeight);
		
		table.addCell(newCell(table1));
		table.addCell(newCell(table1_2));
		
		return table;
	}

	/**
	 * @return
	 */
	private static PdfPTable createRowHeaderCells() {
		PdfPTable table = new PdfPTable(4);
		return table;
	}

	private static PdfPTable createCells() {
		PdfPTable table = new PdfPTable(new float[] {
				_columnLabelWidth, _columnLabelWidth, _columnLabelWidth, _columnLabelWidth});
		table.setTotalWidth(4 * _columnLabelWidth);
		
		PdfPCell cells[][] = new PdfPCell[][] {
			{
				newCell("."),
				newCell("."),
				newCell("5"),
				newCell("$138.00")
			},
			{
				newCell("13"),
				newCell("$599.00"),
				newCell("27"),
				newCell("$995.00")
			},
			{
				newCell("21"),
				newCell("$106.00"),
				newCell("52"),
				newCell("$322.50")
			},
			{
				newCell("."),
				newCell("."),
				newCell("5"),
				newCell("$138.00")
			},
			{
				newCell("13"),
				newCell("$599.00"),
				newCell("27"),
				newCell("$995.00")
			},
			{
				newCell("21"),
				newCell("$106.00"),
				newCell("52"),
				newCell("$322.50")
			}
		};

		for (int i = 0; i < cells.length; i++) {
			for (int j = 0; j < cells[i].length; j++) {
				cells[i][j].setFixedHeight(_heights[i]);
				cells[i][j].setHorizontalAlignment(Element.ALIGN_RIGHT);
				table.addCell(cells[i][j]);
			}
		}
		
		return table;
	}

	private static PdfPTable createRowHeaders() {
		PdfPTable table = new PdfPTable(new float[] {
				_rowLabelWidth, _rowLabelWidth});
		table.setTotalWidth(2 * _rowLabelWidth);
		
		PdfPCell header1 = newCell("YEAR");
		PdfPCell header1_2_1 = newCell("Catalog");

		table.addCell(header1);
		table.addCell(header1_2_1);
		
		return table;
	}

	private static PdfPTable createColumnHeaders() throws DocumentException {
		PdfPTable table = new PdfPTable(new float[] {
				_columnLabelWidth, _columnLabelWidth, _columnLabelWidth, _columnLabelWidth});
		table.setTotalWidth(4 * _columnLabelWidth);
		
		PdfPCell header1 = newCell("CANADA");
		PdfPCell header1_2_1 = newCell("COST_N");
		PdfPCell header1_2_2 = newCell("SALES_COST");

		PdfPCell header2 = newCell("CENRAL");
		PdfPCell header2_2_1 = newCell("COST_N");
		PdfPCell header2_2_2 = newCell("SALES_COST");
		
		header1.setColspan(2);
		header2.setColspan(2);
		
		header1.setHorizontalAlignment(Element.ALIGN_CENTER);
		header2.setHorizontalAlignment(Element.ALIGN_CENTER);

		header1_2_1.setHorizontalAlignment(Element.ALIGN_CENTER);
		header1_2_2.setHorizontalAlignment(Element.ALIGN_CENTER);

		header2_2_1.setHorizontalAlignment(Element.ALIGN_CENTER);
		header2_2_2.setHorizontalAlignment(Element.ALIGN_CENTER);
		
		table.addCell(header1);
		table.addCell(header2);

		table.addCell(header1_2_1);
		table.addCell(header1_2_2);

		table.addCell(header2_2_1);
		table.addCell(header2_2_2);

		return table;
	}

	private static PdfPTable createLabels() {
		PdfPTable table = new PdfPTable(new float[] {2 * _rowLabelWidth});
		table.setTotalWidth(2 * _rowLabelWidth);
		
		PdfPCell label1 = newCell("REGION");
		label1.setHorizontalAlignment(Element.ALIGN_RIGHT);
		PdfPCell label2 = newCell("");

		table.addCell(label1);
		table.addCell(label2);
		
		return table;
	}
}
