/*******************************************************************************
* Copyright (c)                                                                *
*           2005   Tristan Allwood,                                            *
*           2005   Daniel Burke,                                               *
*           2005   Marc Hull,                                                  *
*           2005   Ekaterina Itskova,                                          *
*           2005   Steve Zymler                                                *
*                                                                              *
* Permission is hereby granted, free of charge, to any person obtaining a      *
* copy of this software and associated documentation files (the                *
* "Software"), to deal in the Software without restriction, including          *
* without limitation the rights to use, copy, modify, merge, publish,          *
* distribute, sublicense, and/or sell copies of the Software, and to           *
* permit persons to whom the Software is furnished to do so, subject to        *
* the following conditions:                                                    *
*                                                                              *
* The above copyright notice and this permission notice shall be included      *
* in all copies or substantial portions of the Software.                       *
*                                                                              *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS      *
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF                   *
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.       *
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY         *
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,         *
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE            *
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                       *
*******************************************************************************/
/*
 * Created on 24-Nov-2004
 *
 */
package uk.ac.ic.doc.kevlar.gui.internal.taskpane.savepane;

import org.eclipse.swt.graphics.Point;

import uk.ac.ic.doc.kevlar.gui.GUIManager;
import uk.ac.ic.doc.kevlar.gui.internal.ADrawableObject;
import uk.ac.ic.doc.kevlar.gui.internal.keyboard.KeyboardManager;
import uk.ac.ic.doc.kevlar.gui.internal.keyboard.states.SavePaneState;
import uk.ac.ic.doc.kevlar.gui.internal.taskpane.TaskPaneManager;
import uk.ac.ic.doc.kevlar.gui.internal.taskpane.directorypane.ADirectoryTreeNodeEntry;
import uk.ac.ic.doc.kevlar.gui.internal.taskpane.directorypane.ALoadSavePane;
import uk.ac.ic.doc.kevlar.gui.internal.taskpane.loadpane.LoadPane;
import uk.ac.ic.doc.kevlar.gui.internal.widgets.textbox.TextBox;
import uk.ac.ic.doc.kevlar.hial.IHialContext;

/**
 * @author Kate
 *
 */
public class SavePane extends ALoadSavePane{

	SaveBox saveBox;
	public static Point BOX_SIZE = new Point(160, 140);

	public SavePane(Point position) {
		
		super(position, (int)Math.floor((double)BOX_SIZE.y/ADirectoryTreeNodeEntry.ENTRY_HEIGHT));
		saveBox = new SaveBox(new Point(0,0), BOX_SIZE, this);
		addChild(saveBox);
	}

	public String getName() {
		return "Save Pipelines";
	}
	
	public void save() {
	    
		String directory = getCurrentDirectoryPath();
		String filename = saveBox.getTextBox().getText();
		
		if (filename.equals(""))
		{
			saveBox.displayError("Filename can't be empty. Please enter valid name:");
		}
	//	else if (filename.contains('kx'))
	//	{
			//error message
	//	}
		else
		{
			String fullname = directory+filename+".kvl";
			save(fullname);
			updatePanes();
			//close pane
			
			TaskPaneManager manager = TaskPaneManager.getSingleton();
			manager.contract();
			manager.setExpandButton(true);
			
			saveBox.getTextBox().setText("");
			if (saveBox.isErrorDisplayed())
			{
				saveBox.correctError();
			}
		}
		
		
	}
	
	public void save(String filename)
	{
		IHialContext hialContext = GUIManager.getHialContext();
		GUISaverCallback guiCallback = GUISaverCallback.getSingleton();
		try
		{
			GUIManager.getHial().saveContextToFile(filename, hialContext, guiCallback);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	
	public TextBox getTextBox() {
	    
	    return saveBox.getTextBox();
	    
	}
	
	private void updatePanes()
	{
		//refresh saving pane
	    
		getCurrentFolder().perform();
		
		//super.refresh(getDirectoryNode());

		//refresh loading pane

		LoadPane load = (LoadPane)TaskPaneManager.getSingleton().getPane(TaskPaneManager.LOAD);
		load.getCurrentFolder().perform();
		
	}
	
	public void reset()
	{
		if (saveBox.isErrorDisplayed())
		{
			saveBox.correctError();
		}
	}

}
