// File: ActionSaveAs.java
// Classes: ActionSaveAs
// Author: Kedar Patankar

package EDU.neu.ccs.demeter.tools.apstudio.graphedit;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.File;
import java.util.Vector;
import java.util.Enumeration;
import java.util.Hashtable;
import java.awt.FileDialog;
import java.awt.Point;
import EDU.neu.ccs.demeter.Ident;

/**
 * Action to take the graph structure and write it to a file selected by user */

public class ActionSaveAs extends Action
{

	private String _newName,_currentName;

	public ActionSaveAs(Editor editor,Document d) {super(editor,d); }
	public String name() { return "Save Graph Structure"; }
	public void doIt()
	{
		_currentName = _document.getDocName();
		if(_currentName==null) return; // impossible situation

		String folderName,fileName;

		FileDialog fd = new FileDialog(_editor, "Select Output File", FileDialog.SAVE );
		fd.setDirectory(_editor.getWriteDir());
		_editor.setMessage("System will add extension .cd to filename if not provided by user.");
		fd.setVisible(true);
		_document.mode().setMessage(_editor);
		if ((fileName = fd.getFile()) == null) {return;}
		folderName = fd.getDirectory();
		_editor.setWriteDir(folderName);

		_newName = folderName + fileName;

// Windows bug. It appends this string to filename if extension is not provided
		int a = _newName.lastIndexOf(".*.*");       
		if (a>0)
			_newName = _newName.substring(0,a);

// Checking the extension. If user hasn't given any system will add ".cd" to it.
		int b = _newName.lastIndexOf(".");
		if(b>0)
		{
			if(b==_newName.length()-1)
			{
				_newName = _newName.substring(0,b);				
				_newName += ".cd";
			}
			else
			{
				saveThisFileAs(_newName); // filesave dialog has checked for duplication
				return ;
			}
		}
		else
			_newName +=".cd";
	  	  
// Checking a cd file of this name is already open in the application
		if(_editor.isPresent(_newName))
		{
			String title = "Error";
			String message = "APStudio cannot give a document the same name as an open document" + _newName;
			ActionShowMessage asm= new ActionShowMessage(_editor,title,message);
			asm.doIt();
			return;
		}
// Checking if filename is already used
		boolean problem = false;
		if(new File(_newName).exists())
			problem=true;

		if(problem)
		{
			String title = "File Overwrite ?";
			String message1="File already exists. Do you want to overwrite?";
			String message2=_newName;
			String buttonString="YN";
			AlarmDialog ad = new AlarmDialog(_editor,title,message1,message2,buttonString,"N");
			ad.setVisible(true);
			if (ad.getChoice().equals("YES"))
			{
				saveThisFileAs(_newName);
			}
		}
		else
			saveThisFileAs(_newName);
	}

	private void saveThisFileAs(String filename)
	{
		UGraph graph = _document.toExactGraph();
		if(graph==null)
			return;
		String cdFile = filename;
		String gcdFile = filename+".gcd";
		FileOutputStream cd,gcd;
		try
		{
			cd = new FileOutputStream(cdFile);
			gcd = new FileOutputStream(gcdFile);
		}catch(IOException eb){return;}

			String gcdString,cdString;
			try
			{
				gcdString = gcdToString(graph);
				cdString = cdToString(graph);
			}
			catch(java.lang.Throwable ex)
			{
				_editor.setMessage("Error saving file");
				return;
			}
		
		_editor.setMessage("Saving file "+cdFile);
		if(saveStringToDisk(cd,cdString) && saveStringToDisk(gcd,gcdString))
		{
			_editor.setMessage(cdFile+" saved");
			_document.firstTime(false);
			_document.cdNeedsSaving(false);
			_editor.updateDocname(_currentName,_newName);
		}
		else
			_editor.setMessage("Error saving file");
	}
  
	private String cdToString(UGraph graph)
	{
		String graphString=graph.getCdString();
		return graphString;
	}
	private String gcdToString(UGraph graph)
	{
		String graphString=graph.GetGraphString();
		return graphString;
	}

	private boolean saveStringToDisk(FileOutputStream out,String graphString)
	{
		if (graphString == null)
			graphString = "";
		byte[] buf = new byte[graphString.length()];
		buf = graphString.getBytes();
		try
		{
			out.write(buf);
			out.close();
		}catch (IOException eb){return false;}
		return true;
	}

	public void undoIt() { }

} /* end class ActionSaveAs */

