/*

 * Title: japlot.jaxodraw
 * Description: Graphical user interface for drawing Feynman diagrams
 * @author Daniele Binosi 
 * @author Lukas Theussl  

 Copyright (C) 2003-2006,  Daniele Binosi and Lukas Theussl

 See the file LICENSE in the source distribution home directory
 for a full copy of the GPL (GNU General Public License).

    This file is part of japlot.jaxodraw.

    japlot.jaxodraw is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    japlot.jaxodraw is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


 */
package japlot.jaxodraw;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;

import java.util.ResourceBundle;

import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
import javax.print.attribute.standard.JobName;
import javax.print.attribute.standard.OrientationRequested;

import org.freehep.graphics2d.VectorGraphics;
import javax.swing.JOptionPane;

import jhplot.jadraw.JaObject;
import japlot.Plot;


/** Responsible for printing directly to a printer. */
public class JaxoPrint implements Printable {
    private ResourceBundle language =
        ResourceBundle.getBundle(JaxoPrefs.getPref(JaxoPrefs.PREF_LANGUAGE));
    private JaxoGraph graph;
    private double transX;
    private double transY;
    private double scale = 1.d;
    private Dimension canvasD;
    private String currentTab = "";

    /** The actual printing routine.
     * @param g The specified graphics context.
     * @param pf The specified page format.
     * @param pageIndex The specified page index.
     * @return Either Printable.PAGE_EXISTS or Printable.NO_SUCH_PAGE.
     */
    public final int print(Graphics g, PageFormat pf, int pageIndex) {
        transX = pf.getImageableX();
        transY = pf.getImageableY();

        double scaleX = pf.getImageableWidth() / canvasD.width;
        double scaleY = pf.getImageableHeight() / canvasD.height;

        if (pageIndex == 0) {
            // Graphics2D g2 = (Graphics2D) g;
            VectorGraphics g2 = VectorGraphics.create(g);
 
            scale = scaleX;

            if (scaleY < scaleX) {
                scale = scaleY;
            }

            if (scale > 1.) {
                scale = 1.d;
            }

            g2.translate(transX, transY);
            g2.scale(scale, scale);

            int length = graph.listSize();

            // this is first
            for (int i = 0; i < length; i++) {
                JaObject ob = (JaObject) graph.listElementAt(i);
                if (Plot.drawFirst(ob)) ob.jaxoDraw(g2, false);
            }
            
            // this is second
            for (int i = 0; i < length; i++) {
                JaObject ob = (JaObject) graph.listElementAt(i);
                Plot.drawRest(ob,g2,false);
            }


            return Printable.PAGE_EXISTS;
        } else {
            return Printable.NO_SUCH_PAGE;
        }
    }

    /** The method to be called from the printing menu.
     * @param newGraph The JaxoGraph to be printed.
     * @param d The current dimension of the canvas.
     */
    public final void print(JaxoGraph newGraph, Dimension d) {
        this.graph = newGraph;
        this.canvasD = d;

        PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
        aset.add(OrientationRequested.LANDSCAPE);
        aset.add(new Copies(1));
        aset.add(new JobName(currentTab, null));

        PrinterJob pj = PrinterJob.getPrinterJob();
        pj.setPrintable(this);

        PrintService[] services = PrinterJob.lookupPrintServices();

        PrintService defaultService =
            PrintServiceLookup.lookupDefaultPrintService();

        if (services.length > 0) {
            try {
                pj.setPrintService(services[0]);

                if (pj.printDialog(aset)) {
                    pj.print(aset);
                }
            } catch (PrinterException pe) {
                if (JaxoPrefs.verbose()) {
                    System.err.println(pe);
                }

                JOptionPane.showMessageDialog(null,
                    language.getString(
                        "PrinterException:_Cannot_print_document!"));
            }
        } else if (defaultService != null) {
            try {
                pj.setPrintService(defaultService);

                if (pj.printDialog(aset)) {
                    pj.print(aset);
                }
            } catch (PrinterException pe) {
                if (JaxoPrefs.verbose()) {
                    System.err.println(pe);
                }

                JOptionPane.showMessageDialog(null,
                    language.getString(
                        "PrinterException:_Cannot_print_document!"));
            }
        } else {
            JOptionPane.showMessageDialog(null,
                language.getString("No_printers_found!"));
        }
    }

    /** Sets the currentTab property.
     * @param newTab The name of the current tab.
     */
    public final void setCurrentTab(String newTab) {
        this.currentTab = newTab;
    }
}

 

Ads help maintain this website.