/*******************************************************************************
* 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 20-Oct-2004
 *
 */
package uk.ac.ic.doc.kevlar.gui.internal.taskpane.programpane;

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 uk.ac.ic.doc.kevlar.core.framework.IProgramIDDescription;
import uk.ac.ic.doc.kevlar.gui.internal.animation.AAnimation;
import uk.ac.ic.doc.kevlar.gui.internal.animation.AnimationManager;
import uk.ac.ic.doc.kevlar.gui.internal.animation.SlideAnimation;
import uk.ac.ic.doc.kevlar.gui.internal.animation.SlideOutAnimation;
import uk.ac.ic.doc.kevlar.gui.internal.dragdrop.ADraggableObject;
import uk.ac.ic.doc.kevlar.gui.internal.taskpane.ATaskPane;
import uk.ac.ic.doc.kevlar.gui.internal.taskpane.TaskPaneManager;
import uk.ac.ic.doc.kevlar.hial.IProgramTreeNode;

/**
 * @author mfh02
 *
 */
public abstract class AProgramTreeNodeEntry extends ADraggableObject implements Cloneable {

	public static final int ENTRY_WIDTH = 160;
	public static final int ENTRY_HEIGHT = 28;
	
    public static final int NO_ANIMATION = 0;
    public static final int SLIDE_LEFT = 1;
    public static final int SLIDE_RIGHT = -1;
    	
	protected IProgramTreeNode dataNode;
	protected Image background;
	protected Image rollover;
	protected ATaskPane programPane;
	protected int index;
	protected boolean selected;
	
	IProgramIDDescription progDesc;
	
	public AProgramTreeNodeEntry(IProgramTreeNode dataNode, int entryListIndex, ATaskPane programPane) {

	    super();
	    setPosition(new Point(0, entryListIndex*ENTRY_HEIGHT+3));
	    initialise(dataNode, entryListIndex, programPane, NO_ANIMATION);
		
	}
	
	public AProgramTreeNodeEntry(IProgramTreeNode dataNode, int entryListIndex, ATaskPane programPane, int animationMode) {

	    super();
	    setPosition(new Point(TaskPaneManager.TASK_PANE_INTERNAL_WIDTH*animationMode, entryListIndex*ENTRY_HEIGHT+3));
	    initialise(dataNode, entryListIndex, programPane, animationMode);
	    
	}
	
	public AProgramTreeNodeEntry(IProgramIDDescription progDesc, int entryListIndex, ATaskPane taskPane, int animationMode)
	{
		super();
		setPosition(new Point(TaskPaneManager.TASK_PANE_INTERNAL_WIDTH*animationMode, entryListIndex*ENTRY_HEIGHT+3));
	    initialise(dataNode, entryListIndex, programPane, animationMode);
	    this.progDesc = progDesc;
	}
	
	public void initialise(IProgramTreeNode dataNode, int entryListIndex, ATaskPane programPane, int animationMode) {
	    
	    setSize(new Point(ENTRY_WIDTH, ENTRY_HEIGHT));
		this.dataNode = dataNode;
		this.index = entryListIndex;
		this.programPane = programPane;
		background = null;
		rollover = null;

		if (animationMode != NO_ANIMATION) {
		
		    AnimationManager.getSingleton().addAnimation(new SlideAnimation(this, new Point(0, getPosition().y)));
		    
		}
			    
	}
	
	public IProgramTreeNode getDataNode() {
	    
	    return dataNode;
	    
	}
	
	public int getIndex() {
	    
	    return index;
	    
	}
	
	public AAnimation setIndex(int entryListIndex) {
	    
	    AAnimation anim = new SlideAnimation(this, new Point(0, entryListIndex*ENTRY_HEIGHT+3));
	    AnimationManager.getSingleton().addAnimation(anim);
	    index = entryListIndex;
	    return anim;
	    
	}
	
	public void drawBefore(GC gc, Rectangle redrawArea) {
		
	    if (isMouseOver() || selected) {
	        
	        if (rollover != null) {
	            
	            gc.drawImage(rollover, -redrawArea.x, -redrawArea.y);
	            
	        }
	        
	    } else {
	    
	        if (background != null) {
		
	            gc.drawImage(background, -redrawArea.x, -redrawArea.y);
			
	        }
	        
	    }
		
/*		if (icon != null) {
		    
		    gc.drawImage(icon, 5-redrawArea.x, 1-redrawArea.y);
		    
		} */
	    if (dataNode==null)
	    {
	    	gc.drawText(progDesc.getFriendlyName(), 50-redrawArea.x, 5-redrawArea.y, true );
	    }
		else
		{
			gc.drawText(dataNode.getName(), 50-redrawArea.x, 5-redrawArea.y, true);
		}
		
	}
	
	public void mouseEnter() {

	    super.mouseEnter();
	    
	    redraw();
	    applyRedraw();
	    
	}
	
	public void mouseExit() {
	    
	    super.mouseExit();
	    
	    redraw();
	    applyRedraw();
	    
	}
	
	public void select() {
	    
	    selected = true;
	    redraw();
	    
	}
	
	public void deselect() {
	    
	    selected = false;
	    redraw();
	    
	}
	
	public boolean isSelected() {
	    
	    return selected;
	    
	}
	
	public void dispose(int animationMode) {
	    
	    if (animationMode == SLIDE_LEFT) {
	    
	        AnimationManager.getSingleton().addAnimation(new SlideOutAnimation(this, new Point(-TaskPaneManager.TASK_PANE_INTERNAL_WIDTH, getPosition().y)));
	        
	    } else if (animationMode == SLIDE_RIGHT) {
	        
	        AnimationManager.getSingleton().addAnimation(new SlideOutAnimation(this, new Point(TaskPaneManager.TASK_PANE_INTERNAL_WIDTH, getPosition().y)));
	        
	    }
	    
	}
	
	public abstract void perform();
	
}
