package demo.view;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.font.TextLayout;
import java.awt.geom.Rectangle2D;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.PrinterException;

import y.view.Graph2DPrinter;
import y.view.Graph2DView;

/**
 * Graph2DPrinter that supports printable diagram titles.
 */
public class EnhancedGraph2DPrinter extends Graph2DPrinter
{
  Font   titleFont;
  String titleText;
  Color  titleBarColor;
  Color  titleColor;
  	
  /**
   * @param view
   */
  public EnhancedGraph2DPrinter(Graph2DView view) {
    super(view);
    // TODO Auto-generated constructor stub
    titleFont = new Font("Dialog", Font.PLAIN, 24);
    titleBarColor = Color.lightGray;
    titleColor = Color.black;
   }

  public int print(Graphics g1d, PageFormat pf, int pi ) throws PrinterException
  {
    Graphics2D gfx = (Graphics2D)g1d.create();

    if(titleText != null && !"".equals(titleText))
    {
      TextLayout tl = new TextLayout(titleText, titleFont, gfx.getFontRenderContext());
      double titleHeight = tl.getBounds().getHeight() + 20;        
      
	    //determine the region for the title and the region for the content.
	    Paper p = pf.getPaper();
	    Rectangle2D.Double cr = null;
	    Rectangle2D.Double tr = null; 
	    if(pf.getOrientation() == PageFormat.PORTRAIT)
	    {
	      cr = new Rectangle2D.Double(p.getImageableX(),p.getImageableY()+titleHeight, p.getImageableWidth(), p.getImageableHeight()-titleHeight);
	      tr = new Rectangle2D.Double(p.getImageableX(), p.getImageableY(), p.getImageableWidth(), titleHeight);
	    }
	    else if(pf.getOrientation() == PageFormat.LANDSCAPE)
	    {
	      cr = new Rectangle2D.Double(p.getImageableX()+titleHeight,p.getImageableY(), p.getImageableWidth()-titleHeight, p.getImageableHeight());
	      tr = new Rectangle2D.Double(p.getImageableX(),p.getImageableY(), p.getImageableHeight(), titleHeight);  
	    }
	    else if(pf.getOrientation() == PageFormat.REVERSE_LANDSCAPE)
	    {
	      cr = new Rectangle2D.Double(p.getImageableX(),p.getImageableY(), p.getImageableWidth()-titleHeight, p.getImageableHeight());
	      tr = new Rectangle2D.Double(p.getImageableX(),p.getImageableY(), p.getImageableHeight(), titleHeight);  
	    }
	    
    
	    //print a title bar that spans the whole page
	    if(titleBarColor != null)
	    {
	      gfx.setColor(Color.lightGray);
	      gfx.fill(tr);
	    }
	    
	    //print the title text
	    gfx.setColor(titleColor);
	    Rectangle2D tlb = tl.getBounds();
	    tl.draw(gfx, 
        (float)(tr.x + (tr.width-tlb.getWidth())/2.0),
        (float)(tr.y + (titleHeight + tlb.getHeight())/2.0));
    
	    //reduce size of the paper so that the content is below the title 
	    pf = (PageFormat)pf.clone();
	    p.setImageableArea(cr.x, cr.y, cr.width, cr.height);
	    pf.setPaper(p);
    }
    
    //print the content
    return super.print(g1d, pf, pi);      
  }  
  
  
  /**
   * @return Returns the titleColor.
   */
  public Color getTitleColor()
  {
    return titleColor;
  }
  /**
   * @param titleColor The titleColor to set.
   */
  public void setTitleColor(Color titleColor)
  {
    this.titleColor = titleColor;
  }
  /**
   * @return Returns the titleBarColor.
   */
  public Color getTitleBarColor()
  {
    return titleBarColor;
  }
  /**
   * @param titleBarColor The titleBarColor to set.
   */
  public void setTitleBarColor(Color titleBarColor)
  {
    this.titleBarColor = titleBarColor;
  }
  /**
   * @return Returns the titleFont.
   */
  public Font getTitleFont()
  {
    return titleFont;
  }
  /**
   * @param titleFont The titleFont to set.
   */
  public void setTitleFont(Font titleFont)
  {
    this.titleFont = titleFont;
  }
  /**
   * @return Returns the titleText.
   */
  public String getTitleText()
  {
    return titleText;
  }
  /**
   * @param titleText The titleText to set.
   */
  public void setTitleText(String titleText)
  {
    this.titleText = titleText;
  }
}
