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

import java.io.FileNotFoundException;

import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;

import uk.ac.ic.doc.kevlar.gui.internal.ADrawableObject;
import uk.ac.ic.doc.kevlar.gui.internal.animation.AnimationManager;
import uk.ac.ic.doc.kevlar.gui.internal.animation.ProgressBarAnimation;
import uk.ac.ic.doc.kevlar.gui.internal.caches.ImageCache;

/**
 * @author Marc
 **/
public class ProgressBar extends ADrawableObject {

    private static final String PROGRESS_BAR_SKIN = "progressbar";
    
    protected ProgressBarAnimation anim;
    protected Image barImage;
    protected int position;
    
    public ProgressBar() {
        
        super();
        position = 0;
        barImage = new Image(Display.getDefault(), 1, 1);
        anim = new ProgressBarAnimation(this);
        
    }
    
    public void updateDimensions() {
        
        super.updateDimensions();
        updateImage();
        
    }
    
    public void updateImage() {
        
        if (area.width > 0) {
        
            try {
        
                if (barImage != null) {
                
                    barImage.dispose();
                
                }
            
                Image skinImage = ImageCache.get(PROGRESS_BAR_SKIN+".png");
                Rectangle skinBounds = skinImage.getBounds();
//            barImage = new Image(Display.getDefault(), (area.width/skinBounds.width+2)*skinBounds.width, skinBounds.height);
                barImage = new Image(Display.getDefault(), area.width+skinBounds.width, skinBounds.height);
                GC barImageGC = new GC(barImage);
            
                for (int i=0; i<barImage.getBounds().width; i+=skinBounds.width) {
                
                    barImageGC.drawImage(skinImage, i, 0);
                
                }
            
            } catch (FileNotFoundException e) {
            
                System.err.println("Could not find image: "+PROGRESS_BAR_SKIN);
            
            }
            
        }
        
    }
    
    public void show() {
        
        super.show();
        AnimationManager.getSingleton().addAnimation(anim);
        
    }
    
    public void hide() {
        
        super.hide();
        AnimationManager.getSingleton().removeAnimation(anim);
        
    }
    
    public void animate() {
        
        position++;
        
        if (position+area.width>barImage.getBounds().width) {
            
            position = 0;
            
        }
        
        redraw();
        
    }
    
    public void drawBefore(GC gc, Rectangle redrawArea) {

        gc.drawImage(barImage, -position-redrawArea.x, -redrawArea.y);
        
    }
    
}
