package net.mkonrad.synes.gui.elements;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.font.TextLayout;
import java.awt.geom.Rectangle2D;
import java.text.AttributedCharacterIterator;
import java.text.AttributedString;

import javax.media.j3d.Canvas3D;
import javax.vecmath.Color3f;

import net.mkonrad.synes.gui.style.Colors;
import net.mkonrad.synes.util.DbgTools;

import com.xith.java3d.overlay.LabelOverlay;

public class LabelOverlayExt extends LabelOverlay
{
  private final static Color3f SELECTED_COLOR = Colors.textBorders;
  private boolean _selected;
  
  public LabelOverlayExt(Canvas3D canvas, Rectangle space, String text, Font font, Color color,  boolean selected)
  {
    super(canvas, space, text, font, color, null);
    _selected = selected;
  }

  public void setSelected(boolean s)
  {
    _selected = s;
    repaint();
  }
  
  public void paint(Graphics2D g)
  {
    if (text != null)
    {
      synchronized (text)
      {
        AttributedCharacterIterator characterIterator = text.getIterator(null,
            0, visibleLength);
        if (characterIterator.getEndIndex() > 0)
        {
          
          TextLayout textLayout = new TextLayout(characterIterator, g
              .getFontRenderContext());
          
          if (_selected)
          {
            Rectangle2D outline = new Rectangle2D.Float(0, this.getBounds().height - 2, (float)textLayout.getBounds().getWidth() + 10, 1);
            g.setColor(new Color(SELECTED_COLOR.x, SELECTED_COLOR.y, SELECTED_COLOR.z));
            g.draw(outline);
          }                    
          
          textLayout.draw(g, 4, 3 * getBounds().height / 4);
        }
      }
    }
  }
}
