package org.ajax4jsf.bean;

import java.util.ArrayList;
import java.util.Date;
import java.util.EventListener;
import java.util.EventObject;
import java.util.List;

import org.ajax4jsf.event.PushEventListener;


public class MessageBean implements Runnable {

    private String result;

    private boolean running = true;

    private PushEventListener listener;
    
    private List messages = new ArrayList(10);
    
    private int counter = 0;

    public MessageBean() {
	// TODO Auto-generated constructor stub
    }

    /**
         * @return the result
         */
    public String getResult() {
	return result;
    }

    /**
         * @param result
         *                the result to set
         */
    public void setResult(String result) {
	this.result = result;
    }

    public void addListener(EventListener listener) {
	synchronized (listener) {
	    if (this.listener != listener) {
		this.listener = (PushEventListener) listener;
		Thread th = new Thread(this);
		th.start();

	    }
	}
    }

    public void run() {
	try {
	    while (isRunning()) {
		Thread.sleep(10000);
		Date current = new Date(System.currentTimeMillis());
		if(messages.size()>=10){
		    messages.remove(0);
		}
		messages.add(messages.size(),String.valueOf(counter++)+" at "+current.toString());
		System.out.println("event occurs");
		synchronized (listener) {
		    listener.onEvent(new EventObject(this));
		}
	    }
	} catch (InterruptedException e) {
	    e.printStackTrace();
	}

    }

    /**
         * @return the running
         */
    public synchronized boolean isRunning() {
	return running;
    }

    /**
         * @param running
         *                the running to set
         */
    public synchronized void setRunning(boolean running) {
	this.running = running;
    }

    /**
     * @return the messages
     */
    public List getMessages() {
        return messages;
    }

    /**
     * @param messages the messages to set
     */
    public void setMessages(List messages) {
        this.messages = messages;
    }

}
