/*
 * FSLimitScript.java
 * Transform
 * 
 * Copyright (c) 2001-2008 Flagstone Software Ltd. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 *
 *  * Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright notice, 
 *    this list of conditions and the following disclaimer in the documentation 
 *    and/or other materials provided with the distribution.
 *  * Neither the name of Flagstone Software Ltd. nor the names of its contributors 
 *    may be used to endorse or promote products derived from this software 
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
 * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.flagstone.transform;

/**
 * The FSLimitScript is used to define the execution environment of the Flash
 * Player, limiting the resources available when executing actions.
 * 
 * <p>
 * FSLimitScript can be used to limit the maximum recursion depth and limit the
 * time a sequence of actions can execute for. This provides a rudimentary
 * mechanism for people viewing a movie to regain control of the Flash Player
 * should a script fail.
 * </p>
 * 
 * <table class="datasheet">
 * 
 * <tr>
 * <th align="left" colspan="2">Attributes</th>
 * </tr>
 * 
 * <tr>
 * <td>type</td>
 * <td>Identifies the data structure when it is encoded. Read-only.</td>
 * </tr>
 * 
 * <tr>
 * <td>depth</td>
 * <td>The maximum depth, in the range 1..65535, that a sequence of actions can
 * recurse to.</td>
 * </tr>
 * 
 * <tr>
 * <td>timeout</td>
 * <td>The maximum time, in seconds, that a sequence of actions will execute
 * before the Flash Player present a dialog box asking whether the script should
 * be terminated.</td>
 * </tr>
 * 
 * </table>
 * 
 * <h1 class="datasheet">History</h1>
 * 
 * <p>
 * The FSLimitScript represents the ScriptLimits tag in the Macromedia Flash
 * (SWF) File Format Specification. It was introduced in Flash 7.
 * </p>
 */
public class FSLimitScript extends FSMovieObject
{
	private int depth = 0;
	private int timeout = 0;

	/**
	 * Construct an FSLimitScript object, initialising it with values decoded
	 * from an encoded object.
	 * 
	 * @param coder
	 *            an FSCoder containing the binary data.
	 */
	public FSLimitScript(FSCoder coder)
	{
		super(LimitScript);
		decode(coder);
	}

	/**
	 * Constructs an FSLimitScript object that limits the recursion depth to
	 * <em>depth</em> levels and specifies that any sequence of actions will
	 * timeout after <em>timeout</em> seconds.
	 * 
	 * @param depth
	 *            the maximum depth a sequence of actions can recurse to.
	 * @param timeout
	 *            the time in seconds that a sequence of actions is allowed to
	 *            execute before the Flash Player displays a dialog box asking
	 *            whether the script should be terminated.
	 */
	public FSLimitScript(int depth, int timeout)
	{
		super(LimitScript);
		setDepth(depth);
		setTimeout(timeout);
	}

	/**
	 * Constructs an FSLimitScript object by copying values from an existing
	 * object.
	 * 
	 * @param obj
	 *            an FSLimitScript object.
	 */
	public FSLimitScript(FSLimitScript obj)
	{
		super(obj);
		depth = obj.depth;
		timeout = obj.timeout;
	}

	/**
	 * Gets the maximum recursion level.
	 * 
	 * @return the maximum depth a sequence of actions can recurse to.
	 */
	public int getDepth()
	{
		return depth;
	}

	/**
	 * Sets the maximum recursion level.
	 * 
	 * @param depth
	 *            the maximum depth a sequence of actions can recurse to.
	 */
	public void setDepth(int depth)
	{
		this.depth = depth;
	}

	/**
	 * Gets the maximum time a sequence of actions will execute before the Flash
	 * Player present a dialog box asking whether the script should be
	 * terminated.
	 * 
	 * @return the time in seconds that a sequence of actions is allowed to
	 *         execute.
	 */
	public int getTimeout()
	{
		return timeout;
	}

	/**
	 * Sets the maximum time a sequence of actions will execute before the Flash
	 * Player present a dialog box asking whether the script should be
	 * terminated.
	 * 
	 * @param time
	 *            the time in seconds that a sequence of actions is allowed to
	 *            execute.
	 */
	public void setTimeout(int time)
	{
		timeout = time;
	}

	public boolean equals(Object anObject)
	{
		boolean result = false;

		if (super.equals(anObject))
		{
			FSLimitScript typedObject = (FSLimitScript) anObject;

			result = depth == typedObject.depth;
			result = result && timeout == typedObject.timeout;
		}
		return result;
	}

	public void appendDescription(StringBuffer buffer, int stackDepth)
	{
		buffer.append(name());

		if (stackDepth > 0)
		{
			buffer.append(": { ");
			Transform.append(buffer, "depth", depth);
			Transform.append(buffer, "timeout", timeout);
			buffer.append("}");
		}
	}

	public int length(FSCoder coder)
	{
		super.length(coder);

		length += 4;

		return length;
	}

	public void encode(FSCoder coder)
	{
		super.encode(coder);

		coder.writeWord(depth, 2);
		coder.writeWord(timeout, 2);
		coder.endObject(name());
	}

	public void decode(FSCoder coder)
	{
		super.decode(coder);

		depth = coder.readWord(2, false);
		timeout = coder.readWord(2, false);
		coder.endObject(name());
	}
}
