/*
 *  Draco Econometrics
 *  Copyright (C) 2006-2008 Approximatrix, LLC
 *  http://www.approximatrix.com
 *  
 *  This program 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 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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, see <http://www.gnu.org/licenses/>.
 */

package com.approximatrix.draco.plot.ui;

import com.approximatrix.charting.coordsystem.BoxCoordSystem;
import com.approximatrix.charting.model.ChartDataModel;
import com.approximatrix.charting.swing.BufferedChartPanel;

//For printing
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;

public class ExtPrintableChartPanel extends BufferedChartPanel implements Printable {

	/** This constructor is simply for the purposes of extending.  It should
	 * never be called.
	 */
	private ExtPrintableChartPanel() {
		super(null,null);
	}
	
	/** Creates a new ChartPanel with the given model
     * and title string. 
     * @param model the ChartDataModel
     * @param title the title String
     */
    public ExtPrintableChartPanel(ChartDataModel model, String title) {
    	super(model, title);
    	this.setCoordSystem(new BoxCoordSystem(model));
    	this.getCoordSystem().setPaintGrid(true);
    }
    
    /** Creates a new ChartPanel with the given model
     * and title string and a coordinate system.
     * @param model the ChartDataModel
     * @param title the title String
     * @param coord the id of the coordinate system configuration
     */
    public ExtPrintableChartPanel(ChartDataModel model, String title, int coord) {
        super(model,title,coord);
        this.setCoordSystem(new BoxCoordSystem(model));
        this.getCoordSystem().setPaintGrid(true);
    }
	
	/** Implements the Printable interface */
    public int print(Graphics g, PageFormat pf, int page) throws PrinterException {
    	int return_val;
    	int x = (int)pf.getImageableX();
        int y = (int)pf.getImageableY();
        
        this.setBuffering(false);
        
        Dimension reset = this.getSize();
        
        Graphics2D g2 = (Graphics2D)g;
        
        //g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING ,RenderingHints.VALUE_ANTIALIAS_OFF );
        
        g.translate(x,y);
        
        g2.setColor(Color.white);
        g2.fillRect(0,0,x,y);

        this.setBounds(0,0,(int)pf.getImageableWidth(),(int)pf.getImageableHeight());
        this.paint(g2);

        if (page == 0) {
        	//print_image.paint(g);
        	return_val = Printable.PAGE_EXISTS;
        } else
        	return_val = Printable.NO_SUCH_PAGE;

        // Reset the panel on the way out
        
        this.setSize(reset);
        this.setBuffering(true);
        this.getParent().validate();
        
        //this.validate();
        return return_val;
    }
}
