/*
 *  Draco Econometrics
 *  Copyright (C) 2006-2007 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 java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.RenderingHints;
import java.awt.geom.Rectangle2D;
import java.awt.geom.AffineTransform;
import java.net.URI;

import javax.swing.border.Border;

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

/** Small class for generating a white background on the desktop pane.  This can
 * be modified to use an image.
 * 
 * @author DrLaszloJamf, armstrong
 * @see http://forum.java.sun.com/thread.jspa?forumID=57&threadID=506054
 */
public class CenteredBackgroundBorder implements Border {
    //private final BufferedImage image;
	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 CenteredBackgroundBorder() {
        //this.image = image;
    	
    	svguniverse = new SVGUniverse();
    	svgURI = svguniverse.loadSVG(this.getClass().getResourceAsStream(PATH_TO_BG_PICTURE),BG_PIC_1);
    	scaleXform = new AffineTransform();
    }
 
    public void paintBorder(java.awt.Component c, java.awt.Graphics g, int x, int y, int width, int height) {
        //int x0 = x + (width-image.getWidth())/2;
        //int y0 = y + (height-image.getHeight())/2;
        //g.drawImage(image, x0, y0, null);
    	g.setColor(java.awt.Color.WHITE);
    	g.fillRect(x, y, width, height);
    	
    	Graphics2D g2 = (Graphics2D)g;
    	
    	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);

        	try
        	{
        		diagram.render(g2);
        	}
        	catch (SVGException e)
        	{
        		throw new RuntimeException(e);
        	}
        }

        // Reset to old values
        g2.setTransform(oldXform);
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, oldAliasHint);
    }
 
    public java.awt.Insets getBorderInsets(java.awt.Component c) {
        return new java.awt.Insets(0,0,0,0);
    }
 
    public boolean isBorderOpaque() {
        return true;
    }
}
