/*******************************************************************************
* 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.ISinglelineArgumentInfo;
import uk.ac.ic.doc.kevlar.api.programs.arguments.ITextArgumentValue;
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.TextBoxState;
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.textbox.TextBox;
import uk.ac.ic.doc.kevlar.hial.IArgumentValuePair;

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

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

    public ITextArgumentValue getValueData() {
        
        return (ITextArgumentValue)((IArgumentValuePair)data).getArgValue();
        
    }
    
	public ISinglelineArgumentInfo getArgumentInfo() {
	    
	    return (ISinglelineArgumentInfo)((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));
	    
	    if (invalid) {

	        gc.setForeground(ColorCache.get(255, 0, 0));
		    gc.setFont(FontCache.get("Arial", 7, SWT.BOLD));
		    	        
	    } else {
	    
	        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() {
	    
/*        SingleLineTextArgumentEditor editor = new SingleLineTextArgumentEditor(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.setText(getValueData().getValue());
        PopUpManager.getSingleton().showPopUp(editor);
        
        KeyboardManager.getSingleton().setState(new TextBoxState(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.makeTextValue(s));
	    program.applyArgumentChanges();	    	    
	    
	}

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