package com.uvigo.mhp.ui;

import java.awt.*;
import java.net.*;
import org.havi.ui.*;
import com.uvigo.mhp.looks.*;
import com.uvigo.tMaestro.SCOCreator.tool.MainFrame;


public class HImage extends HIcon {
    public static final int TIME_ALWAYS = 0;
    public static final int TIME_OK = 1;
    public static final int TIME_WRONG = 2;
    public static final int TIME_DELAYED = 4;
    public static final int TIME_LIMITED = 6;
    private String imageURL;
    private int imgWidth, imgHeight;
    private MediaTracker trk;
    private int delayedTimeMode;
    private int delayedTime;
    
    public HImage() {
        imgWidth=imgHeight=20;
        trk=new MediaTracker(this);
        delayedTimeMode=TIME_ALWAYS;
        delayedTime=0;
        setDefaultSize(new Dimension(20,20));
        try {
            setLook(new HGraphicLook1());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    public int getDelayedTimeMode() {
        return delayedTimeMode;
    }

    public void setDelayedTimeMode(int i) {

        int old = delayedTimeMode;
        if (i != old) {
            delayedTimeMode = i;
            repaint();
            firePropertyChange("delayedTimeMode", new Integer(old), new Integer(i));

        }
    }
    public int getDelayedTime() {
        return delayedTime;
    }

    public void setDelayedTime(int m) {
        int old = delayedTime;
        if (m != old) {
            delayedTime = m;
            firePropertyChange("delayedTime", new Integer(old), new Integer(m));
        }
    }
    public String getImageURL() {
        return imageURL;
    }
    
    public void setImageURL(String s) {
        String old=imageURL;
        if ((old==null) || (!(old.equals(s)))) {
            if ((s==null) || (s.equals(""))) {
                imageURL=s;
                Image temp=null;
                setGraphicContent(temp, HState.ALL_STATES);
                repaint();
                firePropertyChange("imageURL", old, s);
            } else {
                try {
                    //M: Direccionamiento relativo a "user.dir"
                        String oBase= new String (MainFrame.oMainFrame.sDir+ "/"); 
                   URL url=new URL("file:"+oBase+s);
                    Image temp=Toolkit.getDefaultToolkit().getImage(url);
                    trk.addImage(temp,0);
                    trk.waitForAll();
                    
                    imgWidth=temp.getWidth(null);
                    imgHeight=temp.getHeight(null);
                    imageURL=s;
                    setGraphicContent(temp, HState.ALL_STATES);
                    repaint();
                    firePropertyChange("imageURL", old, s);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
    }
    
    public void setImage(Image img) {
        imgWidth=img.getWidth(null);
        imgHeight=img.getHeight(null);
        setGraphicContent(img, HState.ALL_STATES);
        repaint();
    }
    
    public Dimension getMinimumSize() {
        return new Dimension(imgWidth, imgHeight);
    }
    
    public Dimension getMaximumSize() {
        return new Dimension(imgWidth, imgHeight);
    }
    
    public Dimension getPreferredSize() {
        return new Dimension(imgWidth, imgHeight);
    }
}
