//----------------------------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.event.ActionEvent;
import java.io.File;
import java.io.FileOutputStream;

import javax.swing.JMenuItem;

import org.huihoo.god4j.GOD4JConstants;
import org.huihoo.god4j.GOD4JFactory;

/**
 * @author liangce
 *
 * To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
public class Quit extends Close
{
	public Quit()
	{
		super();
		setEnabled(true);
	}

	public void actionPerformed(ActionEvent e)
	{
		if(!closeDocumentIfPossible())
		{
			return;
		}
		
		savePreferencesToFile();
		
		System.exit(0);
	}
	
	public static void savePreferencesToFile()
	{
		try
		{
			String fileList = "";
			for (int i = GOD4JFactory.getGOD4J().getRecentFilesMenu().getItemCount(); i > 0;)
			{
				JMenuItem mItem = ((JMenuItem) GOD4JFactory.getGOD4J().getRecentFilesMenu().getMenuComponent(--i));
				fileList += mItem.getText().substring(2);
				if (i > 0)
					fileList += "\n";
			}
			FileOutputStream fos =
				new FileOutputStream(GOD4JConstants.GOD4J_USER_HOME + File.separator + GOD4JConstants.RFL_FILENAME);
			fos.write(fileList.getBytes("UTF8"));
			// Write to file
			fos.flush();
			fos.close();
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}		
	}
}
