package siprp.logic;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.List;

import siprp.ui.SIPRPWindow;

public class SIPRPLogic
{

	/**
	 * Declares an Action
	 * 
	 * @author tsimao
	 * 
	 */
	@Retention(RetentionPolicy.RUNTIME)
	public @interface Action
	{
		/**
		 * true - this action saves data to de database from the components
		 * false - this action reads from the database to the components
		 */
		boolean isSave();
	}
	
	/**
	 * Binds a UI method with an action
	 * 
	 * @author tsimao
	 * 
	 */
	@Retention(RetentionPolicy.RUNTIME)
	public @interface LeafUIActionBinding
	{

		/**
		 * The name of the action this method binds to
		 */
		String [] action();
	}

	/**
	 * Binds a logic methods with a group of actions
	 * 
	 * @author tsimao
	 * 
	 */
	@Retention(RetentionPolicy.RUNTIME)
	public @interface LeafLogicActionBinding
	{

		/**
		 * The name of the action this method binds to
		 */
		String [] actions();
	}

	@Action(isSave = false)
	public static final String ACTION_STARTUP = "ACTION_STARTUP";
	

	private List<SIPRPWindow> registeredWindows = new ArrayList<SIPRPWindow>();

	public void addWindow(SIPRPWindow window)
	{
		registeredWindows .add(window);
	}
	
	public void runAction( String actionName )
	{
		for( SIPRPWindow window : registeredWindows )
		{
			window.runAction( actionName );
		}
	}
	
	public void runAction( String actionName, Object argument )
	{
		for( SIPRPWindow window : registeredWindows )
		{
			window.runAction( actionName, argument );
		}
	}
	
	public void runActionLater( String actionName )
	{
		for( SIPRPWindow window : registeredWindows )
		{
			window.runActionLater( actionName );
		}
	}

}
