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

import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;

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.ScrollAnimation;
import uk.ac.ic.doc.kevlar.gui.internal.widgets.IScrollingWindow;
import uk.ac.ic.doc.kevlar.gui.internal.widgets.StretchyBox;
import uk.ac.ic.doc.kevlar.gui.internal.widgets.scrollbars.VerticalScrollBar;


/**
 * @author Marc
 **/
public abstract class AScrollingTaskPane extends ATaskPane implements IScrollingWindow {

    protected VerticalScrollBar scrollbar;
    protected StretchyBox contents;
      
    public AScrollingTaskPane(Point position) {
        
        super();
        
        scrollbar = new VerticalScrollBar(this);
        contents = new StretchyBox(false, true);
        contents.setSize(new Point(TaskPaneManager.TASK_PANE_INTERNAL_WIDTH-21, 0));
        
        super.addChild(scrollbar);        
        super.addChild(contents);
        
        setPosition(position);
        
    }
    
    public void addChild(ADrawableObject child) {
        
        contents.addChild(child);
        
    }
    
    public void removeChild(ADrawableObject child) {
        
        contents.removeChild(child);
        
    }
    
    public void removeAllChildren() {
        
        contents.removeAllChildren();
        
    }
    
    public void setHorizontalPosition(int position) {
        
        contents.setPosition(new Point(-position, contents.getPosition().y));
        redraw();
        applyRedraw();
        
    }
    
    public void setVerticalPosition(int position) {
        
        contents.setPosition(new Point(contents.getPosition().x, -position));
        redraw();
        applyRedraw();
        
    }
    
    public int getHorizontalPosition() {
        
        return -contents.getPosition().x;
        
    }
    
    public int getVerticalPosition() {
        
        return -contents.getPosition().y;
        
    }
        
    public void updateDimensions() {
        
        super.updateDimensions();
        
        if (scrollbar != null) {
        
            scrollbar.setSize(area.height);
            scrollbar.updateFromWindow();
            
        }
        
    }
    
    public void childDimensionsChanged(ADrawableObject child) {
        
        if (child == contents) {
            
            scrollbar.setMaximum(child.getSize().y);
            
        }
        
    }

    public ADrawableObject getContents() {
        
        return contents;
        
    }
    
	public void scrollTo(ADrawableObject child) {
	    
	    Rectangle childArea = child.getAreaRelativeTo(this);
	    Point newPosition = contents.getPosition();
	    
	    if ((childArea.y+childArea.height+50 > area.height) || (childArea.y-50 < 0)) {
	        
	        if (childArea.y+childArea.height+50 > area.height) {
	            
	            newPosition.y = newPosition.y-((childArea.y+childArea.height+50)-area.height);
	            
	            if (newPosition.y > 0) {
	                
	                newPosition.y = 0;
	                
	            } else if (newPosition.y < -contents.getArea().height+area.height) {
	                
	                newPosition.y = -contents.getArea().height+area.height;
	                
	            }
	            
	        } else if (childArea.y-50 < 0) {
	            
	            newPosition.y = newPosition.y-childArea.y+50;
	            
	            if (newPosition.y > 0) {
	                
	                newPosition.y = 0;
	                
	            } else if (newPosition.y < -contents.getArea().height+area.height) {
	                
	                newPosition.y = -contents.getArea().height+area.height;
	                
	            }
	            
	        }
        
//	        AnimationManager.getSingleton().addAnimation(new ScrollAnimation(this, newPosition));
	        contents.setPosition(newPosition);
	        scrollbar.updateFromWindow();
	        
	    }
                
	}
	
	public void scrollTo(int pos) {
	    
	    Point currentPos = contents.getPosition();
	    
	    if (currentPos.y != pos) {
	    
	        contents.setPosition(new Point(currentPos.x, pos));
	        scrollbar.updateFromWindow();
	        
	    }
	    
	}
	    
}
