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

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.AMouseListenerObject;
import uk.ac.ic.doc.kevlar.gui.internal.animation.AAnimation;
import uk.ac.ic.doc.kevlar.gui.internal.animation.AnimationManager;
import uk.ac.ic.doc.kevlar.gui.internal.animation.SlideAnimation;
import uk.ac.ic.doc.kevlar.gui.internal.animation.SlideOutAnimation;
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.popup.PopUpManager;

/**
 * @author Marc
 **/
public class ListEntry extends AMouseListenerObject implements Comparable {

    protected static final String SELECTED_SKIN = "selectedheading";
	protected static final int ENTRY_INTERNAL_HEIGHT = 17;
	protected static final int ENTRY_EXTERNAL_HEIGHT = 20;
	
	protected ListBox parent;
	protected int positionIndex;
	protected Object data;
	protected String skin;
	protected boolean selected;
	protected boolean disposed;
	
	public ListEntry(ListBox parent, int positionIndex, Object data, String skin) {
		
		super();
		this.parent = parent;
		this.positionIndex = positionIndex;
		this.data = data;
		this.skin = skin;
		this.disposed = false;
		setPosition(new Point(parent.getSize().x, positionIndex*ENTRY_INTERNAL_HEIGHT));
//		setPosition(new Point(0, positionIndex*ENTRY_INTERNAL_HEIGHT));
		setSize(new Point(parent.getSize().x, ENTRY_EXTERNAL_HEIGHT));
		AnimationManager.getSingleton().addAnimation(new SlideAnimation(this, new Point(0, positionIndex*ENTRY_INTERNAL_HEIGHT)));
		
	}
	
	public void select() {
	    
	    if (!selected) {
	    
	        selected = true;
	        redraw();
	        
	    }
	    
	}
	
	public void deselect() {
	    
	    if (selected) {
	    
	        selected = false;
	        redraw();
	        
	    }
	    
	}
	
	public boolean isSelected() {
	    
	    return selected;
	    
	}
	
	public void drawBefore(GC gc, Rectangle redrawArea) {
	    
	    if (selected) {
	        
	        Utils.drawHorizontalBar(gc, SELECTED_SKIN, new Rectangle(-redrawArea.x, -redrawArea.y, area.width, area.height));
		    
	        gc.setForeground(ColorCache.get(255, 255, 255));
	        gc.setFont(FontCache.get("Arial", 8, SWT.BOLD));
	        
	        Utils.drawText(gc, getText(), new Rectangle(10-redrawArea.x, -redrawArea.y, area.width-15, area.height), Utils.ALIGN_LEFT);
		        
	    } else {
	    
	        Utils.drawHorizontalBar(gc, skin, 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, getText(), new Rectangle(10-redrawArea.x, -redrawArea.y, area.width-15, area.height), Utils.ALIGN_LEFT);
	        
	    }
	    
	}
	
	public void mouseRelease(Point position, int button) {
	    
	    if (parent != null && !disposed) {
	        
	        parent.entrySelected(data);
	        
	        PopUpManager.getSingleton().mousePress(position, button);
	    }
	    
	}
	
	public boolean capturesMouse() {
	    
	    return true;
	    
	}
	
	public String getText() {
	    
	    return data.toString();
	    
	}
	
	public Object getData() {
	    
	    return data;
	    
	}
	
	public AAnimation setIndex(int index) {
	    
	    positionIndex = index;
	    AAnimation anim = new SlideAnimation(this, new Point(0, positionIndex*ENTRY_INTERNAL_HEIGHT));
	    AnimationManager.getSingleton().addAnimation(anim);
	    return anim;
	    
	}
	
	public int getIndex() {
	    
	    return positionIndex;
	    
	}
	
	public boolean isDisposed() {
	    
	    return disposed;
	    
	}
	
	public void parentResized(Point size) {
	    
	    setSize(new Point(size.x, ENTRY_EXTERNAL_HEIGHT));
	    
	}
	
	public void dispose() {
	    
		AnimationManager.getSingleton().addAnimation(new SlideOutAnimation(this, new Point(-getSize().x, positionIndex*ENTRY_INTERNAL_HEIGHT)));
		disposed = true;
	    
	}

    public int compareTo(Object arg0) {
        
        if (arg0 instanceof ListEntry) {
            
            return getIndex()-((ListEntry)arg0).getIndex();
            
        }
        
        return 0;
        
    }
	
}
