/**
 *  Filename: $RCSfile: PagePrinter.java,v $
 *  Purpose:
 *  Language: Java
 *  Compiler: JDK 1.3
 *  Authors:  Fabian Hennecke
 *  Version:  $Revision: 1.1.1.1 $
 *            $Date: 2003/07/03 14:59:40 $
 *            $Author: ulmerh $
 *  Copyright (c) Dept. Computer Architecture, University of Tuebingen, Germany
 */

package eva2.tools.print;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.print.PageFormat;
import java.awt.print.Printable;

public class PagePrinter {
    Component c;
    Graphics g;
    PageFormat pf;
    public boolean fit_in_possible = true;

    public PagePrinter(Component c, Graphics g, PageFormat pf) {
        this.c = c;
        this.g = g;
        this.pf = pf;
    }

    public int print() {
        Dimension old = c.getSize();

        int x = (int) pf.getImageableX(),
                y = (int) pf.getImageableY();

        g.translate(x, y);

        double w = (int) pf.getImageableWidth(),
                h = (int) pf.getImageableHeight();

        if (old.width > w || old.height > h) {
            boolean rec_turn = false, rec_fit_in = false;
            if ((old.width > old.height && h > w) ||
                    (old.width < old.height && h < w)) {
                rec_turn = true;
                if (old.width > h || old.height > w) {
                    rec_fit_in = true;
                }
            } else {
                rec_fit_in = true;
            }

            JLabel[] text = new JLabel[4];
            text[0] = new JLabel("The component which should be printed");
            text[1] = new JLabel("is too large.");
            text[2] = new JLabel("You can choose if the component should be");
            JCheckBox cbFitIn = new JCheckBox("fitted-in", rec_fit_in),
                    cbTurn = new JCheckBox("turned", rec_turn);
            text[3] = new JLabel("(Recommended choice is pre-selected)");

            if (!fit_in_possible) {
                cbFitIn.setEnabled(false);
                cbFitIn.setSelected(false);
            }

            GridBagLayout gbl = new GridBagLayout();
            JPanel panel = new JPanel(gbl);
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = gbc.gridy = 0;
            gbc.weightx = gbc.weighty = .5;

            gbc.gridwidth = 2;
            gbl.setConstraints(text[0], gbc);
            panel.add(text[0]);

            gbc.gridy++;
            gbl.setConstraints(text[1], gbc);
            panel.add(text[1]);

            gbc.gridy++;
            gbl.setConstraints(text[2], gbc);
            panel.add(text[2]);

            gbc.gridy++;
            gbc.gridwidth = 1;
            gbl.setConstraints(cbFitIn, gbc);
            panel.add(cbFitIn);

            gbc.gridx++;
            gbl.setConstraints(cbTurn, gbc);
            panel.add(cbTurn);
            gbc.gridx = 0;
            gbc.gridwidth = 2;
            gbc.gridy++;
            gbl.setConstraints(text[3], gbc);
            panel.add(text[3]);

            int choice = JOptionPane.showOptionDialog(c, panel, "Fit-in",
                    JOptionPane.OK_CANCEL_OPTION,
                    JOptionPane.QUESTION_MESSAGE,
                    null, null, null);

            if (choice == JOptionPane.CANCEL_OPTION || choice == JOptionPane.CLOSED_OPTION) {
                return Printable.NO_SUCH_PAGE;
            } else if (choice == JOptionPane.OK_OPTION) {

                if (cbTurn.isSelected()) {
                    BufferedImage img;
                    if (cbFitIn.isSelected()) {
                        double m = Math.min(h / (double) old.width, w / (double) old.height);
                        img = (BufferedImage) c.createImage((int) (old.height * m), (int) (old.width * m));
                        Graphics2D g2 = img.createGraphics();
                        g2.rotate(Math.toRadians(90));
                        g2.translate(0, -old.height * m);
                        c.setSize((int) (old.width * m), (int) (old.height * m));
                        c.paint(g2);
                        c.setSize(old);
                    } else {
                        img = (BufferedImage) c.createImage(old.height, old.width);
                        Graphics2D g2 = img.createGraphics();
                        g2.rotate(Math.toRadians(90));
                        g2.translate(0, -old.height);
                        c.paint(g2);
                    }
                    g.drawImage(img, 0, 0, c.getBackground(), c);
                } else {
                    if (cbFitIn.isSelected()) {
                        double m = Math.min(w / (double) old.width, h / (double) old.height);
                        c.setSize((int) (old.width * m), (int) (old.height * m));
                        c.paint(g);
                        c.setSize(old);
                    } else {
                        c.paint(g);
                    }
                }
            }
        } else {
            c.paint(g);
        }
        return Printable.PAGE_EXISTS;
    }
}