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

import java.io.FileNotFoundException;
import java.util.HashSet;
import java.util.Set;

import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
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.caches.ImageCache;


/**
 * @author Marc
 **/
public class RollOverButton extends AMouseListenerObject {

    protected Image button;
    protected Image rollover;
    protected Image down;
    
    private Set<IClickListener> listeners = new HashSet<IClickListener>();
    
    public RollOverButton(String image, String rolloverImage, String downImage) {
        
        super();
        initialise(image, rolloverImage, downImage);
        
    }
    
    public void initialise(String image, String rolloverImage, String downImage) {
        
        setImage(image);
        setRolloverImage(rolloverImage);
        setDownImage(downImage);
        
        if (button != null) {
            
            Rectangle bounds = button.getBounds();
            setSize(new Point(bounds.width, bounds.height));
            
        }
        
    }
    
    public void setImage(String image) {
        
        try {

            button = ImageCache.get(image);
            
        } catch (FileNotFoundException e) {
            
            button = null;
            
        }
        
    }
    
    public void setImage(Image img) {
        
        button = img;
        
    }
        
    public void setRolloverImage(String rolloverImage) {
            
        try {

            rollover = ImageCache.get(rolloverImage);
            
        } catch (FileNotFoundException e) {
            
            rollover = null;
            
        }
        
    }
    
    public void setRolloverImage(Image img) {
        
        rollover = img;
        
    }
        
    public void setDownImage(String downImage) {
            
        try {

            down = ImageCache.get(downImage);
            
        } catch (FileNotFoundException e) {
            
            down = null;
            
        }
        
    }
    
    public void setDownImage(Image img) {
        
        down = img;
        
    }
    
    public void drawBefore(GC gc, Rectangle redrawArea) {
        
        if (isMouseDown()) {
            
            if (down != null) {
                
                gc.drawImage(down, -redrawArea.x, -redrawArea.y);
                
            }
            
        } else if (isMouseOver()) {
            
            if (rollover != null) {
                
                gc.drawImage(rollover, -redrawArea.x, -redrawArea.y);
                
            }
            
        } else {
            
            if (button != null) {
                
                gc.drawImage(button, -redrawArea.x, -redrawArea.y);
                
            }
            
        }
        
    }
    
    public void mouseEnter() {
        
        redraw();
        applyRedraw();
        
    }
    
    public void mouseExit() {
        
        redraw();
        applyRedraw();
        
    }

    public void mousePress(Point position, int mouseButton) {
        
        redraw();
        applyRedraw();
        
    }

    public void mouseRelease(Point position, int mouseButton) {
        
		super.mouseRelease(position, mouseButton);
		
		if(position.x < 0 || position.x > getSize().x || position.y < 0 || position.y > getSize().y)
		{
		    return;
		}
		
		for(IClickListener listener : listeners)
		{
		    listener.clicked();
		}
        
        redraw();
        applyRedraw();
        
    }
    
    public boolean capturesMouse() {
        
        return true;
        
    }
    
    public void addListener(IClickListener listener)
    {
        listeners.add(listener);
    }
    
    public void removeListener(IClickListener listener)
    {
        listeners.remove(listener);
    }
    
}
