/*******************************************************************************
* Copyright (c)                                                                *
*           2005   Tristan Allwood,                                            *
*           2005   Daniel Burke,                                               *
*           2005   Marc Hull,                                                  *
*           2005   Ekaterina Itskova,                                          *
*           2005   Steve Zymler                                                *
*                                                                              *
* Permission is hereby granted, free of charge, to any person obtaining a      *
* copy of this software and associated documentation files (the                *
* "Software"), to deal in the Software without restriction, including          *
* without limitation the rights to use, copy, modify, merge, publish,          *
* distribute, sublicense, and/or sell copies of the Software, and to           *
* permit persons to whom the Software is furnished to do so, subject to        *
* the following conditions:                                                    *
*                                                                              *
* The above copyright notice and this permission notice shall be included      *
* in all copies or substantial portions of the Software.                       *
*                                                                              *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS      *
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF                   *
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.       *
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY         *
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,         *
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE            *
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                       *
*******************************************************************************/
/*
 * Created on 26-Nov-2004
 *
 */

package uk.ac.ic.doc.kevlar.gui.showers;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.eclipse.swt.widgets.Display;

import uk.ac.ic.doc.kevlar.gui.internal.ADrawableObject;
import uk.ac.ic.doc.kevlar.gui.internal.AMouseListenerObject;
import uk.ac.ic.doc.kevlar.gui.internal.widgets.INewDataListener;
import uk.ac.ic.doc.kevlar.gui.internal.widgets.tab.Tab;
import uk.ac.ic.doc.kevlar.publicInterfaces.IType;

/**
 * @author Daniel Burke
 *
 */
public abstract class AbstractShower extends AMouseListenerObject implements IShower {

	protected boolean removed = false;
	private INewDataListener listener;
	private Adder adder = new Adder();
	List<IType> objects = new ArrayList<IType>();
	private List<INewDataListener> listeners = new ArrayList<INewDataListener>();	
	private IShowerProvider provider;
	
	public AbstractShower(IShowerProvider provider)
	{
		this.provider = provider;
	}
	
    public synchronized boolean isRemoved()
    {
        return removed;
    }

    public synchronized void setRemoved() {
        removed = true;
    }
    
	
	protected class Adder implements Runnable
	{
	    public void run()
	    {
	    	synchronized(AbstractShower.this)
			{
	    		for(IType o : objects)
	    		{
	    			addObjectFromGUIThread(o);
	    		}

	    		objects.clear();
	    		
	    	    for(INewDataListener listener : listeners)
	    	    {
	    	        listener.newData();
	    	    }
			}
	    }
	}
	
	protected class ExecutionStartedNotifier implements Runnable
	{
	    String programName;
	    Calendar startedTime;
	    public ExecutionStartedNotifier(String programName, Calendar startedTime)
	    {
	        this.programName = programName;
	        this.startedTime = startedTime;
	    }
	    
	    public void run()
	    {
	    	synchronized(AbstractShower.this)
			{
	    	    fromGUIThreadExecutionStarted(programName, startedTime);
			}
	    }
	}
	
	public synchronized void executionStarted(String programName, Calendar startedTime)
	{
	    Display.getDefault().asyncExec(new ExecutionStartedNotifier(programName, startedTime));
	}
	
	protected abstract void fromGUIThreadExecutionStarted(String programName, Calendar startedTime);
	
	public synchronized void addObject(IType o)
	{
		boolean needsRefresh = this.objects.size() == 0;
		
	    this.objects.add(o);
	    
	    if(needsRefresh)
	    {
	    	Display.getDefault().asyncExec(adder);
	    }
	}
	
	public synchronized void addAllObjects(List<IType> objects)
	{
		boolean needsRefresh = this.objects.size() == 0;
		
		this.objects.addAll(objects);
		
	    if(needsRefresh)
	    {
	    	Display.getDefault().asyncExec(adder);
	    }
	}
	
	protected abstract void addObjectFromGUIThread(IType o);
	

	public synchronized IShowerProvider getShowerProvider() {
		return provider;
	}
	
    public void addNewDataListener(INewDataListener listener)
    {
        listeners.add(listener);
    }
    
    public void removeNewDataListener(INewDataListener listener)
    {
        listeners.remove(listener);
    }
}
