import java.io.*;
import java.awt.*;

public class TitleBarHandler
{
	JPE jpe;
	TextHandler txth;
	String oldpre="";
	String oldfix="";
	String cs=" ";
	String ss=" ";
	String ls=" ";
	boolean statusmsg=false;

	public TitleBarHandler(JPE jpe) {
		this.jpe=jpe;
	}

	public void say(String line) {
		if (txth==null) txth=jpe.getTextHandler();
		txth.setTitle("JPE : "+line);
		statusmsg=true;
	}

	public void say() {
		if (statusmsg) {
			statusmsg=false;
			return;
		}
		if (txth==null) txth=jpe.getTextHandler();
		if (txth.file==null) {
			txth.setTitle("JPE: No file Selected");
		} else {
			String tmp="JPE : File "+txth.file.getName();
			if (tmp.length()>50) tmp=tmp.substring(0,50);
			if (!oldpre.equals(tmp)) {
				// create a new font based on the old one but smaller
				Font newf=new Font("Arial",Font.BOLD,15);
	       	
				FontMetrics fm=txth.getFontMetrics(newf);
				int ts=fm.stringWidth(tmp);
				oldpre=tmp;
				oldfix="";
				if (ts<600) {
					while (ts<600) {
					 oldfix+=" ";
					 ts=fm.stringWidth(tmp+oldfix);
					}
				}
			}
			txth.setTitle(oldpre+oldfix+ss+" "+cs+" "+ls+"   "+txth.getCursorLine());
		}
	}

	public void setSaveState(boolean state) {
		if (state) {
			ss=" ";
		} else {
			ss="*";
		}
	}

	public void setCompileState(boolean state) {
		if (state) {
			cs="c";
		} else {
			cs=" ";
		}
	}

	public void setLockState(boolean state) {
		if (state) {
			ls="l";
		} else {
			ls=" ";
		}
	}

}
