/*******************************************************************************
* 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 03-Nov-2004
 */
package uk.ac.ic.doc.kevlar.gui.internal.pipeline;

import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;

import uk.ac.ic.doc.kevlar.gui.internal.animation.AAnimation;
import uk.ac.ic.doc.kevlar.gui.internal.help.HelpManager;
import uk.ac.ic.doc.kevlar.publicInterfaces.INode;

/**
 * @author Marc
 **/
public abstract class AIONodeEntry extends AProgramEntry {

	protected INode node;
    protected Node nodeWidget;
    protected Pipe pipe;
	    
    public AIONodeEntry(INode node, int positionIndex, Program program) {
        
		super(positionIndex, node, program);
		this.node = node;
		this.pipe = null;
        
    }

    public void setIndex(int positionIndex) {
        
        if (this.positionIndex != positionIndex) {
            
            super.setIndex(positionIndex);
            
            if (pipe != null) {
                
                pipe.update();
                
            }
            
        }
        
    }
    
    public Point getNodePosition() {
        
/*        Rectangle nodeArea = nodeWidget.getAreaRelativeTo(PipeLineManager.getSingleton().getContents());
        return new Point(nodeArea.x+nodeArea.width/2, nodeArea.y+nodeArea.height/2); */
        
        int y = positionIndex*ENTRY_INTERNAL_HEIGHT+ENTRY_INTERNAL_HEIGHT;
        Point nodePos = nodeWidget.getPosition();
        Point nodeSize = nodeWidget.getSize();
        
        if (parent != null) {
        
            Rectangle parentArea = parent.getAreaRelativeTo(PipeLineManager.getSingleton().getContents());
            return new Point(parentArea.x+area.x+nodePos.x+nodeSize.x/2, parentArea.y+y+nodePos.y+nodeSize.y/2);
            
        } else {
            
            return new Point(0, 0);
            
        }
        
    }
    
    public void select() {
        
        nodeWidget.select();
        
    }
    
    public void deselect() {
        
        nodeWidget.deselect();
        
    }
    
    public boolean isSelected() {
        
        return nodeWidget.isSelected();
        
    }
    
    public void toggleSelect() {
        
        if (!isSelected()) {
            
            PipeLineManager.getSingleton().selectNode(this);
            
        } else {
            
            PipeLineManager.getSingleton().deselectNode();
            deselect();
            
        }
        
    }
    
    public void mousePress(Point position, int mouseButton)
	{
		super.mousePress(position, mouseButton);
	    
/*		boolean isDisplayed = HelpManager.getSingleton().isDisplayed();
		
		if (mouseButton==1 && !isDisplayed)
		{ 
	
			Rectangle newArea = parent.getArea();
			HelpManager.getSingleton().displayIONodeHelp(node.getName(), node.getNodeHelp(), newArea.x+area.width+10, newArea.y, newArea.x-10);
		} */
		
		if (mouseButton == MB_LEFT) {
		    
		    toggleSelect();
		    
		} else if (mouseButton == MB_RIGHT) {
		    
			Rectangle newArea = getAbsoluteArea();
			HelpManager.getSingleton().displayIONodeHelp(program.getName()+"."+node.getName(), node.getNodeHelp(), newArea.x+newArea.width, newArea.y, 0);
		    
		}
		
	}

    public boolean capturesMouse() {
        
        return true;
        
    }
    
    public Pipe getPipe() {
        
        return pipe;
        
    }
    
    public boolean hasPipe() {
        
        return pipe != null;
        
    }
    
    public void setPipe(Pipe pipe) {
        
        this.pipe = pipe;
//        pipe.update();
        
    }

    public void unsetPipe() {
        
        this.pipe = null;
        
    }
       
    public void update() {
        
        if (pipe != null) {
            
            pipe.update();
            
        }
        
    }
    
    public void expand() {
        
        super.expand();
        
        if (pipe != null) {
            
            pipe.expand();
            
        }
        
    }
    
    public AAnimation contract() {
        
        if (pipe != null) {
            
            pipe.contract();
            
        }
        
        return super.contract();
        
    }
    
    public INode getINode()
    {
    	return node;
    }
    
    public void dispose() {
        
        if (PipeLineManager.getSingleton().getSelectedNode() == this) {
            
            PipeLineManager.getSingleton().deselectNode();
            
        }
        
        super.dispose();
        
    }
    
}
