package leaf;

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


public class LeafLogic
{

	/**
	 * 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";
	
	@Action(isSave = false)
	public static final String ACTION_CANCEL = "ACTION_CANCEL";
	

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

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

}
