/*
 * FSSetBackgroundColor.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;

/**
 * FSSetBackgroundColor sets the background colour displayed in every frame in
 * the movie.
 * 
 * <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>color</td>
 * <td>An FSColor object that defines the background colour.</td>
 * </tr>
 * 
 * </table>
 * 
 * <p>
 * Although the colour is specified using an FSColor object the colour displayed
 * is completely opaque - the alpha channel information in the object is
 * ignored.
 * </p>
 * 
 * <p>
 * The background colour must be set before the first frame is displayed
 * otherwise the background colour defaults to white. This is typically the
 * first object in a coder. If more than one FSSetBackgroundColor object is
 * added to a movie then only first one sets the background colour. Subsequent
 * objects are ignored.
 * </p>
 * 
 * <pre>
 *  FSMovie movie = new FSMovie();
 * 
 *  movie.setFrameSize(new FSBounds(0, 0, 8000, 8000)); // twips = 400 x 400 pixels
 *  movie.setFrameRate(1.0f); 1 frame per second.
 * 
 *  movie.add(new FSSetBackgroundColor(new FSColor(0xAD, 0xD8, 0xE6))); // light blue
 * </pre>
 * 
 * <h1 class="datasheet">History</h1>
 * 
 * <p>
 * The FSSetBackgroundColor class represents the SetBackgroundColor tag from the
 * Macromedia Flash (SWF) File Format Specification. It was introduced in Flash
 * 1.
 * </p>
 */
public class FSSetBackgroundColor extends FSMovieObject
{
	private FSColor color = null;

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

	/**
	 * Constructs an FSSetBackgroundColor object with a the specified colour.
	 * 
	 * @param aColor
	 *            the colour for the background.
	 */
	public FSSetBackgroundColor(FSColor aColor)
	{
		super(FSMovieObject.SetBackgroundColor);
		setColor(aColor);
	}

	/**
	 * Constructs an FSSetBackgroundColor object by copying values from an
	 * existing object.
	 * 
	 * @param obj
	 *            an FSSetBackgroundColor object.
	 */
	public FSSetBackgroundColor(FSSetBackgroundColor obj)
	{
		super(obj);
		color = new FSColor(obj.color);
	}

	/**
	 * Gets the colour for the movie background.
	 * 
	 * @return the FSColor object defining the movie's background colour.
	 */
	public FSColor getColor()
	{
		return color;
	}

	/**
	 * Sets the colour for the movie background.
	 * 
	 * @param aColor
	 *            the colour for the background.
	 */
	public void setColor(FSColor aColor)
	{
		color = aColor;
	}

	public Object clone()
	{
		FSSetBackgroundColor anObject = (FSSetBackgroundColor) super.clone();

		anObject.color = (color != null) ? (FSColor) color.clone() : null;

		return anObject;
	}

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

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

			if (color != null)
				result = color.equals(typedObject.color);
			else
				result = color == typedObject.color;
		}
		return result;
	}

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

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

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

		length += color.length(coder);

		return length;
	}

	public void encode(FSCoder coder)
	{
		super.encode(coder);
		color.encode(coder);
		coder.endObject(name());
	}

	public void decode(FSCoder coder)
	{
		super.decode(coder);
		color = new FSColor(coder);
		coder.endObject(name());
	}
}
