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

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

import java.io.File;
import java.util.ArrayList;

import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Display;

import uk.ac.ic.doc.kevlar.Kevlar;
import uk.ac.ic.doc.kevlar.gui.GUIManager;
import uk.ac.ic.doc.kevlar.gui.internal.caches.Converter;
import uk.ac.ic.doc.kevlar.gui.internal.taskpane.Thumbnail;
import uk.ac.ic.doc.kevlar.gui.internal.taskpane.loadpane.GUILoaderCallback;
import uk.ac.ic.doc.kevlar.gui.internal.taskpane.savepane.GUISaverCallback;

/**
 * @author Kate
 *
 */

public abstract class ALoadSavePane extends ADirectoryBrowsePane {

	private IDirectoryTreeNode currentDirectory;
	private ADirectoryTreeNodeEntry folder;
	
	public ALoadSavePane(Point position, int directoryTreeOffset)
	{
		super(position, directoryTreeOffset);
		GUISaverCallback.createSingleton();
		GUILoaderCallback.createSingleton();
		
	//	File rootDir = super.root.getFile();
		
		currentDirectory =super.root;
		folder = new OpenDirectoryEntry(root, this);
		
	}
	
	public void populateWithFiles(int positionIndex, ArrayList childNodes, int animationMode)
	{
		/* concrete implementation for load and save panels */
		
//		int thumbPos = (int)Math.floor(((double)(positionIndex*(ADirectoryTreeNodeEntry.ENTRY_HEIGHT)))/((double)Thumbnail.GAP));
		
//		int nextPosition = positionIndex*(ADirectoryTreeNodeEntry.ENTRY_HEIGHT+3)+10;
//		int nextThumbPos = thumbPos*Thumbnail.GAP;
		
//		int offset = nextThumbPos - nextPosition;
		int offset = positionIndex*ADirectoryTreeNodeEntry.ENTRY_HEIGHT+3;
//	    int offset = 0;
		int thumbPos = 0;
	
		for (int i = 0; i<childNodes.size(); i++) {
		    
		    IDirectoryTreeNode child = (IDirectoryTreeNode) childNodes.get(i);
		    
		    File file = child.getFile();
		    
		    if (child instanceof IFileNode) {
		        
		        ADirectoryTreeNodeEntry entry = getEntry(child);
		        
		        if (entry != null) {
		            
		            entry.setIndex(positionIndex);
		            
		        } else {
		            
		            //entry = new FileEntry((IFileNode)child, positionIndex, this, animationMode);
		            
		            if (file.getName().endsWith(".kvl"))
		            {	
		                try
		                {
		                    String hexData = GUIManager.getHial().loadThumbnailStringFromFile(file.getAbsolutePath());
		                    
		                    ImageData icon = Converter.convertToImage(hexData);
		                    Image image = new Image(Display.getCurrent(), icon);
		                    Thumbnail thumb = new Thumbnail(image, file.getName(), thumbPos++, offset, this);
		                    
		                    entries.add(thumb);
		                    addChild(thumb);
		                    
		                }
		                catch(Exception e){e.printStackTrace();}
		                
		            } 
		            
		            
		        }
		        
		        positionIndex++;
		        
		    } 
		    
		}	
		
	} 
	
	public void setCurrentDirectory(IDirectoryTreeNode path, ADirectoryTreeNodeEntry folder)
	{
		
		currentDirectory = path;
		this.folder = folder;
	}
	
	public String getCurrentDirectoryPath()
	{
		
		String dirSeparator = currentDirectory.getFile().separator;
	//	System.out.println("dir separator: "+dirSeparator);
		String dirPath = currentDirectory.getFile().getAbsolutePath()+dirSeparator;
		
		return dirPath;
	}
	
	public IDirectoryNode getDirectoryNode()
	{
		return (IDirectoryNode)currentDirectory;
	}
	
	public ADirectoryTreeNodeEntry getCurrentFolder()
	{
		return folder;
	}

}
