/*
 *  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.general.ui;

import javax.swing.JDesktopPane;
import javax.swing.JInternalFrame;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Rectangle2D;
import java.awt.geom.AffineTransform;
import java.net.URI;

import com.kitfox.svg.SVGDiagram;
import com.kitfox.svg.SVGUniverse;
import com.kitfox.svg.SVGException;

public class JDesktopBackgroundPane extends JDesktopPane {

	private static final String PATH_TO_BG_PICTURE = "/com/approximatrix/draco/main/ui/images/approximatrix-grey.svg";
    private static final String BG_PIC_1 = "bg1_";
    
    /** If true, the logo is centered in the window.  Otherwise, it is in the lower left. */
    private static final boolean CENTER_LOGO = true;
	
	private URI svgURI = null;
    private SVGUniverse svguniverse = null;
    AffineTransform scaleXform = null;

    public JDesktopBackgroundPane() {
        super();
        initializeSVG();
        this.setUI(new javax.swing.plaf.basic.BasicDesktopPaneUI());
    }

    private void initializeSVG() {
    	svguniverse = new SVGUniverse();
    	svgURI = svguniverse.loadSVG(this.getClass().getResourceAsStream(PATH_TO_BG_PICTURE),BG_PIC_1);
    	scaleXform = new AffineTransform();    
    }
    
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(java.awt.Color.WHITE);
        g.fillRect(0,0,getWidth(), getHeight());

        try {
            paintImage((Graphics2D)g);
        } catch (SVGException e) {
            //continue for now
        }
        
    }
    
    private void paintImage(Graphics2D g2) throws SVGException {
        double width = (double)getWidth();
        double height = (double)getHeight();

       	Object oldAliasHint = g2.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        
        SVGDiagram diagram = svguniverse.getDiagram(svgURI);
        
        final Rectangle2D.Double rect = new Rectangle2D.Double();
        diagram.getViewRect(rect);

        double scaling = (width/2.0)/rect.width;
        if(scaling > height/rect.height)
        	scaling = height/rect.height;
        
        AffineTransform oldXform = g2.getTransform();
        
        if(scaling > 0.0) {
        	scaleXform.setToScale(scaling, scaling);
        	if(CENTER_LOGO)
        		scaleXform.translate((width/2)/scaling-rect.width/2,(height/2)/scaling-rect.height/2);
        	else
        		scaleXform.translate(width/scaling-rect.width,height/scaling-rect.height);
        		
        	g2.transform(scaleXform);

      		diagram.render(g2);
        }

        // Reset to old values
        g2.setTransform(oldXform);
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, oldAliasHint);

    }

}
