/*******************************************************************************
* 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 26-Oct-2004
 *
 */

package uk.ac.ic.doc.kevlar.gui.internal.pipeline;

import org.eclipse.swt.graphics.Point;

import uk.ac.ic.doc.kevlar.api.programs.IProgramInfoNode;
import uk.ac.ic.doc.kevlar.gui.internal.AMouseListenerObject;
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;

/**
 * @author Kate
 *
 */

public abstract class AProgramEntry extends AMouseListenerObject {

	public final static int ENTRY_INTERNAL_HEIGHT = 17;
	public final static int ENTRY_EXTERNAL_HEIGHT = 20;
	
    public static final int NO_ANIMATION = 0;
    public static final int SLIDE_DOWN = 1;
	
	protected Program program;
	protected Program container;
	protected Program oldParent;
	protected IProgramInfoNode data;
	protected int positionIndex;
	protected String prefix;
	protected boolean expanded;
	
	public AProgramEntry(int positionIndex, IProgramInfoNode data, Program program) {
	    
		super();
		this.data = data;
		this.program = program;
		this.oldParent = null;
		this.positionIndex = positionIndex;
		this.prefix = "";
		this.expanded = true;
		setPosition(new Point(0, 5));
		setSizeFillHorizontal(new Point(0, ENTRY_EXTERNAL_HEIGHT));
		
		AnimationManager.getSingleton().addAnimation(new SlideAnimation(this, new Point(0, positionIndex*ENTRY_INTERNAL_HEIGHT+ENTRY_INTERNAL_HEIGHT)));
			    
	}
	
	public void setPrefix(String prefix) {
	    
	    this.prefix = prefix;
	    
	}
	
	public String getPrefix() {
	    
	    return prefix;
	    
	}
	
	public void setIndex(int positionIndex) {
	    
	    if (this.positionIndex != positionIndex) {
	    
	        this.positionIndex = positionIndex;

	        if (expanded) {

	            AnimationManager.getSingleton().addAnimation(new SlideAnimation(this, new Point(0, positionIndex*ENTRY_INTERNAL_HEIGHT+ENTRY_INTERNAL_HEIGHT)));
	        
	        }
	        
	    }
	    
	}
	
	public String getName() {
	    
	    return prefix+data.getName();
	    
	}
	
	public IProgramInfoNode getDataNode() {
	    
	    return data;
	    
	}
	
	protected void setDataNode(IProgramInfoNode data) {
	    
	    this.data = data;
	    redraw();
	    
	}
	
	public Program getProgram() {
	    
	    return program;
	    
	}
	
	public void setContainer(Program container) {
	    
	    this.container = container;
	    
	}
	
	public Program getContainer() {
	    
	    if (container == null) {
	        
	        return program;
	        
	    }
	    
	    return container;
	    
	}
	
	public void dispose() {
	    
	    AnimationManager.getSingleton().addAnimation(new SlideOutAnimation(this, new Point(0, 0)));
	    
	}
	
	public AAnimation contract() {
	    
	    expanded = false;
	    oldParent = (Program)getParent();
	    AAnimation animation = new SlideOutAnimation(this, new Point(0, 0));
	    AnimationManager.getSingleton().addAnimation(animation);
	    return animation;
	    
	}
	
	public void expand() {

	    if (oldParent != null) {
	    
	        expanded = true;
	        oldParent.addChild(this);
	        AnimationManager.getSingleton().addAnimation(new SlideAnimation(this, new Point(0, positionIndex*ENTRY_INTERNAL_HEIGHT+ENTRY_INTERNAL_HEIGHT)));
	        oldParent = null;
	        
	    }
	    
	}
	
	public boolean isExpanded() {
	    
	    return expanded;
	    
	}
	
}
