package com.uvigo.mhp.looks;

import java.awt.*;
import java.util.*;
import org.havi.ui.*;

public class HTextLook2 extends HTextLook {
    
    protected int displacement;
    protected int delay;
    protected String separator;
    private Hashtable managedObjects;
    
    public HTextLook2()
    {
        displacement=10;
        delay=75;
        managedObjects=new Hashtable();
        separator="";
    }
    
    public synchronized void showLook(Graphics g, HVisible visible, int state)
    {
        int hpos=0;
        int vpos=0;
        int width=visible.getWidth();
        int height=visible.getHeight();
        
        if (!(managedObjects.containsKey(visible)))
        {
            // es la primera vez que se dibuja el objeto
            HTextLook2Thread th=new HTextLook2Thread(this, visible);
            th.start();
            managedObjects.put(visible,th);
        }
        
        if (visible.getBackgroundMode()==HVisible.BACKGROUND_FILL)
        {
            g.setColor(visible.getBackground());
            g.fillRect(0,0,width,height);
        }
        
        if (visible.getLookData(this)!=null)
            hpos=((Integer) visible.getLookData(this)).intValue();
        
        String s=visible.getTextContent(HState.NORMAL_STATE);
        if ((s==null) || (s==""))
            return;
        
        g.setFont(visible.getFont());
        FontMetrics fm=g.getFontMetrics();
        String original=new String(s);
        int stringWidth=0;
        while (stringWidth <= width)
        {
            s=s+separator+original;
            stringWidth=fm.stringWidth(s);
        }
        
        g.setColor(visible.getForeground());
        
        switch (visible.getVerticalAlignment())
        {
            case HVisible.VALIGN_CENTER:
                vpos=(height-fm.getDescent()-fm.getAscent())/2;
                break;
            case HVisible.VALIGN_BOTTOM:
                vpos=height-fm.getDescent()-fm.getAscent();
                break;
        }
        vpos+=fm.getAscent();
        g.drawString(s+separator+s, hpos, vpos);
    }
    
    public int getDelay()
    {
        return delay;
    }
    
    public void setDelay(int delay)
    {
        this.delay=delay;
    }
    
    public String getSeparator()
    {
        return separator;
    }
    
    public void setSeparator(String s)
    {
        if (s==null)
            this.separator="";
        else
            this.separator=s;
    }
    
    public int getDisplacement()
    {
        return displacement;
    }
    
    public void setDisplacement(int displacement)
    {
        this.displacement=displacement;
    }
    
    protected void removeManagedObject(HVisible visible)
    {
        managedObjects.remove(visible);
    }
}