package com.uvigo.mhp.ui;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
import org.dvb.ui.*;
import org.havi.ui.*;
import com.uvigo.mhp.looks.*;

public class HInternalFrame extends HPanel implements FocusListener
{
    private boolean movable;
    private boolean dragging;
    private int lastX, lastY;
    
    public HInternalFrame()
    {
        super();
        requestFocus();
        addFocusListener(this);
        dragging=false;
        lastX=lastY=0;
        
        enableEvents(AWTEvent.MOUSE_EVENT_MASK+AWTEvent.MOUSE_MOTION_EVENT_MASK);
    }

    public boolean isMovable()
    {
        return movable;
    }
    
    public void setMovable(boolean b)
    {
        boolean old=movable;
        if (old!=b)
        {
            movable=b;
            firePropertyChange("movable",new Boolean(old), new Boolean(b));
        }
    }
    
    protected void processMouseEvent(MouseEvent e)
    {
        if (getLook()==null) return;
        
        int width=(int) getSize().getWidth();
        int height=(int) getSize().getHeight();
        int x=e.getX();
        int y=e.getY();
        
        HPanelLook look=getLook();
        int zone=look.hitTest(this,new Point(x,y));
        switch (e.getID())
        {
            case MouseEvent.MOUSE_PRESSED:
                if (!((x>=0) & (x<width) & (y>=0) & (y<height)))
                    return;
                
                requestFocus();
                //bringToFront();
                
                if (zone==HPanelLook.CLICK_BAR)
                {
                    if (movable)
                        dragging=true;
                    lastX=x;
                    lastY=y;
                }
                return;
            case MouseEvent.MOUSE_RELEASED:
                if (dragging)
                    dragging=false;
                return;
        }
    }
    
    protected void processMouseMotionEvent(MouseEvent e)
    {
        if (getLook()==null) return;
        if (!dragging) return;
        
        int width=(int) getSize().getWidth();
        int height=(int) getSize().getHeight();
        int x=e.getX();
        int y=e.getY();
        
        HPanelLook look=getLook();
        int zone=look.hitTest(this,new Point(x,y));
        if (e.getID()!=MouseEvent.MOUSE_DRAGGED)
        {
            dragging=false;
            return;
        }
        //if (!dragging) return;
        
        int tempX=getX();
        int tempY=getY();
        setLocation(getX()+x-lastX, getY()+y-lastY);
        lastX=x+(tempX-getX());
        lastY=y+(tempY-getY());
    }
    
    // Los siguientes metodos sobrecargan add* y remove* para incluir el HPanel
    // como FocusListener de los objetos que se le anaden, y para darlo de baja
    // de los que se quitan.
    protected void addImpl(Component comp, Object constraints, int index)
    {
        comp.addFocusListener(this);
        super.addImpl(comp, constraints, index);
    }
    
    public void remove(int index)
    {
        Component comp=getComponent(index);
        comp.removeFocusListener(this);
        super.remove(index);
    }
    
    public void remove(Component comp)
    {
        comp.removeFocusListener(this);
        super.remove(comp);
    }
    
    public void removeAll()
    {
        Component comps[]=getComponents();
        for (int i=0;i<comps.length;i++)
            comps[i].removeFocusListener(this);
        
        super.removeAll();
    }
    
    private synchronized void bringToFront()
    {
        Container c=getParent();
        if (c==null) return;
        
        Component comps[]=c.getComponents();
        int j=0;
        for (int i=0;i<comps.length;i++)
            if (comps[i]==this)
            {
                j=i;
                break;
            }
        for (int i=0;i<comps.length;i++)
            if (comps[i]!=this)
                c.add(comps[i],j);
            else break;
        
        repaint();
    }
    
    public synchronized void focusGained(java.awt.event.FocusEvent e)
    {
        Container c=getParent();
        if (c==null) return;
        if (c.getLayout()!=null) return;
        // los paneles solo se solapan si el contenedor padre no tiene layout
        if (c.getComponents()[0]==this) return;
        bringToFront();
    }
    
    public void focusLost(java.awt.event.FocusEvent e)
    {
    }
}