/*
 * Copyright (c) 2006-2008 MiniMe. Code released under The MIT/X Window System
 * License. Full license text can be found in license.txt
 */
package minime.ui;

import javax.microedition.lcdui.Graphics;

import minime.Drawable;
import minime.Font;
import minime.LoggerListener;
import minime.Portability;
import minime.FontManager;
import minime.TextPanelUtil;
import minime.core.Component;
import minime.core.Event;
import minime.core.ResourceManager;
import minime.core.Runtime;
import minime.Logger;

/**
 * The LogPanel class is used to display Log information.
 * 
 * @author zhou cheng
 * 
 */
public class LogPanel extends Component implements LoggerListener
{
    private final static int DEFAULT_TEXT_COLOR = Portability.BLUE;
    private Logger LOG = Logger.getLogger(this.getClass().getName());
	private TextPanel textPanel = null;
	private boolean needScroll = false;
	
	public LogPanel()
	{
		textPanel = new TextPanel("no log message.",DEFAULT_TEXT_COLOR);
	}    
	
	public void activate()
	{
		LOG.debug("activate");
		Logger.addLogListener(this);
		setNeedLayout();
    }
	public void deactivate()
	{
		LOG.debug("deactivate");
		Logger.removeLogListener(this);
	}
    
    public void layoutImp()
    {
    	textPanel.setText( Logger.getAllString() );
    	if(needScroll)
    	{
    		needScroll=false;
    		textPanel.scrollToEnd();//should be called after textPanel.layout(). 
    	}	
    	textPanel.layout();
    }
    
    public void renderImpl(Graphics gc)
    {    	
		textPanel.render(gc);
	}
    
	public void notifyLogString()
	{// can't log in this function, cause it will run in loop.
		setNeedLayout();
    	needScroll = true;
		Runtime.getInstance().repaint();
	}
	
	public int getBgColor() {
		return textPanel.getBgColor();
	}
	
	public int getHeight() {
		return textPanel.getHeight();
	}
	
	public int getWidth() {
		return textPanel.getWidth();
	}
	
	public void setBgColor(int bgColor) {
		textPanel.setBgColor(bgColor);
	}
	
	public void setFont(Font font) {
		textPanel.setFont(font);
	}
	
	public void setHeight(int h) {
		textPanel.setHeight(h);
	}

	public void setWidth(int w) {
		textPanel.setWidth(w);
	}

	public int getTextColor() {
		return textPanel.getTextColor();
	}

	public void setTextColor(int textColor) {
		textPanel.setTextColor(textColor);
	}

	public boolean onEvent(Event evt) {
		return textPanel.onEvent(evt);
	}

	public int getLeft() {
		return textPanel.getLeft();
	}

	public int getTop() {
		return textPanel.getTop();
	}

	public void setLeft(int left) {
		textPanel.setLeft(left);
	}

	public void setPosition(int left, int top) {
		textPanel.setPosition(left, top);
	}

	public void setTop(int top) {
		textPanel.setTop(top);
	}
}
