/*******************************************************************************
* 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 14-Nov-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.ArgumentModelProvider;
import uk.ac.ic.doc.kevlar.api.programs.arguments.ISetArgumentInfo;
import uk.ac.ic.doc.kevlar.api.programs.arguments.ISetArgumentValue;
import uk.ac.ic.doc.kevlar.gui.internal.ADrawableObject;
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.keyboard.KeyboardManager;
import uk.ac.ic.doc.kevlar.gui.internal.keyboard.states.DropDownBoxState;
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.gui.internal.widgets.IDeselectListener;
import uk.ac.ic.doc.kevlar.gui.internal.widgets.dropdownbox.DropDownBox;
import uk.ac.ic.doc.kevlar.hial.IArgumentValuePair;
import uk.ac.ic.doc.kevlar.publicUtil.collections.IImmutableSet;

/**
 * @author Marc
 **/
public class SetArgument extends Argument implements IDeselectListener {

    protected DropDownBox editor;
    
    public SetArgument(IArgumentValuePair argument, int positionIndex, Program program) {
        
        super(argument, positionIndex, program);
        
        editor = new DropDownBox();
        editor.setSize(new Point(200, 20));
        editor.setDeselectListener(this);
        
    }

    public ISetArgumentValue getValueData() {
        
        return (ISetArgumentValue)((IArgumentValuePair)data).getArgValue();
        
    }
    
	public ISetArgumentInfo getArgumentInfo() {
	    
	    return (ISetArgumentInfo)((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-15)/2, area.height), Utils.ALIGN_LEFT);
	    Utils.drawText(gc, getValueData().getValue(), new Rectangle(10+(area.width-15)/2-redrawArea.x, -redrawArea.y, (area.width-15)/2, area.height), Utils.ALIGN_RIGHT);
	    
	}
	
	public void showEditor() {
	    
/*        SetArgumentEditor editor = new SetArgumentEditor(this);
        editor.setPosition(localToAbsolute(new Point((area.width-15)/2, 0)));
        PopUpManager.getSingleton().showPopUp(editor); */
	    
        editor.setPosition(localToAbsolute(new Point((area.width-15)/2, 0)));
        editor.clearOptions();
        
        IImmutableSet<String> keySet = getArgumentInfo().getSetValues().keySet();
        int i = 0;
        
        for (String s : keySet) {
            
            editor.addOption(s);
            
            if (s.equals(getValueData().getValue())) {
                
                editor.setCurrent(i);
                
            }
            
            i++;
            
        }

        PopUpManager.getSingleton().showPopUp(editor); 
        KeyboardManager.getSingleton().setState(new DropDownBoxState(editor));
	    
	}
	
	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(String s) {
	    
	    IArgumentValuePair pair = getArgumentPair();
	    pair.setArgValue(ArgumentModelProvider.makeSetValue(s));
	    program.applyArgumentChanges();	    
	    
	}

    public void onDeselect(ADrawableObject object) {
        
        if (object == editor) {
            
            setValue(editor.getCurrent());
            PopUpManager.getSingleton().hidePopUp(editor);            
            
        }
        
    }
	    
}
