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

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

import uk.ac.ic.doc.kevlar.gui.internal.caches.ColorCache;
import uk.ac.ic.doc.kevlar.gui.internal.caches.FontCache;
import uk.ac.ic.doc.kevlar.gui.internal.caches.Utils;
import uk.ac.ic.doc.kevlar.gui.internal.dragdrop.ADraggableObject;
import uk.ac.ic.doc.kevlar.gui.internal.pipeline.custombuttons.AddEntryButton;
import uk.ac.ic.doc.kevlar.gui.internal.pipeline.custombuttons.DeleteButton;
import uk.ac.ic.doc.kevlar.gui.internal.pipeline.custombuttons.ExpandButton;
import uk.ac.ic.doc.kevlar.gui.internal.pipeline.custombuttons.RenameButton;
import uk.ac.ic.doc.kevlar.gui.internal.pipeline.custombuttons.ResizeButton;

/**
 * @author Marc
 **/
public class ProgramHeading extends ADraggableObject {

    public static final String PROGRAM_HEADING_SKIN_NAME = "programheading";
    public static final String PROGRAM_HEADING_SELECTED_SKIN_NAME = "programheading_selected";
    public static final String PROGRAM_HEADING_DISABLED_SKIN_NAME = "programheading_disabled";
    public static final String PROGRAM_HEADING_HIGHLIGHTED_SKIN_NAME = "programheading_highlighted";
    public static final String PROGRAM_HEADING_SELECTED_HIGHLIGHTED_SKIN_NAME = "programheading_selected_highlighted";
    public static final int PROGRAM_HEADING_HEIGHT = 20;
        
    private Program program;
	private ResizeButton resizeButton;
	private AddEntryButton addEntryButton;
//	private HelpButton helpButton;
	private RenameButton renameButton;
	private ExpandButton expandButton;
	private DeleteButton deleteButton;
	private boolean selected;
	private boolean disabled;
	private boolean highlighted;
    
    public ProgramHeading(Program program) {
        
        super();
        this.program = program;

        setSizeFillHorizontal(new Point(0, PROGRAM_HEADING_HEIGHT));
        
		resizeButton = new ResizeButton(program);
		resizeButton.setAlignment(true, false);
		resizeButton.setPosition(new Point(20, 1));
		resizeButton.hide();
		addChild(resizeButton);
		        
		deleteButton = new DeleteButton(program);
		deleteButton.setAlignment(true, false);
		deleteButton.setPosition(new Point(32, 1));
		deleteButton.hide();
		addChild(deleteButton);
		
		expandButton = new ExpandButton(program);
		expandButton.setAlignment(true, false);
		expandButton.setPosition(new Point(44, 1));
		expandButton.hide();
		addChild(expandButton);
		
		if (program instanceof Macro) {
		    
		    renameButton = new RenameButton((Macro)program);
		    renameButton.setAlignment(true, false);
		    renameButton.setPosition(new Point(56, 1));
		    renameButton.hide();
		    addChild(renameButton);
		    
		} else {
		
		    addEntryButton = new AddEntryButton(program);
		    addEntryButton.setAlignment(true, false);
		    addEntryButton.setPosition(new Point(56, 1));
		    addEntryButton.hide();
		    addChild(addEntryButton);
		    
/*		    helpButton = new HelpButton(program);
		    helpButton.setAlignment(true, false);
		    helpButton.setPosition(new Point(68, 1));
		    helpButton.hide();
		    addChild(helpButton); */
		    
		}
		
		
    }
    
	public void select() {
	    
	    if (!selected) {
	    
	        selected = true;
	        resizeButton.show();
	        deleteButton.show();
	        expandButton.show();
	        
	        if (program instanceof Macro) {

			    renameButton.show();
	            
	        } else {
	            
	            addEntryButton.show();
//	            helpButton.show();
	            
	        }	        
	        redraw();
	        
	    }
	    
	}
	
	public void deselect() {
	    
	    if (selected) {
	    
	        selected = false;
	        resizeButton.hide();
	        deleteButton.hide();
	        expandButton.hide();
	        
	        if (program instanceof Macro) {
	            
			    renameButton.hide();

	        } else {
	            
	            addEntryButton.hide();
//	            helpButton.hide();
	            
	        }
	        redraw();
	        
	    }
	    
	}
	
	public void highlight() {
	    
	    if (!highlighted) {
	    
	        highlighted = true;
	        redraw();
	        
	    }
	    
	}
	
	public void unhighlight() {
	    
	    if (highlighted) {
	    
	        highlighted = false;
	        redraw();
	        
	    }
	    
	}
	
	public void disable() {
	    
	    if (!disabled) {
	    
	        disabled = true;
	        
	        redraw();
	        
	    }
	    
	}
	
	public void enable() {
	    
	    if (disabled) {
	    
	        disabled = false;	        
	        redraw();
	        
	    }
	    
	}
	
	public boolean isEnabled() {
	    
	    return !disabled;
	    
	}
	    
	public void expand() {
	    
	    expandButton.deselect();
	    
	}
	
	public void contract() {
	    
	    expandButton.select();
	    
	}
	
    public void drawBefore(GC gc, Rectangle redrawArea) {
        
        if (selected) {
        
            if (highlighted) {
                
                Utils.drawHorizontalBar(gc, PROGRAM_HEADING_SELECTED_HIGHLIGHTED_SKIN_NAME, new Rectangle(-redrawArea.x, -redrawArea.y, area.width, PROGRAM_HEADING_HEIGHT));
                gc.setForeground(ColorCache.get(0, 0, 0));
                
            } else {
            
                Utils.drawHorizontalBar(gc, PROGRAM_HEADING_SELECTED_SKIN_NAME, new Rectangle(-redrawArea.x, -redrawArea.y, area.width, PROGRAM_HEADING_HEIGHT));
                gc.setForeground(ColorCache.get(255, 255, 255));
                
            }
        
        } else if (highlighted) {
        
            Utils.drawHorizontalBar(gc, PROGRAM_HEADING_HIGHLIGHTED_SKIN_NAME, new Rectangle(-redrawArea.x, -redrawArea.y, area.width, PROGRAM_HEADING_HEIGHT));
    		gc.setForeground(ColorCache.get(0, 0, 0));
            
        } else if (disabled) {
        
            Utils.drawHorizontalBar(gc, PROGRAM_HEADING_DISABLED_SKIN_NAME, new Rectangle(-redrawArea.x, -redrawArea.y, area.width, PROGRAM_HEADING_HEIGHT));
    		gc.setForeground(ColorCache.get(255, 255, 255));
            
        } else {
            
            Utils.drawHorizontalBar(gc, PROGRAM_HEADING_SKIN_NAME, new Rectangle(-redrawArea.x, -redrawArea.y, area.width, PROGRAM_HEADING_HEIGHT));
    		gc.setForeground(ColorCache.get(255, 255, 255));
            
        }
	    
	    gc.setFont(FontCache.get("Arial", 8, SWT.BOLD));
	    Utils.drawText(gc, program.getName(), new Rectangle(7-redrawArea.x, -redrawArea.y, area.width-65, area.height), Utils.ALIGN_LEFT);
//	    gc.drawText(program.getData().getFriendlyName(), 7-redrawArea.x, 2-redrawArea.y, true);        
        
    }
    
    public void mousePress(Point position, int button) {
        
        super.mousePress(position, button);
        
        PipeLineManager.getSingleton().selectProgram(program);
        
        if (button == MB_RIGHT) {
            
            program.helpButtonPressed();
            
        }
        
    }
    
    public void onDrag(Point offset) {
        
        program.onDrag(offset);
        
    }
    
}
