//----------------------------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 javax.swing.JOptionPane;

import org.huihoo.god4j.GOD4JFactory;

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

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

	public void actionPerformed(ActionEvent e)
	{
		closeDocumentIfPossible();
	}
	
	public static  boolean closeDocumentIfPossible()
	{
		try
		{
			if (GOD4JFactory.getGOD4J().isDirty())
			{
				int answer =
					JOptionPane.showConfirmDialog(
						GOD4JFactory.getGOD4J(),
						sm_jgraph.getString("joptionpane.confirmSave"),
						sm_jgraph.getString("joptionpane.confirm"),
						JOptionPane.YES_NO_CANCEL_OPTION,
						JOptionPane.NO_OPTION);
				if (answer == JOptionPane.YES_OPTION)
				{
					return Save.saveDocumentIfPossible();
				}
				else if (answer == JOptionPane.CANCEL_OPTION)
				{
					return true;
				}
				else
				{
					GOD4JFactory.getGOD4J().loadDocument(null);
					return true;
				}
			}
			else
			{
				GOD4JFactory.getGOD4J().loadDocument(null);
				return true;
			}
		}
		catch (Throwable t)
		{
			return false;
		}
	}
}