package j3deditor.bin.hierarchy.util;

import java.util.ArrayList;

import javax.swing.SwingWorker;


/**
 * Base class for utilities that perform their tasks in
 * a background thread and need to notify scene-editor about
 * updates made to scene.
 * 
 * @author Risto Seene
 * 
 * @see javax.swing.SwingWorker
 */
public abstract class HierarchyWorker extends SwingWorker<Void, Void>{
	
	/** hierarchy event listeners */
	private ArrayList<HierarchyEventListener> listeners;


	/**
	 * Constructor.
	 */
	protected HierarchyWorker(){
		listeners = new ArrayList<HierarchyEventListener>();
	}
	
	/**
	 * @see javax.swing.SwingWorker#doInBackground()
	 */
	@Override
	protected Void doInBackground() throws Exception{
		return null;
	}
	
	/**
	 * Adds the specified <code>HierarchyEventListener</code>.
	 * 
	 * @param listener
	 * 	listener to be added
	 */
	public void addHierarchyListener(HierarchyEventListener listener){
		listeners.add(listener);
	}
	
	/**
	 * Removes the specified <code>HierarchyEventListener</code>.
	 * 
	 * @param listener
	 * 	listener to be removed
	 */
	public void removeHierarchyListener(HierarchyEventListener listener){
		listeners.remove(listener);
	}
	
	/**
	 * Sends the given <code>HierarchyEvent</code> to all listeners.
	 * 
	 * @param e
	 * 	HierarchyEvent to be sent
	 */
	protected void notifyListeners(HierarchyEvent e){
		for(int i=0; i<listeners.size(); i++){
			listeners.get(i).hierarchyEventOccured(e);
		}
	}
	
}
