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

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.api.programs.arguments.IArgumentHelp;
import uk.ac.ic.doc.kevlar.api.programs.arguments.IBasicArgumentInfo;
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.help.HelpManager;
import uk.ac.ic.doc.kevlar.gui.internal.pipeline.AProgramEntry;
import uk.ac.ic.doc.kevlar.gui.internal.pipeline.PipeLineManager;
import uk.ac.ic.doc.kevlar.gui.internal.pipeline.Program;
import uk.ac.ic.doc.kevlar.hial.IArgumentValuePair;


/**
 * @author ei02
 *
 */
public class Argument extends AProgramEntry {

    protected static final String ARGUMENT_SKIN_NAME = "argumentheading";
    
    protected boolean invalid;
    
	public Argument(IArgumentValuePair argument, int positionIndex, Program program) {
	    
		super(positionIndex, argument, program);
		invalid = false;
		
	}
	
	public void setInvalid(boolean invalid) {
	    
	    if (this.invalid != invalid) {
	    
	        this.invalid = invalid;
	        redraw();
	        
	        PipeLineManager.getSingleton().updateExecutable();
	        
	    }
	    
	}
	
	public boolean isInvalid() {
	    
	    return invalid;
	    
	}
	
	public void setDataNode(IArgumentValuePair data) {
	    
	    super.setDataNode(data);
	    
	}
	
	public IArgumentValuePair getArgumentPair() {
	    
	    return (IArgumentValuePair)data;
	    
	}
	
	public void drawBefore(GC gc, Rectangle redrawArea) {

	    Utils.drawHorizontalBar(gc, ARGUMENT_SKIN_NAME, new Rectangle(-redrawArea.x, -redrawArea.y, area.width, area.height));
	    
	    if (invalid) {
	    
	        gc.setForeground(ColorCache.get(255, 0, 0));
	        
	    } else {
	        
	        gc.setForeground(ColorCache.get(0, 0, 0));
	        
	    }
	    
	    gc.setFont(FontCache.get("Arial", 7, SWT.NORMAL));
	    
	    Utils.drawText(gc, prefix+getArgumentPair().getArgInfo().getName(), new Rectangle(10-redrawArea.x, -redrawArea.y, (area.width-15)/2, area.height), Utils.ALIGN_LEFT);
	    
	}
	
	public void showEditor() {
	    
	    /* Default implementation for showEditor */
	    
	}
	
	public void mousePress(Point position, int mouseButton)
	{
		super.mousePress(position, mouseButton);
	    
/*		boolean isDisplayed = HelpManager.getSingleton().isDisplayed();
		
		if (mouseButton==1 && !isDisplayed) {
		    
			IBasicArgumentInfo argInfo = getArgumentInfo();
			IArgumentHelp argDesc = argInfo.getArgumentHelp();
			Rectangle newArea = parent.getArea();
			HelpManager.getSingleton().displayArgumentHelp(argInfo.getName(), argDesc,newArea.x+area.width+10, newArea.y, newArea.x-10);
			
		} */
		
		if (mouseButton == MB_RIGHT) {
		    
			IBasicArgumentInfo argInfo = ((IArgumentValuePair)data).getArgInfo();
			IArgumentHelp argDesc = argInfo.getArgumentHelp();
			Rectangle newArea = getAbsoluteArea();
			HelpManager.getSingleton().displayArgumentHelp(program.getName()+"."+argInfo.getName(), argDesc, newArea.x+newArea.width, newArea.y, 0);
		    
		}
		
	}
	
	public boolean capturesMouse() {
	    
	    return true;
	    
	}

}