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

import org.eclipse.swt.graphics.Point;

import uk.ac.ic.doc.kevlar.gui.GUIManager;
import uk.ac.ic.doc.kevlar.gui.internal.caches.ColorCache;
import uk.ac.ic.doc.kevlar.gui.internal.layout.ILayoutPipe;
import uk.ac.ic.doc.kevlar.gui.internal.layout.ILayoutProgram;
import uk.ac.ic.doc.kevlar.gui.internal.layout.LayoutManager;
import uk.ac.ic.doc.kevlar.gui.internal.widgets.LineSequence;
import uk.ac.ic.doc.kevlar.hial.IRefObject;

/**
 * @author Marc
 **/
public class Pipe extends LineSequence implements ILayoutPipe, IRefObject {

    private OutputNodeEntry source;
    private InputNodeEntry destination;
    private boolean invalid;
    
    public Pipe(OutputNodeEntry source, InputNodeEntry destination) {
        
        this.source = source;
        this.destination = destination;
        
        LayoutManager.getLayout().addPipe(this);
        
        source.setPipe(this);
        destination.setPipe(this);
        
        update();
        invalid = false;
        
    }
    
    public void setInvalid(boolean invalid) {
        
        if (this.invalid != invalid) {
        
            this.invalid = invalid;
            
            if (invalid) {
                
                setColor(ColorCache.get(255, 0, 0));
                
            } else {
                
                setColor(ColorCache.get(0, 0, 0));
                
            }
            
            PipeLineManager.getSingleton().updateExecutable();
            
        }
        
    }
    
    public boolean isInvalid() {
        
        return invalid;
        
    }
    
    public void update() {
                
        System.out.println("Call to Pipe.update()");
        
        Point sourcePosition = source.getNodePosition();
        Point destinationPosition = destination.getNodePosition();
    
        LayoutManager.getLayout().alterPipe(this, sourcePosition, destinationPosition);
        
    }
    
    public void requestRemove() {
        
        if (invalid) {
            
            unset();
            remove();
            
        } else {
            
            GUIManager.getHialContext().removePipe(this);
            
        }
        
    }
    
    public void unset() {
        
        source.unsetPipe();
        destination.unsetPipe();
        
    }
    
    public void setSource(OutputNodeEntry source) {
        
        this.source.unsetPipe();
        this.source = source;
        this.source.setPipe(this);
        update();
        
    }
    
    public void setDestination(InputNodeEntry destination) {
        
        this.destination.unsetPipe();
        this.destination = destination;
        this.destination.setPipe(this);
        update();
        
    }
    
    public void remove() {
        
        super.remove();
        
        LayoutManager.getLayout().removePipe(this);
        PipeLineManager.getSingleton().updateExecutable();
        
    }

    public ILayoutProgram getSource() {
        
        return source.getContainer();
        
    }

    public ILayoutProgram getDestination() {
        
        return destination.getContainer();
        
    }
    
    public void expand() {
        
        if (source.isExpanded() && destination.isExpanded()) {
            
            show();
            
        }
        
    }
    
    public void contract() {
        
        hide();
        
    }

}
