/*******************************************************************************
* 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 25-Dec-2004
 *
 */

package uk.ac.ic.doc.kevlar.gui.internal.taskpane.savepane;


import java.io.FileNotFoundException;

import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;

import uk.ac.ic.doc.kevlar.gui.internal.ADrawableObject;
import uk.ac.ic.doc.kevlar.gui.internal.AMouseListenerObject;
import uk.ac.ic.doc.kevlar.gui.internal.caches.ImageCache;
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.Heading;
import uk.ac.ic.doc.kevlar.gui.internal.widgets.ISelectListener;
import uk.ac.ic.doc.kevlar.gui.internal.widgets.textbox.TextBox;

/**
 * @author Kate
 *
 */

public class SaveBox extends AMouseListenerObject implements ISelectListener {

    private static final String SAVE_BACKGROUND_IMAGE = "savebk.png";
    
	protected Heading topHeading;
	protected Heading bottomHeading;
	protected SaveButton saveButton;
	protected TextBox textBox;
	protected SavePane parent;
	protected Image background;
	private final String topMessage ="Save current pipeline as:" ;
	private boolean errorDisplayed;
	
	public SaveBox(Point position, Point size, SavePane parent)
	{
		super();
		this.parent = parent;
		errorDisplayed = false;
		setPosition(position);
		setSize(size);
		
		try {
		
		    background = ImageCache.get(SAVE_BACKGROUND_IMAGE);
		    
		} catch (FileNotFoundException e) {
		    
		    System.err.println("File not found: "+SAVE_BACKGROUND_IMAGE);
		    background = new Image(Display.getDefault(), 1, 1);
		    
		}
		
		topHeading = new Heading(topMessage, new Point(160, 30), position);
		bottomHeading = new Heading("Save pipeline in the directory:", new Point(160, 50), new Point(position.x, position.y+90));
		bottomHeading.setDivider("divider.png");
		textBox = new TextBox();
		textBox.setPosition(new Point(5, 40));
		textBox.setSize(new Point(150, 20));
		textBox.setSelectListener(this);
		saveButton = new SaveButton(new Point(80, 65), this);
		
		addChild(topHeading);
		addChild(bottomHeading);
		addChild(textBox);
		addChild(saveButton);
	}
	
	public TextBox getTextBox() {
	    
	    return textBox;
	    
	}
	
	public void drawBefore(GC gc, Rectangle redrawArea) {
	    
	    gc.drawImage(background, -redrawArea.x, -redrawArea.y);
	    
	}
	
	public void save()
	{
	    parent.save();	    
	}
	
    public void onSelect(ADrawableObject object) {

	     if (!(KeyboardManager.getSingleton().getState() instanceof SavePaneState)) {
		     
	         KeyboardManager.getSingleton().setState(new SavePaneState());
		         
	     }        
      
    }
    
    public void displayError(String errorMessage)
    {
    	topHeading.setMessage(errorMessage);
    	topHeading.setColour(new Color(Display.getDefault(), 255, 0, 0));
    	errorDisplayed = true;
    	
    	redraw();
    }
    
    public void correctError()
    {
    	topHeading.setMessage(topMessage);
    	topHeading.setColour(new Color(Display.getDefault(), 0, 0, 0));
    	errorDisplayed = false;
    	
    	redraw();
    }
    
    public boolean isErrorDisplayed()
    {
    	return errorDisplayed;
    }
   	
}
