package com.uvigo.mhp.ui;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
import javax.swing.event.*;

import org.havi.ui.*;
import org.havi.ui.event.*;

// Hacer que el desplazamiento sea programable!
public class HScrollPane extends JScrollPane implements Serializable, HAdjustmentListener, ChangeListener
{
    private JViewport vp;
    private HScrollBar horizontalScrollControl, verticalScrollControl;
    
    public HScrollPane()
    {
        super();
        setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
        setOpaque(false);
        setBorder(null);
        vp=getViewport();
        vp.setOpaque(false);
        vp.addChangeListener(this);
    }
    
    public void up()
    {
        Point p=vp.getViewPosition();
        if (p.y==0) return;
        if (p.y>=20)
            p.translate(0,-20);
        else
            p.translate(0,-p.y);
        vp.setViewPosition(p);
        repaint();
    }
    
    public void down()
    {
        Point p=vp.getViewPosition();
        if (p.y>=vp.getViewSize().height-vp.getExtentSize().height) return;
        p.translate(0,20);
        vp.setViewPosition(p);
        repaint();
    }
    
    public void left()
    {
        Point p=vp.getViewPosition();
        if (p.x==0) return;
        if (p.x>=20)
            p.translate(-20,0);
        else
            p.translate(p.x,0);
        vp.setViewPosition(p);
        repaint();
    }
    
    public void right()
    {
        Point p=vp.getViewPosition();
        if (p.x>=vp.getViewSize().width-vp.getExtentSize().width) return;
        p.translate(20,0);
        vp.setViewPosition(p);
        repaint();
    }
   
    public HScrollBar getHorizontalScrollControl()
    {
        return horizontalScrollControl;
    }
    
    public void setHorizontalScrollControl(HScrollBar hrv)
    {
        HScrollBar old=horizontalScrollControl;
        if (old!=null)
            old.removeAdjustmentListener(this);
        
        horizontalScrollControl=hrv;
        if (hrv!=null)
            hrv.addAdjustmentListener(this);
        if (old!=hrv)
            firePropertyChange("horizontalScrollControl", old, hrv);
    }
    
    public HScrollBar getVerticalScrollControl()
    {
        return verticalScrollControl;
    }
    
    public void setVerticalScrollControl(HScrollBar hrv)
    {
        HScrollBar old=verticalScrollControl;
        if (old!=null)
            old.removeAdjustmentListener(this);
        
        verticalScrollControl=hrv;
        if (hrv!=null)
            hrv.addAdjustmentListener(this);
        if (old!=hrv)
            firePropertyChange("verticalScrollControl", old, hrv);
    }
    
    public void valueChanged(HAdjustmentEvent e)
    {
        //System.out.println(e);
        if (e.getSource()==null) return;
        
        if (e.getSource()==horizontalScrollControl)
        {
            int preferredWidth=vp.getViewSize().width;
            int extentWidth=vp.getExtentSize().width;
            if (extentWidth>=preferredWidth) return;
            
            int dif=preferredWidth-extentWidth;
            
            int min=horizontalScrollControl.getMinValue();
            int max=horizontalScrollControl.getMaxValue();
            int cur=horizontalScrollControl.getValue();
            float pos=(float) (cur-min)/(float) (max-min);
            
            //System.out.println(pos+" "+dif+" "+(int) pos*dif);
            Point p=vp.getViewPosition();
            p.x=(int) (pos*dif);
            vp.setViewPosition(p);
            repaint();
        }
        else
        {
            int preferredHeight=vp.getViewSize().height;
            int extentHeight=vp.getExtentSize().height;
            if (extentHeight>=preferredHeight) return;
            
            int dif=preferredHeight-extentHeight;
            
            int min=verticalScrollControl.getMinValue();
            int max=verticalScrollControl.getMaxValue();
            int cur=verticalScrollControl.getValue();
            float pos=(float) (cur-min)/(float) (max-min);
            
            //System.out.println(pos+" "+dif+" "+(int) (pos*dif));
            Point p=vp.getViewPosition();
            p.y=(int) (pos*dif);
            vp.setViewPosition(p);
            repaint();
        }
    }
    
    public void stateChanged(javax.swing.event.ChangeEvent changeEvent)
    {
        //System.out.println(changeEvent);
        // Hay que actualizar los controles de scroll
        Point pt=vp.getViewPosition();
        Dimension preferredSize=vp.getViewSize();
        Dimension extentSize=vp.getExtentSize();
        
        if (horizontalScrollControl!=null)
        {
            if (extentSize.width<preferredSize.width)
            {
                if (!(horizontalScrollControl.isSettingValue()))
                {
                    int xdif=preferredSize.width-extentSize.width;
                    float xpos=((float) pt.x)/((float) xdif);
                    
                    int min=horizontalScrollControl.getMinValue();
                    int max=horizontalScrollControl.getMaxValue();
                    
                    int val=min+ Math.round(xpos*(max-min));
                    horizontalScrollControl.setValue(val);
                }
            }
            else
            {
                horizontalScrollControl.setValue(horizontalScrollControl.getMinValue());
            }
        }
        
        if (verticalScrollControl!=null)
        {
            if (extentSize.height<preferredSize.height)
            {
                if (!(verticalScrollControl.isSettingValue()))
                {
                    int ydif=preferredSize.height-extentSize.height;
                    float ypos=((float) pt.y)/((float) ydif);
                    
                    int min=verticalScrollControl.getMinValue();
                    int max=verticalScrollControl.getMaxValue();
                    
                    int val=min+ Math.round(ypos*(max-min));
                    verticalScrollControl.setValue(val);
                }
            }
            else
            {
                verticalScrollControl.setValue(verticalScrollControl.getMinValue());
            }
        }
    }
    
    public Dimension getMinimumSize()
    {
        Dimension dim=super.getMinimumSize();
        if (dim.width==0)
            dim.width=10;
        if (dim.height==0)
            dim.height=10;
        
        return dim;
    }
    
    public Dimension getMaximumSize()
    {
        Dimension dim=super.getMaximumSize();
        if (dim.width==0)
            dim.width=10;
        if (dim.height==0)
            dim.height=10;
        
        return dim;
    }
    
    public Dimension getPreferredSize()
    {
        Dimension dim=super.getPreferredSize();
        if (dim.width==0)
            dim.width=10;
        if (dim.height==0)
            dim.height=10;
        
        return dim;
    }
    
}