/**
   WMDynamicCaptionCanvas.java
   
   @author  Warren Sack
   @version January 2003

*/

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.applet.*;

public class WMDynamicCaptionCanvas extends Canvas implements MouseMotionListener, MouseListener, Observer {

    private WorldMapApplet applet;
    private String label;
    private int position, drop, delta, labelWidth;
    private boolean ready = false;
    private Image buffer;
    private Graphics bufferGraphics;
    private Dimension size;
    private int width, height;
    private Font font;
    private Color backgroundColor;
    private Color textColor;
    private int state;
    final static int increment = 1;
    final static int decrement = 0;

    public WMDynamicCaptionCanvas (WorldMapApplet applet,
				  String label, 
				 int width, 
				 int height, 
				 Font font,
				 Color backgroundColor,
				 Color textColor) {
	this.applet= applet;
	this.label = label;
	this.width = width;
	this.height = height;
	this.size = new Dimension(width,height);
	this.font = font;
	this.backgroundColor = backgroundColor;
	this.textColor = textColor;
    }

    public void reSize (int width, int height) {
	this.width = width;
	this.height = height;
    }

    public void move () {
	if ( state == increment ) {
	    if ( ( position > 0 ) && ( ( position + labelWidth) >= width ) ) {
		state = decrement;
	    }
	    else { position = position + delta; }
	}
	else {
	    if ( ( position <= 0 ) && ( ( position + labelWidth ) < width ) ) {
		state = increment;
	    }
	    else { position = position - delta; }
	}
	repaint();
    }

    public synchronized void update (Observable o, Object arg) {
	move();
    }

    public void setLabel (String label) {
	this.label = label;
	ready = false;
	repaint();
    }

    public void paint (Graphics g) {
	if ( ready ) {
	    bufferGraphics.setColor(backgroundColor);
	    bufferGraphics.fillRect(0,0,width,height);
	    bufferGraphics.setColor(textColor);
	    bufferGraphics.drawString(label, position, drop);
	    g.drawImage(buffer,0,0,this);
	}
	else {
	    g.setFont(font);
	    FontMetrics f = g.getFontMetrics();
	    labelWidth = f.stringWidth(label);
	    delta = f.stringWidth("e");
	    drop = ( f.getHeight() + f.getDescent() ) / 2;
	    position = width / 4;
	    state = increment;
	    buffer = createImage(width,height);
	    bufferGraphics = buffer.getGraphics();
	    bufferGraphics.setFont(font);
	    ready = true;
	}
    }


    public void mousePressed(MouseEvent e) {;}

    public void mouseReleased(MouseEvent e) {;}

    public void mouseClicked(MouseEvent e) {;}

    public void mouseEntered(MouseEvent e) {
	applet.unsetMapAreasGridPanelFocus();
	int mousePositionLatitude = applet.getSelectedMapArea().latitude;
	int mousePositionLongitude = applet.getSelectedMapArea().longitude;
	String latitudeDirection, longitudeDirection;
	if ( mousePositionLatitude >= 0 ) { latitudeDirection = "north"; }
	else { latitudeDirection = "south"; }
	if ( mousePositionLongitude >= 0 ) { longitudeDirection = "west"; }
	else { longitudeDirection = "east"; }
	String position = "[ " + Math.abs(mousePositionLatitude) + "_" + latitudeDirection + " + " + Math.abs(mousePositionLongitude) + "_" + longitudeDirection + " ]";
	applet.positionPanel.setMousePositionString(position);
	applet.positionPanel.repaint();
	//	applet.languagesMenu.resetMenu(applet.getLanguagesForMapArea(applet.getSelectedMapArea()));
	applet.countriesMenu.resetMenu(applet.getSelectedMapArea().countries);
    }

    public void mouseExited(MouseEvent e) {;}

    // MouseMotionListener methods

    public void mouseMoved(MouseEvent e) {;}

    public void mouseDragged(MouseEvent e) {;}

    public Dimension getPreferredSize () {
	return(new Dimension(width,height));
    }

}
