package nl.nikhef.tools;

import java.lang.reflect.Method;
import java.util.Collection;

import javax.swing.SwingUtilities;

public class AWTDispatcher<T> implements ListenerManager.EventDispatcher<T> {

	private static class DispatchTask<T> implements Runnable
	{

		public final Collection<T> col;
		public final Method m;
		public final Object[] params;
		
		public DispatchTask(Collection<T> c, Method m, Object[] params) 
		{
			this.col = c;
			this.m = m;
			this.params = params;
		}

		@Override
		public void run() {
			for (T obj : col) {
				try {
					m.invoke(obj, params);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		
	}
	
	@Override
	public void dispatch(Collection<T> c, Method m, Object[] params) throws Exception {
		DispatchTask<T> dp = new DispatchTask<T>(c, m, params);
		SwingUtilities.invokeLater(dp);
	}
	


}
