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

import java.io.FileNotFoundException;

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.ArgumentModelProvider;
import uk.ac.ic.doc.kevlar.api.programs.arguments.IBoolArgumentInfo;
import uk.ac.ic.doc.kevlar.api.programs.arguments.IBoolArgumentValue;
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.ImageCache;
import uk.ac.ic.doc.kevlar.gui.internal.caches.Utils;
import uk.ac.ic.doc.kevlar.gui.internal.pipeline.Program;
import uk.ac.ic.doc.kevlar.gui.internal.popup.PopUpManager;
import uk.ac.ic.doc.kevlar.hial.IArgumentValuePair;

/**
 * @author Marc
 **/
public class BoolArgument extends Argument {

    private static final String TICK_IMAGE = "tick.png";
    private static final String CROSS_IMAGE = "cross.png";
    
    public BoolArgument(IArgumentValuePair argument, int positionIndex, Program program) {
        
        super(argument, positionIndex, program);
        
    }

    public IBoolArgumentValue getValueData() {
        
        return (IBoolArgumentValue)((IArgumentValuePair)data).getArgValue();
        
    }
    
	public IBoolArgumentInfo getArgumentInfo() {
	    
	    return (IBoolArgumentInfo)((IArgumentValuePair)data).getArgInfo();
	    
	}
	    
	public void drawBefore(GC gc, Rectangle redrawArea) {

	    Utils.drawHorizontalBar(gc, ARGUMENT_SKIN_NAME, new Rectangle(-redrawArea.x, -redrawArea.y, area.width, area.height));
	    
		gc.setForeground(ColorCache.get(0, 0, 0));
	    gc.setFont(FontCache.get("Arial", 7, SWT.NORMAL));
	    
	    Utils.drawText(gc, getArgumentInfo().getName(), new Rectangle(10-redrawArea.x, -redrawArea.y, area.width-10, area.height), Utils.ALIGN_LEFT);
	    
	    if (getValueData().getValue()) {
	        
	        try {
	        
	            gc.drawImage(ImageCache.get(TICK_IMAGE), area.width-15-redrawArea.x, 6-redrawArea.y);
	            
	        } catch (FileNotFoundException e) {
	            
	            System.err.println("Could not find tick image: "+TICK_IMAGE);
	            
	        }
	        
	    } else {
	        
	        try {
	            
	            gc.drawImage(ImageCache.get(CROSS_IMAGE), area.width-15-redrawArea.x, 6-redrawArea.y);
	            
	        } catch (FileNotFoundException e) {
	            
	            System.err.println("Could not find cross image: "+CROSS_IMAGE);
	            
	        }
	        
	    }
	    
	}
	
	public void showEditor() {
	    
/*        BoolArgumentEditor editor = new BoolArgumentEditor(this);
        editor.setPosition(localToAbsolute(new Point(area.width-25, 0)));
        PopUpManager.getSingleton().showPopUp(editor); */
	    
	    setValue(!getValueData().getValue());
        	    
	}
	
	public void mouseRelease(Point position, int button) {
	    
//	    if (button == MB_LEFT && position.x > (area.width-15)/2) {

	    if (button == MB_LEFT) {
	    
	        showEditor();
	        
	    }	    
	    
	}
	
	public void setValue(boolean b) {
	    
	    IArgumentValuePair pair = getArgumentPair();
	    pair.setArgValue(ArgumentModelProvider.makeBooleanValue(b));
	    program.applyArgumentChanges();
	    
	}
	    
}
