// File: ActionPrint.java
// Classes: ActionPrint
// Author: Kedar Patankar

package edu.neu.ccs.demeter.tools.apstudio.graphedit;

import java.awt.PrintJob;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Rectangle;

public class ActionPrint extends Action
{
	public ActionPrint(Editor e){super(e);}
	public String name(){ return "prints current document";}
	public void doIt()
	{
		PrintJob pjob;

		try{pjob = _editor.getToolkit().getPrintJob(_editor,"Printing document",null);}
		catch(java.lang.Throwable ex){return;}  
// The try block was needed to cover the cancel at filename dialog for Printing to file
		
		if(pjob == null)
			return;
		
		Graphics pg = pjob.getGraphics();
		if(pg == null)
			return;

		Document d=_editor.curDocument();
		Color oldColor = d.getBackground();
		d.setBackground(Color.white);
		int docWidth = d.getBoundingBox().width+20;
		int docHeight = d.getBoundingBox().height+20;
		int pageWidth = pjob.getPageDimension().width;
		int pageHeight = pjob.getPageDimension().height;

		int horz,vert;
		for(horz=0;docWidth>0;horz++,docWidth-=pageWidth);
		for(vert=1,docHeight-=pageHeight;docHeight>0;vert++,docHeight-=pageHeight);
//		System.out.println("horz "+horz+" vert "+vert);
		int x,y,i;

		for(x=0,y=0;vert>0;vert--,y+=pageHeight)
		{
			for(x=0,i=horz;i>0;i--,x+=pageWidth)
			{
				pg = pjob.getGraphics();
				if(pg == null)
				{
					d.setBackground(oldColor);
					pjob.end();
					return;
				}
				Globals.setPrintClip(x,y,pageWidth,pageHeight);
				pg.translate(-x,-y);
				d.printAll(pg);
				pg.dispose();
			}
		}
		d.setBackground(oldColor);

		pjob.end();
	}

	public void undoIt(){}
} /* end class ActionPrint */

