//----------------------------BEGIN LICENSE----------------------------
/* 
 * Willow : the Open Source WorkFlow Project
 * Distributable under GNU LGPL license by gun.org
 *
 * Copyright (C) 2004-2010 huihoo.org 
 * Copyright (C) 2004-2010  ZosaTapo <dertyang@hotmail.com> 
 * 
 * ====================================================================
 * Project Homepage : http://www.huihoo.org/willow
 * Source Forge     : http://sourceforge.net/projects/huihoo
 * Mailing list     : willow@lists.sourceforge.net 
 */
//----------------------------END  LICENSE----------------------------
package org.huihoo.god4j.actions;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Iterator;

import javax.swing.JFileChooser;

import org.huihoo.god4j.GOD4JFactory;
import org.huihoo.god4j.JGraphFrame;
import org.huihoo.god4j.jgraph.XPDLJGraph;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import com.zosatapo.commons.util.FileUtils;

/**
 * @author liangce
 *
 * To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
public class SaveAsJPG    extends ActionBase
{

	public SaveAsJPG()
	{
		super();
		setEnabled(false);
	}

	public void actionPerformed(ActionEvent e)
	{	
		JFileChooser chooser = new JFileChooser(GOD4JFactory.getGOD4J().getWorkingDirectory());
		chooser.rescanCurrentDirectory();
		if (chooser.showSaveDialog(GOD4JFactory.getGOD4J()) == JFileChooser.APPROVE_OPTION)
		{
			Iterator iterator = GOD4JFactory.getGOD4J().getControlPanel().findJGraphs();
			int graph_count = 1;
			String baseFileName = chooser.getSelectedFile().getAbsolutePath();
			String dir=FileUtils.getDirectory(baseFileName);
			String name=FileUtils.getName(baseFileName);
			String ext=FileUtils.getExtenstion(baseFileName);
			
			String fileName=null;
			
			while (iterator.hasNext())
			{
				if(graph_count == 1)
				{
					fileName=baseFileName;
				}
				else
				{
					fileName=dir+File.separator+name+ "_" +(graph_count++) +ext;
				}
				JGraphFrame frame = (JGraphFrame) iterator.next();
				saveAsJPG(fileName, frame.getJGraph());
			}
		}
	}
	
	protected boolean saveAsJPG(String filename, XPDLJGraph jgraph)
	{
		try
		{

			BufferedImage img = getImage(jgraph);
			FileOutputStream fos = new FileOutputStream(filename);
			JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fos);
			encoder.encode(img);
			fos.flush();
			fos.close();

			return true;
		}
		catch (Throwable t)
		{
			t.printStackTrace();
		}

		return false;
	}
	
	// Create a buffered image of the specified graph.
	protected BufferedImage getImage(XPDLJGraph jGraph)
	{

		Object[] cells = jGraph.getRoots();

		if (cells.length > 0)
		{
			Rectangle2D bounds = jGraph.getCellBounds(cells);
			//bounds = jGraph.toScreen(bounds);

			// Create a Buffered Image
			Dimension d = bounds.getBounds().getSize();

			BufferedImage img = new BufferedImage(d.width +50, d.height+50 , BufferedImage.TYPE_INT_RGB);
			Graphics2D graphics = img.createGraphics();
			graphics.setBackground(Color.white);
			jGraph.paint(graphics);

			return img;
		}
		return null;
	}
}
